Skip to content

Commit

Permalink
Passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danaenz committed Feb 26, 2021
1 parent 024a60b commit c43b909
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 28 deletions.
84 changes: 84 additions & 0 deletions src/PublishStateHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace SilverStripe\Versioned;

use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\SS_List;
use SilverStripe\Versioned\Versioned;

/**
* Class PublishStateHelper
*
* functionality which is related to detecting the need of publishing nested objects within a block page
*
* @package App\Helpers
*/
class PublishStateHelper
{
/**
* @param DataObject|Versioned|null $item
* @return bool
*/
public static function checkNeedPublishingItem(?DataObject $item): bool
{
if ($item === null || !$item->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;
}
}
16 changes: 16 additions & 0 deletions tests/php/VersionedNestedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
7 changes: 0 additions & 7 deletions tests/php/VersionedNestedTest/ChildObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ class ChildObject extends DataObject implements TestOnly
'Title' => 'Varchar(255)',
];

/**
* @var array
*/
private static $extensions = [
Versioned::class,
];

/**
* @var array
*/
Expand Down
7 changes: 0 additions & 7 deletions tests/php/VersionedNestedTest/ColumnObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ class ColumnObject extends DataObject implements TestOnly
'Title' => 'Varchar(255)',
];

/**
* @var array
*/
private static $extensions = [
Versioned::class,
];

/**
* @var array
*/
Expand Down
7 changes: 0 additions & 7 deletions tests/php/VersionedNestedTest/GroupObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ class GroupObject extends DataObject implements TestOnly
'Title' => 'Varchar(255)',
];

/**
* @var array
*/
private static $extensions = [
Versioned::class,
];

/**
* @var array
*/
Expand Down
7 changes: 0 additions & 7 deletions tests/php/VersionedNestedTest/PrimaryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ class PrimaryObject extends DataObject implements TestOnly
'Title' => 'Varchar(255)',
];

/**
* @var array
*/
private static $extensions = [
Versioned::class,
];

/**
* @var array
*/
Expand Down

0 comments on commit c43b909

Please sign in to comment.