Completed
Push — master ( 4a6357...521c8c )
by Franco
17s queued 12s
created

testGetNextReviewDateFromPageInheritedSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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\ContentReviewCMSExtension;
9
use SilverStripe\ContentReview\Extensions\ContentReviewDefaultSettings;
10
use SilverStripe\ContentReview\Extensions\ContentReviewOwner;
11
use SilverStripe\ContentReview\Extensions\SiteTreeContentReview;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\ORM\FieldType\DBDate;
15
use SilverStripe\ORM\FieldType\DBDatetime;
16
use SilverStripe\Security\Group;
17
use SilverStripe\Security\Member;
18
use SilverStripe\SiteConfig\SiteConfig;
19
20
/**
21
 * This class tests that settings are inherited correctly based on the inherited,
22
 * custom or disabled settings.
23
 *
24
 * @mixin PHPUnit_Framework_TestCase
25
 */
26
class ContentReviewSettingsTest extends SapphireTest
27
{
28
    /**
29
     * @var string
30
     */
31
    protected static $fixture_file = 'ContentReviewSettingsTest.yml';
32
33
    /**
34
     * @var array
35
     */
36
    protected static $required_extensions = [
37
        SiteTree::class              => [SiteTreeContentReview::class],
38
        Group::class                 => [ContentReviewOwner::class],
39
        Member::class                => [ContentReviewOwner::class],
40
        CMSPageEditController::class => [ContentReviewCMSExtension::class],
41
        SiteConfig::class            => [ContentReviewDefaultSettings::class],
42
    ];
43
44
    public function testAdvanceReviewDate10Days()
45
    {
46
        /** @var Page|SiteTreeContentReview $page */
47
        $page = new Page();
48
49
        $page->ContentReviewType = "Custom";
50
        $page->ReviewPeriodDays = 10;
51
52
        $this->assertTrue($page->advanceReviewDate());
53
54
        $page->write();
55
56
        $this->assertEquals(date('Y-m-d', strtotime("now + 10 days")), $page->NextReviewDate);
57
    }
58
59
    public function testAdvanceReviewDateNull()
60
    {
61
        /** @var Page|SiteTreeContentReview $page */
62
        $page = new Page();
63
64
        $page->ContentReviewType = "Custom";
65
        $page->ReviewPeriodDays = 0;
66
67
        $this->assertFalse($page->advanceReviewDate());
68
69
        $page->write();
70
71
        $this->assertEquals(null, $page->NextReviewDate);
72
    }
73
74 View Code Duplication
    public function testAdvanceReviewFromCustomSettings()
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...
75
    {
76
        /** @var Page|SiteTreeContentReview $page */
77
        $page = $this->objFromFixture(Page::class, "custom");
78
79
        $this->assertTrue($page->advanceReviewDate());
80
81
        $page->write();
82
83
        $this->assertEquals(date('Y-m-d', strtotime("now + " . $page->ReviewPeriodDays . " days")), $page->NextReviewDate);
84
    }
85
86
    public function testAdvanceReviewFromInheritedSettings()
87
    {
88
        // When a parent page is advanced, the next review date of the child is not automatically advanced
89
        $parentPage = $this->objFromFixture(Page::class, "page-1");
90
        $this->assertTrue($parentPage->advanceReviewDate());
91
        $parentPage->write();
92
93
        $page = $this->objFromFixture(Page::class, "page-1-1");
94
        $this->assertEquals(date('Y-m-d', strtotime("now + 5 days")), $parentPage->NextReviewDate);
95
        $this->assertEquals('2011-04-12', $page->NextReviewDate);
96
97
        // When a sub page is advanced, the next review date is advanced by the number of days in the parent
98
        $this->assertTrue($page->advanceReviewDate());
99
        $page->write();
100
        $this->assertEquals(date('Y-m-d', strtotime("now + 5 days")), $page->NextReviewDate);
101
    }
102
103 View Code Duplication
    public function testAdvanceReviewFromInheritedSiteConfigSettings()
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...
104
    {
105
        /** @var Page|SiteTreeContentReview $page */
106
        $page = $this->objFromFixture(Page::class, "inherit");
107
108
        /** @var SiteConfig|ContentReviewDefaultSettings $siteConfig */
109
        $siteConfig = $this->objFromFixture(SiteConfig::class, "default");
110
111
        $this->assertTrue($page->advanceReviewDate());
112
113
        $page->write();
114
115
        $this->assertEquals(date('Y-m-d', strtotime("now + " . $siteConfig->ReviewPeriodDays . " days")), $page->NextReviewDate);
116
    }
117
118 View Code Duplication
    public function testGetSettingsObjectFromCustom()
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...
119
    {
120
        /** @var Page|SiteTreeContentReview $page */
121
        $page = $this->objFromFixture(Page::class, "custom");
122
123
        $this->assertEquals("Custom", $page->ContentReviewType);
124
        $this->assertEquals($page, $page->getOptions());
125
    }
126
127 View Code Duplication
    public function testGetSettingsObjectFromDisabled()
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...
128
    {
129
        /** @var Page|SiteTreeContentReview $page */
130
        $page = $this->objFromFixture(Page::class, "disabled");
131
132
        $this->assertEquals("Disabled", $page->ContentReviewType);
133
        $this->assertFalse($page->getOptions());
134
    }
135
136 View Code Duplication
    public function testGetOptionObjectFromInheritedDisabled()
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...
137
    {
138
        /** @var Page|SiteTreeContentReview $page */
139
        $page = $this->objFromFixture(Page::class, "page-2-1-1");
140
141
        $this->assertEquals("Inherit", $page->ContentReviewType);
142
        $this->assertFalse($page->getOptions());
143
    }
144
145 View Code Duplication
    public function testGetOptionObjectFromDeeplyInheritedPage()
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...
146
    {
147
        /** @var Page|SiteTreeContentReview $page */
148
        $page = $this->objFromFixture(Page::class, "page-3-1-1-1");
149
150
        $this->assertEquals("Inherit", $page->ContentReviewType);
151
        $this->assertInstanceOf(SiteConfig::class, $page->getOptions());
152
    }
153
154
    public function testGetSettingsObjectFromInheritPage()
155
    {
156
        /** @var Page|SiteTreeContentReview $page */
157
        $page = $this->objFromFixture(Page::class, "page-1-1");
158
159
        /** @var Page|SiteTreeContentReview $parentPage */
160
        $parentPage = $this->objFromFixture(Page::class, "page-1");
161
162
        $this->assertEquals("Inherit", $page->ContentReviewType);
163
        $this->assertEquals(get_class($parentPage), get_class($page->getOptions()));
164
        $this->assertEquals($parentPage->ID, $page->getOptions()->ID);
165
    }
166
167 View Code Duplication
    public function testGetSettingsObjectFromInheritedRootPage()
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...
168
    {
169
        /** @var Page|SiteTreeContentReview $page */
170
        $page = $this->objFromFixture(Page::class, "inherit");
171
172
        $this->assertEquals("Inherit", $page->ContentReviewType);
173
        $this->assertEquals($this->objFromFixture(SiteConfig::class, "default")->ID, $page->getOptions()->ID);
174
    }
175
176
    public function testGetNextReviewDateFromCustomSettings()
177
    {
178
        /** @var Page|SiteTreeContentReview $page */
179
        $page = $this->objFromFixture(Page::class, 'custom');
180
181
        $date = $page->getReviewDate();
182
183
        $this->assertEquals('2010-02-01', $date->format('Y-MM-dd'));
184
    }
185
186
    public function testGetNextReviewDateFromSiteConfigInheritedSetting()
187
    {
188
        /** @var Page|SiteTreeContentReview $page */
189
        $page = $this->objFromFixture(Page::class, "inherit");
190
191
        $nextReviewDate = $page->getReviewDate();
192
193
        $this->assertInstanceOf(DBDate::class, $nextReviewDate);
194
195
        /** @var SiteConfig|ContentReviewDefaultSettings $siteConfig */
196
        $siteConfig = $this->objFromFixture(SiteConfig::class, "default");
197
198
        $expected = $this->addDaysToDate(DBDatetime::now(), $siteConfig->ReviewPeriodDays);
199
200
        $this->assertEquals($expected, $nextReviewDate->format('Y-MM-dd'));
201
    }
202
203
    public function testGetNextReviewDateFromPageInheritedSetting()
204
    {
205
        // Although page-1-1 inherits from page-1, it has an independent review date
206
        $page = $this->objFromFixture(Page::class, "page-1-1");
207
        $nextReviewDate = $page->getReviewDate();
208
        $this->assertInstanceOf(DBDate::class, $nextReviewDate);
209
        $this->assertEquals('2011-04-12', $nextReviewDate->format('Y-MM-dd'));
210
    }
211
212
    public function testUpdateNextReviewDateFromCustomToDisabled()
213
    {
214
        /** @var Page|SiteTreeContentReview $page */
215
        $page = $this->objFromFixture(Page::class, "custom");
216
217
        // before write()
218
        $this->assertEquals("2010-02-01", $page->NextReviewDate);
219
220
        $page->ContentReviewType = "Disabled";
221
        $page->write();
222
223
        DataObject::flush_and_destroy_cache();
224
        unset($page);
225
226
        /** @var Page|SiteTreeContentReview $page */
227
        $page = $this->objFromFixture(Page::class, "custom");
228
229
        $this->assertNull($page->NextReviewDate);
230
    }
231
232
    public function testUpdateNextReviewDateFromDisabledToCustom()
233
    {
234
        /** @var Page|SiteTreeContentReview $page */
235
        $page = $this->objFromFixture(Page::class, "disabled");
236
237
        $this->assertNull($page->NextReviewDate);
238
239
        $page->ContentReviewType = "Custom";
240
        $page->ReviewPeriodDays = "7";
241
        $page->write();
242
243
        DataObject::flush_and_destroy_cache();
244
        unset($page);
245
246
        /** @var Page|SiteTreeContentReview $page */
247
        $page = $this->objFromFixture(Page::class, "disabled");
248
249
        $expected = date('Y-m-d', strtotime("+ " . $page->ReviewPeriodDays . " days"));
250
251
        $this->assertEquals($expected, $page->NextReviewDate);
252
    }
253
254
    public function testParentChangedOptionsAndChildShouldToo()
255
    {
256
        /** @var Page|SiteTreeContentReview $parentPage */
257
        $parentPage = $this->objFromFixture(Page::class, "page-1");
258
259
        /** @var Page|SiteTreeContentReview $childPage */
260
        $childPage = $this->objFromFixture(Page::class, "page-1-1");
261
262
        // Parent and child pages have different review dates
263
        $this->assertNotEquals($parentPage->NextReviewDate, $childPage->NextReviewDate);
264
265
        // But if we change the parent page ReviewPeriodDays to 10, the childs stays the same
266
        $parentPage->ReviewPeriodDays = 10;
267
        $parentPage->write();
268
269
        // Flush all the caches!
270
        DataObject::flush_and_destroy_cache();
271
272
        /** @var Page|SiteTreeContentReview $page */
273
        $parentPage = $this->objFromFixture(Page::class, "page-1");
274
275
        /** @var Page|SiteTreeContentReview $page */
276
        $childPage = $this->objFromFixture(Page::class, "page-1-1");
277
278
        // The parent page's date advances, but not the child's
279
        $this->assertEquals('2011-04-12', $childPage->NextReviewDate);
280
        $this->assertEquals($this->addDaysToDate(date('Y-m-d'), 10), $parentPage->NextReviewDate);
281
282
        // Reviewing the child page should, however, advance its review by 10 days
283
        $childPage->advanceReviewDate();
284
        $childPage->write();
285
        $this->assertEquals($this->addDaysToDate(date('Y-m-d'), 10), $childPage->NextReviewDate);
286
    }
287
288
    /**
289
     * Note: all input dates should be DBDatetime or strings in CLDR date format. See {@link DBDate} for information
290
     *
291
     * @param string|DBDatetime $date
292
     * @param int               $days
293
     * @param string            $format
294
     *
295
     * @return bool|string
296
     */
297
    private function addDaysToDate($date, $days, $format = 'Y-MM-dd')
298
    {
299
        if (is_object($date)) {
300
            $sec = strtotime("+ " . $days . " days", $date->getTimestamp());
301
        } else {
302
            $sec = strtotime("+ " . $days . " days", DBDate::create()->setValue($date)->getTimestamp());
303
        }
304
305
        return DBDate::create()->setValue($sec)->format($format);
306
    }
307
}
308