Passed
Push — master ( 301f96...5c3d68 )
by Robbie
05:48
created

php/Controllers/CMSPageHistoryControllerTest.php (3 issues)

1
<?php
2
3
namespace SilverStripe\CMS\Tests\Controllers;
4
5
use SilverStripe\CMS\Controllers\CMSPageHistoryController;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\FunctionalTest;
10
use SilverStripe\Forms\FieldGroup;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\HiddenField;
13
use SilverStripe\Forms\HTMLReadonlyField;
14
use SilverStripe\Forms\TextField;
15
16
class CMSPageHistoryControllerTest extends FunctionalTest
17
{
18
    protected static $fixture_file = 'CMSPageHistoryControllerTest.yml';
19
20
    protected $versionUnpublishedCheck;
21
    protected $versionPublishCheck;
22
    protected $versionUnpublishedCheck2;
23
    protected $versionPublishCheck2;
24
    protected $page;
25
26
    protected static $extra_controllers = [
27
        CMSPageHistoryControllerTest\HistoryController::class,
28
    ];
29
30
    protected function setUp()
31
    {
32
        parent::setUp();
33
34
        Injector::inst()->registerService(
35
            new CMSPageHistoryController(),
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\CMS\Control...MSPageHistoryController has been deprecated: 4.3.0:5.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

35
            /** @scrutinizer ignore-deprecated */ new CMSPageHistoryController(),
Loading history...
36
            CMSPageHistoryController::class
37
        );
38
39
        $this->loginWithPermission('ADMIN');
40
41
        // creates a series of published, unpublished versions of a page
42
        $this->page = SiteTree::create();
43
        $this->page->URLSegment = "test";
44
        $this->page->Content = "new content";
45
        $this->page->write();
46
        $this->versionUnpublishedCheck = $this->page->Version; // v1
47
48
        $this->page->Content = "some further content";
49
        $this->page->publishSingle();
50
        $this->versionPublishCheck = $this->page->Version; // v2
51
52
        $this->page->Content = "No, more changes please";
53
        $this->page->Title = "Changing titles too";
54
        $this->page->write();
55
        $this->versionUnpublishedCheck2 = $this->page->Version; // v3
56
57
        $this->page->Title = "Final Change";
58
        $this->page->publishSingle();
59
        $this->versionPublishCheck2 = $this->page->Version; // v4
60
    }
61
62
    public function testGetEditForm()
63
    {
64
        $controller = new CMSPageHistoryController();
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\CMS\Control...MSPageHistoryController has been deprecated: 4.3.0:5.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
        $controller = /** @scrutinizer ignore-deprecated */ new CMSPageHistoryController();
Loading history...
65
        $controller->setRequest(Controller::curr()->getRequest());
66
67
        // should get the latest version which we cannot rollback to
68
        $form = $controller->getEditForm($this->page->ID);
69
70
        $this->assertTrue($form->Actions()->dataFieldByName('action_doRollback')->isReadonly());
71
72
        $this->assertEquals($this->page->ID, $form->Fields()->dataFieldByName('ID')->Value());
73
        $this->assertEquals($this->versionPublishCheck2, $form->Fields()->dataFieldByName('Version')->Value());
74
75
        $this->assertContains(
76
            'Currently viewing the latest version',
77
            $form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
78
        );
79
80
        // edit form with a given version
81
        $form = $controller->getEditForm($this->page->ID, null, $this->versionPublishCheck);
82
        $this->assertFalse($form->Actions()->dataFieldByName('action_doRollback')->isReadonly());
83
84
        $this->assertEquals($this->page->ID, $form->Fields()->dataFieldByName('ID')->Value());
85
        $this->assertEquals($this->versionPublishCheck, $form->Fields()->dataFieldByName('Version')->Value());
86
        $this->assertContains(
87
            sprintf("Currently viewing version %s.", $this->versionPublishCheck),
88
            $form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
89
        );
90
91
        // check that compare mode updates the message
92
        $form = $controller->getEditForm(
93
            $this->page->ID,
94
            null,
95
            $this->versionPublishCheck,
96
            $this->versionPublishCheck2
97
        );
98
        $this->assertContains(
99
            sprintf("Comparing versions %s", $this->versionPublishCheck),
100
            $form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
101
        );
102
103
        $this->assertContains(
104
            sprintf("and %s", $this->versionPublishCheck2),
105
            $form->Fields()->fieldByName('Root.Main.CurrentlyViewingMessage')->getContent()
106
        );
107
    }
108
109
    /**
110
     * @todo should be less tied to cms theme.
111
     * @todo check highlighting for comparing pages.
112
     */
113
    public function testVersionsForm()
114
    {
115
        $this->get('admin/pages/legacyhistory/show/' . $this->page->ID . '?flush');
116
        $form = $this->cssParser()->getBySelector('#Form_VersionsForm');
117
118
        $this->assertEquals(1, count($form));
119
120
        // check the page ID is present
121
        $hidden = $form[0]->xpath("fieldset/input[@type='hidden']");
122
123
        $this->assertThat($hidden, $this->logicalNot($this->isNull()), 'Hidden ID field exists');
124
        $this->assertEquals($this->page->ID, (int) $hidden[0]->attributes()->value);
125
126
        // ensure that all the versions are present in the table and displayed
127
        $rows = $form[0]->xpath("fieldset/table/tbody/tr");
128
        $this->assertEquals(4, count($rows));
129
    }
130
131
    public function testVersionsFormTableContainsInformation()
132
    {
133
        $this->get('admin/pages/legacyhistory/show/' . $this->page->ID);
134
        $form = $this->cssParser()->getBySelector('#Form_VersionsForm');
135
        $rows = $form[0]->xpath("fieldset/table/tbody/tr");
136
137
        $expected = [
138
            ['version' => $this->versionPublishCheck2, 'status' => 'published'],
139
            ['version' => $this->versionUnpublishedCheck2, 'status' => 'internal'],
140
            ['version' => $this->versionPublishCheck, 'status' => 'published'],
141
            ['version' => $this->versionUnpublishedCheck, 'status' => 'internal'],
142
        ];
143
144
        // goes the reverse order that we created in setUp()
145
        $i = 0;
146
        foreach ($rows as $tr) {
147
            // data-link must be present for the javascript to load new
148
            $this->assertContains($expected[$i]['status'], (string) $tr->attributes()->class);
149
            $i++;
150
        }
151
152
        // test highlighting
153
        $this->assertContains('active', (string) $rows[0]->attributes()->class);
154
        $this->assertThat((string) $rows[1]->attributes()->class, $this->logicalNot($this->stringContains('active')));
155
    }
156
157
    public function testVersionsFormSelectsUnpublishedCheckbox()
158
    {
159
        $this->get('admin/pages/legacyhistory/show/' . $this->page->ID);
160
        $checkbox = $this->cssParser()->getBySelector('#Form_VersionsForm_ShowUnpublished');
161
162
        $this->assertThat($checkbox[0], $this->logicalNot($this->isNull()));
163
        $checked = $checkbox[0]->attributes()->checked;
164
165
        $this->assertThat($checked, $this->logicalNot($this->stringContains('checked')));
166
167
        // viewing an unpublished
168
        $this->get('admin/pages/legacyhistory/show/' . $this->page->ID . '/' . $this->versionUnpublishedCheck);
169
        $checkbox = $this->cssParser()->getBySelector('#Form_VersionsForm_ShowUnpublished');
170
171
        $this->assertThat($checkbox[0], $this->logicalNot($this->isNull()));
172
        $this->assertEquals('checked', (string) $checkbox[0]->attributes()->checked);
173
    }
174
175
    public function testTransformReadonly()
176
    {
177
        /** @var CMSPageHistoryController $history */
178
        $history = new CMSPageHistoryController();
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\CMS\Control...MSPageHistoryController has been deprecated: 4.3.0:5.0.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

178
        $history = /** @scrutinizer ignore-deprecated */ new CMSPageHistoryController();
Loading history...
179
        $history->setRequest(Controller::curr()->getRequest());
180
181
        $fieldList = FieldList::create([
182
            FieldGroup::create('group', [
183
                TextField::create('childField', 'child field'),
184
            ]),
185
            TextField::create('field', 'field', 'My <del>value</del><ins>change</ins>'),
186
            HiddenField::create('hiddenField', 'hidden field'),
187
        ]);
188
189
        $newList = $history->transformReadonly($fieldList);
190
191
        $field = $newList->dataFieldByName('field');
192
        $this->assertTrue($field instanceof HTMLReadonlyField);
193
        $this->assertContains('<ins>', $field->forTemplate());
194
195
        $groupField = $newList->fieldByName('group');
196
        $this->assertTrue($groupField instanceof FieldGroup);
197
198
        $childField = $newList->dataFieldByName('childField');
199
        $this->assertTrue($childField instanceof HTMLReadonlyField);
200
201
        $hiddenField = $newList->dataFieldByName('hiddenField');
202
        $this->assertTrue($hiddenField instanceof HiddenField);
203
    }
204
}
205