Completed
Pull Request — master (#216)
by Will
01:38
created

CommentsExtensionTest::testAttachedToSiteTree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 = 'CommentsTest.yml';
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    protected static $extra_dataobjects = array(
27
        CommentableItem::class,
28
        CommentableItemEnabled::class,
29
        CommentableItemDisabled::class
30
    );
31
32
    public function setUp()
33
    {
34
        parent::setUp();
35
        Config::nest();
36
37
        // Set good default values
38
        Config::modify()->set(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
Bug introduced by
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::modify()->set(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
68
    public function testGetCommentsOption()
69
    {
70
        Config::modify()->set(CommentableItem::class, 'comments', [
71
            'comments_holder_id' => 'some-option'
72
        ]);
73
74
        $item = $this->objFromFixture(CommentableItem::class, 'first');
75
        $this->assertEquals('some-option', $item->getCommentsOption('comments_holder_id'));
76
    }
77
78
    public function testPopulateDefaults()
79
    {
80
        Config::modify()->set(CommentableItem::class, 'comments', array(
81
            'require_moderation_cms' => true,
82
            'require_moderation' => true,
83
            'require_moderation_nonmembers' => true
84
        ));
85
86
        $item = $this->objFromFixture(CommentableItem::class, 'first');
87
        $item->populateDefaults();
88
89
        $this->assertTrue($item->CommentsRequireLogin);
90
91
        Config::modify()->set(CommentableItem::class, 'comments', array(
92
            'require_moderation_cms' => true,
93
            'require_moderation' => true,
94
            'require_moderation_nonmembers' => true
95
        ));
96
97
        $item = $this->objFromFixture(CommentableItem::class, 'first');
98
        $item->populateDefaults();
99
100
        $this->assertFalse($item->CommentsRequireLogin);
101
    }
102
103
    public function testUpdateSettingsFields()
104
    {
105
        $this->markTestSkipped('This needs SiteTree installed');
106
    }
107
108
    public function testGetModerationRequired()
109
    {
110
111
        // the 3 options take precedence in this order, executed if true
112
        Config::modify()->set(CommentableItem::class, 'comments', array(
113
            'require_moderation_cms' => true,
114
            'require_moderation' => true,
115
            'require_moderation_nonmembers' => true
116
        ));
117
118
        // With require moderation CMS set to true, the value of the field
119
        // 'ModerationRequired' is returned
120
        $item = $this->objFromFixture(CommentableItem::class, 'first');
121
        $item->ModerationRequired = 'None';
122
        $item->write();
123
124
        $this->assertEquals('None', $item->getModerationRequired());
125
        $item->ModerationRequired = 'Required';
126
        $item->write();
127
128
        $this->assertEquals('Required', $item->getModerationRequired());
129
130
        $item->ModerationRequired = 'NonMembersOnly';
131
        $item->write();
132
133
        $this->assertEquals('NonMembersOnly', $item->getModerationRequired());
134
135
        Config::modify()->set(CommentableItem::class, 'comments', array(
136
            'require_moderation_cms' => false,
137
            'require_moderation' => true,
138
            'require_moderation_nonmembers' => true
139
        ));
140
        $this->assertEquals('Required', $item->getModerationRequired());
141
142
        Config::modify()->set(CommentableItem::class, 'comments', array(
143
            'require_moderation_cms' => false,
144
            'require_moderation' => false,
145
            'require_moderation_nonmembers' => true
146
        ));
147
        $this->assertEquals('NonMembersOnly', $item->getModerationRequired());
148
149
        Config::modify()->set(CommentableItem::class, 'comments', array(
150
            'require_moderation_cms' => false,
151
            'require_moderation' => false,
152
            'require_moderation_nonmembers' => false
153
        ));
154
        $this->assertEquals('None', $item->getModerationRequired());
155
    }
156
157
    public function testGetCommentsRequireLogin()
158
    {
159
        Config::modify()->set(CommentableItem::class, 'comments', array(
160
            'require_login_cms' => true
161
        ));
162
163
        // With require moderation CMS set to true, the value of the field
164
        // 'ModerationRequired' is returned
165
        $item = $this->objFromFixture(CommentableItem::class, 'first');
166
        $item->CommentsRequireLogin = true;
167
        $this->assertTrue($item->getCommentsRequireLogin());
168
        $item->CommentsRequireLogin = false;
169
        $this->assertFalse($item->getCommentsRequireLogin());
170
171
        Config::modify()->set(CommentableItem::class, 'comments', array(
172
            'require_login_cms' => false,
173
            'require_login' => false
174
        ));
175
        $this->assertFalse($item->getCommentsRequireLogin());
176
        Config::modify()->set(CommentableItem::class, 'comments', array(
177
            'require_login_cms' => false,
178
            'require_login' => true
179
        ));
180
        $this->assertTrue($item->getCommentsRequireLogin());
181
    }
182
183
    public function testAllComments()
184
    {
185
        $item = $this->objFromFixture(CommentableItem::class, 'first');
186
        $this->assertEquals(4, $item->AllComments()->count());
187
    }
188
189
    public function testAllVisibleComments()
190
    {
191
        if (Member::currentUser()) {
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
192
            Member::currentUser()->logOut();
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
193
        }
194
195
        $item = $this->objFromFixture(CommentableItem::class, 'second');
196
        $this->assertEquals(4, $item->AllVisibleComments()->count());
197
    }
198
199
    public function testComments()
200
    {
201
        Config::modify()->set(CommentableItem::class, 'comments', array(
202
            'nested_comments' => false
203
        ));
204
205
        $item = $this->objFromFixture(CommentableItem::class, 'first');
206
        $this->assertEquals(4, $item->Comments()->count());
207
208
        Config::modify()->set(CommentableItem::class, 'comments', array(
209
            'nested_comments' => true
210
        ));
211
212
        $this->assertEquals(1, $item->Comments()->count());
213
    }
214
215
    public function testGetCommentsEnabled()
216
    {
217
        Config::modify()->set(CommentableItem::class, 'comments', array(
218
            'enabled_cms' => true
219
        ));
220
221
        $item = $this->objFromFixture(CommentableItem::class, 'first');
222
        $this->assertTrue($item->getCommentsEnabled());
223
224
        $item->ProvideComments = 0;
225
        $this->assertFalse($item->getCommentsEnabled());
226
    }
227
228
    public function testGetCommentHolderID()
229
    {
230
        $item = $this->objFromFixture(CommentableItem::class, 'first');
231
        Config::modify()->set(CommentableItem::class, 'comments', array(
232
            'comments_holder_id' => 'commentid_test1',
233
        ));
234
        $this->assertEquals('commentid_test1', $item->getCommentHolderID());
235
236
        Config::modify()->set(CommentableItem::class, 'comments', array(
237
            'comments_holder_id' => 'commtentid_test_another',
238
        ));
239
        $this->assertEquals('commtentid_test_another', $item->getCommentHolderID());
240
    }
241
242
243
    public function testGetPostingRequiredPermission()
244
    {
245
        $this->markTestSkipped('TODO');
246
    }
247
248
    public function testCanModerateComments()
249
    {
250
        // ensure nobody logged in
251
        if (Member::currentUser()) {
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
252
            Member::currentUser()->logOut();
0 ignored issues
show
Deprecated Code introduced by
The method SilverStripe\Security\Member::currentUser() has been deprecated with message: 5.0.0 use Security::getCurrentUser()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
253
        }
254
255
        $item = $this->objFromFixture(CommentableItem::class, 'first');
256
        $this->assertFalse($item->canModerateComments());
257
258
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
259
        $this->assertTrue($item->canModerateComments());
260
    }
261
262 View Code Duplication
    public function testGetCommentRSSLink()
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...
263
    {
264
        Config::modify()->set('SilverStripe\\Control\\Director', 'alternate_base_url', 'http://unittesting.local');
265
266
        $item = $this->objFromFixture(CommentableItem::class, 'first');
267
        $link = $item->getCommentRSSLink();
268
        $this->assertEquals('http://unittesting.local/comments/rss', $link);
269
    }
270
271 View Code Duplication
    public function testGetCommentRSSLinkPage()
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...
272
    {
273
        Config::modify()->set('SilverStripe\\Control\\Director', 'alternate_base_url', 'http://unittesting.local');
274
275
        $item = $this->objFromFixture(CommentableItem::class, 'first');
276
        $page = $item->getCommentRSSLinkPage();
277
        $this->assertEquals(
278
            'http://unittesting.local/comments/rss/SilverStripe-Comments-Tests-Stubs-CommentableItem/' . $item->ID,
279
            $page
280
        );
281
    }
282
283
    public function testCommentsForm()
284
    {
285
        Config::modify()->set(CommentableItem::class, 'comments', array(
286
            'include_js' => false,
287
            'comments_holder_id' => 'comments-holder',
288
        ));
289
290
        $item = $this->objFromFixture(CommentableItem::class, 'first');
291
292
        // The comments form is HTML to do assertions by contains
293
        $cf = $item->CommentsForm();
294
        $expected = '<form id="comments-holder" action="/comments'
295
        . '/CommentsForm/" method="post" enctype="application/x-www-form-urlenco'
296
        . 'ded">';
297
298
        $this->assertContains($expected, $cf);
299
        $this->assertContains('<h4>Post your comment</h4>', $cf);
300
        // check the comments form exists
301
        $expected = '<input type="text" name="Name"';
302
        $this->assertContains($expected, $cf);
303
304
        $expected = '<input type="email" name="Email"';
305
        $this->assertContains($expected, $cf);
306
307
        $expected = '<input type="text" name="URL"';
308
        $this->assertContains($expected, $cf);
309
310
        $expected = '<input type="hidden" name="ParentID"';
311
        $this->assertContains($expected, $cf);
312
313
        $expected = '<textarea name="Comment"';
314
        $this->assertContains($expected, $cf);
315
316
        $expected = '<input type="submit" name="action_doPostComment" value="Post" class="action" id="comments-holder_action_doPostComment"';
317
        $this->assertContains($expected, $cf);
318
319
        $expected = '<a href="/comments/spam/';
320
        $this->assertContains($expected, $cf);
321
322
        $expected = '<p>Reply to firstComA 1</p>';
323
        $this->assertContains($expected, $cf);
324
325
        $expected = '<a href="/comments/delete';
326
        $this->assertContains($expected, $cf);
327
328
        $expected = '<p>Reply to firstComA 2</p>';
329
        $this->assertContains($expected, $cf);
330
331
        $expected = '<p>Reply to firstComA 3</p>';
332
        $this->assertContains($expected, $cf);
333
    }
334
335
    public function testAttachedToSiteTree()
336
    {
337
        $this->markTestSkipped('TODO');
338
    }
339
340
    public function testPagedComments()
341
    {
342
        $item = $this->objFromFixture(CommentableItem::class, 'first');
343
        // Ensure Created times are set, as order not guaranteed if all set to 0
344
        $comments = $item->PagedComments()->sort('ID');
345
        $ctr = 0;
346
        $timeBase = time()-10000;
347
        foreach ($comments as $comment) {
348
            $comment->Created = $timeBase + $ctr * 1000;
349
            $comment->write();
350
            $ctr++;
351
        }
352
353
        $results = $item->PagedComments()->toArray();
354
355
        foreach ($results as $result) {
356
            $result->sourceQueryParams = null;
357
        }
358
359
        $this->assertEquals(
360
            $this->objFromFixture(Comment::class, 'firstComA')->Comment,
361
            $results[3]->Comment
362
        );
363
        $this->assertEquals(
364
            $this->objFromFixture(Comment::class, 'firstComAChild1')->Comment,
365
            $results[2]->Comment
366
        );
367
        $this->assertEquals(
368
            $this->objFromFixture(Comment::class, 'firstComAChild2')->Comment,
369
            $results[1]->Comment
370
        );
371
        $this->assertEquals(
372
            $this->objFromFixture(Comment::class, 'firstComAChild3')->Comment,
373
            $results[0]->Comment
374
        );
375
376
        $this->assertEquals(4, sizeof($results));
377
    }
378
379
    public function testUpdateModerationFields()
380
    {
381
        $this->markTestSkipped('TODO');
382
    }
383
384
    public function testUpdateCMSFields()
385
    {
386
        Config::modify()->set(
387
            CommentableItem::class,
388
            'comments',
389
            array(
390
                'require_login_cms' => false
391
            )
392
        );
393
        $this->logInWithPermission('ADMIN');
394
        $item = $this->objFromFixture(CommentableItem::class, 'first');
395
        $item->ProvideComments = true;
396
        $item->write();
397
        $fields = $item->getCMSFields();
398
399
        CommentTestHelper::assertFieldsForTab(
400
            $this,
401
            'Comments.CommentsNewCommentsTab',
402
            array('NewComments'),
403
            $fields
404
        );
405
406
        CommentTestHelper::assertFieldsForTab(
407
            $this,
408
            'Comments.CommentsCommentsTab',
409
            array('ApprovedComments'),
410
            $fields
411
        );
412
413
        CommentTestHelper::assertFieldsForTab(
414
            $this,
415
            'Comments.CommentsSpamCommentsTab',
416
            array('SpamComments'),
417
            $fields
418
        );
419
420
        Config::modify()->set(
421
            CommentableItem::class,
422
            'comments',
423
            array(
424
                'require_login_cms' => true
425
            )
426
        );
427
        $fields = $item->getCMSFields();
428
        CommentTestHelper::assertFieldsForTab($this, 'Root.Settings', array('Comments'), $fields);
429
        $settingsTab = $fields->findOrMakeTab('Root.Settings');
430
        $settingsChildren = $settingsTab->getChildren();
431
        $this->assertEquals(1, $settingsChildren->count());
432
        $fieldGroup = $settingsChildren->first();
433
        $fields = $fieldGroup->getChildren();
434
        CommentTestHelper::assertFieldNames(
435
            $this,
436
            array('ProvideComments', 'CommentsRequireLogin'),
437
            $fields
438
        );
439
440
        Config::modify()->set(
441
            CommentableItem::class,
442
            'comments',
443
            array(
444
                'require_login_cms' => true,
445
                'require_moderation_cms' => true
446
            )
447
        );
448
449
        $fields = $item->getCMSFields();
450
        CommentTestHelper::assertFieldsForTab(
451
            $this,
452
            'Root.Settings',
453
            array('Comments', 'ModerationRequired'),
454
            $fields
455
        );
456
        $settingsTab = $fields->findOrMakeTab('Root.Settings');
457
        $settingsChildren = $settingsTab->getChildren();
458
        $this->assertEquals(2, $settingsChildren->count());
459
        $fieldGroup = $settingsChildren->first();
460
        $fields = $fieldGroup->getChildren();
461
        CommentTestHelper::assertFieldNames(
462
            $this,
463
            array('ProvideComments', 'CommentsRequireLogin'),
464
            $fields
465
        );
466
    }
467
}
468