diff --git a/src/PublishStateHelper.php b/src/PublishStateHelper.php new file mode 100644 index 00000000..f2922510 --- /dev/null +++ b/src/PublishStateHelper.php @@ -0,0 +1,84 @@ +exists()) { + return false; + } + + if ($item->hasExtension(Versioned::class)) { + /** @var $item Versioned */ + return !$item->isPublished() || $item->stagesDiffer(); + } + + return false; + } + + /** + * @param SS_List $list + * @return bool + */ + public static function checkNeedPublishingList(SS_List $list): bool + { + /** @var $item Versioned */ + foreach ($list as $item) { + if (static::checkNeedPublishingItem($item)) { + return true; + } + } + + return false; + } + + /** + * @param DataList $items + * @param int $parentId + * @return bool + */ + public static function checkNeedPublishVersionedItems(DataList $items, int $parentId): bool + { + // check for differences in models + foreach ($items as $item) { + if (PublishStateHelper::checkNeedPublishingItem($item)) { + return true; + } + } + + // check for deletion of a model + $draftCount = $items->count(); + + // we need to fetch live records and compare amount because if a record was deleted from stage + // the above draft items loop will not cover the missing item + $liveCount = Versioned::get_by_stage( + $items->dataClass(), + Versioned::LIVE, + ['ParentID' => $parentId] + )->count(); + + if ($draftCount != $liveCount) { + return true; + } + + return false; + } +} diff --git a/tests/php/VersionedNestedTest.php b/tests/php/VersionedNestedTest.php index 9dfc99f5..09657cb7 100644 --- a/tests/php/VersionedNestedTest.php +++ b/tests/php/VersionedNestedTest.php @@ -8,6 +8,7 @@ use SilverStripe\Versioned\Tests\VersionedNestedTest\ColumnObject; use SilverStripe\Versioned\Tests\VersionedNestedTest\GroupObject; use SilverStripe\Versioned\Tests\VersionedNestedTest\ChildObject; +use SilverStripe\Versioned\Versioned; class VersionedNestedTest extends SapphireTest { @@ -26,6 +27,21 @@ class VersionedNestedTest extends SapphireTest ChildObject::class, ]; + protected static $required_extensions = [ + PrimaryObject::class => [ + Versioned::class, + ], + ColumnObject::class => [ + Versioned::class, + ], + GroupObject::class => [ + Versioned::class, + ], + ChildObject::class => [ + Versioned::class, + ], + ]; + /** * @param string $class * @param string $identifier diff --git a/tests/php/VersionedNestedTest/ChildObject.php b/tests/php/VersionedNestedTest/ChildObject.php index 5145367e..741856de 100644 --- a/tests/php/VersionedNestedTest/ChildObject.php +++ b/tests/php/VersionedNestedTest/ChildObject.php @@ -21,13 +21,6 @@ class ChildObject extends DataObject implements TestOnly 'Title' => 'Varchar(255)', ]; - /** - * @var array - */ - private static $extensions = [ - Versioned::class, - ]; - /** * @var array */ diff --git a/tests/php/VersionedNestedTest/ColumnObject.php b/tests/php/VersionedNestedTest/ColumnObject.php index b10e96bf..bb698f7b 100644 --- a/tests/php/VersionedNestedTest/ColumnObject.php +++ b/tests/php/VersionedNestedTest/ColumnObject.php @@ -21,13 +21,6 @@ class ColumnObject extends DataObject implements TestOnly 'Title' => 'Varchar(255)', ]; - /** - * @var array - */ - private static $extensions = [ - Versioned::class, - ]; - /** * @var array */ diff --git a/tests/php/VersionedNestedTest/GroupObject.php b/tests/php/VersionedNestedTest/GroupObject.php index 296435ae..6c36ed86 100644 --- a/tests/php/VersionedNestedTest/GroupObject.php +++ b/tests/php/VersionedNestedTest/GroupObject.php @@ -21,13 +21,6 @@ class GroupObject extends DataObject implements TestOnly 'Title' => 'Varchar(255)', ]; - /** - * @var array - */ - private static $extensions = [ - Versioned::class, - ]; - /** * @var array */ diff --git a/tests/php/VersionedNestedTest/PrimaryObject.php b/tests/php/VersionedNestedTest/PrimaryObject.php index c1b1d963..2c36ffc6 100644 --- a/tests/php/VersionedNestedTest/PrimaryObject.php +++ b/tests/php/VersionedNestedTest/PrimaryObject.php @@ -27,13 +27,6 @@ class PrimaryObject extends DataObject implements TestOnly 'Title' => 'Varchar(255)', ]; - /** - * @var array - */ - private static $extensions = [ - Versioned::class, - ]; - /** * @var array */