From 58abd268ff0747757702c0d03fe09d4c52ed0651 Mon Sep 17 00:00:00 2001 From: Michal Kleiner Date: Fri, 18 Jun 2021 20:09:35 +1200 Subject: [PATCH 1/3] Ensure joining object is published on write for non-versioned owner objects --- src/Models/DataObjectTaxonomyTerm.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Models/DataObjectTaxonomyTerm.php b/src/Models/DataObjectTaxonomyTerm.php index 64a63d1..1458138 100644 --- a/src/Models/DataObjectTaxonomyTerm.php +++ b/src/Models/DataObjectTaxonomyTerm.php @@ -49,4 +49,22 @@ public function onBeforeDelete() } } } + + + /** + * Ensure the linking object is published to Live stage after writing the owner object that doesn't have versioning. + */ + public function onAfterWrite() + { + parent::onAfterWrite(); + + // explicit comparison to false as using ! may not be obvious enough in this case + if ($this->OwnerObject() && $this->OwnerObject()->hasExtension(Versioned::class) === false) { + if (Versioned::get_stage() === Versioned::DRAFT) { + if ($this->canPublish()) { + $this->doPublish(); + } + } + } + } } From 1be37f0d79c04fff19f1e9d1d484d0ffcba95527 Mon Sep 17 00:00:00 2001 From: Michal Kleiner Date: Mon, 21 Jun 2021 17:22:24 +1200 Subject: [PATCH 2/3] Ensure joining object is deleted from draft when it's removed from live Applies to non-versioned owner objects. --- src/Models/DataObjectTaxonomyTerm.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Models/DataObjectTaxonomyTerm.php b/src/Models/DataObjectTaxonomyTerm.php index 1458138..5d7bed3 100644 --- a/src/Models/DataObjectTaxonomyTerm.php +++ b/src/Models/DataObjectTaxonomyTerm.php @@ -52,7 +52,23 @@ public function onBeforeDelete() /** - * Ensure the linking object is published to Live stage after writing the owner object that doesn't have versioning. + * Ensure the linking object is deleted from Draft when it's deleted from Live for a non-versioned owner object + */ + public function onAfterDelete() + { + if ($this->OwnerObject() && $this->OwnerObject()->hasExtension(Versioned::class) === false) { + if (Versioned::get_stage() === Versioned::LIVE) { + Versioned::withVersionedMode(function () { + Versioned::set_stage(Versioned::DRAFT); + $this->delete(); + }); + } + } + } + + + /** + * Ensure the linking object is published to Live stage after writing a non-versioned owner object */ public function onAfterWrite() { From 89a2ba0baaf7cbb59fb8ef3123c387a001cbc5b0 Mon Sep 17 00:00:00 2001 From: Michal Kleiner Date: Mon, 21 Jun 2021 17:26:01 +1200 Subject: [PATCH 3/3] Add test for non-versioned owner objects --- tests/TaxonomyTermTest.php | 81 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tests/TaxonomyTermTest.php b/tests/TaxonomyTermTest.php index 2813103..8ea150d 100644 --- a/tests/TaxonomyTermTest.php +++ b/tests/TaxonomyTermTest.php @@ -518,6 +518,87 @@ public function testOwnerObjectOwnsTaxonomyTerm() } + /** + * Test a non-versioned model correctly adds and removes versioned tags in both draft and live stages + */ + public function testNonVersionedOwnerObject() + { + $origStage = Versioned::get_stage(); + + // Start in draft mode + Versioned::set_stage(Versioned::DRAFT); + + // Remove extension + OwnerObject::remove_extension(Versioned::class); + + $object6 = $this->objFromFixture(OwnerObject::class, 'object6'); + $this->assertFalse($object6->hasExtension(Versioned::class)); + + $this->assertEquals( + 0, + $object6->Tags()->count(), + 'The object for testing non-versioned owner has 0 terms assigned' + ); + + $rootTerm1 = $this->objFromFixture(TaxonomyTerm::class, 'rootTerm1'); + $rootTerm2 = $this->objFromFixture(TaxonomyTerm::class, 'rootTerm2'); + + $object6->Tags()->add($rootTerm1); + $object6->Tags()->add($rootTerm2); + + $this->assertEquals( + 2, + $object6->Tags()->count(), + 'The object for testing non-versioned owner has 2 terms assigned in draft' + ); + + // Set live mode where non-versioned objects should always be visible + Versioned::set_stage(Versioned::LIVE); + + $this->assertEquals( + 2, + $object6->Tags()->count(), + 'The object for testing non-versioned owner has 2 terms assigned in live' + ); + + // Remove one of the terms in live mode + $object6->Tags()->remove($rootTerm1); + + // Set draft mode where the term deleted in live should be also gone + Versioned::set_stage(Versioned::DRAFT); + + $this->assertEquals( + 1, + $object6->Tags()->count(), + 'The object for testing non-versioned owner has 1 term assigned in draft after first term removed in live' + ); + $this->assertEquals( + $rootTerm2->ID, + $object6->Tags()->first()->ID, + 'The object for testing non-versioned owner has only term 2 assigned in draft after term 1 removed in live' + ); + + // Set live mode where non-versioned objects should always be visible + Versioned::set_stage(Versioned::LIVE); + + $this->assertEquals( + 1, + $object6->Tags()->count(), + 'The object for testing non-versioned owner still has 1 term assigned in live' + ); + $this->assertEquals( + $rootTerm2->ID, + $object6->Tags()->first()->ID, + 'The object for testing non-versioned owner still has only term 2 assigned in live' + ); + + // Restore the orig archived stage + if ($origStage) { + Versioned::set_stage($origStage); + } + } + + /** * Test when a RequiredTypes loop exists, e.g * 1. TypeZ requires TypeY,