2015-05-15 01:51:23 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 04:53:38 +02:00
|
|
|
namespace SilverStripe\CMS\Tests\Controllers;
|
2017-08-09 03:25:12 +02:00
|
|
|
|
2016-10-25 02:22:31 +02:00
|
|
|
use SilverStripe\CMS\BatchActions\CMSBatchAction_Archive;
|
2016-07-22 01:32:32 +02:00
|
|
|
use SilverStripe\CMS\BatchActions\CMSBatchAction_Publish;
|
|
|
|
use SilverStripe\CMS\BatchActions\CMSBatchAction_Restore;
|
2017-09-16 04:20:55 +02:00
|
|
|
use SilverStripe\CMS\BatchActions\CMSBatchAction_Unpublish;
|
2016-07-22 01:32:32 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2017-09-16 04:20:55 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-09-16 04:20:55 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-08-23 04:36:06 +02:00
|
|
|
|
2015-05-15 01:51:23 +02:00
|
|
|
/**
|
|
|
|
* Tests CMS Specific subclasses of {@see CMSBatchAction}
|
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class CMSBatchActionsTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
protected static $fixture_file = 'CMSBatchActionsTest.yml';
|
|
|
|
|
2021-10-27 23:40:52 +02:00
|
|
|
protected function setUp(): void
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
|
2017-09-16 04:20:55 +02:00
|
|
|
// Tests assume strict hierarchy is enabled
|
|
|
|
Config::inst()->update(SiteTree::class, 'enforce_strict_hierarchy', true);
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
// published page
|
|
|
|
$published = $this->objFromFixture(SiteTree::class, 'published');
|
|
|
|
$published->publishSingle();
|
|
|
|
|
|
|
|
// Deleted / archived page
|
|
|
|
$archived = $this->objFromFixture(SiteTree::class, 'archived');
|
|
|
|
$archived->doArchive(); // should archive all children
|
|
|
|
|
|
|
|
// Unpublished
|
|
|
|
$unpublished = $this->objFromFixture(SiteTree::class, 'unpublished');
|
|
|
|
$unpublished->publishSingle();
|
|
|
|
$unpublished->doUnpublish();
|
|
|
|
|
|
|
|
// Modified
|
|
|
|
$modified = $this->objFromFixture(SiteTree::class, 'modified');
|
|
|
|
$modified->publishSingle();
|
|
|
|
$modified->Title = 'modified2';
|
|
|
|
$modified->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test which pages can be published via batch actions
|
|
|
|
*/
|
|
|
|
public function testBatchPublishApplicable()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$pages = Versioned::get_including_deleted(SiteTree::class);
|
|
|
|
$ids = $pages->column('ID');
|
|
|
|
$action = new CMSBatchAction_Publish();
|
|
|
|
|
|
|
|
// Test applicable pages
|
|
|
|
$applicable = $action->applicablePages($ids);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'published'), $applicable);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'archived'), $applicable);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'archivedx'), $applicable);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'archivedy'), $applicable);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'unpublished'), $applicable);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'modified'), $applicable);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test which pages can be unpublished via batch actions
|
|
|
|
*/
|
|
|
|
public function testBatchUnpublishApplicable()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$pages = Versioned::get_including_deleted(SiteTree::class);
|
|
|
|
$ids = $pages->column('ID');
|
|
|
|
$action = new CMSBatchAction_Unpublish();
|
|
|
|
|
|
|
|
// Test applicable page
|
|
|
|
$applicable = $action->applicablePages($ids);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'published'), $applicable);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'archived'), $applicable);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'archivedx'), $applicable);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'archivedy'), $applicable);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'unpublished'), $applicable);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'modified'), $applicable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test which pages can be archived via delete batch actions
|
|
|
|
*/
|
|
|
|
public function testBatchDeleteApplicable()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$pages = Versioned::get_including_deleted(SiteTree::class);
|
|
|
|
$ids = $pages->column('ID');
|
|
|
|
$action = new CMSBatchAction_Archive();
|
|
|
|
|
|
|
|
// Test applicable pages
|
|
|
|
$applicable = $action->applicablePages($ids);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'published'), $applicable);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'archived'), $applicable);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'unpublished'), $applicable);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'modified'), $applicable);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test restore batch actions
|
|
|
|
*/
|
|
|
|
public function testBatchRestoreApplicable()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$pages = Versioned::get_including_deleted(SiteTree::class);
|
|
|
|
$ids = $pages->column('ID');
|
|
|
|
$action = new CMSBatchAction_Restore();
|
|
|
|
|
|
|
|
// Test applicable pages
|
|
|
|
$applicable = $action->applicablePages($ids);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'published'), $applicable);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'archived'), $applicable);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'archivedx'), $applicable);
|
|
|
|
$this->assertContains($this->idFromFixture(SiteTree::class, 'archivedy'), $applicable);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'unpublished'), $applicable);
|
|
|
|
$this->assertNotContains($this->idFromFixture(SiteTree::class, 'modified'), $applicable);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBatchRestore()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$pages = Versioned::get_including_deleted(SiteTree::class);
|
|
|
|
$action = new CMSBatchAction_Restore();
|
|
|
|
$archivedID = $this->idFromFixture(SiteTree::class, 'archived');
|
|
|
|
$archivedxID = $this->idFromFixture(SiteTree::class, 'archivedx');
|
|
|
|
$archivedyID = $this->idFromFixture(SiteTree::class, 'archivedy');
|
|
|
|
|
|
|
|
// Just restore one child
|
2018-03-14 04:34:46 +01:00
|
|
|
$list = $pages->filter('ID', $archivedxID);
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals(1, $list->count());
|
|
|
|
$this->assertEquals($archivedID, $list->first()->ParentID);
|
|
|
|
|
|
|
|
// Run restore
|
2022-04-13 07:07:59 +02:00
|
|
|
$result = json_decode($action->run($list) ?? '', true);
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals(
|
2020-04-19 06:18:01 +02:00
|
|
|
[
|
2017-01-25 21:59:25 +01:00
|
|
|
$archivedxID => $archivedxID
|
2020-04-19 06:18:01 +02:00
|
|
|
],
|
2017-01-25 21:59:25 +01:00
|
|
|
$result['success']
|
|
|
|
);
|
|
|
|
$archivedx = SiteTree::get()->byID($archivedxID);
|
|
|
|
$this->assertNotNull($archivedx);
|
|
|
|
$this->assertEquals(0, $archivedx->ParentID); // Restore to root because parent is unrestored
|
|
|
|
|
|
|
|
// Restore both remaining pages
|
|
|
|
$list = $pages
|
2018-03-14 04:34:46 +01:00
|
|
|
->filter('ID', [$archivedID, $archivedyID])
|
2017-01-25 21:59:25 +01:00
|
|
|
->sort('Title');
|
|
|
|
$this->assertEquals(2, $list->count());
|
|
|
|
$this->assertEquals($archivedID, $list->first()->ParentID); // archivedy
|
|
|
|
$this->assertEquals(0, $list->last()->ParentID); // archived (parent)
|
|
|
|
|
|
|
|
// Run restore
|
2022-04-13 07:07:59 +02:00
|
|
|
$result = json_decode($action->run($list) ?? '', true);
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->assertEquals(
|
2020-04-19 06:18:01 +02:00
|
|
|
[
|
2017-01-25 21:59:25 +01:00
|
|
|
// Order of archived is opposite to order items are passed in, as
|
|
|
|
// these are sorted by level first
|
|
|
|
$archivedID => $archivedID,
|
|
|
|
$archivedyID => $archivedyID
|
2020-04-19 06:18:01 +02:00
|
|
|
],
|
2017-01-25 21:59:25 +01:00
|
|
|
$result['success']
|
|
|
|
);
|
|
|
|
$archived = SiteTree::get()->byID($archivedID);
|
|
|
|
$archivedy = SiteTree::get()->byID($archivedyID);
|
|
|
|
$this->assertNotNull($archived);
|
|
|
|
$this->assertNotNull($archivedy);
|
|
|
|
$this->assertEquals($archivedID, $archivedy->ParentID); // Not restored to root, but to the parent
|
|
|
|
$this->assertEquals(0, $archived->ParentID); // Root stays root
|
|
|
|
}
|
2015-05-15 01:51:23 +02:00
|
|
|
}
|