-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move methods from extension into core - Modify tests to remove App namespacing - Add test objects
- Loading branch information
Showing
8 changed files
with
541 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Versioned\Tests; | ||
|
||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\ORM\ValidationException; | ||
use SilverStripe\Versioned\Tests\VersionedNestedTest\PrimaryObject; | ||
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 | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected static $fixture_file = 'VersionedNestedTest.yml'; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected static $extra_dataobjects = [ | ||
PrimaryObject::class, | ||
ColumnObject::class, | ||
GroupObject::class, | ||
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 | ||
* @param bool $delete | ||
* @throws ValidationException | ||
* @dataProvider objectsProvider | ||
*/ | ||
public function testStageDiffersRecursive(string $class, string $identifier, bool $delete): void | ||
{ | ||
/** @var PrimaryObject $primaryItem */ | ||
$primaryItem = $this->objFromFixture(PrimaryObject::class, 'primary-object-1'); | ||
$primaryItem->publishRecursive(); | ||
|
||
$this->assertFalse($primaryItem->stagesDifferRecursive()); | ||
|
||
$record = $this->objFromFixture($class, $identifier); | ||
|
||
if ($delete) { | ||
$record->delete(); | ||
} else { | ||
$record->Title = 'New Title'; | ||
$record->write(); | ||
} | ||
|
||
$this->assertTrue($primaryItem->stagesDifferRecursive()); | ||
} | ||
|
||
public function objectsProvider(): array | ||
{ | ||
return [ | ||
[PrimaryObject::class, 'primary-object-1', false], | ||
[ColumnObject::class, 'column-1', false], | ||
[GroupObject::class, 'group-1', false], | ||
[ChildObject::class, 'child-object-1', false], | ||
[ColumnObject::class, 'column-1', true], | ||
[GroupObject::class, 'group-1', true], | ||
[ChildObject::class, 'child-object-1', true], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# site-config-1 | ||
# -> primary-object-1 (top level publish object) | ||
# --> column-1 | ||
# ---> group-1 | ||
# ----> child-object-1 | ||
|
||
SilverStripe\Versioned\Tests\VersionedNestedTest\PrimaryObject: | ||
primary-object-1: | ||
Title: PrimaryObject1 | ||
|
||
SilverStripe\Versioned\Tests\VersionedNestedTest\ColumnObject: | ||
column-1: | ||
Title: Column1 | ||
PrimaryObject: =>SilverStripe\Versioned\Tests\VersionedNestedTest\PrimaryObject.primary-object-1 | ||
|
||
SilverStripe\Versioned\Tests\VersionedNestedTest\GroupObject: | ||
group-1: | ||
Title: Group1 | ||
Column: =>SilverStripe\Versioned\Tests\VersionedNestedTest\ColumnObject.column-1 | ||
|
||
SilverStripe\Versioned\Tests\VersionedNestedTest\ChildObject: | ||
child-object-1: | ||
Title: Item1 | ||
Group: =>SilverStripe\Versioned\Tests\VersionedNestedTest\GroupObject.group-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Versioned\Tests\VersionedNestedTest; | ||
|
||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\Versioned\Versioned; | ||
|
||
class ChildObject extends DataObject implements TestOnly | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private static $table_name = 'VersionedNestedTest_ChildObject'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $db = [ | ||
'Title' => 'Varchar(255)', | ||
]; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $has_one = [ | ||
'Group' => GroupObject::class, | ||
]; | ||
} |
Oops, something went wrong.