Issues (160)

tests/php/Model/SiteTreeActionsTest.php (5 issues)

1
<?php
2
3
namespace SilverStripe\CMS\Tests\Model;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\Dev\FunctionalTest;
7
use SilverStripe\ORM\DB;
8
use SilverStripe\Security\Member;
9
use SilverStripe\Security\Security;
10
use SilverStripe\Versioned\Versioned;
11
12
/**
13
 * Possible actions:
14
 * - action_save
15
 * - action_publish
16
 * - action_unpublish
17
 * - action_archive
18
 * - action_rollback
19
 * - action_revert
20
 */
21
class SiteTreeActionsTest extends FunctionalTest
22
{
23
    protected static $fixture_file = 'SiteTreeActionsTest.yml';
24
25
    public function testActionsReadonly()
26
    {
27
        // Publish record
28
        $this->logInWithPermission('ADMIN');
29
        $page = new SiteTreeActionsTest_Page();
30
        $page->CanEditType = 'LoggedInUsers';
0 ignored issues
show
Bug Best Practice introduced by
The property CanEditType does not exist on SilverStripe\CMS\Tests\M...iteTreeActionsTest_Page. Since you implemented __set, consider adding a @property annotation.
Loading history...
31
        $page->write();
32
        $page->publishRecursive();
0 ignored issues
show
The method publishRecursive() does not exist on SilverStripe\CMS\Tests\M...iteTreeActionsTest_Page. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

32
        $page->/** @scrutinizer ignore-call */ 
33
               publishRecursive();
Loading history...
33
34
        // Log in as another user
35
        $readonlyEditor = $this->objFromFixture(Member::class, 'cmsreadonlyeditor');
36
        Security::setCurrentUser($readonlyEditor);
37
38
        // Reload latest version
39
        $page = SiteTree::get()->byID($page->ID);
40
        $actions = $page->getCMSActions();
41
42
        $this->assertNull($actions->dataFieldByName('action_save'));
43
        $this->assertNull($actions->dataFieldByName('action_publish'));
44
        $this->assertNull($actions->dataFieldByName('action_unpublish'));
45
        $this->assertNull($actions->dataFieldByName('action_archive'));
46
        $this->assertNull($actions->dataFieldByName('action_rollback'));
47
        $this->assertNull($actions->dataFieldByName('action_revert'));
48
    }
49
50
    public function testActionsNoDeletePublishedRecord()
51
    {
52
        $this->logInWithPermission('ADMIN');
53
54
        $page = new SiteTreeActionsTest_Page();
55
        $page->CanEditType = 'LoggedInUsers';
0 ignored issues
show
Bug Best Practice introduced by
The property CanEditType does not exist on SilverStripe\CMS\Tests\M...iteTreeActionsTest_Page. Since you implemented __set, consider adding a @property annotation.
Loading history...
56
        $page->write();
57
        $pageID = $page->ID;
58
        $page->publishRecursive();
59
        $page->deleteFromStage(Versioned::DRAFT);
0 ignored issues
show
The method deleteFromStage() does not exist on SilverStripe\CMS\Tests\M...iteTreeActionsTest_Page. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

59
        $page->/** @scrutinizer ignore-call */ 
60
               deleteFromStage(Versioned::DRAFT);
Loading history...
60
61
        // Get the live version of the page
62
        $page = Versioned::get_one_by_stage(SiteTree::class, "Live", "\"SiteTree\".\"ID\" = $pageID");
63
        $this->assertInstanceOf(SiteTree::class, $page);
64
65
        // Check that someone without the right permission can't delete the page
66
        $editor = $this->objFromFixture(Member::class, 'cmsnodeleteeditor');
67
        Security::setCurrentUser($editor);
68
69
        $actions = $page->getCMSActions();
70
        $this->assertNull($actions->dataFieldByName('action_archive'));
71
72
        // Check that someone with the right permission can delete the page
73
        /** @var Member $member */
74
        $member = $this->objFromFixture(Member::class, 'cmseditor');
75
        Security::setCurrentUser($member);
76
        $actions = $page->getCMSActions();
77
        $this->assertNotNull($actions->dataFieldByName('action_archive'));
78
    }
79
80
    public function testActionsPublishedRecord()
81
    {
82
        $author = $this->objFromFixture(Member::class, 'cmseditor');
83
        Security::setCurrentUser($author);
84
85
        /** @var SiteTree $page */
86
        $page = SiteTree::create();
87
        $page->CanEditType = 'LoggedInUsers';
88
        $page->write();
89
        $page->publishRecursive();
90
91
        // Reload latest version
92
        $page = SiteTree::get()->byID($page->ID);
93
94
        $actions = $page->getCMSActions();
95
96
        $this->assertNotNull($actions->dataFieldByName('action_save'));
97
        $this->assertNotNull($actions->dataFieldByName('action_publish'));
98
        $this->assertNotNull($actions->dataFieldByName('action_unpublish'));
99
        $this->assertNotNull($actions->dataFieldByName('action_archive'));
100
        $this->assertNull($actions->dataFieldByName('action_rollback'));
101
        $this->assertNull($actions->dataFieldByName('action_revert'));
102
    }
103
104
    public function testActionsDeletedFromStageRecord()
105
    {
106
        $author = $this->objFromFixture(Member::class, 'cmseditor');
107
        Security::setCurrentUser($author);
108
109
        $page = SiteTree::create();
110
        $page->CanEditType = 'LoggedInUsers';
111
        $page->write();
112
        $this->assertTrue($page->canPublish());
113
        $pageID = $page->ID;
114
        $page->publishRecursive();
115
        $page->deleteFromStage('Stage');
116
117
        // Get the live version of the page
118
        $page = Versioned::get_one_by_stage(SiteTree::class, "Live", "\"SiteTree\".\"ID\" = $pageID");
119
        $this->assertInstanceOf(SiteTree::class, $page);
120
121
        $actions = $page->getCMSActions();
122
123
        $this->assertNull($actions->dataFieldByName('action_save'));
124
        $this->assertNull($actions->dataFieldByName('action_publish'));
125
        $this->assertNull($actions->dataFieldByName('action_unpublish'));
126
        $this->assertNotNull($actions->dataFieldByName('action_archive'));
127
        $this->assertNull($actions->dataFieldByName('action_rollback'));
128
        $this->assertNotNull($actions->dataFieldByName('action_revert'));
129
    }
130
131
    public function testActionsChangedOnStageRecord()
132
    {
133
        $author = $this->objFromFixture(Member::class, 'cmseditor');
134
        Security::setCurrentUser($author);
135
136
        $page = SiteTree::create();
137
        $page->CanEditType = 'LoggedInUsers';
138
        $page->write();
139
        $this->assertTrue($page->canPublish());
140
        $page->publishRecursive();
141
        $page->Content = 'Changed on Stage';
142
        $page->write();
143
        $page->flushCache();
144
145
        // Reload latest version
146
        $page = SiteTree::get()->byID($page->ID);
147
148
        $actions = $page->getCMSActions();
149
        $this->assertNotNull($actions->dataFieldByName('action_save'));
150
        $this->assertNotNull($actions->dataFieldByName('action_publish'));
151
        $this->assertNotNull($actions->dataFieldByName('action_unpublish'));
152
        $this->assertNotNull($actions->dataFieldByName('action_archive'));
153
        $this->assertNotNull($actions->dataFieldByName('action_rollback'));
154
        $this->assertNull($actions->dataFieldByName('action_revert'));
155
    }
156
157
    public function testActionsViewingOldVersion()
158
    {
159
        $p = SiteTree::create();
160
        $p->Content = 'test page first version';
161
        $p->write();
162
        $p->Content = 'new content';
163
        $p->write();
164
165
        // Looking at the old version, the ability to rollback to that version is available
166
        $version = DB::query(
167
            'SELECT "Version" FROM "SiteTree_Versions" WHERE "Content" = \'test page first version\''
168
        )->value();
169
        $old = Versioned::get_version(SiteTree::class, $p->ID, $version);
0 ignored issues
show
$version of type string is incompatible with the type integer expected by parameter $version of SilverStripe\Versioned\Versioned::get_version(). ( Ignorable by Annotation )

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

169
        $old = Versioned::get_version(SiteTree::class, $p->ID, /** @scrutinizer ignore-type */ $version);
Loading history...
170
        $actions = $old->getCMSActions();
171
        $this->assertNull($actions->dataFieldByName('action_save'));
172
        $this->assertNull($actions->dataFieldByName('action_publish'));
173
        $this->assertNull($actions->dataFieldByName('action_unpublish'));
174
        $this->assertNotNull($actions->dataFieldByName('action_email'));
175
        $this->assertNotNull($actions->dataFieldByName('action_rollback'));
176
    }
177
}
178