silverstripe /
silverstripe-cms
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\CMS\Tests\Controllers; |
||
| 4 | |||
| 5 | use SilverStripe\CMS\BatchActions\CMSBatchAction_Archive; |
||
| 6 | use SilverStripe\CMS\BatchActions\CMSBatchAction_Publish; |
||
| 7 | use SilverStripe\CMS\BatchActions\CMSBatchAction_Restore; |
||
| 8 | use SilverStripe\CMS\BatchActions\CMSBatchAction_Unpublish; |
||
| 9 | use SilverStripe\CMS\Model\SiteTree; |
||
| 10 | use SilverStripe\Core\Config\Config; |
||
| 11 | use SilverStripe\Dev\SapphireTest; |
||
| 12 | use SilverStripe\Versioned\Versioned; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Tests CMS Specific subclasses of {@see CMSBatchAction} |
||
| 16 | */ |
||
| 17 | class CMSBatchActionsTest extends SapphireTest |
||
| 18 | { |
||
| 19 | protected static $fixture_file = 'CMSBatchActionsTest.yml'; |
||
| 20 | |||
| 21 | protected function setUp() : void |
||
| 22 | { |
||
| 23 | parent::setUp(); |
||
| 24 | |||
| 25 | $this->logInWithPermission('ADMIN'); |
||
| 26 | |||
| 27 | // Tests assume strict hierarchy is enabled |
||
| 28 | Config::inst()->update(SiteTree::class, 'enforce_strict_hierarchy', true); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 29 | |||
| 30 | // published page |
||
| 31 | $published = $this->objFromFixture(SiteTree::class, 'published'); |
||
| 32 | $published->publishSingle(); |
||
| 33 | |||
| 34 | // Deleted / archived page |
||
| 35 | $archived = $this->objFromFixture(SiteTree::class, 'archived'); |
||
| 36 | $archived->doArchive(); // should archive all children |
||
| 37 | |||
| 38 | // Unpublished |
||
| 39 | $unpublished = $this->objFromFixture(SiteTree::class, 'unpublished'); |
||
| 40 | $unpublished->publishSingle(); |
||
| 41 | $unpublished->doUnpublish(); |
||
| 42 | |||
| 43 | // Modified |
||
| 44 | $modified = $this->objFromFixture(SiteTree::class, 'modified'); |
||
| 45 | $modified->publishSingle(); |
||
| 46 | $modified->Title = 'modified2'; |
||
| 47 | $modified->write(); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Test which pages can be published via batch actions |
||
| 52 | */ |
||
| 53 | public function testBatchPublishApplicable() |
||
| 54 | { |
||
| 55 | $this->logInWithPermission('ADMIN'); |
||
| 56 | $pages = Versioned::get_including_deleted(SiteTree::class); |
||
| 57 | $ids = $pages->column('ID'); |
||
| 58 | $action = new CMSBatchAction_Publish(); |
||
| 59 | |||
| 60 | // Test applicable pages |
||
| 61 | $applicable = $action->applicablePages($ids); |
||
| 62 | $this->assertContains($this->idFromFixture(SiteTree::class, 'published'), $applicable); |
||
| 63 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'archived'), $applicable); |
||
| 64 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'archivedx'), $applicable); |
||
| 65 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'archivedy'), $applicable); |
||
| 66 | $this->assertContains($this->idFromFixture(SiteTree::class, 'unpublished'), $applicable); |
||
| 67 | $this->assertContains($this->idFromFixture(SiteTree::class, 'modified'), $applicable); |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Test which pages can be unpublished via batch actions |
||
| 73 | */ |
||
| 74 | public function testBatchUnpublishApplicable() |
||
| 75 | { |
||
| 76 | $this->logInWithPermission('ADMIN'); |
||
| 77 | $pages = Versioned::get_including_deleted(SiteTree::class); |
||
| 78 | $ids = $pages->column('ID'); |
||
| 79 | $action = new CMSBatchAction_Unpublish(); |
||
| 80 | |||
| 81 | // Test applicable page |
||
| 82 | $applicable = $action->applicablePages($ids); |
||
| 83 | $this->assertContains($this->idFromFixture(SiteTree::class, 'published'), $applicable); |
||
| 84 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'archived'), $applicable); |
||
| 85 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'archivedx'), $applicable); |
||
| 86 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'archivedy'), $applicable); |
||
| 87 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'unpublished'), $applicable); |
||
| 88 | $this->assertContains($this->idFromFixture(SiteTree::class, 'modified'), $applicable); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Test which pages can be archived via delete batch actions |
||
| 93 | */ |
||
| 94 | public function testBatchDeleteApplicable() |
||
| 95 | { |
||
| 96 | $this->logInWithPermission('ADMIN'); |
||
| 97 | $pages = Versioned::get_including_deleted(SiteTree::class); |
||
| 98 | $ids = $pages->column('ID'); |
||
| 99 | $action = new CMSBatchAction_Archive(); |
||
| 100 | |||
| 101 | // Test applicable pages |
||
| 102 | $applicable = $action->applicablePages($ids); |
||
| 103 | $this->assertContains($this->idFromFixture(SiteTree::class, 'published'), $applicable); |
||
| 104 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'archived'), $applicable); |
||
| 105 | $this->assertContains($this->idFromFixture(SiteTree::class, 'unpublished'), $applicable); |
||
| 106 | $this->assertContains($this->idFromFixture(SiteTree::class, 'modified'), $applicable); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Test restore batch actions |
||
| 111 | */ |
||
| 112 | public function testBatchRestoreApplicable() |
||
| 113 | { |
||
| 114 | $this->logInWithPermission('ADMIN'); |
||
| 115 | $pages = Versioned::get_including_deleted(SiteTree::class); |
||
| 116 | $ids = $pages->column('ID'); |
||
| 117 | $action = new CMSBatchAction_Restore(); |
||
| 118 | |||
| 119 | // Test applicable pages |
||
| 120 | $applicable = $action->applicablePages($ids); |
||
| 121 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'published'), $applicable); |
||
| 122 | $this->assertContains($this->idFromFixture(SiteTree::class, 'archived'), $applicable); |
||
| 123 | $this->assertContains($this->idFromFixture(SiteTree::class, 'archivedx'), $applicable); |
||
| 124 | $this->assertContains($this->idFromFixture(SiteTree::class, 'archivedy'), $applicable); |
||
| 125 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'unpublished'), $applicable); |
||
| 126 | $this->assertNotContains($this->idFromFixture(SiteTree::class, 'modified'), $applicable); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function testBatchRestore() |
||
| 130 | { |
||
| 131 | $this->logInWithPermission('ADMIN'); |
||
| 132 | $pages = Versioned::get_including_deleted(SiteTree::class); |
||
| 133 | $action = new CMSBatchAction_Restore(); |
||
| 134 | $archivedID = $this->idFromFixture(SiteTree::class, 'archived'); |
||
| 135 | $archivedxID = $this->idFromFixture(SiteTree::class, 'archivedx'); |
||
| 136 | $archivedyID = $this->idFromFixture(SiteTree::class, 'archivedy'); |
||
| 137 | |||
| 138 | // Just restore one child |
||
| 139 | $list = $pages->filter('ID', $archivedxID); |
||
| 140 | $this->assertEquals(1, $list->count()); |
||
| 141 | $this->assertEquals($archivedID, $list->first()->ParentID); |
||
| 142 | |||
| 143 | // Run restore |
||
| 144 | $result = json_decode($action->run($list), true); |
||
| 145 | $this->assertEquals( |
||
| 146 | [ |
||
| 147 | $archivedxID => $archivedxID, |
||
| 148 | ], |
||
| 149 | $result['success'] |
||
| 150 | ); |
||
| 151 | $archivedx = SiteTree::get()->byID($archivedxID); |
||
| 152 | $this->assertNotNull($archivedx); |
||
| 153 | $this->assertEquals(0, $archivedx->ParentID); // Restore to root because parent is unrestored |
||
| 154 | |||
| 155 | // Restore both remaining pages |
||
| 156 | $list = $pages |
||
| 157 | ->filter('ID', [$archivedID, $archivedyID]) |
||
| 158 | ->sort('Title'); |
||
| 159 | $this->assertEquals(2, $list->count()); |
||
| 160 | $this->assertEquals($archivedID, $list->first()->ParentID); // archivedy |
||
| 161 | $this->assertEquals(0, $list->last()->ParentID); // archived (parent) |
||
| 162 | |||
| 163 | // Run restore |
||
| 164 | $result = json_decode($action->run($list), true); |
||
| 165 | $this->assertEquals( |
||
| 166 | [ |
||
| 167 | // Order of archived is opposite to order items are passed in, as |
||
| 168 | // these are sorted by level first |
||
| 169 | $archivedID => $archivedID, |
||
| 170 | $archivedyID => $archivedyID, |
||
| 171 | ], |
||
| 172 | $result['success'] |
||
| 173 | ); |
||
| 174 | $archived = SiteTree::get()->byID($archivedID); |
||
| 175 | $archivedy = SiteTree::get()->byID($archivedyID); |
||
| 176 | $this->assertNotNull($archived); |
||
| 177 | $this->assertNotNull($archivedy); |
||
| 178 | $this->assertEquals($archivedID, $archivedy->ParentID); // Not restored to root, but to the parent |
||
| 179 | $this->assertEquals(0, $archived->ParentID); // Root stays root |
||
| 180 | } |
||
| 181 | } |
||
| 182 |