Skip to content

Commit

Permalink
Merge pull request #29 from chrometoasters/pulls/28-publish-on-unvers…
Browse files Browse the repository at this point in the history
…ioned-write

Ensure joining object is published on write for non-versioned owner objects
  • Loading branch information
michalkleiner authored Jun 21, 2021
2 parents 0f0624b + 89a2ba0 commit 65ac84d
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Models/DataObjectTaxonomyTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,38 @@ public function onBeforeDelete()
}
}
}


/**
* 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()
{
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();
}
}
}
}
}
81 changes: 81 additions & 0 deletions tests/TaxonomyTermTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 65ac84d

Please sign in to comment.