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