Back to index

plone3  3.1.7
test_ATReferences.py
Go to the documentation of this file.
00001 #
00002 
00003 from Products.PloneTestCase import PloneTestCase
00004 PloneTestCase.setupPloneSite()
00005 
00006 class TestATReferences(PloneTestCase.PloneTestCase):
00007 
00008     def afterSetUp(self):
00009         # we need to have the Manager role to be able to add things
00010         # to the portal root
00011         self.setRoles(['Manager',])
00012 
00013         # add an additional user
00014         self.portal.acl_users.userFolderAddUser('reviewer', 'reviewer',
00015                                                 ['Manager'], '')
00016         
00017         # add a folder with two documents in it
00018         self.portal.invokeFactory('Folder', 'fol')
00019         self.portal.fol.invokeFactory('Document', 'doc1')
00020         self.portal.fol.invokeFactory('Document', 'doc2')
00021 
00022     def test_referencesDataGetSavedAndRestored(self):
00023         # this case checks restoring a version with a reference to
00024         # a working copy with no reference
00025         repo = self.portal.portal_repository
00026         fol = self.portal.fol
00027         doc1 = self.portal.fol.doc1
00028         doc2 = self.portal.fol.doc2
00029 
00030         repo.applyVersionControl(doc1)
00031         repo.applyVersionControl(doc2)
00032         relationship = 'dumb_relationship'
00033         doc1.addReference(doc2, relationship=relationship)
00034         doc1.setTitle('v1')
00035         repo.save(doc1)
00036         from Products.Archetypes.config import REFERENCE_ANNOTATION as \
00037              refs_container_name
00038         refs = getattr(doc1, refs_container_name).objectValues()
00039         doc1.deleteReference(doc2)
00040         should_be_empty_now = getattr(doc1, refs_container_name).objectValues()
00041         self.failIf(should_be_empty_now)
00042         repo.revert(doc1, 1)
00043         after_retrieve_refs = getattr(doc1, refs_container_name).objectValues()
00044         self.assertEqual(refs[0].targetUID, after_retrieve_refs[0].targetUID)
00045         self.assertEqual(refs[0].sourceUID, after_retrieve_refs[0].sourceUID)
00046         self.assertEqual(refs[0].relationship,
00047                          after_retrieve_refs[0].relationship)
00048 
00049     def test_referencesAreSavedAndRestored(self):
00050         # this case checks restoring a version with a reference to
00051         # a working copy with no reference
00052 
00053         repo = self.portal.portal_repository
00054         fol = self.portal.fol
00055         doc1 = self.portal.fol.doc1
00056         doc2 = self.portal.fol.doc2
00057 
00058         repo.applyVersionControl(doc1)
00059         repo.applyVersionControl(doc2)
00060 
00061         doc1.addReference(doc2)
00062         doc1.setTitle('v1')
00063         repo.save(doc1)
00064         doc1.deleteReference(doc2)
00065         self.failIf(doc1.getReferences(targetObject=doc2))
00066         repo.revert(doc1, 1)
00067         self.assertEqual(doc1.getReferences(targetObject=doc2), [doc2])
00068 
00069     def test_referencesDataGetSavedAndRestored2(self):
00070         # this case checks restoring a version with no refs, to a workin copy
00071         # with a ref, without using RetainATRefs
00072 
00073         repo = self.portal.portal_repository
00074         fol = self.portal.fol
00075         doc1 = self.portal.fol.doc1
00076         doc2 = self.portal.fol.doc2
00077 
00078         repo.applyVersionControl(doc1)
00079         repo.applyVersionControl(doc2)
00080         doc1.setTitle('v1')
00081         repo.save(doc1)
00082         relationship = 'dumb_relationship'
00083         doc1.addReference(doc2, relationship=relationship)
00084         doc1.setTitle('v2')
00085         from Products.Archetypes.config import REFERENCE_ANNOTATION as \
00086              refs_container_name
00087         refs = getattr(doc1, refs_container_name).objectValues()
00088 
00089         repo.revert(doc1, 1)
00090         should_be_empty_now = getattr(doc1, refs_container_name).objectValues()
00091         self.failIf(should_be_empty_now)
00092 
00093     def test_referencesAreSavedAndRestored2(self):
00094         # this case checks restoring a version with no refs, to a workin copy
00095         # with a ref, without using RetainATRefs
00096 
00097         repo = self.portal.portal_repository
00098         fol = self.portal.fol
00099         doc1 = self.portal.fol.doc1
00100         doc2 = self.portal.fol.doc2
00101 
00102         repo.applyVersionControl(doc1)
00103         repo.applyVersionControl(doc2)
00104 
00105         doc1.setTitle('v1')
00106         repo.save(doc1)
00107         doc1.addReference(doc2)
00108         self.assertEqual(doc1.getReferences(targetObject=doc2), [doc2])
00109         repo.revert(doc1, 1)
00110         self.failIf(doc1.getReferences(targetObject=doc2))
00111         # The above does not fail because ReferenceCatalog.getReferences calls
00112         # _resolveBrains after a catalog query to get the reference objects - so
00113         # the returned list is empty. But the reference_catalog still has the reference
00114         # indexed:
00115         rc = self.portal.reference_catalog
00116         self.failIf(rc(sourceUID=doc1.UID()))
00117         
00118         
00119 
00120     def test_contentReferencesAreSavedAndRestored(self):
00121 
00122         from Products.Archetypes.ReferenceEngine import ContentReference
00123         repo = self.portal.portal_repository
00124         fol = self.portal.fol
00125         doc1 = self.portal.fol.doc1
00126         doc2 = self.portal.fol.doc2
00127 
00128         repo.applyVersionControl(doc1)
00129         repo.applyVersionControl(doc2)
00130 
00131 #  XXX Simply using this kind of ref doesn't work
00132 #         doc1.addReference(doc2, referenceClass=ContentReference,
00133 #         contentType='Document')
00134 #         doc1.setTitle('v1')
00135 #         ref_doc = doc1.getReferenceImpl(targetObject=doc2)[0]
00136 #         ref_doc.setTitle('ref_doc v1')
00137 #         repo.save(doc1)
00138 #         doc1.deleteReference(doc2)
00139 #         self.failIf(doc1.getReferences(targetObject=doc2))
00140 #         repo.revert(doc1, 1)
00141 #         self.assertEqual(aq_base(doc1.getReferences(targetObject=doc2)[0]),
00142 #                          aq_base(doc2))
00143 #         ref_doc = doc1.getReferenceImpl(targetObject=doc2)[0]
00144 #         self.assertEqual('ref_doc v1', ref_doc.getTitle())
00145 
00146     def test_referencesAreDeleted(self):
00147 
00148         repo = self.portal.portal_repository
00149         fol = self.portal.fol
00150         doc1 = self.portal.fol.doc1
00151         doc2 = self.portal.fol.doc2
00152 
00153         repo.applyVersionControl(doc1)
00154         repo.applyVersionControl(doc2)
00155 
00156         doc1.addReference(doc2)
00157         doc1.setTitle('v1')
00158         repo.save(doc1)
00159         fol.manage_delObjects('doc2')
00160         repo.revert(doc1, 1)
00161         self.assertEqual(doc1.getReferences(), [])
00162         self.failIf(doc1.getReferenceImpl())
00163 
00164     def test_refcatalogIsUpdatedWithInsideRefsAndATRefsBetweenChildrenObjs(self):
00165 
00166         repo = self.portal.portal_repository
00167         fol = self.portal.fol
00168         doc1 = self.portal.fol.doc1
00169         doc2 = self.portal.fol.doc2
00170 
00171         # just configure the standard folder to treat the childrens as
00172         # inside refrences. For this we reconfigure the standard modifiers.
00173         portal_modifier = self.portal.portal_modifier
00174         portal_modifier.edit("OMOutsideChildrensModifier", enabled=False, 
00175                              condition="python: False")
00176         portal_modifier.edit("OMInsideChildrensModifier", enabled=True, 
00177                              condition="python: portal_type=='Folder'")
00178         repo.applyVersionControl(fol)
00179         doc1.setTitle('v1')
00180         doc1.addReference(doc2)
00181         doc2.addReference(doc1)
00182         repo.save(fol)
00183         
00184         doc1.setTitle('changed')
00185         doc1.deleteReference(doc2)
00186         doc2.deleteReference(doc1)
00187         self.failIf(doc1.getReferences())
00188         self.failIf(doc2.getReferences())
00189         repo.revert(fol, 1)
00190         
00191         doc1 = self.portal.fol.doc1
00192         doc2 = self.portal.fol.doc2
00193         self.assertEqual(doc1.Title(), 'v1')
00194         self.assertEqual([doc1], doc2.getReferences())
00195         self.assertEqual([doc2], doc1.getReferences())
00196         
00197     def test_refOnWorkingCopyArePreserved(self):
00198         repo = self.portal.portal_repository
00199         fol = self.portal.fol
00200         doc1 = self.portal.fol.doc1
00201         portal_modifier = self.portal.portal_modifier
00202         portal_modifier.edit("RetainATRefs",
00203                              enabled=True, 
00204                              condition="python: True")
00205         repo.applyVersionControl(doc1)
00206         doc1.addReference(fol)
00207         repo.save(doc1)
00208         repo.revert(doc1, 1)
00209         self.assertEqual([fol], doc1.getReferences())
00210 
00211 from unittest import TestSuite, makeSuite
00212 def test_suite():
00213     suite = TestSuite()
00214     suite.addTest(makeSuite(TestATReferences))
00215     return suite