Completed
Pull Request — master (#216)
by Robbie
10:05
created

CommentsExtensionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 15
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
    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()) {
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...
219
            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...
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
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...
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
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...
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 = '<form id="comments-holder" action="/comments'
262
        . '/CommentsForm/" method="post" enctype="application/x-www-form-urlenco'
263
        . 'ded">';
264
265
        $this->assertContains($expected, $cf);
266
        $this->assertContains('<h4>Post your comment</h4>', $cf);
267
        // check the comments form exists
268
        $expected = '<input type="text" name="Name"';
269
        $this->assertContains($expected, $cf);
270
271
        $expected = '<input type="email" name="Email"';
272
        $this->assertContains($expected, $cf);
273
274
        $expected = '<input type="text" name="URL"';
275
        $this->assertContains($expected, $cf);
276
277
        $expected = '<input type="hidden" name="ParentID"';
278
        $this->assertContains($expected, $cf);
279
280
        $expected = '<textarea name="Comment"';
281
        $this->assertContains($expected, $cf);
282
283
        $expected = '<input type="submit" name="action_doPostComment" value="Post" class="action" id="comments-holder_action_doPostComment"';
284
        $this->assertContains($expected, $cf);
285
286
        $expected = '<a href="/comments/spam/';
287
        $this->assertContains($expected, $cf);
288
289
        $expected = '<p>Reply to firstComA 1</p>';
290
        $this->assertContains($expected, $cf);
291
292
        $expected = '<a href="/comments/delete';
293
        $this->assertContains($expected, $cf);
294
295
        $expected = '<p>Reply to firstComA 2</p>';
296
        $this->assertContains($expected, $cf);
297
298
        $expected = '<p>Reply to firstComA 3</p>';
299
        $this->assertContains($expected, $cf);
300
    }
301
302
    public function testAttachedToSiteTree()
303
    {
304
        $this->markTestSkipped('TODO');
305
    }
306
307
    public function testPagedComments()
308
    {
309
        $item = $this->objFromFixture(CommentableItem::class, 'first');
310
        // Ensure Created times are set, as order not guaranteed if all set to 0
311
        $comments = $item->PagedComments()->sort('ID');
312
        $ctr = 0;
313
        $timeBase = time()-10000;
314
        foreach ($comments as $comment) {
315
            $comment->Created = $timeBase + $ctr * 1000;
316
            $comment->write();
317
            $ctr++;
318
        }
319
320
        $results = $item->PagedComments()->toArray();
321
322
        foreach ($results as $result) {
323
            $result->sourceQueryParams = null;
324
        }
325
326
        $this->assertEquals(
327
            $this->objFromFixture(Comment::class, 'firstComA')->Comment,
328
            $results[3]->Comment
329
        );
330
        $this->assertEquals(
331
            $this->objFromFixture(Comment::class, 'firstComAChild1')->Comment,
332
            $results[2]->Comment
333
        );
334
        $this->assertEquals(
335
            $this->objFromFixture(Comment::class, 'firstComAChild2')->Comment,
336
            $results[1]->Comment
337
        );
338
        $this->assertEquals(
339
            $this->objFromFixture(Comment::class, 'firstComAChild3')->Comment,
340
            $results[0]->Comment
341
        );
342
343
        $this->assertEquals(4, sizeof($results));
344
    }
345
346
    public function testUpdateModerationFields()
347
    {
348
        $this->markTestSkipped('TODO');
349
    }
350
351
    public function testUpdateCMSFields()
352
    {
353
        Config::modify()->merge(
354
            CommentableItem::class,
355
            'comments',
356
            array(
357
                'require_login_cms' => false
358
            )
359
        );
360
        $this->logInWithPermission('ADMIN');
361
        $item = $this->objFromFixture(CommentableItem::class, 'first');
362
        $item->ProvideComments = true;
363
        $item->write();
364
        $fields = $item->getCMSFields();
365
        // print_r($item->getCMSFields());
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
366
367
        CommentTestHelper::assertFieldsForTab(
368
            $this,
369
            'Root.Comments.CommentsNewCommentsTab',
370
            array('NewComments'),
371
            $fields
372
        );
373
374
        CommentTestHelper::assertFieldsForTab(
375
            $this,
376
            'Root.Comments.CommentsCommentsTab',
377
            array('ApprovedComments'),
378
            $fields
379
        );
380
381
        CommentTestHelper::assertFieldsForTab(
382
            $this,
383
            'Root.Comments.CommentsSpamCommentsTab',
384
            array('SpamComments'),
385
            $fields
386
        );
387
388
        Config::modify()->merge(
389
            CommentableItem::class,
390
            'comments',
391
            array(
392
                'require_login_cms' => true
393
            )
394
        );
395
        $fields = $item->getCMSFields();
396
        CommentTestHelper::assertFieldsForTab($this, 'Root.Settings', array('Comments'), $fields);
397
        $settingsTab = $fields->findOrMakeTab('Root.Settings');
398
        $settingsChildren = $settingsTab->getChildren();
399
        $this->assertEquals(1, $settingsChildren->count());
400
        $fieldGroup = $settingsChildren->first();
401
        $fields = $fieldGroup->getChildren();
402
        CommentTestHelper::assertFieldNames(
403
            $this,
404
            array('ProvideComments', 'CommentsRequireLogin'),
405
            $fields
406
        );
407
408
        Config::modify()->merge(
409
            CommentableItem::class,
410
            'comments',
411
            array(
412
                'require_login_cms' => true,
413
                'require_moderation_cms' => true
414
            )
415
        );
416
417
        $fields = $item->getCMSFields();
418
        CommentTestHelper::assertFieldsForTab(
419
            $this,
420
            'Root.Settings',
421
            array('Comments', 'ModerationRequired'),
422
            $fields
423
        );
424
        $settingsTab = $fields->findOrMakeTab('Root.Settings');
425
        $settingsChildren = $settingsTab->getChildren();
426
        $this->assertEquals(2, $settingsChildren->count());
427
        $fieldGroup = $settingsChildren->first();
428
        $fields = $fieldGroup->getChildren();
429
        CommentTestHelper::assertFieldNames(
430
            $this,
431
            array('ProvideComments', 'CommentsRequireLogin'),
432
            $fields
433
        );
434
    }
435
}
436