Completed
Push — master ( 4a6357...521c8c )
by Franco
10s
created

SiteTreeContentReviewTest::testOwnerNames()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\ContentReview\Tests;
4
5
use Page;
6
use SilverStripe\CMS\Controllers\CMSPageEditController;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\ContentReview\Extensions\SiteTreeContentReview;
9
use SilverStripe\ContentReview\Extensions\ContentReviewOwner;
10
use SilverStripe\ContentReview\Extensions\ContentReviewCMSExtension;
11
use SilverStripe\ContentReview\Extensions\ContentReviewDefaultSettings;
12
use SilverStripe\Security\Group;
13
use SilverStripe\Security\Member;
14
use SilverStripe\SiteConfig\SiteConfig;
15
use SilverStripe\ORM\FieldType\DBDatetime;
16
use SilverStripe\Versioned\Versioned;
17
18
/**
19
 * @mixin PHPUnit_Framework_TestCase
20
 */
21
class SiteTreeContentReviewTest extends ContentReviewBaseTest
22
{
23
    /**
24
     * @var string
25
     */
26
    protected static $fixture_file = 'ContentReviewTest.yml';
27
28
    /**
29
     * @var array
30
     */
31
    protected static $required_extensions = [
32
        SiteTree::class              => [SiteTreeContentReview::class],
33
        Group::class                 => [ContentReviewOwner::class],
34
        Member::class                => [ContentReviewOwner::class],
35
        CMSPageEditController::class => [ContentReviewCMSExtension::class],
36
        SiteConfig::class            => [ContentReviewDefaultSettings::class],
37
    ];
38
39
    public function testOwnerNames()
40
    {
41
        /** @var Member $editor */
42
        $editor = $this->objFromFixture(Member::class, "editor");
43
44
        $this->logInAs($editor);
45
46
        /** @var Page|SiteTreeContentReview $page */
47
        $page = new Page();
48
        $page->ReviewPeriodDays = 10;
49
        $page->ContentReviewType = "Custom";
50
51
        $page->ContentReviewUsers()->push($editor);
52
        $page->write();
53
54
        $this->assertTrue($page->canPublish());
55
        $this->assertTrue($page->doPublish());
56
        $this->assertEquals($page->OwnerNames, "Test Editor", "Test Editor should be the owner");
57
58
        /** @var Page|SiteTreeContentReview $page */
59
        $page = $this->objFromFixture(Page::class, "about");
60
61
        $page->OwnerUsers()->removeAll();
62
        $page->write();
63
64
        $this->assertTrue($page->canPublish());
65
        $this->assertTrue($page->doPublish());
66
        $this->assertEquals("", $page->OwnerNames);
67
    }
68
69
    public function testPermissionsExists()
70
    {
71
        $perms = singleton(SiteTreeContentReview::class)->providePermissions();
72
73
        $this->assertTrue(isset($perms["EDIT_CONTENT_REVIEW_FIELDS"]));
74
    }
75
76 View Code Duplication
    public function testUserWithPermissionCanEdit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        /** @var Member $editor */
79
        $editor = $this->objFromFixture(Member::class, "editor");
80
81
        $this->logInAs($editor);
82
83
        /** @var Page|SiteTreeContentReview $page */
84
        $page = new Page();
85
86
        $fields = $page->getSettingsFields();
87
88
        $this->assertNotNull($fields->dataFieldByName("NextReviewDate"));
89
    }
90
91 View Code Duplication
    public function testUserWithoutPermissionCannotEdit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        /** @var Member $author */
94
        $author = $this->objFromFixture(Member::class, "author");
95
96
        $this->logInAs($author);
97
98
        /** @var Page|SiteTreeContentReview $page */
99
        $page = new Page();
100
101
        $fields = $page->getSettingsFields();
102
103
        $this->assertNull($fields->dataFieldByName("NextReviewDate"));
104
    }
105
106
    public function testAutomaticallyToNotSetReviewDate()
107
    {
108
        /** @var Member $editor */
109
        $editor = $this->objFromFixture(Member::class, "editor");
110
111
        $this->logInAs($editor);
112
113
        /** @var Page|SiteTreeContentReview $page */
114
        $page = new Page();
115
116
        $page->ReviewPeriodDays = 10;
117
        $page->write();
118
119
        $this->assertTrue($page->doPublish());
120
        $this->assertEquals(null, $page->NextReviewDate);
121
    }
122
123
    public function testAddReviewNote()
124
    {
125
        /** @var Member $author */
126
        $author = $this->objFromFixture(Member::class, "author");
127
128
        /** @var Page|SiteTreeContentReview $page */
129
        $page = $this->objFromFixture(Page::class, "home");
130
131
        $page->addReviewNote($author, "This is a message");
132
133
        /** @var Page|SiteTreeContentReview $page */
134
        $homepage = $this->objFromFixture(Page::class, "home");
135
136
        $this->assertEquals(1, $homepage->ReviewLogs()->count());
137
        $this->assertEquals("This is a message", $homepage->ReviewLogs()->first()->Note);
138
    }
139
140
    public function testGetContentReviewOwners()
141
    {
142
        /** @var Page|SiteTreeContentReview $page */
143
        $page = $this->objFromFixture(Page::class, "group-owned");
144
145
        $owners = $page->ContentReviewOwners();
146
147
        $this->assertEquals(1, $owners->count());
148
        $this->assertEquals("[email protected]", $owners->first()->Email);
149
    }
150
151 View Code Duplication
    public function testCanNotBeReviewBecauseNoReviewDate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
    {
153
        DBDatetime::set_mock_now("2010-01-01 12:00:00");
154
155
        /** @var Member $author */
156
        $author = $this->objFromFixture(Member::class, "author");
157
158
        /** @var Page|SiteTreeContentReview $page */
159
        $page = $this->objFromFixture(Page::class, "no-review");
160
161
        $this->assertFalse($page->canBeReviewedBy($author));
162
163
        DBDatetime::clear_mock_now();
164
    }
165
166 View Code Duplication
    public function testCanNotBeReviewedBecauseInFuture()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168
        DBDatetime::set_mock_now("2010-01-01 12:00:00");
169
170
        /** @var Member $author */
171
        $author = $this->objFromFixture(Member::class, "author");
172
173
        /** @var Page|SiteTreeContentReview $page */
174
        $page = $this->objFromFixture(Page::class, "staff");
175
176
        $this->assertFalse($page->canBeReviewedBy($author));
177
178
        DBDatetime::clear_mock_now();
179
    }
180
181 View Code Duplication
    public function testCanNotBeReviewedByUser()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        DBDatetime::set_mock_now("2010-03-01 12:00:00");
184
185
        /** @var Member $author */
186
        $author = $this->objFromFixture(Member::class, "author");
187
188
        /** @var Page|SiteTreeContentReview $page */
189
        $page = $this->objFromFixture(Page::class, "home");
190
191
        $this->assertFalse($page->canBeReviewedBy($author));
192
193
        DBDatetime::clear_mock_now();
194
    }
195
196 View Code Duplication
    public function testCanBeReviewedByUser()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
    {
198
        DBDatetime::set_mock_now("2010-03-01 12:00:00");
199
200
        /** @var Member $author */
201
        $author = $this->objFromFixture(Member::class, "author");
202
203
        /** @var Page|SiteTreeContentReview $page */
204
        $page = $this->objFromFixture(Page::class, "staff");
205
206
        $this->assertTrue($page->canBeReviewedBy($author));
207
208
        DBDatetime::clear_mock_now();
209
    }
210
211 View Code Duplication
    public function testCanNotBeReviewedByGroup()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
212
    {
213
        DBDatetime::set_mock_now("2010-03-01 12:00:00");
214
215
        /** @var Member $author */
216
        $author = $this->objFromFixture(Member::class, "editor");
217
218
        /** @var Page|SiteTreeContentReview $page */
219
        $page = $this->objFromFixture(Page::class, "contact");
220
221
        $this->assertFalse($page->canBeReviewedBy($author));
222
223
        DBDatetime::clear_mock_now();
224
    }
225
226 View Code Duplication
    public function testCanBeReviewedByGroup()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
    {
228
        DBDatetime::set_mock_now("2010-03-01 12:00:00");
229
230
        /** @var Member $author */
231
        $author = $this->objFromFixture(Member::class, "author");
232
233
        /** @var Page|SiteTreeContentReview $page */
234
        $page = $this->objFromFixture(Page::class, "contact");
235
236
        $this->assertTrue($page->canBeReviewedBy($author));
237
238
        DBDatetime::clear_mock_now();
239
    }
240
241 View Code Duplication
    public function testCanBeReviewedFromInheritedSetting()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
    {
243
        DBDatetime::set_mock_now("2013-03-01 12:00:00");
244
245
        /** @var Member $author */
246
        $author = $this->objFromFixture(Member::class, "author");
247
248
        /** @var Page|SiteTreeContentReview $parentPage */
249
        $parentPage = $this->objFromFixture(Page::class, "contact");
250
251
        $parentPage->NextReviewDate = "2013-01-01";
252
        $parentPage->write();
253
254
        /** @var Page|SiteTreeContentReview $page */
255
        $page = $this->objFromFixture(Page::class, "contact-child");
256
257
        $this->assertTrue($page->canBeReviewedBy($author));
258
259
        DBDatetime::clear_mock_now();
260
    }
261
262
    public function testUnModifiedPagesDontChangeEditor()
263
    {
264
        DBDatetime::set_mock_now("2013-03-01 12:00:00");
265
266
        /** @var Member $author */
267
        $author = $this->objFromFixture(Member::class, "author");
268
        $this->logInAs($author);
269
270
        // Page which is un-modified doesn't advance version of have an editor assigned
271
        $contactPage = $this->objFromFixture(Page::class, "contact");
272
        $contactPageVersion = $contactPage->Version;
273
        $contactPage->write();
274
        $this->assertEmpty($contactPage->LastEditedByName);
0 ignored issues
show
Bug introduced by
The property LastEditedByName does not seem to exist. Did you mean LastEdited?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
275
        $this->assertEquals(
276
            $contactPageVersion,
277
            Versioned::get_versionnumber_by_stage(SiteTree::class, 'Stage', $contactPage->ID, false)
278
        );
279
280
        // Page with modifications gets marked
281
        $homePage = $this->objFromFixture(Page::class, "home");
282
        $homePageVersion = $homePage->Version;
283
        $homePage->Content = '<p>Welcome!</p>';
284
        $homePage->write();
285
        $this->assertNotEmpty($homePage->LastEditedByName);
0 ignored issues
show
Bug introduced by
The property LastEditedByName does not seem to exist. Did you mean LastEdited?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
286
        $this->assertEquals($author->getTitle(), $homePage->LastEditedByName);
0 ignored issues
show
Bug introduced by
The property LastEditedByName does not seem to exist. Did you mean LastEdited?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
287
        $this->assertGreaterThan(
288
            $homePageVersion,
289
            Versioned::get_versionnumber_by_stage(SiteTree::class, 'Stage', $homePage->ID, false)
290
        );
291
292
        DBDatetime::clear_mock_now();
293
    }
294
295 View Code Duplication
    public function testReviewActionVisibleForAuthor()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
296
    {
297
        DBDatetime::set_mock_now("2020-03-01 12:00:00");
298
299
        /** @var Page|SiteTreeContentReview $page */
300
        $page = $this->objFromFixture(Page::class, "contact");
301
302
        /** @var Member $author */
303
        $author = $this->objFromFixture(Member::class, "author");
304
305
        $this->logInAs($author);
306
307
        $fields = $page->getCMSActions();
308
309
        $this->assertNotNull($fields->fieldByName("ActionMenus.ReviewContent"));
310
311
        DBDatetime::clear_mock_now();
312
    }
313
314 View Code Duplication
    public function testReviewActionNotVisibleForEditor()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
315
    {
316
        DBDatetime::set_mock_now("2020-03-01 12:00:00");
317
318
        /** @var Page|SiteTreeContentReview $page */
319
        $page = $this->objFromFixture(Page::class, "contact");
320
321
        /** @var Member $author */
322
        $author = $this->objFromFixture(Member::class, "editor");
323
324
        $this->logInAs($author);
325
326
        $fields = $page->getCMSActions();
327
328
        $this->assertNull($fields->fieldByName("ActionMenus.ReviewContent"));
329
330
        DBDatetime::clear_mock_now();
331
    }
332
}
333