Completed
Pull Request — master (#421)
by Robbie
02:31
created

BlogTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 13
dl 0
loc 344
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 14 1
A tearDown() 0 7 1
A testGetExcludedSiteTreeClassNames() 0 23 2
B testGetArchivedBlogPosts() 0 27 2
B testArchiveLinks() 0 38 1
A testArchiveYear() 0 7 1
A getStatusOf() 0 4 1
B testRoles() 0 120 1
B testFilteredCategories() 0 40 1
A requestURL() 0 8 1
A assertIDsEquals() 0 12 3
1
<?php
2
3
use SilverStripe\Blog\Model\BlogController;
4
use SilverStripe\CMS\Controllers\ContentController;
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\HTTPRequest;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, HTTPRequest.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\ORM\DataModel;
11
use SilverStripe\ORM\FieldType\DBDatetime;
12
use SilverStripe\ORM\SS_List;
13
use SilverStripe\Security\Member;
14
15
/**
16
 * @mixin PHPUnit_Framework_TestCase
17
 */
18
class BlogTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
19
{
20
    /**
21
     * @var string
22
     */
23
    public static $fixture_file = 'blog.yml';
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function setUp()
29
    {
30
        parent::setUp();
31
32
        Config::nest();
33
        DBDatetime::set_mock_now('2013-10-10 20:00:00');
34
35
        /**
36
         * @var Blog $blog
37
         */
38
        $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog');
39
40
        $blog->publish('Stage', 'Live');
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function tearDown()
47
    {
48
        DBDatetime::clear_mock_now();
49
        Config::unnest();
50
51
        parent::tearDown();
52
    }
53
54
    public function testGetExcludedSiteTreeClassNames()
55
    {
56
        $member = Member::currentUser();
57
58
        if ($member) {
59
            $member->logout();
60
        }
61
62
        /**
63
         * @var Blog $blog
64
         */
65
        $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog');
66
67
        Config::inst()->update('SilverStripe\\Blog\\Model\\BlogPost', 'show_in_sitetree', true);
68
        $classes = $blog->getExcludedSiteTreeClassNames();
69
70
        $this->assertNotContains('SilverStripe\\Blog\\Model\\BlogPost', $classes, 'BlogPost class should be hidden.');
71
72
        Config::inst()->update('SilverStripe\\Blog\\Model\\BlogPost', 'show_in_sitetree', false);
73
        $classes = $blog->getExcludedSiteTreeClassNames();
74
75
        $this->assertContains('SilverStripe\\Blog\\Model\\BlogPost', $classes, 'BlogPost class should be hidden.');
76
    }
77
78
    public function testGetArchivedBlogPosts()
79
    {
80
        $member = Member::currentUser();
81
82
        if ($member) {
83
            $member->logout();
84
        }
85
86
        /**
87
         * @var Blog $blog
88
         */
89
        $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog');
90
91
        $archive = $blog->getArchivedBlogPosts(2013);
92
93
        $this->assertEquals(2, $archive->count(), 'Incorrect Yearly Archive count for 2013');
94
        $this->assertEquals('First Post', $archive->first()->Title, 'Incorrect First Blog post');
95
        $this->assertEquals('Second Post', $archive->last()->Title, 'Incorrect Last Blog post');
96
97
        $archive = $blog->getArchivedBlogPosts(2013, 10);
98
99
        $this->assertEquals(1, $archive->count(), 'Incorrect monthly archive count.');
100
101
        $archive = $blog->getArchivedBlogPosts(2013, 10, 01);
102
103
        $this->assertEquals(1, $archive->count(), 'Incorrect daily archive count.');
104
    }
105
106
    public function testArchiveLinks()
107
    {
108
        /**
109
         * @var Blog $blog
110
         */
111
        $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog');
112
113
        $link = Controller::join_links($blog->Link('archive'), '2013', '10', '01');
114
115
        $this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200');
116
117
        $link = Controller::join_links($blog->Link('archive'), '2013', '10');
118
119
        $this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200');
120
121
        $link = Controller::join_links($blog->Link('archive'), '2013');
122
123
        $this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200');
124
125
        $link = Controller::join_links($blog->Link('archive'), '2011', '10', '01');
126
127
        $this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200');
128
129
        $link = Controller::join_links($blog->Link('archive'));
130
        $this->assertEquals(200, $this->getStatusOf($link), 'HTTP Status should be 200');
131
132
        $link = Controller::join_links($blog->Link('archive'), 'invalid-year');
133
134
        $this->assertEquals(404, $this->getStatusOf($link), 'HTTP Status should be 404');
135
136
        $link = Controller::join_links($blog->Link('archive'), '2013', '99');
137
138
        $this->assertEquals(404, $this->getStatusOf($link), 'HTTP Status should be 404');
139
140
        $link = Controller::join_links($blog->Link('archive'), '2013', '10', '99');
141
142
        $this->assertEquals(404, $this->getStatusOf($link), 'HTTP Status should be 404');
143
    }
144
145
    /*
146
     * Test archive year
147
     */
148
    public function testArchiveYear()
149
    {
150
        $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog');
151
        $controller = new BlogController($blog);
152
        $this->requestURL($controller, 'first-post/archive/');
153
        $this->assertEquals(2013, $controller->getArchiveYear(), 'getArchiveYear should return 2013');
154
    }
155
156
    /**
157
     * @param string $link
158
     *
159
     * @return int
160
     */
161
    protected function getStatusOf($link)
162
    {
163
        return Director::test($link)->getStatusCode();
164
    }
165
166
    public function testRoles()
167
    {
168
        /**
169
         * @var Blog $firstBlog
170
         */
171
        $firstBlog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog');
172
173
        /**
174
         * @var Blog $fourthBlog
175
         */
176
        $fourthBlog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FourthBlog');
177
178
        /**
179
         * @var BlogPost $postA
180
         */
181
        $postA = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostA');
182
183
        /**
184
         * @var BlogPost $postB
185
         */
186
        $postB = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostB');
187
188
        /**
189
         * @var BlogPost $postC
190
         */
191
        $postC = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'PostC');
192
193
        /**
194
         * @var Member $editor
195
         */
196
        $editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'BlogEditor');
197
198
        /**
199
         * @var Member $writer
200
         */
201
        $writer = $this->objFromFixture('SilverStripe\\Security\\Member', 'Writer');
202
203
        /**
204
         * @var Member $contributor
205
         */
206
        $contributor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Contributor');
207
208
        /**
209
         * @var Member $visitor
210
         */
211
        $visitor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Visitor');
212
213
        $this->assertEquals('Editor', $fourthBlog->RoleOf($editor));
214
        $this->assertEquals('Contributor', $fourthBlog->RoleOf($contributor));
215
        $this->assertEquals('Writer', $fourthBlog->RoleOf($writer));
216
        $this->assertEmpty($fourthBlog->RoleOf($visitor));
217
        $this->assertEquals('Author', $postA->RoleOf($writer));
218
        $this->assertEquals('Author', $postA->RoleOf($contributor));
219
        $this->assertEquals('Editor', $postA->RoleOf($editor));
220
        $this->assertEmpty($postA->RoleOf($visitor));
221
222
        // Test RoleOf with string values given
223
        $this->assertEquals('Editor', $fourthBlog->RoleOf((string)(int)$editor->ID));
224
        $this->assertEquals('Contributor', $fourthBlog->RoleOf((string)(int)$contributor->ID));
225
        $this->assertEquals('Writer', $fourthBlog->RoleOf((string)(int)$writer->ID));
226
        $this->assertEmpty($fourthBlog->RoleOf((string)(int)$visitor->ID));
227
        $this->assertEquals('Author', $postA->RoleOf((string)(int)$writer->ID));
228
        $this->assertEquals('Author', $postA->RoleOf((string)(int)$contributor->ID));
229
        $this->assertEquals('Editor', $postA->RoleOf((string)(int)$editor->ID));
230
        $this->assertEmpty($postA->RoleOf((string)(int)$visitor->ID));
231
232
        // Test RoleOf with int values given
233
        $this->assertEquals('Editor', $fourthBlog->RoleOf((int)$editor->ID));
234
        $this->assertEquals('Contributor', $fourthBlog->RoleOf((int)$contributor->ID));
235
        $this->assertEquals('Writer', $fourthBlog->RoleOf((int)$writer->ID));
236
        $this->assertEmpty($fourthBlog->RoleOf((int)$visitor->ID));
237
        $this->assertEquals('Author', $postA->RoleOf((int)$writer->ID));
238
        $this->assertEquals('Author', $postA->RoleOf((int)$contributor->ID));
239
        $this->assertEquals('Editor', $postA->RoleOf((int)$editor->ID));
240
        $this->assertEmpty($postA->RoleOf((int)$visitor->ID));
241
242
        $this->assertTrue($fourthBlog->canEdit($editor));
243
        $this->assertFalse($firstBlog->canEdit($editor));
244
        $this->assertTrue($fourthBlog->canAddChildren($editor));
245
        $this->assertFalse($firstBlog->canAddChildren($editor));
246
        $this->assertTrue($postA->canEdit($editor));
247
        $this->assertTrue($postB->canEdit($editor));
248
        $this->assertTrue($postC->canEdit($editor));
249
        $this->assertTrue($postA->canPublish($editor));
250
        $this->assertTrue($postB->canPublish($editor));
251
        $this->assertTrue($postC->canPublish($editor));
252
253
        $this->assertFalse($fourthBlog->canEdit($writer));
254
        $this->assertFalse($firstBlog->canEdit($writer));
255
        $this->assertTrue($fourthBlog->canAddChildren($writer));
256
        $this->assertFalse($firstBlog->canAddChildren($writer));
257
        $this->assertTrue($postA->canEdit($writer));
258
        $this->assertFalse($postB->canEdit($writer));
259
        $this->assertTrue($postC->canEdit($writer));
260
        $this->assertTrue($postA->canPublish($writer));
261
        $this->assertFalse($postB->canPublish($writer));
262
        $this->assertTrue($postC->canPublish($writer));
263
264
        $this->assertFalse($fourthBlog->canEdit($contributor));
265
        $this->assertFalse($firstBlog->canEdit($contributor));
266
        $this->assertTrue($fourthBlog->canAddChildren($contributor));
267
        $this->assertFalse($firstBlog->canAddChildren($contributor));
268
        $this->assertTrue($postA->canEdit($contributor));
269
        $this->assertFalse($postB->canEdit($contributor));
270
        $this->assertTrue($postC->canEdit($contributor));
271
        $this->assertFalse($postA->canPublish($contributor));
272
        $this->assertFalse($postB->canPublish($contributor));
273
        $this->assertFalse($postC->canPublish($contributor));
274
275
        $this->assertFalse($fourthBlog->canEdit($visitor));
276
        $this->assertFalse($firstBlog->canEdit($visitor));
277
        $this->assertFalse($fourthBlog->canAddChildren($visitor));
278
        $this->assertFalse($firstBlog->canAddChildren($visitor));
279
        $this->assertFalse($postA->canEdit($visitor));
280
        $this->assertFalse($postB->canEdit($visitor));
281
        $this->assertFalse($postC->canEdit($visitor));
282
        $this->assertFalse($postA->canPublish($visitor));
283
        $this->assertFalse($postB->canPublish($visitor));
284
        $this->assertFalse($postC->canPublish($visitor));
285
    }
286
287
    public function testFilteredCategories()
288
    {
289
        $blog = $this->objFromFixture('SilverStripe\\Blog\\Model\\Blog', 'FirstBlog');
290
        $controller = new BlogController($blog);
291
292
        // Root url
293
        $this->requestURL($controller, 'first-post');
294
        $this->assertIDsEquals(
295
            $blog->AllChildren()->column('ID'),
296
            $controller->PaginatedList()->column('ID')
297
        );
298
299
300
        // RSS
301
        $this->requestURL($controller, 'first-post/rss');
302
        $this->assertIDsEquals(
303
            $blog->AllChildren()->column('ID'),
304
            $controller->PaginatedList()->column('ID')
305
        );
306
307
        // Posts
308
        $firstPostID = $this->idFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'FirstBlogPost');
309
        $secondPostID = $this->idFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'SecondBlogPost');
310
        $firstFuturePostID = $this->idFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'FirstFutureBlogPost');
311
        $secondFuturePostID = $this->idFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'SecondFutureBlogPost');
312
313
        // Request first tag
314
        $this->requestURL($controller, 'first-post/tag/first-tag');
315
        $this->assertIDsEquals(
316
            array($firstPostID, $firstFuturePostID, $secondFuturePostID),
317
            $controller->PaginatedList()
318
        );
319
320
        // Request 2013 posts
321
        $this->requestURL($controller, 'first-post/archive/2013');
322
        $this->assertIDsEquals(
323
            array($firstPostID, $secondPostID, $secondFuturePostID),
324
            $controller->PaginatedList()
325
        );
326
    }
327
328
    /**
329
     * Mock a request against a given controller
330
     *
331
     * @param ContentController $controller
332
     * @param string $url
333
     */
334
    protected function requestURL(ContentController $controller, $url)
335
    {
336
        $request = new HTTPRequest('get', $url);
337
        $request->match('$URLSegment//$Action/$ID/$OtherID');
338
        $request->shift();
339
        $controller->init();
340
        $controller->handleRequest($request, new DataModel());
341
    }
342
343
    /**
344
     * Assert these id lists match
345
     *
346
     * @param array|SS_List $left
347
     * @param array|SS_List $right
348
     */
349
    protected function assertIDsEquals($left, $right)
350
    {
351
        if ($left instanceof SS_List) {
352
            $left = $left->column('ID');
353
        }
354
        if ($right instanceof SS_List) {
355
            $right = $right->column('ID');
356
        }
357
        asort($left);
358
        asort($right);
359
        $this->assertEquals(array_values($left), array_values($right));
360
    }
361
}
362