1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Blog\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Blog\Model\BlogPost; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
9
|
|
|
use SilverStripe\Security\Member; |
10
|
|
|
use SilverStripe\Versioned\Versioned; |
11
|
|
|
|
12
|
|
|
class BlogPostTest extends SapphireTest |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* {@inheritDoc} |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected static $fixture_file = 'blog.yml'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
|
|
public function tearDown() |
24
|
|
|
{ |
25
|
|
|
DBDatetime::clear_mock_now(); |
26
|
|
|
parent::tearDown(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @dataProvider canViewProvider |
31
|
|
|
*/ |
32
|
|
|
public function testCanView($date, $user, $page, $canView, $stage) |
33
|
|
|
{ |
34
|
|
|
$userRecord = $this->objFromFixture(Member::class, $user); |
35
|
|
|
$pageRecord = $this->objFromFixture(BlogPost::class, $page); |
36
|
|
|
DBDatetime::set_mock_now($date); |
37
|
|
|
if ($stage === 'Live') { |
38
|
|
|
$pageRecord->publishSingle(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
Versioned::set_stage($stage); |
42
|
|
|
$this->assertEquals($canView, $pageRecord->canView($userRecord)); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array Format: |
47
|
|
|
* - mock now date |
48
|
|
|
* - user role (see fixture) |
49
|
|
|
* - blog post fixture ID |
50
|
|
|
* - expected result |
51
|
|
|
* - versioned stage |
52
|
|
|
*/ |
53
|
|
|
public function canViewProvider() |
54
|
|
|
{ |
55
|
|
|
$someFutureDate = '2013-10-10 20:00:00'; |
56
|
|
|
$somePastDate = '2009-10-10 20:00:00'; |
57
|
|
|
return [ |
58
|
|
|
// Check this post given the date has passed |
59
|
|
|
[$someFutureDate, 'Editor', 'PostA', true, 'Stage'], |
60
|
|
|
[$someFutureDate, 'Contributor', 'PostA', true, 'Stage'], |
61
|
|
|
[$someFutureDate, 'BlogEditor', 'PostA', true, 'Stage'], |
62
|
|
|
[$someFutureDate, 'Writer', 'PostA', true, 'Stage'], |
63
|
|
|
|
64
|
|
|
// Check unpublished pages |
65
|
|
|
[$somePastDate, 'Editor', 'PostA', true, 'Stage'], |
66
|
|
|
[$somePastDate, 'Contributor', 'PostA', true, 'Stage'], |
67
|
|
|
[$somePastDate, 'BlogEditor', 'PostA', true, 'Stage'], |
68
|
|
|
[$somePastDate, 'Writer', 'PostA', true, 'Stage'], |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
// Test a page that was authored by another user |
72
|
|
|
|
73
|
|
|
// Check this post given the date has passed |
74
|
|
|
[$someFutureDate, 'Editor', 'FirstBlogPost', true, 'Stage'], |
75
|
|
|
[$someFutureDate, 'Contributor', 'FirstBlogPost', true, 'Stage'], |
76
|
|
|
[$someFutureDate, 'BlogEditor', 'FirstBlogPost', true, 'Stage'], |
77
|
|
|
[$someFutureDate, 'Writer', 'FirstBlogPost', true, 'Stage'], |
78
|
|
|
|
79
|
|
|
// Check future pages in draft stage - users with "view draft pages" permission should |
80
|
|
|
// be able to see this, but visitors should not |
81
|
|
|
[$somePastDate, 'Editor', 'FirstBlogPost', true, 'Stage'], |
82
|
|
|
[$somePastDate, 'Contributor', 'FirstBlogPost', true, 'Stage'], |
83
|
|
|
[$somePastDate, 'BlogEditor', 'FirstBlogPost', true, 'Stage'], |
84
|
|
|
[$somePastDate, 'Writer', 'FirstBlogPost', true, 'Stage'], |
85
|
|
|
[$somePastDate, 'Visitor', 'FirstBlogPost', false, 'Stage'], |
86
|
|
|
|
87
|
|
|
// No future pages in live stage should be visible, even to users that can edit them (in draft) |
88
|
|
|
[$somePastDate, 'Editor', 'FirstBlogPost', false, 'Live'], |
89
|
|
|
[$somePastDate, 'Contributor', 'FirstBlogPost', false, 'Live'], |
90
|
|
|
[$somePastDate, 'BlogEditor', 'FirstBlogPost', false, 'Live'], |
91
|
|
|
[$somePastDate, 'Writer', 'FirstBlogPost', false, 'Live'], |
92
|
|
|
[$somePastDate, 'Visitor', 'FirstBlogPost', false, 'Live'], |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testCandidateAuthors() |
97
|
|
|
{ |
98
|
|
|
$blogpost = $this->objFromFixture(BlogPost::class, 'PostC'); |
99
|
|
|
|
100
|
|
|
$this->assertEquals(7, $blogpost->getCandidateAuthors()->count()); |
101
|
|
|
|
102
|
|
|
//Set the group to draw Members from |
103
|
|
|
Config::inst()->update(BlogPost::class, 'restrict_authors_to_group', 'blogusers'); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$this->assertEquals(3, $blogpost->getCandidateAuthors()->count()); |
106
|
|
|
|
107
|
|
|
// Test cms field is generated |
108
|
|
|
$fields = $blogpost->getCMSFields(); |
109
|
|
|
$this->assertNotEmpty($fields->dataFieldByName('Authors')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function testCanViewFuturePost() |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'NullPublishDate'); |
115
|
|
|
|
116
|
|
|
$editor = $this->objFromFixture(Member::class, 'BlogEditor'); |
117
|
|
|
$this->assertTrue($blogPost->canView($editor)); |
|
|
|
|
118
|
|
|
|
119
|
|
|
$visitor = $this->objFromFixture(Member::class, 'Visitor'); |
120
|
|
|
$this->assertFalse($blogPost->canView($visitor)); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* The purpose of getDate() is to act as a proxy for PublishDate in the default RSS |
125
|
|
|
* template, rather than copying the entire template. |
126
|
|
|
*/ |
127
|
|
|
public function testGetDate() |
128
|
|
|
{ |
129
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'NullPublishDate'); |
130
|
|
|
$this->assertNull($blogPost->getDate()); |
131
|
|
|
|
132
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'PostA'); |
133
|
|
|
$this->assertEquals('2012-01-09 15:00:00', $blogPost->getDate()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testMinutesToRead() |
137
|
|
|
{ |
138
|
|
|
/** @var BlogPost $blogPost */ |
139
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'FirstBlogPost'); |
140
|
|
|
|
141
|
|
|
// over 400 words, should take slightly longer than 2 minutes |
142
|
|
|
$this->assertEquals(2, $blogPost->MinutesToRead()); |
143
|
|
|
|
144
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'SecondBlogPost'); |
145
|
|
|
|
146
|
|
|
// over 200 words, should take slighter longer than 1 minute |
147
|
|
|
$this->assertEquals(1, $blogPost->MinutesToRead()); |
148
|
|
|
|
149
|
|
|
$blogPost = $this->objFromFixture(BlogPost::class, 'ThirdBlogPost'); |
150
|
|
|
// less than 200 words, should take less than a minute thus return an integer of 0 (zero) |
151
|
|
|
$this->assertEquals(0, $blogPost->MinutesToRead()); |
152
|
|
|
|
153
|
|
|
$this->expectException(\InvalidArgumentException::class); |
154
|
|
|
$blogPost->MinutesToRead('not-a-number'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.