diff --git a/tests/Behat/features/restore-to-draft.feature b/tests/Behat/features/restore-to-draft.feature new file mode 100644 index 00000000..6abf1cc4 --- /dev/null +++ b/tests/Behat/features/restore-to-draft.feature @@ -0,0 +1,25 @@ +@javascript +Feature: Restore to draft + As a CMS author + I want to restore archived version to draft + + Background: + Given a "page" "Home" + And a "page" "MyPage" + And the "group" "EDITOR" has permissions "Access to 'Pages' section" and "Access to 'Archive' section" + And I am logged in as a member of "EDITOR" group + And I go to "/admin/pages" + And I should see "MyPage" + And I click on "MyPage" in the tree + And I press the "Publish" button + And I click "More options" in the "#ActionMenus" element + And I press the "Unpublish and archive" button, confirming the dialog + + Scenario: I can restore archived version to draft + When I go to "/admin/archive" + Then I should see "MyPage" in the "#Form_EditForm" element + Then I click "MyPage" in the "#Form_EditForm" element + Then I press the "Restore to draft" button + Then I should see "Successfully restored the page" in the "#Form_EditForm" element + When I go to "/admin/pages" + And I should see "MyPage" in the ".cms-tree [data-pagetype='Page']:nth-of-type(2).status-addedtodraft" element diff --git a/tests/Extensions/ArchiveRestoreActionTest.php b/tests/Extensions/ArchiveRestoreActionTest.php new file mode 100644 index 00000000..6ea5dc07 --- /dev/null +++ b/tests/Extensions/ArchiveRestoreActionTest.php @@ -0,0 +1,86 @@ +<?php + +namespace SilverStripe\VersionedAdmin\Tests\Extensions; + +use SilverStripe\VersionedAdmin\Tests\Extensions\Controller\TestController; +use SilverStripe\Control\HTTPRequest; +use SilverStripe\Control\HTTPResponse; +use SilverStripe\Control\Session; +use SilverStripe\Dev\SapphireTest; +use SilverStripe\Forms\FieldList; +use SilverStripe\Forms\Form; +use SilverStripe\Forms\FormAction; +use SilverStripe\Forms\GridField\GridField; +use SilverStripe\Forms\GridField\GridFieldConfig_Base; +use SilverStripe\Forms\GridField\GridFieldDetailForm; +use SilverStripe\ORM\ArrayList; +use SilverStripe\View\ArrayData; +use SilverStripe\Versioned\VersionedGridFieldItemRequest; +use SilverStripe\VersionedAdmin\ArchiveAdmin; +use SilverStripe\VersionedAdmin\Tests\Controllers\HistoryViewerControllerTest\ViewableVersionedObject; + +class ArchiveRestoreActionTest extends SapphireTest +{ + protected static $fixture_file = 'ArchiveRestoreActionTest.yml'; + + protected static $extra_dataobjects = [ + ViewableVersionedObject::class, + ]; + + protected function setUp(): void + { + parent::setUp(); + $this->logInWithPermission('ADMIN'); + } + + protected function tearDown(): void + { + $this->logOut(); + parent::tearDown(); + } + + public function testDoRestore() + { + $object = $this->objFromFixture(ViewableVersionedObject::class, 'object_1'); + $gridField = GridField::create('Test', 'Test', ArrayList::create(), GridFieldConfig_Base::create()); + $gridField->setModelClass(ViewableVersionedObject::class); + $controller = TestController::create(ViewableVersionedObject::class); + $controller->setRequest(new HTTPRequest('GET', '/')); + $controller->getRequest()->setSession(new Session([])); + $form = Form::create($controller, 'TestForm', FieldList::create()); + + $itemRequest = VersionedGridFieldItemRequest::create( + $gridField, + $form, + $object, + $controller, + 'test' + ); + $object->doArchive(); + + $response = $itemRequest->doRestore([], $form); + $this->assertEquals($response->getStatusCode(), '302', 'Redirect status code should be 302'); + } + + public function testUpdateItemEditForm() + { + $object = $this->objFromFixture(ViewableVersionedObject::class, 'object_1'); + $gridField = GridField::create('Test', 'Test', ArrayList::create(), GridFieldConfig_Base::create()); + $controller = TestController::create(ViewableVersionedObject::class); + $controller->setRequest(new HTTPRequest('GET', '/')); + $controller->getRequest()->setSession(new Session([])); + $form = Form::create($controller, 'TestForm', FieldList::create()); + + $itemRequest = VersionedGridFieldItemRequest::create( + $gridField, + $form, + $object, + $controller, + 'test' + ); + + $itemRequest->updateItemEditForm($form); + $actions = $form->Actions(); + $this->assertInstanceOf(FormAction::class, $actions->fieldByName('action_doRestore')); + } +} diff --git a/tests/Extensions/ArchiveRestoreActionTest.yml b/tests/Extensions/ArchiveRestoreActionTest.yml new file mode 100644 index 00000000..5f074df5 --- /dev/null +++ b/tests/Extensions/ArchiveRestoreActionTest.yml @@ -0,0 +1,3 @@ +SilverStripe\VersionedAdmin\Tests\Controllers\HistoryViewerControllerTest\ViewableVersionedObject: + object_1: + Title: My Object diff --git a/tests/Extensions/Controller/TestController.php b/tests/Extensions/Controller/TestController.php new file mode 100644 index 00000000..6afa5ede --- /dev/null +++ b/tests/Extensions/Controller/TestController.php @@ -0,0 +1,18 @@ +<?php + +namespace SilverStripe\VersionedAdmin\Tests\Extensions\Controller; + +use SilverStripe\VersionedAdmin\ArchiveAdmin; +use SilverStripe\Dev\TestOnly; +use SilverStripe\VersionedAdmin\Tests\Controllers\HistoryViewerControllerTest\ViewableVersionedObject; + +class TestController extends ArchiveAdmin implements TestOnly +{ + private static $url_segment = 'test-archive'; + + public function __construct($modelClass) + { + parent::__construct(); + $this->modelClass = $modelClass; + } +}