Completed
Push — master ( e42a4c...f187a0 )
by Daniel
03:17
created

tests/CommentingControllerTest.php (19 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
/**
4
 * @package comments
5
 * @subpackage tests
6
 */
7
class CommentingControllerTest extends FunctionalTest
8
{
9
10
    public static $fixture_file = 'CommentsTest.yml';
11
12
    protected $extraDataObjects = array(
13
        'CommentableItem'
14
    );
15
16
    protected $securityEnabled;
17
18
    public function tearDown()
19
    {
20
        if ($this->securityEnabled) {
21
            SecurityToken::enable();
22
        } else {
23
            SecurityToken::disable();
24
        }
25
        parent::tearDown();
26
    }
27
28
    public function setUp()
29
    {
30
        parent::setUp();
31
        $this->securityEnabled = SecurityToken::is_enabled();
32
    }
33
34 View Code Duplication
    public function testApprove()
35
    {
36
        SecurityToken::disable();
37
38
        // mark a comment as spam then approve it
39
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
40
        $comment = $this->objFromFixture('Comment', 'firstComA');
41
        $comment->markSpam();
42
        $st = new Comment_SecurityToken($comment);
0 ignored issues
show
$comment is of type object<DataObject>|null, but the function expects a object<Comment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
        $url = 'CommentingController/approve/' . $comment->ID;
44
        $url = $st->addToUrl($url, Member::currentUser());
0 ignored issues
show
\Member::currentUser() is of type object<DataObject>|null, but the function expects a object<Member>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
        $response = $this->get($url);
46
        $this->assertEquals(200, $response->getStatusCode());
47
        $comment = DataObject::get_by_id('Comment', $comment->ID);
48
49
        // Need to use 0,1 here instead of false, true for SQLite
50
        $this->assertEquals(0, $comment->IsSpam);
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
        $this->assertEquals(1, $comment->Moderated);
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
53
        // try and approve a non existent comment
54
        $response = $this->get('CommentingController/approve/100000');
55
        $this->assertEquals(404, $response->getStatusCode());
56
    }
57
58
    public function testSetGetOwnerController()
59
    {
60
        $commController = new CommentingController();
61
        $commController->setOwnerController(Controller::curr());
62
        $this->assertEquals(Controller::curr(), $commController->getOwnerController());
63
        $commController->setOwnerController(null);
64
        $this->assertNull($commController->getOwnerController());
65
    }
66
67 View Code Duplication
    public function testHam()
68
    {
69
        SecurityToken::disable();
70
71
        // mark a comment as spam then ham it
72
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
73
        $comment = $this->objFromFixture('Comment', 'firstComA');
74
        $comment->markSpam();
75
        $st = new Comment_SecurityToken($comment);
0 ignored issues
show
$comment is of type object<DataObject>|null, but the function expects a object<Comment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
        $url = 'CommentingController/ham/' . $comment->ID;
77
        $url = $st->addToUrl($url, Member::currentUser());
0 ignored issues
show
\Member::currentUser() is of type object<DataObject>|null, but the function expects a object<Member>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        $response = $this->get($url);
79
        $this->assertEquals(200, $response->getStatusCode());
80
        $comment = DataObject::get_by_id('Comment', $comment->ID);
81
82
        // Need to use 0,1 here instead of false, true for SQLite
83
        $this->assertEquals(0, $comment->IsSpam);
84
        $this->assertEquals(1, $comment->Moderated);
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
86
        // try and ham a non existent comment
87
        $response = $this->get('CommentingController/ham/100000');
88
        $this->assertEquals(404, $response->getStatusCode());
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
    }
90
91
    public function testSpam()
92
    {
93
        // mark a comment as approved then spam it
94
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
95
        $comment = $this->objFromFixture('Comment', 'firstComA');
96
        $comment->markApproved();
97
        $st = new Comment_SecurityToken($comment);
0 ignored issues
show
$comment is of type object<DataObject>|null, but the function expects a object<Comment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
        $url = 'CommentingController/spam/' . $comment->ID;
99
        $url = $st->addToUrl($url, Member::currentUser());
0 ignored issues
show
\Member::currentUser() is of type object<DataObject>|null, but the function expects a object<Member>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
        $response = $this->get($url);
101
        $this->assertEquals(200, $response->getStatusCode());
102
        $comment = DataObject::get_by_id('Comment', $comment->ID);
103
104
        // Need to use 0,1 here instead of false, true for SQLite
105
        $this->assertEquals(1, $comment->IsSpam);
106
        $this->assertEquals(1, $comment->Moderated);
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
108
        // try and spam a non existent comment
109
        $response = $this->get('CommentingController/spam/100000');
110
        $this->assertEquals(404, $response->getStatusCode());
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
    }
112
113
    public function testRSS()
114
    {
115
        // Delete the newly added children of firstComA so as not to have to recalculate values below
116
        $this->objFromFixture('Comment', 'firstComAChild1')->delete();
117
        $this->objFromFixture('Comment', 'firstComAChild2')->delete();
118
        $this->objFromFixture('Comment', 'firstComAChild3')->delete();
119
120
        $item = $this->objFromFixture('CommentableItem', 'first');
121
122
123
        // comments sitewide
124
        $response = $this->get('CommentingController/rss');
125
        $this->assertEquals(10, substr_count($response->getBody(), "<item>"), "10 approved, non spam comments on page 1");
126
127
        $response = $this->get('CommentingController/rss?start=10');
128
        $this->assertEquals(4, substr_count($response->getBody(), "<item>"), "3 approved, non spam comments on page 2");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
130
        // all comments on a type
131
        $response = $this->get('CommentingController/rss/CommentableItem');
132
        $this->assertEquals(10, substr_count($response->getBody(), "<item>"));
133
134
        $response = $this->get('CommentingController/rss/CommentableItem?start=10');
135
        $this->assertEquals(4, substr_count($response->getBody(), "<item>"), "3 approved, non spam comments on page 2");
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
137
        // specific page
138
        $response = $this->get('CommentingController/rss/CommentableItem/'.$item->ID);
139
        $this->assertEquals(1, substr_count($response->getBody(), "<item>"));
140
        $this->assertContains('<dc:creator>FA</dc:creator>', $response->getBody());
141
142
        // test accessing comments on a type that doesn't exist
143
        $response = $this->get('CommentingController/rss/Fake');
144
        $this->assertEquals(404, $response->getStatusCode());
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
    }
146
147
    // This is returning a 404 which looks logical code wise but also a bit weird.
148
    // Test module on a clean install and check what the actual URL is first
149
/*    public function testReply() {
150
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
151
        $comment = $this->objFromFixture('Comment', 'firstComA');
152
        $item = $this->objFromFixture('CommentableItem', 'first');
153
154
        $st = new Comment_SecurityToken($comment);
155
        $url = 'CommentingController/reply/' . $item->ID.'?ParentCommentID=' . $comment->ID;
156
        error_log($url);
157
        $response = $this->get($url);
158
        error_log(print_r($response,1));
159
160
        $this->assertEquals(200, $response->getStatusCode());
161
162
    }
163
*/
164
/*
165
    public function testCommentsFormLoadMemberData() {
166
        Config::inst()->update('CommentableItem', 'comments', array(
167
            'use_preview' => false
168
        ));
169
        $this->logInAs('visitor');
170
        SecurityToken::disable();
171
        $parent = $this->objFromFixture('CommentableItem', 'first');
172
        $parent->CommentsRequireLogin = true;
173
        $parent->PostingRequiredPermission = true;
174
        //$parent->write();
175
        $commController = new CommentingController();
176
        $commController->setOwnerRecord($parent);
177
178
        $form = $commController->CommentsForm();
179
        $commentsFields = $form->Fields()->first()->FieldList();
180
        $expected = array('Name', 'Email', 'URL', 'Comment', 'PreviewComment');
181
        CommentTestHelper::assertFieldNames($this, $expected, $commentsFields);
182
    }
183
*/
184
185
    public function testCommentsFormUsePreview()
186
    {
187
        // test with preview on
188
        Config::inst()->update('CommentableItem', 'comments', array(
189
            'use_preview' => true
190
        ));
191
192
        $this->objFromFixture('Comment', 'firstComAChild1')->delete();
193
        $this->objFromFixture('Comment', 'firstComAChild2')->delete();
194
        $this->objFromFixture('Comment', 'firstComAChild3')->delete();
195
196
        SecurityToken::disable();
197
        $this->autoFollowRedirection = false;
198
        $parent = $this->objFromFixture('CommentableItem', 'first');
199
        $commController = new CommentingController();
200
        $commController->setOwnerRecord($parent);
201
202
        $form = $commController->CommentsForm();
203
        $commentsFields = $form->Fields()->first()->FieldList();
204
        $expected = array('Name', 'Email', 'URL', 'Comment', 'PreviewComment');
205
        CommentTestHelper::assertFieldNames($this, $expected, $commentsFields);
206
207
        // Turn off preview.  Assert lack of preview field
208
        Config::inst()->update('CommentableItem', 'comments', array(
209
            'use_preview' => false
210
        ));
211
        $form = $commController->CommentsForm();
212
        $commentsFields = $form->Fields()->first()->FieldList();
213
        $expected = array('Name', 'Email', 'URL', 'Comment');
214
        CommentTestHelper::assertFieldNames($this, $expected, $commentsFields);
215
    }
216
217
    public function testCommentsForm()
218
    {
219
        // Delete the newly added children of firstComA so as not to change this test
220
        $this->objFromFixture('Comment', 'firstComAChild1')->delete();
221
        $this->objFromFixture('Comment', 'firstComAChild2')->delete();
222
        $this->objFromFixture('Comment', 'firstComAChild3')->delete();
223
224
        SecurityToken::disable();
225
        $this->autoFollowRedirection = false;
226
        $parent = $this->objFromFixture('CommentableItem', 'first');
227
228
        // Test posting to base comment
229
        $response = $this->post('CommentingController/CommentsForm',
230
            array(
231
                'Name' => 'Poster',
232
                'Email' => '[email protected]',
233
                'Comment' => 'My Comment',
234
                'ParentID' => $parent->ID,
235
                'BaseClass' => 'CommentableItem',
236
                'action_doPostComment' => 'Post'
237
            )
238
        );
239
        $this->assertEquals(302, $response->getStatusCode());
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
240
        $this->assertStringStartsWith('CommentableItem_Controller#comment-', $response->getHeader('Location'));
0 ignored issues
show
The method assertStringStartsWith() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
241
        $this->assertDOSEquals(
242
            array(array(
243
                'Name' => 'Poster',
244
                'Email' => '[email protected]',
245
                'Comment' => 'My Comment',
246
                'ParentID' => $parent->ID,
247
                'BaseClass' => 'CommentableItem',
248
            )),
249
            Comment::get()->filter('Email', '[email protected]')
250
        );
251
252
        // Test posting to parent comment
253
        $parentComment = $this->objFromFixture('Comment', 'firstComA');
254
        $this->assertEquals(0, $parentComment->ChildComments()->count());
255
256
        $response = $this->post(
257
            'CommentingController/reply/'.$parentComment->ID,
258
            array(
259
                'Name' => 'Test Author',
260
                'Email' => '[email protected]',
261
                'Comment' => 'Making a reply to firstComA',
262
                'ParentID' => $parent->ID,
263
                'BaseClass' => 'CommentableItem',
264
                'ParentCommentID' => $parentComment->ID,
265
                'action_doPostComment' => 'Post'
266
            )
267
        );
268
        $this->assertEquals(302, $response->getStatusCode());
0 ignored issues
show
The method assertEquals() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
269
        $this->assertStringStartsWith('CommentableItem_Controller#comment-', $response->getHeader('Location'));
0 ignored issues
show
The method assertStringStartsWith() does not seem to exist on object<CommentingControllerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
270
        $this->assertDOSEquals(array(array(
271
            'Name' => 'Test Author',
272
                'Email' => '[email protected]',
273
                'Comment' => 'Making a reply to firstComA',
274
                'ParentID' => $parent->ID,
275
                'BaseClass' => 'CommentableItem',
276
                'ParentCommentID' => $parentComment->ID
277
        )), $parentComment->ChildComments());
278
    }
279
}
280