Completed
Pull Request — master (#195)
by Robbie
02:53
created

CommentsExtensionTest   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 460
Duplicated Lines 6.09 %

Coupling/Cohesion

Components 3
Dependencies 10

Importance

Changes 0
Metric Value
wmc 28
lcom 3
cbo 10
dl 28
loc 460
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 28 28 1
A tearDown() 0 5 1
A testPopulateDefaults() 0 4 1
A testUpdateSettingsFields() 0 4 1
B testGetModerationRequired() 0 41 1
B testGetCommentsRequireLogin() 0 25 1
A testAllComments() 0 4 1
A testAllVisibleComments() 0 4 1
A testComments() 0 4 1
A testGetCommentsEnabled() 0 4 1
A testGetCommentHolderID() 0 13 1
A testGetPostingRequiredPermission() 0 4 1
A testCanModerateComments() 0 13 2
A testGetCommentRSSLink() 0 6 1
A testGetCommentRSSLinkPage() 0 9 1
B testCommentsForm() 0 88 2
A testAttachedToSiteTree() 0 4 1
B testPagedComments() 0 38 3
A testGetCommentsOption() 0 4 1
A testUpdateModerationFields() 0 4 1
B testUpdateCMSFields() 0 89 1
B testDeprecatedMethods() 0 25 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\Comments\Tests;
4
5
use PHPUnit_Framework_Error_Deprecated;
6
use SilverStripe\Comments\Extensions\CommentsExtension;
7
use SilverStripe\Comments\Model\Comment;
8
use SilverStripe\Comments\Tests\CommentTestHelper;
9
use SilverStripe\Comments\Tests\Stubs\CommentableItem;
10
use SilverStripe\Comments\Tests\Stubs\CommentableItemDisabled;
11
use SilverStripe\Comments\Tests\Stubs\CommentableItemEnabled;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\Security\Member;
15
use SilverStripe\View\Requirements;
16
17
class CommentsExtensionTest extends SapphireTest
18
{
19
    /**
20
     * {@inheritDoc}
21
     */
22
    public static $fixture_file = 'comments/tests/CommentsTest.yml';
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    protected $extraDataObjects = array(
28
        CommentableItem::class,
29
        CommentableItemEnabled::class,
30
        CommentableItemDisabled::class
31
    );
32
33 View Code Duplication
    public function setUp()
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...
34
    {
35
        parent::setUp();
36
        Config::nest();
37
38
        // Set good default values
39
        Config::inst()->update(CommentsExtension::class, 'comments', array(
40
            'enabled' => true,
41
            'enabled_cms' => false,
42
            'require_login' => false,
43
            'require_login_cms' => false,
44
            'required_permission' => false,
45
            'require_moderation_nonmembers' => false,
46
            'require_moderation' => false,
47
            'require_moderation_cms' => false,
48
            'frontend_moderation' => false,
49
            'Member' => false,
50
        ));
51
52
        $this->requiredExtensions = array(
53
            'CommentableItem' => CommentsExtension::class
54
        );
55
56
        // Configure this dataobject
57
        Config::inst()->update(CommentableItem::class, 'comments', array(
58
            'enabled_cms' => true
59
        ));
60
    }
61
62
    public function tearDown()
63
    {
64
        Config::unnest();
65
        parent::tearDown();
66
    }
67
68
    public function testPopulateDefaults()
69
    {
70
        $this->markTestSkipped('TODO');
71
    }
72
73
    public function testUpdateSettingsFields()
74
    {
75
        $this->markTestSkipped('This needs SiteTree installed');
76
    }
77
78
    public function testGetModerationRequired()
79
    {
80
81
        // the 3 options take precedence in this order, executed if true
82
        Config::inst()->update(CommentableItem::class, 'comments', array(
83
            'require_moderation_cms' => true,
84
            'require_moderation' => true,
85
            'require_moderation_nonmembers' => true
86
        ));
87
88
        // With require moderation CMS set to true, the value of the field
89
        // 'ModerationRequired' is returned
90
        $item = $this->objFromFixture(CommentableItem::class, 'first');
91
        $item->ModerationRequired = 'None';
92
        $this->assertEquals('None', $item->getModerationRequired());
93
        $item->ModerationRequired = 'Required';
94
        $this->assertEquals('Required', $item->getModerationRequired());
95
        $item->ModerationRequired = 'NonMembersOnly';
96
        $this->assertEquals('NonMembersOnly', $item->getModerationRequired());
97
98
        Config::inst()->update(CommentableItem::class, 'comments', array(
99
            'require_moderation_cms' => false,
100
            'require_moderation' => true,
101
            'require_moderation_nonmembers' => true
102
        ));
103
        $this->assertEquals('Required', $item->getModerationRequired());
104
105
        Config::inst()->update(CommentableItem::class, 'comments', array(
106
            'require_moderation_cms' => false,
107
            'require_moderation' => false,
108
            'require_moderation_nonmembers' => true
109
        ));
110
        $this->assertEquals('NonMembersOnly', $item->getModerationRequired());
111
112
        Config::inst()->update(CommentableItem::class, 'comments', array(
113
            'require_moderation_cms' => false,
114
            'require_moderation' => false,
115
            'require_moderation_nonmembers' => false
116
        ));
117
        $this->assertEquals('None', $item->getModerationRequired());
118
    }
119
120
    public function testGetCommentsRequireLogin()
121
    {
122
        Config::inst()->update(CommentableItem::class, 'comments', array(
123
            'require_login_cms' => true
124
        ));
125
126
        // With require moderation CMS set to true, the value of the field
127
        // 'ModerationRequired' is returned
128
        $item = $this->objFromFixture(CommentableItem::class, 'first');
129
        $item->CommentsRequireLogin = true;
130
        $this->assertTrue($item->getCommentsRequireLogin());
131
        $item->CommentsRequireLogin = false;
132
        $this->assertFalse($item->getCommentsRequireLogin());
133
134
        Config::inst()->update(CommentableItem::class, 'comments', array(
135
            'require_login_cms' => false,
136
            'require_login' => false
137
        ));
138
        $this->assertFalse($item->getCommentsRequireLogin());
139
        Config::inst()->update(CommentableItem::class, 'comments', array(
140
            'require_login_cms' => false,
141
            'require_login' => true
142
        ));
143
        $this->assertTrue($item->getCommentsRequireLogin());
144
    }
145
146
    public function testAllComments()
147
    {
148
        $this->markTestSkipped('TODO');
149
    }
150
151
    public function testAllVisibleComments()
152
    {
153
        $this->markTestSkipped('TODO');
154
    }
155
156
    public function testComments()
157
    {
158
        $this->markTestSkipped('TODO');
159
    }
160
161
    public function testGetCommentsEnabled()
162
    {
163
        $this->markTestSkipped('TODO');
164
    }
165
166
    public function testGetCommentHolderID()
167
    {
168
        $item = $this->objFromFixture(CommentableItem::class, 'first');
169
        Config::inst()->update(CommentableItem::class, 'comments', array(
170
            'comments_holder_id' => 'commentid_test1',
171
        ));
172
        $this->assertEquals('commentid_test1', $item->getCommentHolderID());
173
174
        Config::inst()->update(CommentableItem::class, 'comments', array(
175
            'comments_holder_id' => 'commtentid_test_another',
176
        ));
177
        $this->assertEquals('commtentid_test_another', $item->getCommentHolderID());
178
    }
179
180
181
    public function testGetPostingRequiredPermission()
182
    {
183
        $this->markTestSkipped('TODO');
184
    }
185
186
    public function testCanModerateComments()
187
    {
188
        // ensure nobody logged in
189
        if (Member::currentUser()) {
190
            Member::currentUser()->logOut();
191
        }
192
193
        $item = $this->objFromFixture(CommentableItem::class, 'first');
194
        $this->assertFalse($item->canModerateComments());
195
196
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
197
        $this->assertTrue($item->canModerateComments());
198
    }
199
200
    public function testGetCommentRSSLink()
201
    {
202
        $item = $this->objFromFixture(CommentableItem::class, 'first');
203
        $link = $item->getCommentRSSLink();
204
        $this->assertEquals('/comments/rss', $link);
205
    }
206
207
208
    public function testGetCommentRSSLinkPage()
209
    {
210
        $item = $this->objFromFixture(CommentableItem::class, 'first');
211
        $page = $item->getCommentRSSLinkPage();
212
        $this->assertEquals(
213
            '/comments/rss/SilverStripe-Comments-Tests-Stubs-CommentableItem/' . $item->ID,
214
            $page
215
        );
216
    }
217
218
    public function testCommentsForm()
219
    {
220
        Config::inst()->update(
221
            CommentableItem::class,
222
            'comments',
223
            array(
224
                'include_js' => false
225
            )
226
        );
227
        $item = $this->objFromFixture(CommentableItem::class, 'first');
228
229
        // The comments form is HTML to do assertions by contains
230
        $cf = $item->CommentsForm();
231
        $expected = '<form id="Form_CommentsForm" action="/comments'
232
        . '/CommentsForm" method="post" enctype="application/x-www-form-urlenco'
233
        . 'ded">';
234
        $this->assertContains($expected, $cf);
235
        $this->assertContains('<h4>Post your comment</h4>', $cf);
236
237
        // check the comments form exists
238
        $expected = '<input type="text" name="Name" value="ADMIN User" class="text" id="Form_CommentsForm_Name" required="required"';
239
        $this->assertContains($expected, $cf);
240
241
        $expected = '<input type="email" name="Email" value="[email protected]" class="email text" id="Form_CommentsForm_Email"';
242
        $this->assertContains($expected, $cf);
243
244
        $expected = '<input type="text" name="URL" class="text" id="Form_CommentsForm_URL" data-msg-url="Please enter a valid URL"';
245
        $this->assertContains($expected, $cf);
246
247
        $expected = '<input type="hidden" name="ParentID" value="' . $item->ID . '" class="hidden" id="Form_CommentsForm_ParentID" />';
248
        $this->assertContains($expected, $cf);
249
250
        $expected = '<textarea name="Comment" class="textarea" id="Form_CommentsForm_Comment" required="required"';
251
        $this->assertContains($expected, $cf);
252
253
        $expected = '<input type="submit" name="action_doPostComment" value="Post" class="action" id="Form_CommentsForm_action_doPostComment"';
254
        $this->assertContains($expected, $cf);
255
256
        $expected = '<a href="/comments/spam/';
257
        $this->assertContains($expected, $cf);
258
259
        $expected = '<p>Reply to firstComA 1</p>';
260
        $this->assertContains($expected, $cf);
261
262
        $expected = '<a href="/comments/delete';
263
        $this->assertContains($expected, $cf);
264
265
        $expected = '<p>Reply to firstComA 2</p>';
266
        $this->assertContains($expected, $cf);
267
268
        $expected = '<p>Reply to firstComA 3</p>';
269
        $this->assertContains($expected, $cf);
270
271
        // Check for JS inclusion
272
        $backend = Requirements::backend();
273
        $this->assertEquals(
274
            array(),
275
            $backend->getJavascript()
276
        );
277
278
        Config::inst()->update(
279
            CommentableItem::class,
280
            'comments',
281
            array(
282
                'include_js' => true
283
            )
284
        );
285
        $cf = $item->CommentsForm();
0 ignored issues
show
Unused Code introduced by
$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 $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
286
287
        $backend = Requirements::backend();
288
        $javascriptRequirements = $backend->getJavascript();
289
        $expected = array(
290
            'framework/admin/thirdparty/jquery/jquery.js',
291
            'framework/admin/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js',
292
            'framework/admin/thirdparty/jquery-form/jquery.form.js',
293
            'comments/thirdparty/jquery-validate/jquery.validate.min.js',
294
            /**
295
             * @todo: Is there a replacement for this? The docs are unclear
296
             */
297
            // 'framework/admin/client/src/i18n.js',
298
            'comments/javascript/lang/en.js',
299
            'comments/javascript/CommentsInterface.js'
300
        );
301
302
        foreach ($expected as $javascript) {
303
            $this->assertArrayHasKey($javascript, $javascriptRequirements);
304
        };
305
    }
306
307
    public function testAttachedToSiteTree()
308
    {
309
        $this->markTestSkipped('TODO');
310
    }
311
312
    public function testPagedComments()
313
    {
314
        $item = $this->objFromFixture(CommentableItem::class, 'first');
315
        // Ensure Created times are set, as order not guaranteed if all set to 0
316
        $comments = $item->PagedComments()->sort('ID');
317
        $ctr = 0;
318
        $timeBase = time()-10000;
319
        foreach ($comments as $comment) {
320
            $comment->Created = $timeBase + $ctr * 1000;
321
            $comment->write();
322
            $ctr++;
323
        }
324
325
        $results = $item->PagedComments()->toArray();
326
327
        foreach ($results as $result) {
328
            $result->sourceQueryParams = null;
329
        }
330
331
        $this->assertEquals(
332
            $this->objFromFixture(Comment::class, 'firstComA')->Comment,
333
            $results[3]->Comment
334
        );
335
        $this->assertEquals(
336
            $this->objFromFixture(Comment::class, 'firstComAChild1')->Comment,
337
            $results[2]->Comment
338
        );
339
        $this->assertEquals(
340
            $this->objFromFixture(Comment::class, 'firstComAChild2')->Comment,
341
            $results[1]->Comment
342
        );
343
        $this->assertEquals(
344
            $this->objFromFixture(Comment::class, 'firstComAChild3')->Comment,
345
            $results[0]->Comment
346
        );
347
348
        $this->assertEquals(4, sizeof($results));
349
    }
350
351
    public function testGetCommentsOption()
352
    {
353
        $this->markTestSkipped('TODO');
354
    }
355
356
    public function testUpdateModerationFields()
357
    {
358
        $this->markTestSkipped('TODO');
359
    }
360
361
    public function testUpdateCMSFields()
362
    {
363
        Config::inst()->update(
364
            CommentableItem::class,
365
            'comments',
366
            array(
367
                'require_login_cms' => false
368
            )
369
        );
370
        $this->logInWithPermission('ADMIN');
371
        $item = $this->objFromFixture(CommentableItem::class, 'first');
372
        $item->ProvideComments = true;
373
        $item->write();
374
        $fields = $item->getCMSFields();
375
        CommentTestHelper::assertFieldsForTab(
376
            $this,
377
            'Root.Comments',
378
            array('CommentsNewCommentsTab', 'CommentsCommentsTab', 'CommentsSpamCommentsTab'),
379
            $fields
380
        );
381
382
        CommentTestHelper::assertFieldsForTab(
383
            $this,
384
            'Root.Comments.CommentsNewCommentsTab',
385
            array('NewComments'),
386
            $fields
387
        );
388
389
        CommentTestHelper::assertFieldsForTab(
390
            $this,
391
            'Root.Comments.CommentsCommentsTab',
392
            array('ApprovedComments'),
393
            $fields
394
        );
395
396
        CommentTestHelper::assertFieldsForTab(
397
            $this,
398
            'Root.Comments.CommentsSpamCommentsTab',
399
            array('SpamComments'),
400
            $fields
401
        );
402
403
        Config::inst()->update(
404
            CommentableItem::class,
405
            'comments',
406
            array(
407
                'require_login_cms' => true
408
            )
409
        );
410
        $fields = $item->getCMSFields();
411
        CommentTestHelper::assertFieldsForTab($this, 'Root.Settings', array('Comments'), $fields);
412
        $settingsTab = $fields->findOrMakeTab('Root.Settings');
413
        $settingsChildren = $settingsTab->getChildren();
414
        $this->assertEquals(1, $settingsChildren->count());
415
        $fieldGroup = $settingsChildren->first();
416
        $fields = $fieldGroup->getChildren();
417
        CommentTestHelper::assertFieldNames(
418
            $this,
419
            array('ProvideComments', 'CommentsRequireLogin'),
420
            $fields
421
        );
422
423
        Config::inst()->update(
424
            CommentableItem::class,
425
            'comments',
426
            array(
427
                'require_login_cms' => true,
428
                'require_moderation_cms' => true
429
            )
430
        );
431
432
        $fields = $item->getCMSFields();
433
        CommentTestHelper::assertFieldsForTab(
434
            $this,
435
            'Root.Settings',
436
            array('Comments', 'ModerationRequired'),
437
            $fields
438
        );
439
        $settingsTab = $fields->findOrMakeTab('Root.Settings');
440
        $settingsChildren = $settingsTab->getChildren();
441
        $this->assertEquals(2, $settingsChildren->count());
442
        $fieldGroup = $settingsChildren->first();
443
        $fields = $fieldGroup->getChildren();
444
        CommentTestHelper::assertFieldNames(
445
            $this,
446
            array('ProvideComments', 'CommentsRequireLogin'),
447
            $fields
448
        );
449
    }
450
451
    public function testDeprecatedMethods()
452
    {
453
        $item = $this->objFromFixture(CommentableItem::class, 'first');
454
        $methodNames = array(
455
            'getRssLinkPage',
456
            'getRssLink',
457
            'PageComments',
458
            'getPostingRequiresPermission',
459
            'canPost',
460
            'getCommentsConfigured'
461
        );
462
463
        foreach ($methodNames as $methodName) {
464
            try {
465
                $item->$methodName();
466
                $this->fail('Method ' . $methodName .' should be depracated');
467
            } catch (PHPUnit_Framework_Error_Deprecated $e) {
468
                $expected = 'SilverStripe\\Comments\\Extensions\\CommentsExtension->' . $methodName . ' is '
469
                . 'deprecated.';
470
                $this->assertStringStartsWith($expected, $e->getMessage());
471
            }
472
        }
473
474
        // ooh,  $this->setExpectedException('ExpectedException', 'Expected Message');
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
475
    }
476
}
477