Skip to content

Commit

Permalink
Merge pull request #334 from creative-commoners/pulls/2.2/add-tests-d…
Browse files Browse the repository at this point in the history
…orestore

MNT New test cases for doRestore button
  • Loading branch information
GuySartorelli authored Apr 12, 2024
2 parents 4ea609d + dee7670 commit 085e933
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/Behat/features/restore-to-draft.feature
Original file line number Diff line number Diff line change
@@ -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
86 changes: 86 additions & 0 deletions tests/Extensions/ArchiveRestoreActionTest.php
Original file line number Diff line number Diff line change
@@ -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'));
}
}
3 changes: 3 additions & 0 deletions tests/Extensions/ArchiveRestoreActionTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SilverStripe\VersionedAdmin\Tests\Controllers\HistoryViewerControllerTest\ViewableVersionedObject:
object_1:
Title: My Object
18 changes: 18 additions & 0 deletions tests/Extensions/Controller/TestController.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 085e933

Please sign in to comment.