Back to index

plone3  3.1.7
test_uidannotation.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2004 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """Test the unique id annotation.
00014 
00015 $Id: test_uidannotation.py 77004 2007-06-24 08:57:54Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 import transaction
00022 from AccessControl.SecurityManagement import newSecurityManager
00023 from Acquisition import Implicit
00024 
00025 from zope.component import getSiteManager
00026 from zope.component import getUtility
00027 from zope.interface import implements
00028 
00029 from Products.CMFCore.PortalFolder import PortalFolder
00030 from Products.CMFCore.tests.base.dummy import DummyContent
00031 from Products.CMFCore.tests.base.testcase import SecurityTest
00032 from Products.CMFCore.tests.test_CMFCatalogAware import SimpleFolder
00033 from Products.CMFCore.tests.test_CMFCatalogAware import TheClass
00034 from Products.CMFCore.tests.test_PortalFolder import _AllowedUser
00035 from Products.CMFCore.tests.test_PortalFolder import _SensitiveSecurityPolicy
00036 from Products.CMFUid.interfaces import IUniqueIdAnnotationManagement
00037 from Products.CMFUid.interfaces import IUniqueIdHandler
00038 from Products.CMFUid.interfaces import UniqueIdError
00039 from Products.CMFUid.testing import UidEventZCMLLayer
00040 
00041 UID_ATTRNAME = 'cmf_uid'
00042 
00043 
00044 class DummyUniqueIdHandlerTool(Implicit):
00045 
00046     implements(IUniqueIdHandler)
00047 
00048     def __init__(self):
00049         self.counter = 0
00050 
00051     def register(self, ob):
00052 
00053         uid_assigner = getUtility(IUniqueIdAnnotationManagement)
00054         annotation = uid_assigner(ob, UID_ATTRNAME)
00055         annotation.setUid( self.counter )
00056         self.counter += 1
00057 
00058     def unregister(self, ob):
00059 
00060         try:
00061            delattr(ob, UID_ATTRNAME)
00062         except AttributeError:
00063            raise UniqueIdError
00064 
00065 
00066 class UniqueIdAnnotationToolTests(SecurityTest):
00067 
00068     layer = UidEventZCMLLayer
00069 
00070     def _getTargetClass(self):
00071         from Products.CMFUid.UniqueIdAnnotationTool \
00072                 import UniqueIdAnnotationTool
00073 
00074         return UniqueIdAnnotationTool
00075 
00076     def setUp(self):
00077         SecurityTest.setUp(self)
00078         self.root._setObject('portal_uidannotation', self._getTargetClass()())
00079         self.root._setObject('portal_uidhandler', DummyUniqueIdHandlerTool())
00080 
00081         sm = getSiteManager()
00082         sm.registerUtility( self.root.portal_uidannotation
00083                           , IUniqueIdAnnotationManagement
00084                           )
00085 
00086         self.root._setObject('dummy', DummyContent(id='dummy'))
00087         self.root._setObject('site', SimpleFolder('site'))
00088 
00089         transaction.savepoint(optimistic=True)
00090 
00091     def _initPolicyAndUser( self
00092                           , a_lambda=None
00093                           , v_lambda=None
00094                           , c_lambda=None
00095                           ):
00096         from AccessControl import SecurityManager
00097 
00098         def _promiscuous( *args, **kw ):
00099             return 1
00100 
00101         if a_lambda is None:
00102             a_lambda = _promiscuous
00103 
00104         if v_lambda is None:
00105             v_lambda = _promiscuous
00106 
00107         if c_lambda is None:
00108             c_lambda = _promiscuous
00109 
00110         scp = _SensitiveSecurityPolicy( v_lambda, c_lambda )
00111         SecurityManager.setSecurityPolicy( scp )
00112         newSecurityManager( None
00113                           , _AllowedUser(a_lambda).__of__(self.app.acl_users))
00114 
00115     def test_z3interfaces(self):
00116         from zope.interface.verify import verifyClass
00117         from Products.CMFUid.interfaces import IUniqueIdAnnotation
00118         from Products.CMFUid.interfaces import IUniqueIdAnnotationManagement
00119         from Products.CMFUid.UniqueIdAnnotationTool \
00120                 import UniqueIdAnnotation
00121 
00122         verifyClass(IUniqueIdAnnotationManagement, self._getTargetClass())
00123         verifyClass(IUniqueIdAnnotation, UniqueIdAnnotation)
00124 
00125     def test_setAndGetUid(self):
00126         dummy = self.root.dummy
00127         annotation = self.root.portal_uidannotation(dummy, UID_ATTRNAME)
00128 
00129         self.assertEqual(annotation(), None)
00130         annotation.setUid(13)
00131         self.assertEqual(annotation(), 13)
00132 
00133     # copy/rename/add events: Just to remember
00134     #
00135     # add/import obj:
00136     #   obj.manage_afterAdd(obj, obj, folder)
00137     #
00138     # move/rename obj:
00139     #   obj.manage_beforeDelete(obj, obj, source_folder)
00140     #   obj.manage_afterAdd(obj_at_target, obj_at_target, target_folder)
00141     #
00142     # copy and paste (clone) obj:
00143     #   obj.manage_afterAdd(obj_at_target, obj_at_target, target_folder)
00144     #   obj.manage_afterClone(obj_at_target, obj_at_target)
00145 
00146     def test_simulateItemAddAssigningUid(self):
00147         # an annotated object is set in place
00148         dummy = DummyContent(id='dummycontent')
00149         self.root.portal_uidannotation.assign_on_add = True
00150         self.root._setObject('dummycontent', dummy)
00151 
00152         annotation = getattr(dummy, UID_ATTRNAME, None)
00153 
00154         self.failUnless(annotation is not None)
00155     
00156     def test_simulateItemAddRemovingUid(self):
00157         # an annotated object is set in place
00158         dummy = DummyContent(id='dummycontent')
00159         annotation = self.root.portal_uidannotation(dummy, UID_ATTRNAME)
00160         self.root._setObject('dummycontent', dummy)
00161 
00162         self.assertRaises(AttributeError, getattr, dummy, UID_ATTRNAME)
00163 
00164     def test_simulateItemAddAssignsNewUid(self):
00165         # an annotated object is set in place
00166         dummy = DummyContent(id='dummycontent')
00167         annotation = self.root.portal_uidannotation(dummy, UID_ATTRNAME)
00168         self.root.portal_uidannotation.assign_on_add = True
00169         self.root._setObject('dummycontent', dummy)
00170 
00171         self.failIf( getattr(dummy, UID_ATTRNAME)()==annotation() )
00172 
00173     def test_simulateItemAddDoesNotTouchUid(self):
00174         # an annotated object is set in place
00175         dummy = DummyContent(id='dummycontent')
00176         annotation = self.root.portal_uidannotation(dummy, UID_ATTRNAME)
00177         self.root.portal_uidannotation.remove_on_add = False
00178         self.root._setObject('dummycontent', dummy)
00179 
00180         self.assertEqual(getattr(dummy, UID_ATTRNAME), annotation)
00181 
00182     def test_simulateItemRename(self):
00183         # an object is set in place, annotated and then renamed
00184         self._initPolicyAndUser() # allow copy/paste operations
00185         dummy = TheClass('dummy')
00186         site = self.root.site
00187         site._setObject('dummy', dummy)
00188         annotation = self.root.portal_uidannotation(dummy, UID_ATTRNAME)
00189 
00190         transaction.savepoint(optimistic=True)
00191 
00192         site.manage_renameObject(id='dummy', new_id='dummy2')
00193         new_annotation = getattr(site.dummy2, UID_ATTRNAME)
00194         self.assertEqual(annotation(), new_annotation())
00195 
00196     def test_simulateItemCloneRemovingUid1(self):
00197         # an object is set in place, annotated and then copied
00198         self._initPolicyAndUser() # allow copy/paste operations
00199         dummy = TheClass('dummy')
00200         site = self.root.site
00201         site._setObject('dummy', dummy)
00202         annotation = self.root.portal_uidannotation(dummy, UID_ATTRNAME)
00203         self.root._setObject('folder1', SimpleFolder('folder1'))
00204 
00205         transaction.savepoint(optimistic=True)
00206         cookie = site.manage_copyObjects(ids=['dummy'])
00207         self.root.folder1.manage_pasteObjects( cookie )
00208 
00209         self.assertRaises(AttributeError, getattr, self.root.folder1.dummy, UID_ATTRNAME)
00210 
00211     def test_simulateItemCloneRemovingUid2(self):
00212         # an object is set in place, annotated and then copied
00213         self._initPolicyAndUser() # allow copy/paste operations
00214         dummy = TheClass('dummy')
00215         site = self.root.site
00216         site._setObject('dummy', dummy)
00217         annotation = self.root.portal_uidannotation(dummy, UID_ATTRNAME)
00218         self.root.portal_uidannotation.remove_on_add = False
00219         self.root._setObject('folder1', SimpleFolder('folder1'))
00220 
00221         transaction.savepoint(optimistic=True)
00222         cookie = site.manage_copyObjects(ids=['dummy'])
00223         self.root.folder1.manage_pasteObjects( cookie )
00224 
00225         self.assertRaises(AttributeError, getattr, self.root.folder1.dummy, UID_ATTRNAME)
00226 
00227     def test_simulateItemCloneDoesNotTouchUid(self):
00228         # an object is set in place, annotated, and then copied
00229         self._initPolicyAndUser() # allow copy/paste operations
00230         dummy = TheClass('dummy')
00231         site = self.root.site
00232         site._setObject('dummy', dummy)
00233         annotation = self.root.portal_uidannotation(dummy, UID_ATTRNAME)
00234         self.root.portal_uidannotation.remove_on_clone = False
00235         self.root._setObject('folder1', SimpleFolder('folder1'))
00236 
00237         transaction.savepoint(optimistic=True)
00238         cookie = site.manage_copyObjects(ids=['dummy'])
00239         self.root.folder1.manage_pasteObjects( cookie )
00240         new_annotation = getattr(self.root.folder1.dummy, UID_ATTRNAME)
00241 
00242         self.assertEqual(annotation(), new_annotation() )
00243 
00244     def test_simulateItemCloneAssignsNewUid(self):
00245         # an object is set in place, annotated, and then copied
00246         self._initPolicyAndUser() # allow copy/paste operations
00247         dummy = TheClass('dummy')
00248         site = self.root.site
00249         site._setObject('dummy', dummy)
00250         annotation = self.root.portal_uidannotation(dummy, UID_ATTRNAME)
00251         self.root.portal_uidannotation.assign_on_clone = True
00252         self.root._setObject('folder1', SimpleFolder('folder1'))
00253 
00254         transaction.savepoint(optimistic=True)
00255         cookie = site.manage_copyObjects(ids=['dummy'])
00256         self.root.folder1.manage_pasteObjects( cookie )
00257         new_annotation = getattr(self.root.folder1.dummy, UID_ATTRNAME)
00258 
00259         self.failIf( annotation() == new_annotation() )
00260 
00261     def test_simulateNestedFolderCloneRemovingUid1(self):
00262         self.root.site._setObject( 'foo', SimpleFolder(id='foo') )
00263         self.root.site._setObject( 'foo2', SimpleFolder(id='foo2') )
00264         foo = self.root.site.foo
00265         foo._setObject( 'sub1', SimpleFolder(id='sub1') )
00266         foo.sub1._setObject( 'sub2', SimpleFolder(id='sub2') )
00267         foo.sub1.sub2._setObject( 'baz', DummyContent(id='baz', catalog=1) )
00268         baz = foo.sub1.sub2.baz
00269         annotation = self.root.portal_uidannotation(baz, UID_ATTRNAME)
00270         self.assertEqual( getattr(baz, UID_ATTRNAME), annotation )
00271 
00272         transaction.savepoint(optimistic=True)
00273         cookie = self.root.site.manage_copyObjects(ids='foo')
00274         self.root.site.foo2.manage_pasteObjects(cookie)
00275 
00276         self.assertRaises(AttributeError, getattr, self.root.site.foo2.foo.sub1.sub2.baz, UID_ATTRNAME)
00277 
00278 
00279 def test_suite():
00280     return unittest.TestSuite((
00281         unittest.makeSuite(UniqueIdAnnotationToolTests),
00282         ))
00283 
00284 if __name__ == '__main__':
00285     from Products.CMFCore.testing import run
00286     run(test_suite())