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 | /** |
||
19 | * {@inheritDoc} |
||
20 | */ |
||
21 | protected static $fixture_file = 'comments/tests/CommentsTest.yml'; |
||
22 | |||
23 | /** |
||
24 | * {@inheritDoc} |
||
25 | */ |
||
26 | protected $extraDataObjects = array( |
||
27 | CommentableItem::class, |
||
28 | CommentableItemEnabled::class, |
||
29 | CommentableItemDisabled::class |
||
30 | ); |
||
31 | |||
32 | View Code Duplication | public function setUp() |
|
0 ignored issues
–
show
|
|||
33 | { |
||
34 | parent::setUp(); |
||
35 | Config::nest(); |
||
36 | |||
37 | // Set good default values |
||
38 | Config::inst()->update(CommentsExtension::class, 'comments', array( |
||
39 | 'enabled' => true, |
||
40 | 'enabled_cms' => false, |
||
41 | 'require_login' => false, |
||
42 | 'require_login_cms' => false, |
||
43 | 'required_permission' => false, |
||
44 | 'require_moderation_nonmembers' => false, |
||
45 | 'require_moderation' => false, |
||
46 | 'require_moderation_cms' => false, |
||
47 | 'frontend_moderation' => false, |
||
48 | 'Member' => false, |
||
49 | )); |
||
50 | |||
51 | $this->requiredExtensions = array( |
||
0 ignored issues
–
show
The property
requiredExtensions does not seem to exist. Did you mean required_extensions ?
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...
|
|||
52 | 'CommentableItem' => CommentsExtension::class |
||
53 | ); |
||
54 | |||
55 | // Configure this dataobject |
||
56 | Config::inst()->update(CommentableItem::class, 'comments', array( |
||
57 | 'enabled_cms' => true |
||
58 | )); |
||
59 | } |
||
60 | |||
61 | public function tearDown() |
||
62 | { |
||
63 | Config::unnest(); |
||
64 | parent::tearDown(); |
||
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::inst()->update(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 | $this->assertEquals('None', $item->getModerationRequired()); |
||
92 | $item->ModerationRequired = 'Required'; |
||
93 | $this->assertEquals('Required', $item->getModerationRequired()); |
||
94 | $item->ModerationRequired = 'NonMembersOnly'; |
||
95 | $this->assertEquals('NonMembersOnly', $item->getModerationRequired()); |
||
96 | |||
97 | Config::inst()->update(CommentableItem::class, 'comments', array( |
||
98 | 'require_moderation_cms' => false, |
||
99 | 'require_moderation' => true, |
||
100 | 'require_moderation_nonmembers' => true |
||
101 | )); |
||
102 | $this->assertEquals('Required', $item->getModerationRequired()); |
||
103 | |||
104 | Config::inst()->update(CommentableItem::class, 'comments', array( |
||
105 | 'require_moderation_cms' => false, |
||
106 | 'require_moderation' => false, |
||
107 | 'require_moderation_nonmembers' => true |
||
108 | )); |
||
109 | $this->assertEquals('NonMembersOnly', $item->getModerationRequired()); |
||
110 | |||
111 | Config::inst()->update(CommentableItem::class, 'comments', array( |
||
112 | 'require_moderation_cms' => false, |
||
113 | 'require_moderation' => false, |
||
114 | 'require_moderation_nonmembers' => false |
||
115 | )); |
||
116 | $this->assertEquals('None', $item->getModerationRequired()); |
||
117 | } |
||
118 | |||
119 | public function testGetCommentsRequireLogin() |
||
120 | { |
||
121 | Config::inst()->update(CommentableItem::class, 'comments', array( |
||
122 | 'require_login_cms' => true |
||
123 | )); |
||
124 | |||
125 | // With require moderation CMS set to true, the value of the field |
||
126 | // 'ModerationRequired' is returned |
||
127 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
128 | $item->CommentsRequireLogin = true; |
||
129 | $this->assertTrue($item->getCommentsRequireLogin()); |
||
130 | $item->CommentsRequireLogin = false; |
||
131 | $this->assertFalse($item->getCommentsRequireLogin()); |
||
132 | |||
133 | Config::inst()->update(CommentableItem::class, 'comments', array( |
||
134 | 'require_login_cms' => false, |
||
135 | 'require_login' => false |
||
136 | )); |
||
137 | $this->assertFalse($item->getCommentsRequireLogin()); |
||
138 | Config::inst()->update(CommentableItem::class, 'comments', array( |
||
139 | 'require_login_cms' => false, |
||
140 | 'require_login' => true |
||
141 | )); |
||
142 | $this->assertTrue($item->getCommentsRequireLogin()); |
||
143 | } |
||
144 | |||
145 | public function testAllComments() |
||
146 | { |
||
147 | $this->markTestSkipped('TODO'); |
||
148 | } |
||
149 | |||
150 | public function testAllVisibleComments() |
||
151 | { |
||
152 | $this->markTestSkipped('TODO'); |
||
153 | } |
||
154 | |||
155 | public function testComments() |
||
156 | { |
||
157 | $this->markTestSkipped('TODO'); |
||
158 | } |
||
159 | |||
160 | public function testGetCommentsEnabled() |
||
161 | { |
||
162 | $this->markTestSkipped('TODO'); |
||
163 | } |
||
164 | |||
165 | public function testGetCommentHolderID() |
||
166 | { |
||
167 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
168 | Config::inst()->update(CommentableItem::class, 'comments', array( |
||
169 | 'comments_holder_id' => 'commentid_test1', |
||
170 | )); |
||
171 | $this->assertEquals('commentid_test1', $item->getCommentHolderID()); |
||
172 | |||
173 | Config::inst()->update(CommentableItem::class, 'comments', array( |
||
174 | 'comments_holder_id' => 'commtentid_test_another', |
||
175 | )); |
||
176 | $this->assertEquals('commtentid_test_another', $item->getCommentHolderID()); |
||
177 | } |
||
178 | |||
179 | |||
180 | public function testGetPostingRequiredPermission() |
||
181 | { |
||
182 | $this->markTestSkipped('TODO'); |
||
183 | } |
||
184 | |||
185 | public function testCanModerateComments() |
||
186 | { |
||
187 | // ensure nobody logged in |
||
188 | if (Member::currentUser()) { |
||
189 | Member::currentUser()->logOut(); |
||
190 | } |
||
191 | |||
192 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
193 | $this->assertFalse($item->canModerateComments()); |
||
194 | |||
195 | $this->logInWithPermission('CMS_ACCESS_CommentAdmin'); |
||
196 | $this->assertTrue($item->canModerateComments()); |
||
197 | } |
||
198 | |||
199 | View Code Duplication | public function testGetCommentRSSLink() |
|
200 | { |
||
201 | Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', 'http://unittesting.local'); |
||
202 | |||
203 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
204 | $link = $item->getCommentRSSLink(); |
||
205 | $this->assertEquals('http://unittesting.local/comments/rss', $link); |
||
206 | } |
||
207 | |||
208 | View Code Duplication | public function testGetCommentRSSLinkPage() |
|
209 | { |
||
210 | Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', 'http://unittesting.local'); |
||
211 | |||
212 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
213 | $page = $item->getCommentRSSLinkPage(); |
||
214 | $this->assertEquals( |
||
215 | 'http://unittesting.local/comments/rss/SilverStripe-Comments-Tests-Stubs-CommentableItem/' . $item->ID, |
||
216 | $page |
||
217 | ); |
||
218 | } |
||
219 | |||
220 | public function testCommentsForm() |
||
221 | { |
||
222 | Config::inst()->update( |
||
223 | CommentableItem::class, |
||
224 | 'comments', |
||
225 | array( |
||
226 | 'include_js' => false |
||
227 | ) |
||
228 | ); |
||
229 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
230 | |||
231 | // The comments form is HTML to do assertions by contains |
||
232 | $cf = $item->CommentsForm(); |
||
233 | $expected = '<form id="Form_CommentsForm" action="/comments' |
||
234 | . '/CommentsForm" method="post" enctype="application/x-www-form-urlenco' |
||
235 | . 'ded">'; |
||
236 | $this->assertContains($expected, $cf); |
||
237 | $this->assertContains('<h4>Post your comment</h4>', $cf); |
||
238 | |||
239 | // check the comments form exists |
||
240 | $expected = '<input type="text" name="Name" value="ADMIN User" class="text" id="Form_CommentsForm_Name" required="required"'; |
||
241 | $this->assertContains($expected, $cf); |
||
242 | |||
243 | $expected = '<input type="email" name="Email" value="[email protected]" class="email text" id="Form_CommentsForm_Email"'; |
||
244 | $this->assertContains($expected, $cf); |
||
245 | |||
246 | $expected = '<input type="text" name="URL" class="text" id="Form_CommentsForm_URL" data-msg-url="Please enter a valid URL"'; |
||
247 | $this->assertContains($expected, $cf); |
||
248 | |||
249 | $expected = '<input type="hidden" name="ParentID" value="' . $item->ID . '" class="hidden" id="Form_CommentsForm_ParentID" />'; |
||
250 | $this->assertContains($expected, $cf); |
||
251 | |||
252 | $expected = '<textarea name="Comment" class="textarea" id="Form_CommentsForm_Comment" required="required"'; |
||
253 | $this->assertContains($expected, $cf); |
||
254 | |||
255 | $expected = '<input type="submit" name="action_doPostComment" value="Post" class="action" id="Form_CommentsForm_action_doPostComment"'; |
||
256 | $this->assertContains($expected, $cf); |
||
257 | |||
258 | $expected = '<a href="/comments/spam/'; |
||
259 | $this->assertContains($expected, $cf); |
||
260 | |||
261 | $expected = '<p>Reply to firstComA 1</p>'; |
||
262 | $this->assertContains($expected, $cf); |
||
263 | |||
264 | $expected = '<a href="/comments/delete'; |
||
265 | $this->assertContains($expected, $cf); |
||
266 | |||
267 | $expected = '<p>Reply to firstComA 2</p>'; |
||
268 | $this->assertContains($expected, $cf); |
||
269 | |||
270 | $expected = '<p>Reply to firstComA 3</p>'; |
||
271 | $this->assertContains($expected, $cf); |
||
272 | |||
273 | // Check for JS inclusion |
||
274 | $backend = Requirements::backend(); |
||
275 | $this->assertEquals( |
||
276 | array(), |
||
277 | $backend->getJavascript() |
||
278 | ); |
||
279 | |||
280 | Config::inst()->update( |
||
281 | CommentableItem::class, |
||
282 | 'comments', |
||
283 | array( |
||
284 | 'include_js' => true |
||
285 | ) |
||
286 | ); |
||
287 | $cf = $item->CommentsForm(); |
||
0 ignored issues
–
show
$cf is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the
Loading history...
|
|||
288 | |||
289 | $backend = Requirements::backend(); |
||
290 | $javascriptRequirements = $backend->getJavascript(); |
||
291 | $expected = array( |
||
292 | 'framework/admin/thirdparty/jquery/jquery.js', |
||
293 | 'framework/admin/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js', |
||
294 | 'framework/admin/thirdparty/jquery-form/jquery.form.js', |
||
295 | 'comments/thirdparty/jquery-validate/jquery.validate.min.js', |
||
296 | /** |
||
297 | * @todo: Is there a replacement for this? The docs are unclear |
||
298 | */ |
||
299 | // 'framework/admin/client/src/i18n.js', |
||
300 | 'comments/javascript/lang/en.js', |
||
301 | 'comments/javascript/CommentsInterface.js' |
||
302 | ); |
||
303 | |||
304 | foreach ($expected as $javascript) { |
||
305 | $this->assertArrayHasKey($javascript, $javascriptRequirements); |
||
306 | }; |
||
307 | } |
||
308 | |||
309 | public function testAttachedToSiteTree() |
||
310 | { |
||
311 | $this->markTestSkipped('TODO'); |
||
312 | } |
||
313 | |||
314 | public function testPagedComments() |
||
315 | { |
||
316 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
317 | // Ensure Created times are set, as order not guaranteed if all set to 0 |
||
318 | $comments = $item->PagedComments()->sort('ID'); |
||
319 | $ctr = 0; |
||
320 | $timeBase = time()-10000; |
||
321 | foreach ($comments as $comment) { |
||
322 | $comment->Created = $timeBase + $ctr * 1000; |
||
323 | $comment->write(); |
||
324 | $ctr++; |
||
325 | } |
||
326 | |||
327 | $results = $item->PagedComments()->toArray(); |
||
328 | |||
329 | foreach ($results as $result) { |
||
330 | $result->sourceQueryParams = null; |
||
331 | } |
||
332 | |||
333 | $this->assertEquals( |
||
334 | $this->objFromFixture(Comment::class, 'firstComA')->Comment, |
||
335 | $results[3]->Comment |
||
336 | ); |
||
337 | $this->assertEquals( |
||
338 | $this->objFromFixture(Comment::class, 'firstComAChild1')->Comment, |
||
339 | $results[2]->Comment |
||
340 | ); |
||
341 | $this->assertEquals( |
||
342 | $this->objFromFixture(Comment::class, 'firstComAChild2')->Comment, |
||
343 | $results[1]->Comment |
||
344 | ); |
||
345 | $this->assertEquals( |
||
346 | $this->objFromFixture(Comment::class, 'firstComAChild3')->Comment, |
||
347 | $results[0]->Comment |
||
348 | ); |
||
349 | |||
350 | $this->assertEquals(4, sizeof($results)); |
||
351 | } |
||
352 | |||
353 | public function testGetCommentsOption() |
||
354 | { |
||
355 | $this->markTestSkipped('TODO'); |
||
356 | } |
||
357 | |||
358 | public function testUpdateModerationFields() |
||
359 | { |
||
360 | $this->markTestSkipped('TODO'); |
||
361 | } |
||
362 | |||
363 | public function testUpdateCMSFields() |
||
364 | { |
||
365 | Config::inst()->update( |
||
366 | CommentableItem::class, |
||
367 | 'comments', |
||
368 | array( |
||
369 | 'require_login_cms' => false |
||
370 | ) |
||
371 | ); |
||
372 | $this->logInWithPermission('ADMIN'); |
||
373 | $item = $this->objFromFixture(CommentableItem::class, 'first'); |
||
374 | $item->ProvideComments = true; |
||
375 | $item->write(); |
||
376 | $fields = $item->getCMSFields(); |
||
377 | CommentTestHelper::assertFieldsForTab( |
||
378 | $this, |
||
379 | 'Root.Comments', |
||
380 | array('CommentsNewCommentsTab', 'CommentsCommentsTab', 'CommentsSpamCommentsTab'), |
||
381 | $fields |
||
382 | ); |
||
383 | |||
384 | CommentTestHelper::assertFieldsForTab( |
||
385 | $this, |
||
386 | 'Root.Comments.CommentsNewCommentsTab', |
||
387 | array('NewComments'), |
||
388 | $fields |
||
389 | ); |
||
390 | |||
391 | CommentTestHelper::assertFieldsForTab( |
||
392 | $this, |
||
393 | 'Root.Comments.CommentsCommentsTab', |
||
394 | array('ApprovedComments'), |
||
395 | $fields |
||
396 | ); |
||
397 | |||
398 | CommentTestHelper::assertFieldsForTab( |
||
399 | $this, |
||
400 | 'Root.Comments.CommentsSpamCommentsTab', |
||
401 | array('SpamComments'), |
||
402 | $fields |
||
403 | ); |
||
404 | |||
405 | Config::inst()->update( |
||
406 | CommentableItem::class, |
||
407 | 'comments', |
||
408 | array( |
||
409 | 'require_login_cms' => true |
||
410 | ) |
||
411 | ); |
||
412 | $fields = $item->getCMSFields(); |
||
413 | CommentTestHelper::assertFieldsForTab($this, 'Root.Settings', array('Comments'), $fields); |
||
414 | $settingsTab = $fields->findOrMakeTab('Root.Settings'); |
||
415 | $settingsChildren = $settingsTab->getChildren(); |
||
416 | $this->assertEquals(1, $settingsChildren->count()); |
||
417 | $fieldGroup = $settingsChildren->first(); |
||
418 | $fields = $fieldGroup->getChildren(); |
||
419 | CommentTestHelper::assertFieldNames( |
||
420 | $this, |
||
421 | array('ProvideComments', 'CommentsRequireLogin'), |
||
422 | $fields |
||
423 | ); |
||
424 | |||
425 | Config::inst()->update( |
||
426 | CommentableItem::class, |
||
427 | 'comments', |
||
428 | array( |
||
429 | 'require_login_cms' => true, |
||
430 | 'require_moderation_cms' => true |
||
431 | ) |
||
432 | ); |
||
433 | |||
434 | $fields = $item->getCMSFields(); |
||
435 | CommentTestHelper::assertFieldsForTab( |
||
436 | $this, |
||
437 | 'Root.Settings', |
||
438 | array('Comments', 'ModerationRequired'), |
||
439 | $fields |
||
440 | ); |
||
441 | $settingsTab = $fields->findOrMakeTab('Root.Settings'); |
||
442 | $settingsChildren = $settingsTab->getChildren(); |
||
443 | $this->assertEquals(2, $settingsChildren->count()); |
||
444 | $fieldGroup = $settingsChildren->first(); |
||
445 | $fields = $fieldGroup->getChildren(); |
||
446 | CommentTestHelper::assertFieldNames( |
||
447 | $this, |
||
448 | array('ProvideComments', 'CommentsRequireLogin'), |
||
449 | $fields |
||
450 | ); |
||
451 | } |
||
452 | } |
||
453 |
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.