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