Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
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() |
|
90 | |||
91 | View Code Duplication | public function testUserWithoutPermissionCannotEdit() |
|
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() |
|
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() |
|
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() |
|
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() |
|
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() |
|
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() |
|
240 | |||
241 | View Code Duplication | public function testCanBeReviewedFromInheritedSetting() |
|
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"); |
||
294 | |||
295 | View Code Duplication | public function testReviewActionVisibleForAuthor() |
|
313 | |||
314 | View Code Duplication | public function testReviewActionNotVisibleForEditor() |
|
315 | { |
||
316 | DBDatetime::set_mock_now("2020-03-01 12:00:00"); |
||
317 | |||
333 |
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.