Issues (245)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/CommentsExtensionTest.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 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...
222
            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...
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 View Code Duplication
    public function testGetCommentRSSLink()
0 ignored issues
show
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...
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 View Code Duplication
    public function testGetCommentRSSLinkPage()
0 ignored issues
show
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...
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" id="comments-holder_action_doPostComment"';
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());
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...
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