Back to index

plone3  3.1.7
test_KeepLastNVersionsTool.py
Go to the documentation of this file.
00001 #########################################################################
00002 # Copyright (c) 2006 Gregoire Weber
00003 # All Rights Reserved.
00004 # 
00005 # This file is part of CMFEditions.
00006 # 
00007 # CMFEditions is free software; you can redistribute it and/or modify
00008 # it under the terms of the GNU General Public License as published by
00009 # the Free Software Foundation; either version 2 of the License, or
00010 # (at your option) any later version.
00011 # 
00012 # CMFEditions is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 # GNU General Public License for more details.
00016 # 
00017 # You should have received a copy of the GNU General Public License
00018 # along with CMFEditions; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00020 #########################################################################
00021 """Test the keep the last n version purge policy
00022 
00023 $Id$
00024 """
00025 
00026 from Products.PloneTestCase import PloneTestCase
00027 PloneTestCase.setupPloneSite()
00028 
00029 from Interface.Verify import verifyObject
00030 from Products.CMFEditions.interfaces.IPurgePolicy import IPurgePolicy
00031 
00032 from DummyTools import PurgePolicyTestDummyStorage
00033 from DummyTools import DummyData, RemovedData
00034 
00035 
00036 class TestKeepLastNVersionsTool(PloneTestCase.PloneTestCase):
00037 
00038     def afterSetUp(self):
00039         # we need to have the Manager role to be able to add things
00040         # to the portal root
00041         self.setRoles(['Manager',])
00042         # add an additional user
00043         self.portal.acl_users.userFolderAddUser('reviewer', 'reviewer',
00044                                                 ['Manager'], '')
00045         
00046         # install test storage 
00047         self._setDummyTool(PurgePolicyTestDummyStorage())
00048         
00049     def _setDummyTool(self, tool):
00050         setattr(self.portal, tool.getId(), tool)
00051 
00052     def test00_interface(self):
00053         portal_purgepolicy = self.portal.portal_purgepolicy
00054 
00055         # test interface conformance
00056         verifyObject(IPurgePolicy, portal_purgepolicy)
00057 
00058     def test01_beforeSaveHookInfinite(self):
00059         purgepolicy = self.portal.portal_purgepolicy
00060         storage = self.portal.portal_historiesstorage
00061         
00062         storage.save(history_id=1, obj=DummyData(0))
00063         res = purgepolicy.beforeSaveHook(history_id=1, obj=2, metadata={})
00064         self.failUnless(res)
00065         self.assertEqual(len(storage.getHistory(history_id=1)), 1)
00066 
00067     def test02_beforeSaveHookKeepsMaximumTwoVersions(self):
00068         purgepolicy = self.portal.portal_purgepolicy
00069         storage = self.portal.portal_historiesstorage
00070         purgepolicy.maxNumberOfVersionsToKeep = 2
00071         
00072         # call hook explicitely before save (dummy tool doesn't call it, 
00073         # we wan't to do it explicitely)
00074         res = purgepolicy.beforeSaveHook(history_id=1, obj=2, metadata={})
00075         self.failUnless(res)
00076         storage.save(history_id=1, obj=DummyData(0))
00077         self.assertEqual(len(storage.getHistory(history_id=1)), 1)
00078         
00079         # call hook explicitely before save (dummy tool doesn't call it, 
00080         # we wan't to do it explicitely)
00081         res = purgepolicy.beforeSaveHook(history_id=1, obj=2, metadata={})
00082         self.failUnless(res)
00083         storage.save(history_id=1, obj=DummyData(1))
00084         self.assertEqual(len(storage.getHistory(history_id=1)), 2)
00085         
00086         # check the first saved version was purged
00087         res = purgepolicy.beforeSaveHook(history_id=1, obj=2, metadata={})
00088         self.failUnless(res)
00089         storage.save(history_id=1, obj=DummyData(2))
00090         history = storage.getHistory(history_id=1)
00091         self.assertEqual(len(history), 2)
00092         self.assertEqual(history[0].data, 1)
00093         self.assertEqual(history[1].data, 2)
00094 
00095     def test03_retrieveOlderSubstitute(self):
00096         purgepolicy = self.portal.portal_purgepolicy
00097         storage = self.portal.portal_historiesstorage
00098         
00099         # build history
00100         storage.save(history_id=1, obj=RemovedData())
00101         storage.save(history_id=1, obj=DummyData(1))
00102         storage.save(history_id=1, obj=RemovedData())
00103         storage.save(history_id=1, obj=RemovedData())
00104         storage.save(history_id=1, obj=DummyData(4))
00105         
00106         # next newer
00107         data = purgepolicy.retrieveSubstitute(history_id=1, selector=0)
00108         self.assertEquals(data.data, 1)
00109         # next older
00110         data = purgepolicy.retrieveSubstitute(history_id=1, selector=2)
00111         self.assertEquals(data.data, 1)
00112         # next older
00113         data = purgepolicy.retrieveSubstitute(history_id=1, selector=3)
00114         self.assertEquals(data.data, 1)
00115 
00116 
00117 from unittest import TestSuite, makeSuite
00118 def test_suite():
00119     suite = TestSuite()
00120     suite.addTest(makeSuite(TestKeepLastNVersionsTool))
00121     return suite