Back to index

plone3  3.1.7
test_librarymanager.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2003-2005 Kupu Contributors. All rights reserved.
00004 #
00005 # This software is distributed under the terms of the Kupu
00006 # License. See LICENSE.txt for license text. For a list of Kupu
00007 # Contributors see CREDITS.txt.
00008 #
00009 ##############################################################################
00010 """Tests for the library tool
00011 
00012 $Id: test_librarymanager.py 41232 2007-03-24 13:02:55Z hannosch $
00013 """
00014 
00015 import os, sys
00016 if __name__ == '__main__':
00017     execfile(os.path.join(sys.path[0], 'framework.py'))
00018 
00019 from Products.PloneTestCase import PloneTestCase
00020 PloneTestCase.setupPloneSite(products=['kupu'])
00021 
00022 import Acquisition
00023 from Products.kupu.plone.plonelibrarytool import PloneKupuLibraryTool
00024 
00025 class FakeMembershipTool:
00026     isAnonymousUser = lambda self: True
00027 
00028 class FakePortal(Acquisition.Implicit):
00029     absolute_url = lambda(self): None
00030     portal_membership = FakeMembershipTool()
00031 
00032 class FakeContextObject(Acquisition.Implicit):
00033 
00034     __allow_access_to_unprotected_subobjects__ = True
00035     isPrincipiaFolderish = True
00036     REQUEST = 'whatever'
00037     absolute_url = lambda(self): u"The answer is 42"
00038 
00039 class TestILibraryManager(PloneTestCase.PloneTestCase):
00040     """Test the implementation of ILibraryManger in KupuLibraryTool"""
00041 
00042     def afterSetUp(self):
00043         self.libs = self.makeLibraries()
00044 
00045     def makeLibraries(self):
00046         # need to use Plone specific tool for Acquisition. Sucks.
00047         libs = PloneKupuLibraryTool()
00048         self.portal = FakePortal()
00049         context = FakeContextObject()
00050         libs = libs.__of__(self.portal)
00051         self.context = context.__of__(self.portal)
00052         libs.addLibrary('foo_id', 'Foobar', 'foobar', 'foosrc', 'fooicon')
00053         libs.addLibrary('bar_id', 'Barfoo', 'barfoo', 'barsrc', 'baricon')
00054         libs.addLibrary('baz_id', 'Baz', 'foobarbaz', 'bazsrc', 'bazicon')
00055         return libs
00056 
00057     def test_get_library(self):
00058         libs = self.libs
00059         expected = (
00060             dict(id='foo_id', title='Foobar', uri='foobar',
00061                  src='foosrc', icon='fooicon'),
00062             dict(id='bar_id', title='Barfoo', uri='barfoo',
00063                  src='barsrc', icon='baricon'),
00064             dict(id='baz_id', title='Baz', uri='foobarbaz',
00065                  src='bazsrc', icon='bazicon'),
00066             )
00067         self.assertEqual(libs.getLibraries(self.context), expected)
00068 
00069     def test_expressions(self):
00070         libs = self.libs
00071         context = self.context
00072         new_libs = (
00073             dict(id='foo_id', title='Foobar', uri='python:42',
00074                  src='foosrc', icon='fooicon'),
00075             dict(id='bar_id', title='Barfoo', uri='python:object',
00076                  src='barsrc', icon='baricon'),
00077             dict(id='baz_id', title='Baz', uri='string:${object/absolute_url}',
00078                  src='bazsrc', icon='bazicon'),
00079             )
00080         libs.updateLibraries(new_libs)
00081 
00082         expected = (
00083             dict(id='foo_id', title='Foobar', uri=42,
00084                  src='foosrc', icon='fooicon'),
00085             dict(id='bar_id', title='Barfoo', uri=context,
00086                  src='barsrc', icon='baricon'),
00087             dict(id='baz_id', title='Baz', uri=u"The answer is 42",
00088                  src='bazsrc', icon='bazicon')
00089             )
00090         v = libs.getLibraries(context)
00091         self.assertEqual(v, expected)
00092 
00093     def test_delete(self):
00094         libs = self.libs
00095         libs.deleteLibraries([1])
00096         expected = (
00097             dict(id='foo_id', title='Foobar', uri='foobar',
00098                  src='foosrc', icon='fooicon'),
00099             dict(id='baz_id', title='Baz', uri='foobarbaz',
00100                  src='bazsrc', icon='bazicon'),
00101             )
00102         self.assertEqual(libs.getLibraries(self.context), expected)
00103 
00104         libs = self.makeLibraries()
00105         libs.deleteLibraries([0, 1])
00106         expected = (
00107             dict(id='baz_id', title='Baz', uri='foobarbaz',
00108                  src='bazsrc', icon='bazicon'),
00109             )
00110         self.assertEqual(libs.getLibraries(self.context), expected)
00111 
00112     def test_update(self):
00113         libs = self.libs
00114         context = self.context
00115         new_libs = (
00116             dict(id='foo_new_id', title='Newfoo', uri="python:object",
00117                  src='foonewsrc', icon="foonewicon"),
00118             dict(id='just_a_new_id'),
00119             dict(src="python:'you stink'.upper()"),
00120             )
00121         libs.updateLibraries(new_libs)
00122         expected = (
00123             dict(id='foo_new_id', title='Newfoo', uri=context,
00124                  src='foonewsrc', icon='foonewicon'),
00125             dict(id='just_a_new_id', title='Barfoo', uri='barfoo',
00126                  src='barsrc', icon='baricon'),
00127             dict(id='baz_id', title='Baz', uri='foobarbaz',
00128                  src="YOU STINK", icon='bazicon'),
00129             )
00130         self.assertEqual(libs.getLibraries(context), expected)
00131 
00132     def test_move(self):
00133         libs = self.libs
00134         libs.moveUp([1])
00135         expected = (
00136             dict(id='bar_id', title='Barfoo', uri='barfoo',
00137                  src='barsrc', icon='baricon'),
00138             dict(id='foo_id', title='Foobar', uri='foobar',
00139                  src='foosrc', icon='fooicon'),
00140             dict(id='baz_id', title='Baz', uri='foobarbaz',
00141                  src='bazsrc', icon='bazicon'),
00142             )
00143         self.assertEqual(libs.getLibraries(self.context), expected)
00144 
00145         libs.moveDown([1])
00146         expected = (
00147             dict(id='bar_id', title='Barfoo', uri='barfoo',
00148                  src='barsrc', icon='baricon'),
00149             dict(id='baz_id', title='Baz', uri='foobarbaz',
00150                  src='bazsrc', icon='bazicon'),
00151             dict(id='foo_id', title='Foobar', uri='foobar',
00152                  src='foosrc', icon='fooicon'),
00153             )
00154         self.assertEqual(libs.getLibraries(self.context), expected)
00155 
00156         libs.moveUp([1, 2])
00157         expected = (
00158             dict(id='baz_id', title='Baz', uri='foobarbaz',
00159                  src='bazsrc', icon='bazicon'),
00160             dict(id='foo_id', title='Foobar', uri='foobar',
00161                  src='foosrc', icon='fooicon'),
00162             dict(id='bar_id', title='Barfoo', uri='barfoo',
00163                  src='barsrc', icon='baricon'),
00164             )
00165         self.assertEqual(libs.getLibraries(self.context), expected)
00166 
00167         libs.moveDown([2])
00168         expected = (
00169             dict(id='bar_id', title='Barfoo', uri='barfoo',
00170                  src='barsrc', icon='baricon'),
00171             dict(id='foo_id', title='Foobar', uri='foobar',
00172                  src='foosrc', icon='fooicon'),
00173             dict(id='baz_id', title='Baz', uri='foobarbaz',
00174                  src='bazsrc', icon='bazicon'),
00175             )
00176         self.assertEqual(libs.getLibraries(self.context), expected)
00177 
00178 if __name__ == '__main__':
00179     framework()
00180 else:
00181     # While framework.py provides its own test_suite()
00182     # method the testrunner utility does not.
00183     from unittest import TestSuite, makeSuite
00184     def test_suite():
00185         suite = TestSuite()
00186         suite.addTest(makeSuite(TestILibraryManager))
00187         return suite