From 0db24fc03872168bf6f0bf345aa30ae6811c8c3b Mon Sep 17 00:00:00 2001 From: Sergey Shevchenko Date: Fri, 21 Jan 2022 13:27:02 +1300 Subject: [PATCH] FIX SiteTree::getCMSActions unpublish action (#2703) * TEST SiteTree::getCMSAction produces valid actions * FIX Fix invalid SiteTree unpublish action declaration * refactor: moved new functions to conform with existing naming conventions see https://github.com/silverstripe/silverstripe-cms/pull/2703#discussion_r789208512 --- code/Model/SiteTree.php | 2 +- tests/php/Model/SiteTreeTest.php | 210 +++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+), 1 deletion(-) diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index b8e10c1c..3c985fd3 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -2365,7 +2365,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi // "unpublish" if ($isPublished && $isOnDraft && $canUnpublish) { $moreOptions->push( - FormAction::create('unpublish', _t(__CLASS__.'.BUTTONUNPUBLISH', 'Unpublish'), 'delete') + FormAction::create('unpublish', _t(__CLASS__.'.BUTTONUNPUBLISH', 'Unpublish')) ->setDescription(_t(__CLASS__.'.BUTTONUNPUBLISHDESC', 'Remove this page from the published site')) ->addExtraClass('btn-secondary' . ($this->isHomePage() ? ' homepage-warning' : '')) ); diff --git a/tests/php/Model/SiteTreeTest.php b/tests/php/Model/SiteTreeTest.php index c35dd4e7..b58d1bfd 100644 --- a/tests/php/Model/SiteTreeTest.php +++ b/tests/php/Model/SiteTreeTest.php @@ -1744,4 +1744,214 @@ class SiteTreeTest extends SapphireTest }); } } + + public function testGetCMSActions() + { + // Create new page on DRAFT + $page = SiteTree::create(); + $page->Content = md5(rand(0, PHP_INT_MAX)); + $page->write(); + + // BEGIN DRAFT + $actions = $page->getCMSActions(); + $this->assertNotNull( + $actions->fieldByName('MajorActions.action_save'), + 'save action present for a saved draft page' + ); + $this->assertNotNull( + $actions->fieldByName('MajorActions.action_publish'), + 'publish action present for a saved draft page' + ); + $this->assertNotNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_archive'), + 'archive action present for a saved draft page' + ); + $this->assertNotNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_addtocampaign'), + 'addtocampaign action present for a saved draft page' + ); + $this->assertNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_unpublish'), + 'no unpublish action present for a saved draft page' + ); + $this->assertNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_rollback'), + 'no rollback action present for a saved draft page' + ); + $this->assertNull( + $actions->fieldByName('MajorActions.action_restore'), + 'no restore action present for a saved draft page' + ); + // END DRAFT + + // BEGIN PUBLISHED + $page->doPublish(); + $actions = $page->getCMSActions(); + $this->assertNull( + $actions->fieldByName('MajorActions.action_save'), + 'no save action present for a published page' + ); + $this->assertNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_archive'), + 'no archive action present for a saved draft page' + ); + $this->assertNotNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_rollback'), + 'rollback action present for a published page' + ); + $this->assertNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_unpublish'), + 'no unpublish action present for a published page' + ); + $this->assertNotNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_addtocampaign'), + 'addtocampaign action present for a published page' + ); + $this->assertNull( + $actions->fieldByName('MajorActions.action_restore'), + 'no restore action present for a published page' + ); + // END PUBLISHED + + // BEGIN DRAFT AFTER PUBLISHED + $page->Content = md5(rand(0, PHP_INT_MAX)); + $page->write(); + $actions = $page->getCMSActions(); + + $this->assertNotNull( + $actions->fieldByName('MajorActions.action_save'), + 'save action present for a changed published page' + ); + $this->assertNotNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_unpublish'), + 'unpublish action present for a changed published page' + ); + $this->assertNotNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_archive'), + 'archive action present for a changed published page' + ); + $this->assertNotNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_rollback'), + 'rollback action present for a changed published page' + ); + $this->assertNotNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_addtocampaign'), + 'addtocampaign action present for a changed published page' + ); + $this->assertNull( + $actions->fieldByName('MajorActions.action_restore'), + 'no restore action present for a changed published page' + ); + // END DRAFT AFTER PUBLISHED + + // BEGIN ARCHIVED + $page->doArchive(); + $actions = $page->getCMSActions(); + + $this->assertNull( + $actions->fieldByName('MajorActions.action_save'), + 'no save action present for a archived page' + ); + $this->assertNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_unpublish'), + 'no unpublish action present for a archived page' + ); + $this->assertNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_archive'), + 'no archive action present for a archived page' + ); + $this->assertNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_rollback'), + 'no rollback action present for a archived page' + ); + $this->assertNull( + $actions->fieldByName('ActionMenus.MoreOptions.action_addtocampaign'), + 'no addtocampaign action present for a archived page' + ); + $this->assertNotNull( + $actions->fieldByName('MajorActions.action_restore'), + 'restore action present for a archived page' + ); + // END ARCHIVED + } + + public function testGetCMSActionsWithoutForms() + { + // Create new page on DRAFT + $page = SiteTree::create(); + $page->Content = md5(rand(0, PHP_INT_MAX)); + $page->write(); + + // BEGIN DRAFT + $actions = $page->getCMSActions(); + + $this->assertEmpty( + $actions->fieldByName('MajorActions.action_save')->getForm(), + 'save action has no form when page is draft' + ); + $this->assertEmpty( + $actions->fieldByName('MajorActions.action_publish')->getForm(), + 'publish action has no form when page is draft' + ); + $this->assertEmpty( + $actions->fieldByName('ActionMenus.MoreOptions.action_archive')->getForm(), + 'archive action has no form when page is draft' + ); + $this->assertEmpty( + $actions->fieldByName('ActionMenus.MoreOptions.action_addtocampaign')->getForm(), + 'addtocampaign action has no form when page is draft' + ); + // END DRAFT + + // BEGIN PUBLISHED + $page->doPublish(); + $actions = $page->getCMSActions(); + + $this->assertEmpty( + $actions->fieldByName('ActionMenus.MoreOptions.action_rollback')->getForm(), + 'rollback action has no form when page is published' + ); + $this->assertEmpty( + $actions->fieldByName('ActionMenus.MoreOptions.action_addtocampaign')->getForm(), + 'addtocampaign action has no form when page is published' + ); + // END PUBLISHED + + // BEGIN DRAFT AFTER PUBLISHED + $page->Content = md5(rand(0, PHP_INT_MAX)); + $page->write(); + $actions = $page->getCMSActions(); + + $this->assertEmpty( + $actions->fieldByName('MajorActions.action_save')->getForm(), + 'save action has no form when page is draft after published' + ); + $this->assertEmpty( + $actions->fieldByName('ActionMenus.MoreOptions.action_unpublish')->getForm(), + 'unpublish action has no form when page is draft after published' + ); + $this->assertEmpty( + $actions->fieldByName('ActionMenus.MoreOptions.action_archive')->getForm(), + 'archive action has no form when page is draft after published' + ); + $this->assertEmpty( + $actions->fieldByName('ActionMenus.MoreOptions.action_rollback')->getForm(), + 'rollback action has no form when page is draft after published' + ); + $this->assertEmpty( + $actions->fieldByName('ActionMenus.MoreOptions.action_addtocampaign')->getForm(), + 'addtocampaign action has no form when page is draft after published' + ); + // END DRAFT AFTER PUBLISHED + + // BEGIN ARCHIVED + $page->doArchive(); + $actions = $page->getCMSActions(); + + $this->assertEmpty( + $actions->fieldByName('MajorActions.action_restore')->getForm(), + 'retore action has no form when page archived' + ); + // END ARCHIVED + } }