1 | <?php |
||
22 | class BlogTest extends SapphireTest |
||
23 | { |
||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected static $fixture_file = 'blog.yml'; |
||
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | public function setUp() |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function tearDown() |
||
57 | |||
58 | public function testGetExcludedSiteTreeClassNames() |
||
59 | { |
||
60 | $member = Member::currentUser(); |
||
61 | |||
62 | if ($member) { |
||
63 | $member->logout(); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @var Blog $blog |
||
68 | */ |
||
69 | $blog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
||
70 | |||
71 | Config::inst()->update(BlogPost::class, 'show_in_sitetree', true); |
||
72 | $classes = $blog->getExcludedSiteTreeClassNames(); |
||
73 | |||
74 | $this->assertNotContains(BlogPost::class, $classes, 'BlogPost class should be hidden.'); |
||
75 | |||
76 | Config::inst()->update(BlogPost::class, 'show_in_sitetree', false); |
||
77 | $classes = $blog->getExcludedSiteTreeClassNames(); |
||
78 | |||
79 | $this->assertContains(BlogPost::class, $classes, 'BlogPost class should be hidden.'); |
||
80 | } |
||
81 | |||
82 | public function testGetArchivedBlogPosts() |
||
109 | |||
110 | public function testArchiveLinks() |
||
148 | |||
149 | /* |
||
150 | * Test archive year |
||
151 | */ |
||
152 | public function testArchiveYear() |
||
153 | { |
||
154 | $blog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
||
155 | $controller = new BlogController($blog); |
||
156 | $this->requestURL($controller, 'first-post/archive/'); |
||
157 | $this->assertEquals(2013, $controller->getArchiveYear(), 'getArchiveYear should return 2013'); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param string $link |
||
162 | * |
||
163 | * @return int |
||
164 | */ |
||
165 | protected function getStatusOf($link) |
||
169 | |||
170 | public function testRoles() |
||
171 | { |
||
172 | /** |
||
173 | * @var Blog $firstBlog |
||
174 | */ |
||
175 | $firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
||
176 | |||
177 | /** |
||
178 | * @var Blog $fourthBlog |
||
179 | */ |
||
180 | $fourthBlog = $this->objFromFixture(Blog::class, 'FourthBlog'); |
||
181 | |||
182 | /** |
||
183 | * @var BlogPost $postA |
||
184 | */ |
||
185 | $postA = $this->objFromFixture(BlogPost::class, 'PostA'); |
||
186 | |||
187 | /** |
||
188 | * @var BlogPost $postB |
||
189 | */ |
||
190 | $postB = $this->objFromFixture(BlogPost::class, 'PostB'); |
||
191 | |||
192 | /** |
||
193 | * @var BlogPost $postC |
||
194 | */ |
||
195 | $postC = $this->objFromFixture(BlogPost::class, 'PostC'); |
||
196 | |||
197 | /** |
||
198 | * @var Member $editor |
||
199 | */ |
||
200 | $editor = $this->objFromFixture(Member::class, 'BlogEditor'); |
||
201 | |||
202 | /** |
||
203 | * @var Member $writer |
||
204 | */ |
||
205 | $writer = $this->objFromFixture(Member::class, 'Writer'); |
||
206 | |||
207 | /** |
||
208 | * @var Member $contributor |
||
209 | */ |
||
210 | $contributor = $this->objFromFixture(Member::class, 'Contributor'); |
||
211 | |||
212 | /** |
||
213 | * @var Member $visitor |
||
214 | */ |
||
215 | $visitor = $this->objFromFixture(Member::class, 'Visitor'); |
||
216 | |||
217 | $this->assertEquals('Editor', $fourthBlog->RoleOf($editor)); |
||
218 | $this->assertEquals('Contributor', $fourthBlog->RoleOf($contributor)); |
||
219 | $this->assertEquals('Writer', $fourthBlog->RoleOf($writer)); |
||
220 | $this->assertEmpty($fourthBlog->RoleOf($visitor)); |
||
221 | $this->assertEquals('Author', $postA->RoleOf($writer)); |
||
222 | $this->assertEquals('Author', $postA->RoleOf($contributor)); |
||
223 | $this->assertEquals('Editor', $postA->RoleOf($editor)); |
||
224 | $this->assertEmpty($postA->RoleOf($visitor)); |
||
225 | |||
226 | // Test RoleOf with string values given |
||
227 | $this->assertEquals('Editor', $fourthBlog->RoleOf((string)(int)$editor->ID)); |
||
228 | $this->assertEquals('Contributor', $fourthBlog->RoleOf((string)(int)$contributor->ID)); |
||
229 | $this->assertEquals('Writer', $fourthBlog->RoleOf((string)(int)$writer->ID)); |
||
230 | $this->assertEmpty($fourthBlog->RoleOf((string)(int)$visitor->ID)); |
||
231 | $this->assertEquals('Author', $postA->RoleOf((string)(int)$writer->ID)); |
||
232 | $this->assertEquals('Author', $postA->RoleOf((string)(int)$contributor->ID)); |
||
233 | $this->assertEquals('Editor', $postA->RoleOf((string)(int)$editor->ID)); |
||
234 | $this->assertEmpty($postA->RoleOf((string)(int)$visitor->ID)); |
||
235 | |||
236 | // Test RoleOf with int values given |
||
237 | $this->assertEquals('Editor', $fourthBlog->RoleOf((int)$editor->ID)); |
||
238 | $this->assertEquals('Contributor', $fourthBlog->RoleOf((int)$contributor->ID)); |
||
239 | $this->assertEquals('Writer', $fourthBlog->RoleOf((int)$writer->ID)); |
||
240 | $this->assertEmpty($fourthBlog->RoleOf((int)$visitor->ID)); |
||
241 | $this->assertEquals('Author', $postA->RoleOf((int)$writer->ID)); |
||
242 | $this->assertEquals('Author', $postA->RoleOf((int)$contributor->ID)); |
||
243 | $this->assertEquals('Editor', $postA->RoleOf((int)$editor->ID)); |
||
244 | $this->assertEmpty($postA->RoleOf((int)$visitor->ID)); |
||
245 | |||
246 | $this->assertTrue($fourthBlog->canEdit($editor)); |
||
247 | $this->assertFalse($firstBlog->canEdit($editor)); |
||
248 | $this->assertTrue($fourthBlog->canAddChildren($editor)); |
||
249 | $this->assertFalse($firstBlog->canAddChildren($editor)); |
||
250 | $this->assertTrue($postA->canEdit($editor)); |
||
251 | $this->assertTrue($postB->canEdit($editor)); |
||
252 | $this->assertTrue($postC->canEdit($editor)); |
||
253 | $this->assertTrue($postA->canPublish($editor)); |
||
254 | $this->assertTrue($postB->canPublish($editor)); |
||
255 | $this->assertTrue($postC->canPublish($editor)); |
||
256 | |||
257 | $this->assertFalse($fourthBlog->canEdit($writer)); |
||
258 | $this->assertFalse($firstBlog->canEdit($writer)); |
||
259 | $this->assertTrue($fourthBlog->canAddChildren($writer)); |
||
260 | $this->assertFalse($firstBlog->canAddChildren($writer)); |
||
261 | $this->assertTrue($postA->canEdit($writer)); |
||
262 | $this->assertFalse($postB->canEdit($writer)); |
||
263 | $this->assertTrue($postC->canEdit($writer)); |
||
264 | $this->assertTrue($postA->canPublish($writer)); |
||
265 | $this->assertFalse($postB->canPublish($writer)); |
||
266 | $this->assertTrue($postC->canPublish($writer)); |
||
267 | |||
268 | $this->assertFalse($fourthBlog->canEdit($contributor)); |
||
269 | $this->assertFalse($firstBlog->canEdit($contributor)); |
||
270 | $this->assertTrue($fourthBlog->canAddChildren($contributor)); |
||
271 | $this->assertFalse($firstBlog->canAddChildren($contributor)); |
||
272 | $this->assertTrue($postA->canEdit($contributor)); |
||
273 | $this->assertFalse($postB->canEdit($contributor)); |
||
274 | $this->assertTrue($postC->canEdit($contributor)); |
||
275 | $this->assertFalse($postA->canPublish($contributor)); |
||
276 | $this->assertFalse($postB->canPublish($contributor)); |
||
277 | $this->assertFalse($postC->canPublish($contributor)); |
||
278 | |||
279 | $this->assertFalse($fourthBlog->canEdit($visitor)); |
||
280 | $this->assertFalse($firstBlog->canEdit($visitor)); |
||
281 | $this->assertFalse($fourthBlog->canAddChildren($visitor)); |
||
282 | $this->assertFalse($firstBlog->canAddChildren($visitor)); |
||
283 | $this->assertFalse($postA->canEdit($visitor)); |
||
284 | $this->assertFalse($postB->canEdit($visitor)); |
||
285 | $this->assertFalse($postC->canEdit($visitor)); |
||
286 | $this->assertFalse($postA->canPublish($visitor)); |
||
287 | $this->assertFalse($postB->canPublish($visitor)); |
||
288 | $this->assertFalse($postC->canPublish($visitor)); |
||
289 | } |
||
290 | |||
291 | public function testFilteredCategories() |
||
292 | { |
||
293 | $blog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
||
294 | $controller = new BlogController($blog); |
||
295 | |||
296 | // Root url |
||
297 | $this->requestURL($controller, 'first-post'); |
||
298 | $this->assertIDsEquals( |
||
299 | $blog->AllChildren()->column('ID'), |
||
300 | $controller->PaginatedList()->column('ID') |
||
301 | ); |
||
302 | |||
303 | |||
304 | // RSS |
||
305 | $this->requestURL($controller, 'first-post/rss'); |
||
306 | $this->assertIDsEquals( |
||
307 | $blog->AllChildren()->column('ID'), |
||
308 | $controller->PaginatedList()->column('ID') |
||
309 | ); |
||
310 | |||
311 | // Posts |
||
312 | $firstPostID = $this->idFromFixture(BlogPost::class, 'FirstBlogPost'); |
||
313 | $secondPostID = $this->idFromFixture(BlogPost::class, 'SecondBlogPost'); |
||
314 | $firstFuturePostID = $this->idFromFixture(BlogPost::class, 'FirstFutureBlogPost'); |
||
315 | $secondFuturePostID = $this->idFromFixture(BlogPost::class, 'SecondFutureBlogPost'); |
||
316 | |||
317 | // Request first tag |
||
318 | $this->requestURL($controller, 'first-post/tag/first-tag'); |
||
319 | $this->assertIDsEquals( |
||
320 | array($firstPostID, $firstFuturePostID, $secondFuturePostID), |
||
321 | $controller->PaginatedList() |
||
322 | ); |
||
323 | |||
324 | // Request 2013 posts |
||
325 | $this->requestURL($controller, 'first-post/archive/2013'); |
||
326 | $this->assertIDsEquals( |
||
327 | array($firstPostID, $secondPostID, $secondFuturePostID), |
||
328 | $controller->PaginatedList() |
||
329 | ); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Mock a request against a given controller |
||
334 | * |
||
335 | * @param ContentController $controller |
||
336 | * @param string $url |
||
337 | */ |
||
338 | protected function requestURL(ContentController $controller, $url) |
||
346 | |||
347 | /** |
||
348 | * Assert these id lists match |
||
349 | * |
||
350 | * @param array|SS_List $left |
||
351 | * @param array|SS_List $right |
||
352 | */ |
||
353 | protected function assertIDsEquals($left, $right) |
||
365 | } |
||
366 |