These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SilverStripe\Comments\Tests; |
||
4 | |||
5 | use SilverStripe\Comments\Extensions\CommentsExtension; |
||
6 | use SilverStripe\Comments\Model\Comment; |
||
7 | use SilverStripe\Comments\Tests\CommentTestHelper; |
||
8 | use SilverStripe\Comments\Tests\Stubs\CommentableItem; |
||
9 | use SilverStripe\Comments\Tests\Stubs\CommentableItemDisabled; |
||
10 | use SilverStripe\Comments\Tests\Stubs\CommentableItemEnabled; |
||
11 | use SilverStripe\Core\Config\Config; |
||
12 | use SilverStripe\Dev\SapphireTest; |
||
13 | use SilverStripe\Security\Member; |
||
14 | use SilverStripe\View\Requirements; |
||
15 | |||
16 | class CommentsExtensionTest extends SapphireTest |
||
17 | { |
||
18 | protected static $fixture_file = 'CommentsTest.yml'; |
||
19 | |||
20 | protected static $extra_dataobjects = [ |
||
21 | CommentableItem::class, |
||
22 | CommentableItemEnabled::class, |
||
23 | CommentableItemDisabled::class, |
||
24 | ]; |
||
25 | |||
26 | protected static $required_extensions = [ |
||
27 | CommentableItem::class => [ |
||
28 | CommentsExtension::class, |
||
29 | ], |
||
30 | ]; |
||
31 | |||
32 | protected function setUp() |
||
33 | { |
||
34 | parent::setUp(); |
||
35 | |||
36 | // Set good default values |
||
37 | Config::modify()->merge(CommentsExtension::class, 'comments', [ |
||
38 | 'enabled' => true, |
||
39 | 'enabled_cms' => false, |
||
40 | 'require_login' => false, |
||
41 | 'require_login_cms' => false, |
||
42 | 'required_permission' => false, |
||
43 | 'require_moderation_nonmembers' => false, |
||
44 | 'require_moderation' => false, |
||
45 | 'require_moderation_cms' => false, |
||
46 | 'frontend_moderation' => false, |
||
47 | 'Member' => false, |
||
48 | ]); |
||
49 | |||
50 | // Configure this dataobject |
||
51 | Config::modify()->merge(CommentableItem::class, 'comments', [ |
||
52 | 'enabled_cms' => true |
||
53 | ]); |
||
54 | } |
||
55 | |||
56 | |||
57 | public function testGetCommentsOption() |
||
58 | { |
||
59 | Config::modify()->merge(CommentableItem::class, 'comments', [ |
||
60 | 'comments_holder_id' => 'some-option' |
||
61 | ]); |
||
62 | |||
63 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
64 | $this->assertEquals('some-option', $item->getCommentsOption('comments_holder_id')); |
||
65 | } |
||
66 | |||
67 | public function testPopulateDefaults() |
||
68 | { |
||
69 | $this->markTestSkipped('TODO'); |
||
70 | } |
||
71 | |||
72 | public function testUpdateSettingsFields() |
||
73 | { |
||
74 | $this->markTestSkipped('This needs SiteTree installed'); |
||
75 | } |
||
76 | |||
77 | public function testGetModerationRequired() |
||
78 | { |
||
79 | |||
80 | // the 3 options take precedence in this order, executed if true |
||
81 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
82 | 'require_moderation_cms' => true, |
||
83 | 'require_moderation' => true, |
||
84 | 'require_moderation_nonmembers' => true |
||
85 | )); |
||
86 | |||
87 | // With require moderation CMS set to true, the value of the field |
||
88 | // 'ModerationRequired' is returned |
||
89 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
90 | $item->ModerationRequired = 'None'; |
||
91 | $item->write(); |
||
92 | |||
93 | $this->assertEquals('None', $item->getModerationRequired()); |
||
94 | $item->ModerationRequired = 'Required'; |
||
95 | $item->write(); |
||
96 | |||
97 | $this->assertEquals('Required', $item->getModerationRequired()); |
||
98 | |||
99 | $item->ModerationRequired = 'NonMembersOnly'; |
||
100 | $item->write(); |
||
101 | |||
102 | $this->assertEquals('NonMembersOnly', $item->getModerationRequired()); |
||
103 | |||
104 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
105 | 'require_moderation_cms' => false, |
||
106 | 'require_moderation' => true, |
||
107 | 'require_moderation_nonmembers' => true |
||
108 | )); |
||
109 | $this->assertEquals('Required', $item->getModerationRequired()); |
||
110 | |||
111 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
112 | 'require_moderation_cms' => false, |
||
113 | 'require_moderation' => false, |
||
114 | 'require_moderation_nonmembers' => true |
||
115 | )); |
||
116 | $this->assertEquals('NonMembersOnly', $item->getModerationRequired()); |
||
117 | |||
118 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
119 | 'require_moderation_cms' => false, |
||
120 | 'require_moderation' => false, |
||
121 | 'require_moderation_nonmembers' => false |
||
122 | )); |
||
123 | $this->assertEquals('None', $item->getModerationRequired()); |
||
124 | } |
||
125 | |||
126 | public function testGetCommentsRequireLogin() |
||
127 | { |
||
128 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
129 | 'require_login_cms' => true |
||
130 | )); |
||
131 | |||
132 | // With require moderation CMS set to true, the value of the field |
||
133 | // 'ModerationRequired' is returned |
||
134 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
135 | $item->CommentsRequireLogin = true; |
||
136 | $this->assertTrue($item->getCommentsRequireLogin()); |
||
137 | $item->CommentsRequireLogin = false; |
||
138 | $this->assertFalse($item->getCommentsRequireLogin()); |
||
139 | |||
140 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
141 | 'require_login_cms' => false, |
||
142 | 'require_login' => false |
||
143 | )); |
||
144 | $this->assertFalse($item->getCommentsRequireLogin()); |
||
145 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
146 | 'require_login_cms' => false, |
||
147 | 'require_login' => true |
||
148 | )); |
||
149 | $this->assertTrue($item->getCommentsRequireLogin()); |
||
150 | } |
||
151 | |||
152 | public function testAllComments() |
||
153 | { |
||
154 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
155 | $this->assertEquals(4, $item->AllComments()->count()); |
||
156 | } |
||
157 | |||
158 | public function testAllVisibleComments() |
||
159 | { |
||
160 | $this->logOut(); |
||
161 | |||
162 | $item = $this->objFromFixture(CommentableItem::class, 'second'); |
||
163 | $this->assertEquals(2, $item->AllVisibleComments()->count()); |
||
164 | } |
||
165 | |||
166 | public function testComments() |
||
167 | { |
||
168 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
169 | 'nested_comments' => false |
||
170 | )); |
||
171 | |||
172 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
173 | $this->assertEquals(4, $item->Comments()->count()); |
||
174 | |||
175 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
176 | 'nested_comments' => true |
||
177 | )); |
||
178 | |||
179 | $this->assertEquals(1, $item->Comments()->count()); |
||
180 | } |
||
181 | |||
182 | public function testGetCommentsEnabled() |
||
183 | { |
||
184 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
185 | 'enabled_cms' => true |
||
186 | )); |
||
187 | |||
188 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
189 | $this->assertTrue($item->getCommentsEnabled()); |
||
190 | |||
191 | $item->ProvideComments = 0; |
||
192 | $this->assertFalse($item->getCommentsEnabled()); |
||
193 | } |
||
194 | |||
195 | public function testGetCommentHolderID() |
||
196 | { |
||
197 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
198 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
199 | 'comments_holder_id' => 'commentid_test1', |
||
200 | )); |
||
201 | $this->assertEquals('commentid_test1', $item->getCommentHolderID()); |
||
202 | |||
203 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
204 | 'comments_holder_id' => 'commtentid_test_another', |
||
205 | )); |
||
206 | $this->assertEquals('commtentid_test_another', $item->getCommentHolderID()); |
||
207 | } |
||
208 | |||
209 | |||
210 | public function testGetPostingRequiredPermission() |
||
211 | { |
||
212 | $this->markTestSkipped('TODO'); |
||
213 | } |
||
214 | |||
215 | public function testCanModerateComments() |
||
216 | { |
||
217 | // ensure nobody logged in |
||
218 | if (Member::currentUser()) { |
||
219 | Member::currentUser()->logOut(); |
||
220 | } |
||
221 | |||
222 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
223 | $this->assertFalse($item->canModerateComments()); |
||
224 | |||
225 | $this->logInWithPermission('CMS_ACCESS_CommentAdmin'); |
||
226 | $this->assertTrue($item->canModerateComments()); |
||
227 | } |
||
228 | |||
229 | View Code Duplication | public function testGetCommentRSSLink() |
|
0 ignored issues
–
show
|
|||
230 | { |
||
231 | Config::modify()->merge('SilverStripe\\Control\\Director', 'alternate_base_url', 'http://unittesting.local'); |
||
232 | |||
233 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
234 | $link = $item->getCommentRSSLink(); |
||
235 | $this->assertEquals('http://unittesting.local/comments/rss', $link); |
||
236 | } |
||
237 | |||
238 | View Code Duplication | public function testGetCommentRSSLinkPage() |
|
0 ignored issues
–
show
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...
|
|||
239 | { |
||
240 | Config::modify()->merge('SilverStripe\\Control\\Director', 'alternate_base_url', 'http://unittesting.local'); |
||
241 | |||
242 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
243 | $page = $item->getCommentRSSLinkPage(); |
||
244 | $this->assertEquals( |
||
245 | 'http://unittesting.local/comments/rss/SilverStripe-Comments-Tests-Stubs-CommentableItem/' . $item->ID, |
||
246 | $page |
||
247 | ); |
||
248 | } |
||
249 | |||
250 | public function testCommentsForm() |
||
251 | { |
||
252 | Config::modify()->merge(CommentableItem::class, 'comments', array( |
||
253 | 'include_js' => false, |
||
254 | 'comments_holder_id' => 'comments-holder', |
||
255 | )); |
||
256 | |||
257 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
258 | |||
259 | // The comments form is HTML to do assertions by contains |
||
260 | $cf = $item->CommentsForm(); |
||
261 | $expected = '/comments/CommentsForm/" method="post" enctype="application/x-www-form-urlencoded">'; |
||
262 | |||
263 | $this->assertContains($expected, $cf); |
||
264 | $this->assertContains('<h4>Post your comment</h4>', $cf); |
||
265 | // check the comments form exists |
||
266 | $expected = '<input type="text" name="Name"'; |
||
267 | $this->assertContains($expected, $cf); |
||
268 | |||
269 | $expected = '<input type="email" name="Email"'; |
||
270 | $this->assertContains($expected, $cf); |
||
271 | |||
272 | $expected = '<input type="text" name="URL"'; |
||
273 | $this->assertContains($expected, $cf); |
||
274 | |||
275 | $expected = '<input type="hidden" name="ParentID"'; |
||
276 | $this->assertContains($expected, $cf); |
||
277 | |||
278 | $expected = '<textarea name="Comment"'; |
||
279 | $this->assertContains($expected, $cf); |
||
280 | |||
281 | $expected = '<input type="submit" name="action_doPostComment" value="Post" class="action" id="comments-holder_action_doPostComment"'; |
||
282 | $this->assertContains($expected, $cf); |
||
283 | |||
284 | $expected = '/comments/spam/'; |
||
285 | $this->assertContains($expected, $cf); |
||
286 | |||
287 | $expected = '<p>Reply to firstComA 1</p>'; |
||
288 | $this->assertContains($expected, $cf); |
||
289 | |||
290 | $expected = '/comments/delete'; |
||
291 | $this->assertContains($expected, $cf); |
||
292 | |||
293 | $expected = '<p>Reply to firstComA 2</p>'; |
||
294 | $this->assertContains($expected, $cf); |
||
295 | |||
296 | $expected = '<p>Reply to firstComA 3</p>'; |
||
297 | $this->assertContains($expected, $cf); |
||
298 | } |
||
299 | |||
300 | public function testAttachedToSiteTree() |
||
301 | { |
||
302 | $this->markTestSkipped('TODO'); |
||
303 | } |
||
304 | |||
305 | public function testPagedComments() |
||
306 | { |
||
307 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
308 | // Ensure Created times are set, as order not guaranteed if all set to 0 |
||
309 | $comments = $item->PagedComments()->sort('ID'); |
||
310 | $ctr = 0; |
||
311 | $timeBase = time()-10000; |
||
312 | foreach ($comments as $comment) { |
||
313 | $comment->Created = $timeBase + $ctr * 1000; |
||
314 | $comment->write(); |
||
315 | $ctr++; |
||
316 | } |
||
317 | |||
318 | $results = $item->PagedComments()->toArray(); |
||
319 | |||
320 | foreach ($results as $result) { |
||
321 | $result->sourceQueryParams = null; |
||
322 | } |
||
323 | |||
324 | $this->assertEquals( |
||
325 | $this->objFromFixture(Comment::class, 'firstComA')->Comment, |
||
326 | $results[3]->Comment |
||
327 | ); |
||
328 | $this->assertEquals( |
||
329 | $this->objFromFixture(Comment::class, 'firstComAChild1')->Comment, |
||
330 | $results[2]->Comment |
||
331 | ); |
||
332 | $this->assertEquals( |
||
333 | $this->objFromFixture(Comment::class, 'firstComAChild2')->Comment, |
||
334 | $results[1]->Comment |
||
335 | ); |
||
336 | $this->assertEquals( |
||
337 | $this->objFromFixture(Comment::class, 'firstComAChild3')->Comment, |
||
338 | $results[0]->Comment |
||
339 | ); |
||
340 | |||
341 | $this->assertEquals(4, sizeof($results)); |
||
342 | } |
||
343 | |||
344 | public function testUpdateModerationFields() |
||
345 | { |
||
346 | $this->markTestSkipped('TODO'); |
||
347 | } |
||
348 | |||
349 | public function testUpdateCMSFields() |
||
350 | { |
||
351 | Config::modify()->merge( |
||
352 | CommentableItem::class, |
||
353 | 'comments', |
||
354 | array( |
||
355 | 'require_login_cms' => false |
||
356 | ) |
||
357 | ); |
||
358 | $this->logInWithPermission('ADMIN'); |
||
359 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
360 | $item->ProvideComments = true; |
||
361 | $item->write(); |
||
362 | $fields = $item->getCMSFields(); |
||
363 | // print_r($item->getCMSFields()); |
||
364 | |||
365 | CommentTestHelper::assertFieldsForTab( |
||
366 | $this, |
||
367 | 'Root.Comments.CommentsNewCommentsTab', |
||
368 | array('NewComments'), |
||
369 | $fields |
||
370 | ); |
||
371 | |||
372 | CommentTestHelper::assertFieldsForTab( |
||
373 | $this, |
||
374 | 'Root.Comments.CommentsCommentsTab', |
||
375 | array('ApprovedComments'), |
||
376 | $fields |
||
377 | ); |
||
378 | |||
379 | CommentTestHelper::assertFieldsForTab( |
||
380 | $this, |
||
381 | 'Root.Comments.CommentsSpamCommentsTab', |
||
382 | array('SpamComments'), |
||
383 | $fields |
||
384 | ); |
||
385 | |||
386 | Config::modify()->merge( |
||
387 | CommentableItem::class, |
||
388 | 'comments', |
||
389 | array( |
||
390 | 'require_login_cms' => true |
||
391 | ) |
||
392 | ); |
||
393 | $fields = $item->getCMSFields(); |
||
394 | CommentTestHelper::assertFieldsForTab($this, 'Root.Settings', array('Comments'), $fields); |
||
395 | $settingsTab = $fields->findOrMakeTab('Root.Settings'); |
||
396 | $settingsChildren = $settingsTab->getChildren(); |
||
397 | $this->assertEquals(1, $settingsChildren->count()); |
||
398 | $fieldGroup = $settingsChildren->first(); |
||
399 | $fields = $fieldGroup->getChildren(); |
||
400 | CommentTestHelper::assertFieldNames( |
||
401 | $this, |
||
402 | array('ProvideComments', 'CommentsRequireLogin'), |
||
403 | $fields |
||
404 | ); |
||
405 | |||
406 | Config::modify()->merge( |
||
407 | CommentableItem::class, |
||
408 | 'comments', |
||
409 | array( |
||
410 | 'require_login_cms' => true, |
||
411 | 'require_moderation_cms' => true |
||
412 | ) |
||
413 | ); |
||
414 | |||
415 | $fields = $item->getCMSFields(); |
||
416 | CommentTestHelper::assertFieldsForTab( |
||
417 | $this, |
||
418 | 'Root.Settings', |
||
419 | array('Comments', 'ModerationRequired'), |
||
420 | $fields |
||
421 | ); |
||
422 | $settingsTab = $fields->findOrMakeTab('Root.Settings'); |
||
423 | $settingsChildren = $settingsTab->getChildren(); |
||
424 | $this->assertEquals(2, $settingsChildren->count()); |
||
425 | $fieldGroup = $settingsChildren->first(); |
||
426 | $fields = $fieldGroup->getChildren(); |
||
427 | CommentTestHelper::assertFieldNames( |
||
428 | $this, |
||
429 | array('ProvideComments', 'CommentsRequireLogin'), |
||
430 | $fields |
||
431 | ); |
||
432 | } |
||
433 | } |
||
434 |
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.