silverstripe-cms/tests/php/Controllers/CMSPageHistoryControllerTest.php

206 lines
8.0 KiB
PHP
Raw Normal View History

<?php
2017-08-09 04:53:38 +02:00
namespace SilverStripe\CMS\Tests\Controllers;
2017-08-09 03:25:12 +02:00
use SilverStripe\CMS\Controllers\CMSPageHistoryController;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HTMLReadonlyField;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\TextField;
2017-08-09 03:25:12 +02:00
2017-01-25 21:59:25 +01:00
class CMSPageHistoryControllerTest extends FunctionalTest
{
protected static $fixture_file = 'CMSPageHistoryControllerTest.yml';
protected $versionUnpublishedCheck;
protected $versionPublishCheck;
protected $versionUnpublishedCheck2;
protected $versionPublishCheck2;
2017-01-25 21:59:25 +01:00
protected $page;
protected static $extra_controllers = [
CMSPageHistoryControllerTest\HistoryController::class,
];
2021-10-27 23:40:52 +02:00
protected function setUp(): void
2017-01-25 21:59:25 +01:00
{
parent::setUp();
Injector::inst()->registerService(
new CMSPageHistoryController(),
CMSPageHistoryController::class
);
2017-01-25 21:59:25 +01:00
$this->loginWithPermission('ADMIN');
// creates a series of published, unpublished versions of a page
$this->page = new SiteTree();
2017-01-25 21:59:25 +01:00
$this->page->URLSegment = "test";
$this->page->Content = "new content";
$this->page->write();
$this->versionUnpublishedCheck = $this->page->Version; // v1
2017-01-25 21:59:25 +01:00
$this->page->Content = "some further content";
$this->page->publishSingle();
$this->versionPublishCheck = $this->page->Version; // v2
2017-01-25 21:59:25 +01:00
$this->page->Content = "No, more changes please";
$this->page->Title = "Changing titles too";
$this->page->write();
$this->versionUnpublishedCheck2 = $this->page->Version; // v3
2017-01-25 21:59:25 +01:00
$this->page->Title = "Final Change";
$this->page->publishSingle();
$this->versionPublishCheck2 = $this->page->Version; // v4
2017-01-25 21:59:25 +01:00
}
public function testGetEditForm()
{
$controller = new CMSPageHistoryController();
$controller->setRequest(Controller::curr()->getRequest());
2017-01-25 21:59:25 +01:00
// should get the latest version which we cannot rollback to
$form = $controller->getEditForm($this->page->ID);
$this->assertTrue($form->Actions()->dataFieldByName('action_doRollback')->isReadonly());
$this->assertEquals($this->page->ID, $form->Fields()->dataFieldByName('ID')->Value());
$this->assertEquals($this->versionPublishCheck2, $form->Fields()->dataFieldByName('Version')->Value());
2021-10-27 23:40:52 +02:00
$this->assertStringContainsString(
2017-01-25 21:59:25 +01:00
'Currently viewing the latest version',
$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
);
// edit form with a given version
$form = $controller->getEditForm($this->page->ID, null, $this->versionPublishCheck);
$this->assertFalse($form->Actions()->dataFieldByName('action_doRollback')->isReadonly());
$this->assertEquals($this->page->ID, $form->Fields()->dataFieldByName('ID')->Value());
$this->assertEquals($this->versionPublishCheck, $form->Fields()->dataFieldByName('Version')->Value());
2021-10-27 23:40:52 +02:00
$this->assertStringContainsString(
2017-01-25 21:59:25 +01:00
sprintf("Currently viewing version %s.", $this->versionPublishCheck),
$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
);
// check that compare mode updates the message
$form = $controller->getEditForm(
$this->page->ID,
null,
$this->versionPublishCheck,
$this->versionPublishCheck2
);
2021-10-27 23:40:52 +02:00
$this->assertStringContainsString(
2017-01-25 21:59:25 +01:00
sprintf("Comparing versions %s", $this->versionPublishCheck),
$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
);
2021-10-27 23:40:52 +02:00
$this->assertStringContainsString(
2017-01-25 21:59:25 +01:00
sprintf("and %s", $this->versionPublishCheck2),
$form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
);
}
/**
* @todo should be less tied to cms theme.
* @todo check highlighting for comparing pages.
*/
public function testVersionsForm()
{
$this->get('admin/pages/legacyhistory/show/' . $this->page->ID);
2017-01-25 21:59:25 +01:00
$form = $this->cssParser()->getBySelector('#Form_VersionsForm');
2022-04-13 07:07:59 +02:00
$this->assertEquals(1, count($form ?? []));
2017-01-25 21:59:25 +01:00
// check the page ID is present
$hidden = $form[0]->xpath("fieldset/input[@type='hidden']");
$this->assertThat($hidden, $this->logicalNot($this->isNull()), 'Hidden ID field exists');
$this->assertEquals($this->page->ID, (int) $hidden[0]->attributes()->value);
// ensure that all the versions are present in the table and displayed
$rows = $form[0]->xpath("fieldset/table/tbody/tr");
2022-04-13 07:07:59 +02:00
$this->assertEquals(4, count($rows ?? []));
2017-01-25 21:59:25 +01:00
}
public function testVersionsFormTableContainsInformation()
{
$this->get('admin/pages/legacyhistory/show/' . $this->page->ID);
2017-01-25 21:59:25 +01:00
$form = $this->cssParser()->getBySelector('#Form_VersionsForm');
$rows = $form[0]->xpath("fieldset/table/tbody/tr");
$expected = [
['version' => $this->versionPublishCheck2, 'status' => 'published'],
['version' => $this->versionUnpublishedCheck2, 'status' => 'internal'],
['version' => $this->versionPublishCheck, 'status' => 'published'],
['version' => $this->versionUnpublishedCheck, 'status' => 'internal'],
];
2017-01-25 21:59:25 +01:00
// goes the reverse order that we created in setUp()
$i = 0;
foreach ($rows as $tr) {
// data-link must be present for the javascript to load new
2021-10-27 23:40:52 +02:00
$this->assertStringContainsString($expected[$i]['status'], (string) $tr->attributes()->class);
2017-01-25 21:59:25 +01:00
$i++;
}
// test highlighting
2021-10-27 23:40:52 +02:00
$this->assertStringContainsString('active', (string) $rows[0]->attributes()->class);
2017-01-25 21:59:25 +01:00
$this->assertThat((string) $rows[1]->attributes()->class, $this->logicalNot($this->stringContains('active')));
}
public function testVersionsFormSelectsUnpublishedCheckbox()
{
$this->get('admin/pages/legacyhistory/show/' . $this->page->ID);
2017-01-25 21:59:25 +01:00
$checkbox = $this->cssParser()->getBySelector('#Form_VersionsForm_ShowUnpublished');
$this->assertThat($checkbox[0], $this->logicalNot($this->isNull()));
2021-10-27 23:40:52 +02:00
$checked = $checkbox[0]->attributes()->checked ?: '';
2017-01-25 21:59:25 +01:00
$this->assertThat($checked, $this->logicalNot($this->stringContains('checked')));
// viewing an unpublished
$this->get('admin/pages/legacyhistory/show/' . $this->page->ID . '/' . $this->versionUnpublishedCheck);
2017-01-25 21:59:25 +01:00
$checkbox = $this->cssParser()->getBySelector('#Form_VersionsForm_ShowUnpublished');
$this->assertThat($checkbox[0], $this->logicalNot($this->isNull()));
$this->assertEquals('checked', (string) $checkbox[0]->attributes()->checked);
}
public function testTransformReadonly()
{
/** @var CMSPageHistoryController $history */
$history = new CMSPageHistoryController();
$history->setRequest(Controller::curr()->getRequest());
$fieldList = FieldList::create([
FieldGroup::create('group', [
TextField::create('childField', 'child field'),
]),
TextField::create('field', 'field', 'My <del>value</del><ins>change</ins>'),
HiddenField::create('hiddenField', 'hidden field'),
]);
$newList = $history->transformReadonly($fieldList);
$field = $newList->dataFieldByName('field');
$this->assertTrue($field instanceof HTMLReadonlyField);
2021-10-27 23:40:52 +02:00
$this->assertStringContainsString('<ins>', $field->forTemplate());
$groupField = $newList->fieldByName('group');
$this->assertTrue($groupField instanceof FieldGroup);
$childField = $newList->dataFieldByName('childField');
$this->assertTrue($childField instanceof HTMLReadonlyField);
$hiddenField = $newList->dataFieldByName('hiddenField');
$this->assertTrue($hiddenField instanceof HiddenField);
}
}