1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Blog\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Blog\Model\Blog; |
6
|
|
|
use SilverStripe\Blog\Model\BlogController; |
7
|
|
|
use SilverStripe\Blog\Model\BlogPost; |
8
|
|
|
use SilverStripe\CMS\Controllers\ContentController; |
9
|
|
|
use SilverStripe\Control\Controller; |
10
|
|
|
use SilverStripe\Control\Director; |
11
|
|
|
use SilverStripe\Control\HTTPRequest; |
12
|
|
|
use SilverStripe\Core\Config\Config; |
13
|
|
|
use SilverStripe\Dev\SapphireTest; |
14
|
|
|
use SilverStripe\ORM\DataModel; |
15
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
16
|
|
|
use SilverStripe\ORM\SS_List; |
17
|
|
|
use SilverStripe\Security\Member; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @mixin PHPUnit_Framework_TestCase |
21
|
|
|
*/ |
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() |
33
|
|
|
{ |
34
|
|
|
parent::setUp(); |
35
|
|
|
|
36
|
|
|
Config::nest(); |
37
|
|
|
DBDatetime::set_mock_now('2013-10-10 20:00:00'); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Blog $blog |
41
|
|
|
*/ |
42
|
|
|
$blog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
43
|
|
|
|
44
|
|
|
$blog->publish('Stage', 'Live'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function tearDown() |
51
|
|
|
{ |
52
|
|
|
DBDatetime::clear_mock_now(); |
53
|
|
|
Config::unnest(); |
54
|
|
|
|
55
|
|
|
parent::tearDown(); |
56
|
|
|
} |
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() |
83
|
|
|
{ |
84
|
|
|
$member = Member::currentUser(); |
85
|
|
|
|
86
|
|
|
if ($member) { |
87
|
|
|
$member->logout(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var Blog $blog |
92
|
|
|
*/ |
93
|
|
|
$blog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
94
|
|
|
|
95
|
|
|
$archive = $blog->getArchivedBlogPosts(2013); |
96
|
|
|
|
97
|
|
|
$this->assertEquals(2, $archive->count(), 'Incorrect Yearly Archive count for 2013'); |
98
|
|
|
$this->assertEquals('First Post', $archive->first()->Title, 'Incorrect First Blog post'); |
99
|
|
|
$this->assertEquals('Second Post', $archive->last()->Title, 'Incorrect Last Blog post'); |
100
|
|
|
|
101
|
|
|
$archive = $blog->getArchivedBlogPosts(2013, 10); |
102
|
|
|
|
103
|
|
|
$this->assertEquals(1, $archive->count(), 'Incorrect monthly archive count.'); |
104
|
|
|
|
105
|
|
|
$archive = $blog->getArchivedBlogPosts(2013, 10, 01); |
106
|
|
|
|
107
|
|
|
$this->assertEquals(1, $archive->count(), 'Incorrect daily archive count.'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testArchiveLinks() |
111
|
|
|
{ |
112
|
|
|
/** |
113
|
|
|
* @var Blog $blog |
114
|
|
|
*/ |
115
|
|
|
$blog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
116
|
|
|
|
117
|
|
|
$link = Controller::join_links($blog->Link('archive'), '2013', '10', '01'); |
118
|
|
|
|
119
|
|
|
$this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200'); |
120
|
|
|
|
121
|
|
|
$link = Controller::join_links($blog->Link('archive'), '2013', '10'); |
122
|
|
|
|
123
|
|
|
$this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200'); |
124
|
|
|
|
125
|
|
|
$link = Controller::join_links($blog->Link('archive'), '2013'); |
126
|
|
|
|
127
|
|
|
$this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200'); |
128
|
|
|
|
129
|
|
|
$link = Controller::join_links($blog->Link('archive'), '2011', '10', '01'); |
130
|
|
|
|
131
|
|
|
$this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200'); |
132
|
|
|
|
133
|
|
|
$link = Controller::join_links($blog->Link('archive')); |
134
|
|
|
$this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200'); |
135
|
|
|
|
136
|
|
|
$link = Controller::join_links($blog->Link('archive'), 'invalid-year'); |
137
|
|
|
|
138
|
|
|
$this->assertEquals(404, $this->getStatusOf($link), 'HTTP Status should be 404'); |
139
|
|
|
|
140
|
|
|
$link = Controller::join_links($blog->Link('archive'), '2013', '99'); |
141
|
|
|
|
142
|
|
|
$this->assertEquals(404, $this->getStatusOf($link), 'HTTP Status should be 404'); |
143
|
|
|
|
144
|
|
|
$link = Controller::join_links($blog->Link('archive'), '2013', '10', '99'); |
145
|
|
|
|
146
|
|
|
$this->assertEquals(404, $this->getStatusOf($link), 'HTTP Status should be 404'); |
147
|
|
|
} |
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) |
166
|
|
|
{ |
167
|
|
|
return Director::test($link)->getStatusCode(); |
168
|
|
|
} |
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) |
339
|
|
|
{ |
340
|
|
|
$request = new HTTPRequest('get', $url); |
341
|
|
|
$request->match('$URLSegment//$Action/$ID/$OtherID'); |
342
|
|
|
$request->shift(); |
343
|
|
|
$controller->doInit(); |
344
|
|
|
$controller->handleRequest($request, new DataModel()); |
345
|
|
|
} |
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) |
354
|
|
|
{ |
355
|
|
|
if ($left instanceof SS_List) { |
356
|
|
|
$left = $left->column('ID'); |
357
|
|
|
} |
358
|
|
|
if ($right instanceof SS_List) { |
359
|
|
|
$right = $right->column('ID'); |
360
|
|
|
} |
361
|
|
|
asort($left); |
362
|
|
|
asort($right); |
363
|
|
|
$this->assertEquals(array_values($left), array_values($right)); |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|