diff --git a/news/89.bugfix b/news/89.bugfix new file mode 100644 index 0000000..6a24dd6 --- /dev/null +++ b/news/89.bugfix @@ -0,0 +1,3 @@ +- Update relations on Check-In WorkingCopy, by trigger an ObjectModifiedEvent event +- black and flake8 formatting +[2silver] \ No newline at end of file diff --git a/plone/app/iterate/dexterity/policy.py b/plone/app/iterate/dexterity/policy.py index f88c69c..e90911c 100644 --- a/plone/app/iterate/dexterity/policy.py +++ b/plone/app/iterate/dexterity/policy.py @@ -13,6 +13,7 @@ from zope.component import adapter from zope.component import queryAdapter from zope.event import notify +from zope.lifecycleevent import ObjectModifiedEvent @adapter(IDexterityIterateAware) @@ -34,6 +35,10 @@ def checkin(self, checkin_message): new_baseline = copier.merge() # don't need to unlock the lock disappears with old baseline deletion notify(AfterCheckinEvent(new_baseline, checkin_message)) + + # update our new_baseline, ex. for all References ( and BackReferences) + notify(ObjectModifiedEvent(new_baseline)) + return new_baseline def _get_relation_to_baseline(self): diff --git a/plone/app/iterate/tests/test_iterate.py b/plone/app/iterate/tests/test_iterate.py index 8fcf288..9739f6a 100644 --- a/plone/app/iterate/tests/test_iterate.py +++ b/plone/app/iterate/tests/test_iterate.py @@ -142,9 +142,13 @@ def test_folderContents(self): version of the folder. UIDs of contained content are also preserved.""" container = self.portal.docs - folder = container[container.invokeFactory(type_name="Folder", id="foo-folder")] + folder = container[ + container.invokeFactory(type_name="Folder", id="foo-folder") + ] existing_doc = folder[ - folder.invokeFactory(type_name="Document", id="existing-folder-item") + folder.invokeFactory( + type_name="Document", id="existing-folder-item" + ) ] existing_doc_uid = existing_doc.UID() @@ -152,7 +156,9 @@ def test_folderContents(self): wc = ICheckinCheckoutPolicy(folder).checkout(container) new_doc = wc[ wc.invokeFactory( - type_name="Document", id="new-folder-item", text="new folder item text" + type_name="Document", + id="new-folder-item", + text="new folder item text", ) ] new_doc_uid = new_doc.UID() @@ -161,10 +167,14 @@ def test_folderContents(self): catalog = getToolByName(self.portal, "portal_catalog") self.assertTrue("existing-folder-item" in new_folder) - self.assertEqual(new_folder["existing-folder-item"].UID(), existing_doc_uid) + self.assertEqual( + new_folder["existing-folder-item"].UID(), existing_doc_uid + ) self.assertTrue("new-folder-item" in new_folder) self.assertEqual(new_folder["new-folder-item"].UID(), new_doc_uid) - brains = catalog(path="/".join(new_folder["new-folder-item"].getPhysicalPath())) + brains = catalog( + path="/".join(new_folder["new-folder-item"].getPhysicalPath()) + ) self.assertTrue(brains) self.assertTrue( "new folder item text" in new_folder["new-folder-item"].getText() @@ -281,3 +291,51 @@ def test_relationship_deleted_on_cancel_checkout(self): rels = list(catalog.findRelations({"from_id": obj_id})) self.assertEqual(len(rels), 0) + + def test_baseline_relations_updated_on_checkin(self): + # Ensure that relations between the baseline and + # and other objects are up-to-date on checkin + from zope.event import notify + from zope.lifecycleevent import ObjectModifiedEvent + from z3c.relationfield import RelationValue + + folder = self.portal.docs + baseline = folder.doc1 + target = folder.doc2 + + intids = component.getUtility(IIntIds) + catalog = component.getUtility(ICatalog) + + target_id = intids.getId(target) + target_rel = [RelationValue(target_id)] + + # Test, if nothing is present in the relation catalog + rels = list(catalog.findRelations({"to_id": target_id})) + self.assertEqual(len(rels), 0) + + # set relatedItems on baseline + baseline.relatedItems = target_rel + notify(ObjectModifiedEvent(baseline)) + + # Test, if relation is present in the relation catalog + rels = list(catalog.findRelations({"to_id": target_id})) + self.assertEqual(len(rels), 1) + + # make a workingcopy from baseline + wc = ICheckinCheckoutPolicy(baseline).checkout(folder) + + # remove relations on wc + wc.relatedItems = [] + notify(ObjectModifiedEvent(wc)) + + # baseline -> target relation should be still there, we are on wc + rels = list(catalog.findRelations({"to_id": target_id})) + self.assertEqual(len(rels), 1) + + # baseline -> target relation should be empty now + # because we replaced our baseline with our wc (with empty relations) + baseline = ICheckinCheckoutPolicy(wc).checkin("updated") + rels = list(catalog.findRelations({"to_id": target_id})) + + # new baseline's relatedItems should be empty + self.assertEqual(len(rels), 0)