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
	public static $fixture_file = 'CommentsTest.yml';
10
11
	protected $extraDataObjects = array(
12
		'CommentableItem'
13
	);
14
15
	protected $securityEnabled;
16
17
	public function tearDown() {
18
		if($this->securityEnabled) {
19
			SecurityToken::enable();
20
		} else {
21
			SecurityToken::disable();
22
		}
23
		parent::tearDown();
24
	}
25
26
	public function setUp() {
27
		parent::setUp();
28
		$this->securityEnabled = SecurityToken::is_enabled();
29
	}
30
31 View Code Duplication
    public function testApprove() {
32
        SecurityToken::disable();
33
34
        // mark a comment as spam then approve it
35
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
36
        $comment = $this->objFromFixture('Comment', 'firstComA');
37
        $comment->markSpam();
38
        $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...
39
        $url = 'CommentingController/approve/' . $comment->ID;
40
        $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...
41
        $response = $this->get($url);
42
        $this->assertEquals(200, $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...
43
        $comment = DataObject::get_by_id('Comment', $comment->ID);
44
45
        // Need to use 0,1 here instead of false, true for SQLite
46
        $this->assertEquals(0, $comment->IsSpam);
47
        $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...
48
49
        // try and approve a non existent comment
50
        $response = $this->get('CommentingController/approve/100000');
51
        $this->assertEquals(404, $response->getStatusCode());
52
53
    }
54
55
    public function testSetGetOwnerController() {
56
        $commController = new CommentingController();
57
        $commController->setOwnerController(Controller::curr());
58
        $this->assertEquals(Controller::curr(), $commController->getOwnerController());
59
        $commController->setOwnerController(null);
60
        $this->assertNull($commController->getOwnerController());
61
    }
62
63 View Code Duplication
    public function testHam() {
64
        SecurityToken::disable();
65
66
        // mark a comment as spam then ham it
67
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
68
        $comment = $this->objFromFixture('Comment', 'firstComA');
69
        $comment->markSpam();
70
        $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...
71
        $url = 'CommentingController/ham/' . $comment->ID;
72
        $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...
73
        $response = $this->get($url);
74
        $this->assertEquals(200, $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...
75
        $comment = DataObject::get_by_id('Comment', $comment->ID);
76
77
        // Need to use 0,1 here instead of false, true for SQLite
78
        $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...
79
        $this->assertEquals(1, $comment->Moderated);
80
81
        // try and ham a non existent comment
82
        $response = $this->get('CommentingController/ham/100000');
83
        $this->assertEquals(404, $response->getStatusCode());
84
85
    }
86
87
    public function testSpam() {
88
        // mark a comment as approved then spam it
89
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
90
        $comment = $this->objFromFixture('Comment', 'firstComA');
91
        $comment->markApproved();
92
        $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...
93
        $url = 'CommentingController/spam/' . $comment->ID;
94
        $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...
95
        $response = $this->get($url);
96
        $this->assertEquals(200, $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...
97
        $comment = DataObject::get_by_id('Comment', $comment->ID);
98
99
        // Need to use 0,1 here instead of false, true for SQLite
100
        $this->assertEquals(1, $comment->IsSpam);
101
        $this->assertEquals(1, $comment->Moderated);
102
103
        // try and spam a non existent comment
104
        $response = $this->get('CommentingController/spam/100000');
105
        $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...
106
107
    }
108
109
	public function testRSS() {
110
        // Delete the newly added children of firstComA so as not to have to recalculate values below
111
        $this->objFromFixture('Comment', 'firstComAChild1')->delete();
112
        $this->objFromFixture('Comment', 'firstComAChild2')->delete();
113
        $this->objFromFixture('Comment', 'firstComAChild3')->delete();
114
115
        $item = $this->objFromFixture('CommentableItem', 'first');
116
117
118
		// comments sitewide
119
		$response = $this->get('CommentingController/rss');
120
		$this->assertEquals(10, substr_count($response->getBody(), "<item>"), "10 approved, non spam comments on page 1");
121
122
		$response = $this->get('CommentingController/rss?start=10');
123
		$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...
124
125
		// all comments on a type
126
		$response = $this->get('CommentingController/rss/CommentableItem');
127
		$this->assertEquals(10, substr_count($response->getBody(), "<item>"));
128
129
		$response = $this->get('CommentingController/rss/CommentableItem?start=10');
130
		$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...
131
132
		// specific page
133
		$response = $this->get('CommentingController/rss/CommentableItem/'.$item->ID);
134
		$this->assertEquals(1, substr_count($response->getBody(), "<item>"));
135
		$this->assertContains('<dc:creator>FA</dc:creator>', $response->getBody());
136
137
		// test accessing comments on a type that doesn't exist
138
		$response = $this->get('CommentingController/rss/Fake');
139
		$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...
140
	}
141
142
    // This is returning a 404 which looks logical code wise but also a bit weird.
143
    // Test module on a clean install and check what the actual URL is first
144
/*    public function testReply() {
145
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
146
        $comment = $this->objFromFixture('Comment', 'firstComA');
147
        $item = $this->objFromFixture('CommentableItem', 'first');
148
149
        $st = new Comment_SecurityToken($comment);
150
        $url = 'CommentingController/reply/' . $item->ID.'?ParentCommentID=' . $comment->ID;
151
        error_log($url);
152
        $response = $this->get($url);
153
        error_log(print_r($response,1));
154
155
        $this->assertEquals(200, $response->getStatusCode());
156
157
    }
158
*/
159
/*
160
    public function testCommentsFormLoadMemberData() {
161
        Config::inst()->update('CommentableItem', 'comments', array(
162
            'use_preview' => false
163
        ));
164
        $this->logInAs('visitor');
165
        SecurityToken::disable();
166
        $parent = $this->objFromFixture('CommentableItem', 'first');
167
        $parent->CommentsRequireLogin = true;
168
        $parent->PostingRequiredPermission = true;
169
        //$parent->write();
170
        $commController = new CommentingController();
171
        $commController->setOwnerRecord($parent);
172
173
        $form = $commController->CommentsForm();
174
        $commentsFields = $form->Fields()->first()->FieldList();
175
        $expected = array('Name', 'Email', 'URL', 'Comment', 'PreviewComment');
176
        CommentTestHelper::assertFieldNames($this, $expected, $commentsFields);
177
    }
178
*/
179
180
    public function testCommentsFormUsePreview() {
181
        // test with preview on
182
        Config::inst()->update('CommentableItem', 'comments', array(
183
            'use_preview' => true
184
        ));
185
186
        $this->objFromFixture('Comment', 'firstComAChild1')->delete();
187
        $this->objFromFixture('Comment', 'firstComAChild2')->delete();
188
        $this->objFromFixture('Comment', 'firstComAChild3')->delete();
189
190
        SecurityToken::disable();
191
        $this->autoFollowRedirection = false;
192
        $parent = $this->objFromFixture('CommentableItem', 'first');
193
        $commController = new CommentingController();
194
        $commController->setOwnerRecord($parent);
195
196
        $form = $commController->CommentsForm();
197
        $commentsFields = $form->Fields()->first()->FieldList();
198
        $expected = array('Name', 'Email', 'URL', 'Comment', 'PreviewComment');
199
        CommentTestHelper::assertFieldNames($this, $expected, $commentsFields);
200
201
        // Turn off preview.  Assert lack of preview field
202
        Config::inst()->update('CommentableItem', 'comments', array(
203
            'use_preview' => false
204
        ));
205
        $form = $commController->CommentsForm();
206
        $commentsFields = $form->Fields()->first()->FieldList();
207
        $expected = array('Name', 'Email', 'URL', 'Comment');
208
        CommentTestHelper::assertFieldNames($this, $expected, $commentsFields);
209
    }
210
211
	public function testCommentsForm() {
212
        // Delete the newly added children of firstComA so as not to change this test
213
        $this->objFromFixture('Comment', 'firstComAChild1')->delete();
214
        $this->objFromFixture('Comment', 'firstComAChild2')->delete();
215
        $this->objFromFixture('Comment', 'firstComAChild3')->delete();
216
217
		SecurityToken::disable();
218
		$this->autoFollowRedirection = false;
219
		$parent = $this->objFromFixture('CommentableItem', 'first');
220
221
		// Test posting to base comment
222
		$response = $this->post('CommentingController/CommentsForm',
223
			array(
224
				'Name' => 'Poster',
225
				'Email' => '[email protected]',
226
				'Comment' => 'My Comment',
227
				'ParentID' => $parent->ID,
228
				'BaseClass' => 'CommentableItem',
229
				'action_doPostComment' => 'Post'
230
			)
231
		);
232
		$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...
233
		$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...
234
		$this->assertDOSEquals(
235
			array(array(
236
				'Name' => 'Poster',
237
				'Email' => '[email protected]',
238
				'Comment' => 'My Comment',
239
				'ParentID' => $parent->ID,
240
				'BaseClass' => 'CommentableItem',
241
			)),
242
			Comment::get()->filter('Email', '[email protected]')
243
		);
244
245
		// Test posting to parent comment
246
		$parentComment = $this->objFromFixture('Comment', 'firstComA');
247
		$this->assertEquals(0, $parentComment->ChildComments()->count());
248
249
		$response = $this->post(
250
			'CommentingController/reply/'.$parentComment->ID,
251
			array(
252
				'Name' => 'Test Author',
253
				'Email' => '[email protected]',
254
				'Comment' => 'Making a reply to firstComA',
255
				'ParentID' => $parent->ID,
256
				'BaseClass' => 'CommentableItem',
257
				'ParentCommentID' => $parentComment->ID,
258
				'action_doPostComment' => 'Post'
259
			)
260
		);
261
		$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...
262
		$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...
263
		$this->assertDOSEquals(array(array(
264
			'Name' => 'Test Author',
265
				'Email' => '[email protected]',
266
				'Comment' => 'Making a reply to firstComA',
267
				'ParentID' => $parent->ID,
268
				'BaseClass' => 'CommentableItem',
269
				'ParentCommentID' => $parentComment->ID
270
		)), $parentComment->ChildComments());
271
272
273
	}
274
}
275