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); |
|
|
|
|
39
|
|
|
$url = 'CommentingController/approve/' . $comment->ID; |
40
|
|
|
$url = $st->addToUrl($url, Member::currentUser()); |
|
|
|
|
41
|
|
|
$response = $this->get($url); |
42
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
71
|
|
|
$url = 'CommentingController/ham/' . $comment->ID; |
72
|
|
|
$url = $st->addToUrl($url, Member::currentUser()); |
|
|
|
|
73
|
|
|
$response = $this->get($url); |
74
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
93
|
|
|
$url = 'CommentingController/spam/' . $comment->ID; |
94
|
|
|
$url = $st->addToUrl($url, Member::currentUser()); |
|
|
|
|
95
|
|
|
$response = $this->get($url); |
96
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
|
|
|
|
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()); |
|
|
|
|
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"); |
|
|
|
|
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"); |
|
|
|
|
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()); |
|
|
|
|
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()); |
|
|
|
|
233
|
|
|
$this->assertStringStartsWith('CommentableItem_Controller#comment-', $response->getHeader('Location')); |
|
|
|
|
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()); |
|
|
|
|
262
|
|
|
$this->assertStringStartsWith('CommentableItem_Controller#comment-', $response->getHeader('Location')); |
|
|
|
|
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
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.