CommentsExtensionTest   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 419
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 235
c 2
b 1
f 0
dl 0
loc 419
rs 10
wmc 23

20 Methods

Rating   Name   Duplication   Size   Complexity  
A testPagedComments() 0 37 3
B testUpdateCMSFields() 0 82 1
A testCanModerateComments() 0 12 2
A testUpdateSettingsFields() 0 3 1
A testUpdateModerationFields() 0 3 1
A setUp() 0 21 1
A testGetCommentsEnabled() 0 11 1
A testGetCommentRSSLinkPage() 0 9 1
A testAttachedToSiteTree() 0 3 1
A testGetCommentsOption() 0 8 1
A testGetCommentRSSLink() 0 7 1
A testGetCommentHolderID() 0 12 1
A testAllVisibleComments() 0 6 1
A testAllComments() 0 4 1
A testComments() 0 14 1
A testGetCommentsRequireLogin() 0 24 1
A testCommentsForm() 0 50 1
A testPopulateDefaults() 0 3 1
A testGetPostingRequiredPermission() 0 3 1
A testGetModerationRequired() 0 47 1
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\FunctionalTest;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\Security\Member;
15
use SilverStripe\View\Requirements;
16
17
class CommentsExtensionTest extends FunctionalTest
18
{
19
    protected static $fixture_file = 'CommentsTest.yml';
20
21
    protected static $disable_themes = true;
22
23
    protected static $extra_dataobjects = [
24
        CommentableItem::class,
25
        CommentableItemEnabled::class,
26
        CommentableItemDisabled::class,
27
    ];
28
29
    protected static $required_extensions = [
30
        CommentableItem::class => [
31
            CommentsExtension::class,
32
        ],
33
    ];
34
35
    protected function setUp()
36
    {
37
        parent::setUp();
38
39
        // Set good default values
40
        Config::modify()->merge(CommentsExtension::class, 'comments', [
41
            'enabled' => true,
42
            'enabled_cms' => false,
43
            'require_login' => false,
44
            'require_login_cms' => false,
45
            'required_permission' => false,
46
            'require_moderation_nonmembers' => false,
47
            'require_moderation' => false,
48
            'require_moderation_cms' => false,
49
            'frontend_moderation' => false,
50
            'Member' => false,
51
        ]);
52
53
        // Configure this dataobject
54
        Config::modify()->merge(CommentableItem::class, 'comments', [
55
            'enabled_cms' => true
56
        ]);
57
    }
58
59
60
    public function testGetCommentsOption()
61
    {
62
        Config::modify()->merge(CommentableItem::class, 'comments', [
63
            'comments_holder_id' => 'some-option'
64
        ]);
65
66
        $item = $this->objFromFixture(CommentableItem::class, 'first');
67
        $this->assertEquals('some-option', $item->getCommentsOption('comments_holder_id'));
68
    }
69
70
    public function testPopulateDefaults()
71
    {
72
        $this->markTestSkipped('TODO');
73
    }
74
75
    public function testUpdateSettingsFields()
76
    {
77
        $this->markTestSkipped('This needs SiteTree installed');
78
    }
79
80
    public function testGetModerationRequired()
81
    {
82
83
        // the 3 options take precedence in this order, executed if true
84
        Config::modify()->merge(CommentableItem::class, 'comments', array(
85
            'require_moderation_cms' => true,
86
            'require_moderation' => true,
87
            'require_moderation_nonmembers' => true
88
        ));
89
90
        // With require moderation CMS set to true, the value of the field
91
        // 'ModerationRequired' is returned
92
        $item = $this->objFromFixture(CommentableItem::class, 'first');
93
        $item->ModerationRequired = 'None';
94
        $item->write();
95
96
        $this->assertEquals('None', $item->getModerationRequired());
97
        $item->ModerationRequired = 'Required';
98
        $item->write();
99
100
        $this->assertEquals('Required', $item->getModerationRequired());
101
102
        $item->ModerationRequired = 'NonMembersOnly';
103
        $item->write();
104
105
        $this->assertEquals('NonMembersOnly', $item->getModerationRequired());
106
107
        Config::modify()->merge(CommentableItem::class, 'comments', array(
108
            'require_moderation_cms' => false,
109
            'require_moderation' => true,
110
            'require_moderation_nonmembers' => true
111
        ));
112
        $this->assertEquals('Required', $item->getModerationRequired());
113
114
        Config::modify()->merge(CommentableItem::class, 'comments', array(
115
            'require_moderation_cms' => false,
116
            'require_moderation' => false,
117
            'require_moderation_nonmembers' => true
118
        ));
119
        $this->assertEquals('NonMembersOnly', $item->getModerationRequired());
120
121
        Config::modify()->merge(CommentableItem::class, 'comments', array(
122
            'require_moderation_cms' => false,
123
            'require_moderation' => false,
124
            'require_moderation_nonmembers' => false
125
        ));
126
        $this->assertEquals('None', $item->getModerationRequired());
127
    }
128
129
    public function testGetCommentsRequireLogin()
130
    {
131
        Config::modify()->merge(CommentableItem::class, 'comments', array(
132
            'require_login_cms' => true
133
        ));
134
135
        // With require moderation CMS set to true, the value of the field
136
        // 'ModerationRequired' is returned
137
        $item = $this->objFromFixture(CommentableItem::class, 'first');
138
        $item->CommentsRequireLogin = true;
139
        $this->assertTrue($item->getCommentsRequireLogin());
140
        $item->CommentsRequireLogin = false;
141
        $this->assertFalse($item->getCommentsRequireLogin());
142
143
        Config::modify()->merge(CommentableItem::class, 'comments', array(
144
            'require_login_cms' => false,
145
            'require_login' => false
146
        ));
147
        $this->assertFalse($item->getCommentsRequireLogin());
148
        Config::modify()->merge(CommentableItem::class, 'comments', array(
149
            'require_login_cms' => false,
150
            'require_login' => true
151
        ));
152
        $this->assertTrue($item->getCommentsRequireLogin());
153
    }
154
155
    public function testAllComments()
156
    {
157
        $item = $this->objFromFixture(CommentableItem::class, 'first');
158
        $this->assertEquals(4, $item->AllComments()->count());
159
    }
160
161
    public function testAllVisibleComments()
162
    {
163
        $this->logOut();
164
165
        $item = $this->objFromFixture(CommentableItem::class, 'second');
166
        $this->assertEquals(2, $item->AllVisibleComments()->count());
167
    }
168
169
    public function testComments()
170
    {
171
        Config::modify()->merge(CommentableItem::class, 'comments', array(
172
            'nested_comments' => false
173
        ));
174
175
        $item = $this->objFromFixture(CommentableItem::class, 'first');
176
        $this->assertEquals(4, $item->Comments()->count());
177
178
        Config::modify()->merge(CommentableItem::class, 'comments', array(
179
            'nested_comments' => true
180
        ));
181
182
        $this->assertEquals(1, $item->Comments()->count());
183
    }
184
185
    public function testGetCommentsEnabled()
186
    {
187
        Config::modify()->merge(CommentableItem::class, 'comments', array(
188
            'enabled_cms' => true
189
        ));
190
191
        $item = $this->objFromFixture(CommentableItem::class, 'first');
192
        $this->assertTrue($item->getCommentsEnabled());
193
194
        $item->ProvideComments = 0;
195
        $this->assertFalse($item->getCommentsEnabled());
196
    }
197
198
    public function testGetCommentHolderID()
199
    {
200
        $item = $this->objFromFixture(CommentableItem::class, 'first');
201
        Config::modify()->merge(CommentableItem::class, 'comments', array(
202
            'comments_holder_id' => 'commentid_test1',
203
        ));
204
        $this->assertEquals('commentid_test1', $item->getCommentHolderID());
205
206
        Config::modify()->merge(CommentableItem::class, 'comments', array(
207
            'comments_holder_id' => 'commtentid_test_another',
208
        ));
209
        $this->assertEquals('commtentid_test_another', $item->getCommentHolderID());
210
    }
211
212
213
    public function testGetPostingRequiredPermission()
214
    {
215
        $this->markTestSkipped('TODO');
216
    }
217
218
    public function testCanModerateComments()
219
    {
220
        // ensure nobody logged in
221
        if (Member::currentUser()) {
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

221
        if (/** @scrutinizer ignore-deprecated */ Member::currentUser()) {

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

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

Loading history...
222
            Member::currentUser()->logOut();
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Security\Member::logOut() has been deprecated: Use Security::setCurrentUser(null) or an IdentityStore Logs this member out. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

222
            /** @scrutinizer ignore-deprecated */ Member::currentUser()->logOut();

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

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

Loading history...
Deprecated Code introduced by
The function SilverStripe\Security\Member::currentUser() has been deprecated: 5.0.0 use Security::getCurrentUser() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

222
            /** @scrutinizer ignore-deprecated */ Member::currentUser()->logOut();

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

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

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