Back to index

plone3  3.1.7
testOrderSupport.py
Go to the documentation of this file.
00001 #
00002 # Test our OrderSupport implementation
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 
00007 import transaction
00008 
00009 
00010 class TestOrderSupport(PloneTestCase.PloneTestCase):
00011 
00012     def afterSetUp(self):
00013         membership = self.portal.portal_membership
00014         # Add a bunch of subobjects we can order later on
00015         self.folder.invokeFactory('Document', id='foo')
00016         self.folder.invokeFactory('Document', id='bar')
00017         self.folder.invokeFactory('Document', id='baz')
00018 
00019     def testGetObjectPosition(self):
00020         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00021         self.assertEqual(self.folder.getObjectPosition('bar'), 1)
00022         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00023 
00024     def testMoveObject(self):
00025         self.folder.moveObjectToPosition('foo', 1)
00026         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00027         self.assertEqual(self.folder.getObjectPosition('foo'), 1)
00028         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00029 
00030     def testMoveObjectToSamePos(self):
00031         self.folder.moveObjectToPosition('bar', 1)
00032         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00033         self.assertEqual(self.folder.getObjectPosition('bar'), 1)
00034         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00035 
00036     def testMoveObjectToFirstPos(self):
00037         self.folder.moveObjectToPosition('bar', 0)
00038         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00039         self.assertEqual(self.folder.getObjectPosition('foo'), 1)
00040         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00041 
00042     def testMoveObjectToLastPos(self):
00043         self.folder.moveObjectToPosition('bar', 2)
00044         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00045         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00046         self.assertEqual(self.folder.getObjectPosition('bar'), 2)
00047 
00048     def testMoveObjectOutLowerBounds(self):
00049         # Pos will be normalized to 0
00050         self.folder.moveObjectToPosition('bar', -1)
00051         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00052         self.assertEqual(self.folder.getObjectPosition('foo'), 1)
00053         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00054 
00055     def testMoveObjectOutUpperBounds(self):
00056         # Pos will be normalized to 2
00057         self.folder.moveObjectToPosition('bar', 3)
00058         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00059         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00060         self.assertEqual(self.folder.getObjectPosition('bar'), 2)
00061 
00062     def testMoveObjectsUp(self):
00063         self.folder.moveObjectsUp(['bar'])
00064         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00065         self.assertEqual(self.folder.getObjectPosition('foo'), 1)
00066         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00067 
00068     def testMoveObjectsDown(self):
00069         self.folder.moveObjectsDown(['bar'])
00070         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00071         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00072         self.assertEqual(self.folder.getObjectPosition('bar'), 2)
00073 
00074     def testMoveObjectsToTop(self):
00075         self.folder.moveObjectsToTop(['bar'])
00076         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00077         self.assertEqual(self.folder.getObjectPosition('foo'), 1)
00078         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00079 
00080     def testMoveObjectsToBottom(self):
00081         self.folder.moveObjectsToBottom(['bar'])
00082         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00083         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00084         self.assertEqual(self.folder.getObjectPosition('bar'), 2)
00085 
00086     def testMoveTwoObjectsUp(self):
00087         self.folder.moveObjectsUp(['bar', 'baz'])
00088         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00089         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00090         self.assertEqual(self.folder.getObjectPosition('foo'), 2)
00091 
00092     def testMoveTwoObjectsDown(self):
00093         self.folder.moveObjectsDown(['foo', 'bar'])
00094         self.assertEqual(self.folder.getObjectPosition('baz'), 0)
00095         self.assertEqual(self.folder.getObjectPosition('foo'), 1)
00096         self.assertEqual(self.folder.getObjectPosition('bar'), 2)
00097 
00098     def testMoveTwoObjectsToTop(self):
00099         self.folder.moveObjectsToTop(['bar', 'baz'])
00100         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00101         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00102         self.assertEqual(self.folder.getObjectPosition('foo'), 2)
00103 
00104     def testMoveTwoObjectsToBottom(self):
00105         self.folder.moveObjectsToBottom(['foo', 'bar'])
00106         self.assertEqual(self.folder.getObjectPosition('baz'), 0)
00107         self.assertEqual(self.folder.getObjectPosition('foo'), 1)
00108         self.assertEqual(self.folder.getObjectPosition('bar'), 2)
00109 
00110     def testRenameObject(self):
00111         # Renaming should not change position
00112         transaction.savepoint(optimistic=True) # make rename work
00113         self.folder.manage_renameObjects(['bar'], ['barney'])
00114         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00115         self.assertEqual(self.folder.getObjectPosition('barney'), 1)
00116         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00117 
00118     def testRenameFirstObject(self):
00119         # Renaming should not change position
00120         transaction.savepoint(optimistic=True) # make rename work
00121         self.folder.manage_renameObjects(['foo'], ['flintstone'])
00122         self.assertEqual(self.folder.getObjectPosition('flintstone'), 0)
00123         self.assertEqual(self.folder.getObjectPosition('bar'), 1)
00124         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00125 
00126     def testRenameLastObject(self):
00127         # Renaming should not change position
00128         transaction.savepoint(optimistic=True) # make rename work
00129         self.folder.manage_renameObjects(['baz'], ['bedrock'])
00130         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00131         self.assertEqual(self.folder.getObjectPosition('bar'), 1)
00132         self.assertEqual(self.folder.getObjectPosition('bedrock'), 2)
00133 
00134     def testOrderObjects(self):
00135         self.folder.orderObjects('id')
00136         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00137         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00138         self.assertEqual(self.folder.getObjectPosition('foo'), 2)
00139 
00140     def DISABLED_test_manage_move_objects_up(self):
00141         # Make sure ZMI method works
00142         self.folder.manage_move_objects_up(self.app.REQUEST, ids=['bar'])
00143         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00144         self.assertEqual(self.folder.getObjectPosition('foo'), 1)
00145         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00146 
00147     def DISABLED_test_manage_move_objects_down(self):
00148         # Make sure ZMI method works
00149         self.folder.manage_move_objects_down(self.app.REQUEST, ids=['bar'])
00150         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00151         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00152         self.assertEqual(self.folder.getObjectPosition('bar'), 2)
00153 
00154     def DISABLED_test_manage_move_objects_to_top(self):
00155         # Make sure ZMI method works
00156         self.folder.manage_move_objects_to_top(self.app.REQUEST, ids=['bar'])
00157         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00158         self.assertEqual(self.folder.getObjectPosition('foo'), 1)
00159         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00160 
00161     def DISABLED_test_manage_move_objects_to_bottom(self):
00162         # Make sure ZMI method works
00163         self.folder.manage_move_objects_to_bottom(self.app.REQUEST, ids=['bar'])
00164         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00165         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00166         self.assertEqual(self.folder.getObjectPosition('bar'), 2)
00167 
00168     def testSubsetIds(self):
00169         self.folder.moveObjectsByDelta(['baz'], -1, ['foo', 'bar', 'baz'])
00170         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00171         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00172         self.assertEqual(self.folder.getObjectPosition('bar'), 2)
00173 
00174     def testSkipObjectsNotInSubsetIds(self):
00175         self.folder.moveObjectsByDelta(['baz'], -1, ['foo', 'baz'])
00176         self.assertEqual(self.folder.getObjectPosition('baz'), 0)
00177         self.assertEqual(self.folder.getObjectPosition('bar'), 1) # Did not move
00178         self.assertEqual(self.folder.getObjectPosition('foo'), 2)
00179 
00180     def testIgnoreNonObjects(self):
00181         #Fix for (http://dev.plone.org/plone/ticket/3959) non contentish objects
00182         #cause errors, we should just ignore them
00183         self.folder.moveObjectsByDelta(['bar','blah'], -1)
00184         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00185         self.assertEqual(self.folder.getObjectPosition('foo'), 1)
00186         self.assertEqual(self.folder.getObjectPosition('baz'), 2)
00187 
00188     def testMoveCMFObjectsOnly(self):
00189         # Plone speciality
00190         self.folder.manage_addProduct['OFSP'].manage_addDTMLMethod('wilma', file='')
00191         self.folder.moveObject('wilma', 2)
00192         # Non-CMF object should keep position
00193         self.folder.moveObjectToPosition('foo', 2)
00194         self.assertEqual(self.folder.getObjectPosition('bar'), 0)
00195         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00196         self.assertEqual(self.folder.getObjectPosition('wilma'), 2) # Did not move
00197         self.assertEqual(self.folder.getObjectPosition('foo'), 3)
00198 
00199     def testMoveUpCMFObjectsOnly(self):
00200         # Plone speciality
00201         self.folder.manage_addProduct['OFSP'].manage_addDTMLMethod('wilma', file='')
00202         self.folder.moveObject('wilma', 2)
00203         # Non-CMF object should keep position
00204         self.folder.moveObjectsUp(['baz'])
00205         self.assertEqual(self.folder.getObjectPosition('foo'), 0)
00206         self.assertEqual(self.folder.getObjectPosition('baz'), 1)
00207         self.assertEqual(self.folder.getObjectPosition('wilma'), 2) # Did not move
00208         self.assertEqual(self.folder.getObjectPosition('bar'), 3)
00209 
00210 
00211 class TestOrderSupportInPortal(PloneTestCase.PloneTestCase):
00212 
00213     def afterSetUp(self):
00214         self.setRoles(['Manager'])
00215         # Add a bunch of subobjects we can order later on
00216         self.portal.invokeFactory('Document', id='foo')
00217         self.portal.invokeFactory('Document', id='bar')
00218         self.portal.invokeFactory('Document', id='baz')
00219         # Move them to the top
00220         self.portal.moveObjectsByDelta(ids=['foo', 'bar', 'baz'],
00221                                        delta=-len(self.portal._objects),
00222                                        subset_ids=self.portal.objectIds())
00223 
00224     def testRenameObject(self):
00225         # Renaming should not change position
00226         transaction.savepoint(optimistic=True) # make rename work
00227         self.portal.manage_renameObjects(['bar'], ['barney'])
00228         self.assertEqual(self.portal.getObjectPosition('foo'), 0)
00229         self.assertEqual(self.portal.getObjectPosition('barney'), 1)
00230         self.assertEqual(self.portal.getObjectPosition('baz'), 2)
00231 
00232 
00233 def test_suite():
00234     from unittest import TestSuite, makeSuite
00235     suite = TestSuite()
00236     suite.addTest(makeSuite(TestOrderSupport))
00237     suite.addTest(makeSuite(TestOrderSupportInPortal))
00238     return suite