|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class PostTest extends FunctionalTest |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
public static $fixture_file = "forum/tests/ForumTest.yml"; |
|
6
|
|
|
|
|
7
|
|
|
// fixes permission issues with these tests, we don't need to test versioning anyway. |
|
8
|
|
|
// without this, SiteTree::canView() would always return false even though CanViewType == Anyone. |
|
9
|
|
|
public static $use_draft_site = true; |
|
10
|
|
|
|
|
11
|
|
|
public function setUp() |
|
12
|
|
|
{ |
|
13
|
|
|
parent::setUp(); |
|
14
|
|
|
|
|
15
|
|
|
//track the default state of tokens |
|
16
|
|
|
$this->useToken = SecurityToken::is_enabled(); |
|
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public function tearDown() |
|
20
|
|
|
{ |
|
21
|
|
|
parent::tearDown(); |
|
22
|
|
|
|
|
23
|
|
|
//if the token is turned on reset it before the next test run |
|
24
|
|
|
if ($this->useToken) { |
|
25
|
|
|
SecurityToken::enable(); |
|
26
|
|
|
} else { |
|
27
|
|
|
SecurityToken::disable(); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function testPermissions() |
|
32
|
|
|
{ |
|
33
|
|
|
$member1 = $this->objFromFixture('Member', 'test1'); |
|
34
|
|
|
$member2 = $this->objFromFixture('Member', 'test2'); |
|
35
|
|
|
$moderator = $this->objFromFixture('Member', 'moderator'); |
|
36
|
|
|
$admin = $this->objFromFixture('Member', 'admin'); |
|
37
|
|
|
|
|
38
|
|
|
$postMember2 = $this->objFromFixture('Post', 'Post18'); |
|
39
|
|
|
|
|
40
|
|
|
// read only thread post |
|
41
|
|
|
$member1->logIn(); |
|
42
|
|
|
$postReadonly = $this->objFromFixture('Post', 'ReadonlyThreadPost'); |
|
43
|
|
|
$this->assertFalse($postReadonly->canEdit()); // Even though it's user's own |
|
|
|
|
|
|
44
|
|
|
$this->assertTrue($postReadonly->canView()); |
|
|
|
|
|
|
45
|
|
|
$this->assertFalse($postReadonly->canCreate()); |
|
|
|
|
|
|
46
|
|
|
$this->assertFalse($postReadonly->canDelete()); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
// normal thread. They can post to these |
|
49
|
|
|
$member1->logIn(); |
|
50
|
|
|
$this->assertFalse($postMember2->canEdit()); // Not user's post |
|
|
|
|
|
|
51
|
|
|
$this->assertTrue($postMember2->canView()); |
|
|
|
|
|
|
52
|
|
|
$this->assertTrue($postMember2->canCreate()); |
|
|
|
|
|
|
53
|
|
|
$this->assertFalse($postMember2->canDelete()); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
// Check the user has full rights on his own post |
|
56
|
|
|
$member2->logIn(); |
|
57
|
|
|
$this->assertTrue($postMember2->canEdit()); // User's post |
|
|
|
|
|
|
58
|
|
|
$this->assertTrue($postMember2->canView()); |
|
|
|
|
|
|
59
|
|
|
$this->assertTrue($postMember2->canCreate()); |
|
|
|
|
|
|
60
|
|
|
$this->assertTrue($postMember2->canDelete()); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
// Moderator can delete posts, even if he doesn't own them |
|
63
|
|
|
$moderator->logIn(); |
|
64
|
|
|
$this->assertFalse($postMember2->canEdit()); |
|
|
|
|
|
|
65
|
|
|
$this->assertTrue($postMember2->canView()); |
|
|
|
|
|
|
66
|
|
|
$this->assertTrue($postMember2->canCreate()); |
|
|
|
|
|
|
67
|
|
|
$this->assertTrue($postMember2->canDelete()); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
// Admins should have full rights, even if they're not moderators or own the post |
|
70
|
|
|
$admin->logIn(); |
|
71
|
|
|
$this->assertTrue($postMember2->canEdit()); |
|
|
|
|
|
|
72
|
|
|
$this->assertTrue($postMember2->canView()); |
|
|
|
|
|
|
73
|
|
|
$this->assertTrue($postMember2->canCreate()); |
|
|
|
|
|
|
74
|
|
|
$this->assertTrue($postMember2->canDelete()); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testGetTitle() |
|
78
|
|
|
{ |
|
79
|
|
|
$post = $this->objFromFixture('Post', 'Post1'); |
|
80
|
|
|
$reply = $this->objFromFixture('Post', 'Post2'); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertEquals($post->Title, "Test Thread"); |
|
|
|
|
|
|
83
|
|
|
$this->assertEquals($reply->Title, "Re: Test Thread"); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$first = $this->objFromFixture('Post', 'Post3'); |
|
86
|
|
|
$this->assertEquals($first->Title, 'Another Test Thread'); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testIssFirstPost() |
|
90
|
|
|
{ |
|
91
|
|
|
$first = $this->objFromFixture('Post', 'Post1'); |
|
92
|
|
|
$this->assertTrue($first->isFirstPost()); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
$notFirst = $this->objFromFixture('Post', 'Post2'); |
|
95
|
|
|
$this->assertFalse($notFirst->isFirstPost()); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testReplyLink() |
|
99
|
|
|
{ |
|
100
|
|
|
$post = $this->objFromFixture('Post', 'Post1'); |
|
101
|
|
|
$this->assertContains($post->Thread()->URLSegment .'/reply/'.$post->ThreadID, $post->ReplyLink()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testShowLink() |
|
105
|
|
|
{ |
|
106
|
|
|
$post = $this->objFromFixture('Post', 'Post1'); |
|
107
|
|
|
Forum::$posts_per_page = 8; |
|
108
|
|
|
|
|
109
|
|
|
// test for show link on first page |
|
110
|
|
|
$this->assertContains($post->Thread()->URLSegment .'/show/'.$post->ThreadID, $post->ShowLink()); |
|
111
|
|
|
|
|
112
|
|
|
// test for link that should be last post on the first page |
|
113
|
|
|
$eighthPost = $this->objFromFixture('Post', 'Post9'); |
|
114
|
|
|
$this->assertContains($eighthPost->Thread()->URLSegment .'/show/'.$eighthPost->ThreadID.'#post'.$eighthPost->ID, $eighthPost->ShowLink()); |
|
115
|
|
|
|
|
116
|
|
|
// test for a show link on a subpage |
|
117
|
|
|
$lastPost = $this->objFromFixture('Post', 'Post10'); |
|
118
|
|
|
$this->assertContains($lastPost->Thread()->URLSegment .'/show/'. $lastPost->ThreadID . '?start=8#post'.$lastPost->ID, $lastPost->ShowLink()); |
|
119
|
|
|
|
|
120
|
|
|
// this is the last post on page 2 |
|
121
|
|
|
$lastPost = $this->objFromFixture('Post', 'Post17'); |
|
122
|
|
|
$this->assertContains($lastPost->Thread()->URLSegment .'/show/'. $lastPost->ThreadID . '?start=8#post'.$lastPost->ID, $lastPost->ShowLink()); |
|
123
|
|
|
|
|
124
|
|
|
// test for a show link on the last subpage |
|
125
|
|
|
$lastPost = $this->objFromFixture('Post', 'Post18'); |
|
126
|
|
|
$this->assertContains($lastPost->Thread()->URLSegment .'/show/'. $lastPost->ThreadID . '?start=16#post'.$lastPost->ID, $lastPost->ShowLink()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testEditLink() |
|
130
|
|
|
{ |
|
131
|
|
|
$post = $this->objFromFixture('Post', 'Post1'); |
|
132
|
|
|
|
|
133
|
|
|
// should be false since we're not logged in. |
|
134
|
|
|
if ($member = Member::currentUser()) { |
|
135
|
|
|
$member->logOut(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$this->assertFalse($post->EditLink()); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
// logged in as the member. Should be able to edit it |
|
141
|
|
|
$member = $this->objFromFixture('Member', 'test1'); |
|
142
|
|
|
$member->logIn(); |
|
143
|
|
|
|
|
144
|
|
|
$this->assertContains($post->Thread()->URLSegment .'/editpost/'. $post->ID, $post->EditLink()); |
|
145
|
|
|
|
|
146
|
|
|
// log in as another member who is not |
|
147
|
|
|
$member->logOut(); |
|
148
|
|
|
|
|
149
|
|
|
$memberOther = $this->objFromFixture('Member', 'test2'); |
|
150
|
|
|
$memberOther->logIn(); |
|
151
|
|
|
|
|
152
|
|
|
$this->assertFalse($post->EditLink()); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
View Code Duplication |
public function testDeleteLink() |
|
|
|
|
|
|
156
|
|
|
{ |
|
157
|
|
|
$post = $this->objFromFixture('Post', 'Post1'); |
|
158
|
|
|
|
|
159
|
|
|
//enable token |
|
160
|
|
|
SecurityToken::enable(); |
|
161
|
|
|
|
|
162
|
|
|
// should be false since we're not logged in. |
|
163
|
|
|
if ($member = Member::currentUser()) { |
|
164
|
|
|
$member->logOut(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$this->assertFalse($post->EditLink()); |
|
|
|
|
|
|
168
|
|
|
$this->assertFalse($post->DeleteLink()); |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
// logged in as the moderator. Should be able to delete the post. |
|
171
|
|
|
$member = $this->objFromFixture('Member', 'moderator'); |
|
172
|
|
|
$member->logIn(); |
|
173
|
|
|
|
|
174
|
|
|
$this->assertContains($post->Thread()->URLSegment .'/deletepost/'. $post->ID, $post->DeleteLink()); |
|
175
|
|
|
|
|
176
|
|
|
// because this is the first post test for the class which is used in javascript |
|
177
|
|
|
$this->assertContains("class=\"deleteLink firstPost\"", $post->DeleteLink()); |
|
178
|
|
|
|
|
179
|
|
|
$member->logOut(); |
|
180
|
|
|
|
|
181
|
|
|
// log in as another member who is not in a position to delete this post |
|
182
|
|
|
$member = $this->objFromFixture('Member', 'test2'); |
|
183
|
|
|
$member->logIn(); |
|
184
|
|
|
|
|
185
|
|
|
$this->assertFalse($post->DeleteLink()); |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
// log in as someone who can moderate this post (and therefore delete it) |
|
188
|
|
|
$member = $this->objFromFixture('Member', 'moderator'); |
|
189
|
|
|
$member->logIn(); |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
//check for the existance of a CSRF token |
|
193
|
|
|
$this->assertContains("SecurityID=", $post->DeleteLink()); |
|
194
|
|
|
|
|
195
|
|
|
// should be able to edit post since they're moderators |
|
196
|
|
|
$this->assertContains($post->Thread()->URLSegment .'/deletepost/'. $post->ID, $post->DeleteLink()); |
|
197
|
|
|
|
|
198
|
|
|
// test that a 2nd post doesn't have the first post ID hook |
|
199
|
|
|
$memberOthersPost = $this->objFromFixture('Post', 'Post2'); |
|
200
|
|
|
|
|
201
|
|
|
$this->assertFalse(strstr($memberOthersPost->DeleteLink(), "firstPost")); |
|
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
View Code Duplication |
public function testMarkAsSpamLink() |
|
|
|
|
|
|
205
|
|
|
{ |
|
206
|
|
|
$post = $this->objFromFixture('Post', 'Post1'); |
|
207
|
|
|
|
|
208
|
|
|
//enable token |
|
209
|
|
|
SecurityToken::enable(); |
|
210
|
|
|
|
|
211
|
|
|
// should be false since we're not logged in. |
|
212
|
|
|
if ($member = Member::currentUser()) { |
|
213
|
|
|
$member->logOut(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$this->assertFalse($post->EditLink()); |
|
|
|
|
|
|
217
|
|
|
$this->assertFalse($post->MarkAsSpamLink()); |
|
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
// logged in as the moderator. Should be able to mark the post as spam. |
|
220
|
|
|
$member = $this->objFromFixture('Member', 'moderator'); |
|
221
|
|
|
$member->logIn(); |
|
222
|
|
|
|
|
223
|
|
|
$this->assertContains($post->Thread()->URLSegment .'/markasspam/'. $post->ID, $post->MarkAsSpamLink()); |
|
224
|
|
|
|
|
225
|
|
|
// because this is the first post test for the class which is used in javascript |
|
226
|
|
|
$this->assertContains("class=\"markAsSpamLink firstPost\"", $post->MarkAsSpamLink()); |
|
227
|
|
|
|
|
228
|
|
|
$member->logOut(); |
|
229
|
|
|
|
|
230
|
|
|
// log in as another member who is not in a position to mark post as spam this post |
|
231
|
|
|
$member = $this->objFromFixture('Member', 'test2'); |
|
232
|
|
|
$member->logIn(); |
|
233
|
|
|
|
|
234
|
|
|
$this->assertFalse($post->MarkAsSpamLink()); |
|
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
// log in as someone who can moderate this post (and therefore mark as spam) |
|
237
|
|
|
$member = $this->objFromFixture('Member', 'moderator'); |
|
238
|
|
|
$member->logIn(); |
|
239
|
|
|
|
|
240
|
|
|
|
|
241
|
|
|
//check for the existance of a CSRF token |
|
242
|
|
|
$this->assertContains("SecurityID=", $post->MarkAsSpamLink()); |
|
243
|
|
|
|
|
244
|
|
|
// should be able to edit post since they're moderators |
|
245
|
|
|
$this->assertContains($post->Thread()->URLSegment .'/markasspam/'. $post->ID, $post->MarkAsSpamLink()); |
|
246
|
|
|
|
|
247
|
|
|
// test that a 2nd post doesn't have the first post ID hook |
|
248
|
|
|
$memberOthersPost = $this->objFromFixture('Post', 'Post2'); |
|
249
|
|
|
|
|
250
|
|
|
$this->assertFalse(strstr($memberOthersPost->MarkAsSpamLink(), "firstPost")); |
|
|
|
|
|
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
public function testBanAndGhostLink() |
|
254
|
|
|
{ |
|
255
|
|
|
$post = $this->objFromFixture('Post', 'Post1'); |
|
256
|
|
|
|
|
257
|
|
|
// should be false since we're not logged in. |
|
258
|
|
|
if ($member = Member::currentUser()) { |
|
259
|
|
|
$member->logOut(); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
$this->assertFalse($post->EditLink()); |
|
|
|
|
|
|
263
|
|
|
$this->assertFalse($post->BanLink()); |
|
|
|
|
|
|
264
|
|
|
$this->assertFalse($post->GhostLink()); |
|
|
|
|
|
|
265
|
|
|
|
|
266
|
|
|
// logged in as the moderator. Should be able to mark the post as spam. |
|
267
|
|
|
$member = $this->objFromFixture('Member', 'moderator'); |
|
268
|
|
|
$member->logIn(); |
|
269
|
|
|
|
|
270
|
|
|
$forum = $post->Thread()->Forum(); |
|
271
|
|
|
$this->assertContains($forum->URLSegment . '/ban/' . $post->AuthorID, $post->BanLink()); |
|
272
|
|
|
$this->assertContains($forum->URLSegment . '/ghost/' . $post->AuthorID, $post->GhostLink()); |
|
273
|
|
|
|
|
274
|
|
|
$member->logOut(); |
|
275
|
|
|
|
|
276
|
|
|
// log in as another member who is not in a position to mark post as spam this post |
|
277
|
|
|
$member = $this->objFromFixture('Member', 'test2'); |
|
278
|
|
|
$member->logIn(); |
|
279
|
|
|
|
|
280
|
|
|
$this->assertFalse($post->BanLink()); |
|
|
|
|
|
|
281
|
|
|
$this->assertFalse($post->GhostLink()); |
|
|
|
|
|
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
public function testGetUpdated() |
|
285
|
|
|
{ |
|
286
|
|
|
$post = new Post(); |
|
287
|
|
|
$post->Content = "Original Content"; |
|
|
|
|
|
|
288
|
|
|
$post->write(); |
|
289
|
|
|
|
|
290
|
|
|
$this->assertNull($post->Updated); |
|
|
|
|
|
|
291
|
|
|
sleep(2); |
|
292
|
|
|
$post->Content = "Some Content Now"; |
|
|
|
|
|
|
293
|
|
|
$post->write(); |
|
294
|
|
|
|
|
295
|
|
|
$this->assertNotNull($post->Updated); |
|
|
|
|
|
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
public function testRSSContent() |
|
299
|
|
|
{ |
|
300
|
|
|
// @todo escaping tests. They are handled by bbcode parser tests? |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
public function testRSSAuthor() |
|
304
|
|
|
{ |
|
305
|
|
|
// @todo |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
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.