1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Blog\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Blog\Model\Blog; |
6
|
|
|
use SilverStripe\Blog\Model\BlogCategory; |
7
|
|
|
use SilverStripe\Blog\Model\BlogPost; |
8
|
|
|
use SilverStripe\Control\Controller; |
9
|
|
|
use SilverStripe\Dev\FunctionalTest; |
10
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
11
|
|
|
use SilverStripe\ORM\ValidationException; |
12
|
|
|
use SilverStripe\Security\Member; |
13
|
|
|
use SilverStripe\Security\Security; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @mixin PHPUnit_Framework_TestCase |
17
|
|
|
*/ |
18
|
|
|
class BlogCategoryTest extends FunctionalTest |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected static $fixture_file = 'blog.yml'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
DBDatetime::set_mock_now('2013-10-10 20:00:00'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function tearDown() |
39
|
|
|
{ |
40
|
|
|
DBDatetime::clear_mock_now(); |
41
|
|
|
|
42
|
|
|
parent::tearDown(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Tests that any blog posts returned from $category->BlogPosts() many_many are published, |
47
|
|
|
* both by normal 'save & publish' functionality and by publish date. |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function testBlogPosts() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$member = Security::getCurrentUser(); |
52
|
|
|
|
53
|
|
|
if ($member) { |
54
|
|
|
Security::setCurrentUser(null); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->objFromFixture(BlogPost::class, 'FirstBlogPost'); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var BlogCategory $category |
61
|
|
|
*/ |
62
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'FirstCategory'); |
63
|
|
|
|
64
|
|
|
$this->assertEquals(5, $category->BlogPosts()->count(), 'Category blog post count'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @see https://github.com/silverstripe/silverstripe-blog/issues/376 |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function testAllowMultibyteUrlSegment() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
/** @var Blog $blog */ |
73
|
|
|
$blog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
74
|
|
|
|
75
|
|
|
$cat = new BlogCategory(); |
76
|
|
|
$cat->Title = 'تست'; |
77
|
|
|
$cat->write(); |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
// urlencoded |
81
|
|
|
$this->assertEquals('%D8%AA%D8%B3%D8%AA', $cat->URLSegment); |
82
|
|
|
$expectedLink = Controller::join_links($blog->Link('category'), '%D8%AA%D8%B3%D8%AA'); |
83
|
|
|
$actualLink = $blog->Categories(false)->byID($cat->ID)->getLink(); |
84
|
|
|
$this->assertEquals($expectedLink, $actualLink); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testCanView() |
88
|
|
|
{ |
89
|
|
|
$this->useDraftSite(); |
90
|
|
|
|
91
|
|
|
$this->objFromFixture(Member::class, 'Admin'); |
92
|
|
|
|
93
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor'); |
94
|
|
|
/** @var Blog $secondBlog */ |
95
|
|
|
$secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog'); |
96
|
|
|
$category = $secondBlog->Categories(false)->find('URLSegment', 'second-category'); |
97
|
|
|
$this->assertFalse($category->canView($editor), 'Editor should not be able to view category.'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The first blog can be viewed by anybody. |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function testCanEdit() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$this->useDraftSite(); |
106
|
|
|
|
107
|
|
|
$admin = $this->objFromFixture(Member::class, 'Admin'); |
108
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor'); |
109
|
|
|
|
110
|
|
|
/** @var Blog $firstBlog */ |
111
|
|
|
$firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
112
|
|
|
$firstCategory = $firstBlog->Categories(false)->find('URLSegment', 'first-category'); |
113
|
|
|
|
114
|
|
|
$this->assertTrue($firstCategory->canEdit($admin), 'Admin should be able to edit category.'); |
115
|
|
|
$this->assertTrue($firstCategory->canEdit($editor), 'Editor should be able to edit category.'); |
116
|
|
|
|
117
|
|
|
/** @var Blog $secondBlog */ |
118
|
|
|
$secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog'); |
119
|
|
|
$secondCategory = $secondBlog->Categories(false)->find('URLSegment', 'second-category'); |
120
|
|
|
|
121
|
|
|
$this->assertTrue($secondCategory->canEdit($admin), 'Admin should be able to edit category.'); |
122
|
|
|
$this->assertFalse($secondCategory->canEdit($editor), 'Editor should not be able to edit category.'); |
123
|
|
|
|
124
|
|
|
/** @var Blog $secondBlog */ |
125
|
|
|
$thirdBlog = $this->objFromFixture(Blog::class, 'ThirdBlog'); |
126
|
|
|
$thirdCategory = $thirdBlog->Categories(false)->find('URLSegment', 'third-category'); |
127
|
|
|
|
128
|
|
|
$this->assertTrue($thirdCategory->canEdit($admin), 'Admin should always be able to edit category.'); |
129
|
|
|
$this->assertTrue($thirdCategory->canEdit($editor), 'Editor should be able to edit category.'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
View Code Duplication |
public function testCanCreate() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$this->useDraftSite(); |
135
|
|
|
|
136
|
|
|
$admin = $this->objFromFixture(Member::class, 'Admin'); |
137
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor'); |
138
|
|
|
|
139
|
|
|
$category = BlogCategory::singleton(); |
140
|
|
|
|
141
|
|
|
$this->assertTrue($category->canCreate($admin), 'Admin should be able to create category.'); |
142
|
|
|
$this->assertTrue($category->canCreate($editor), 'Editor should be able to create category.'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
View Code Duplication |
public function testCanDelete() |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$this->useDraftSite(); |
148
|
|
|
|
149
|
|
|
$admin = $this->objFromFixture(Member::class, 'Admin'); |
150
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor'); |
151
|
|
|
|
152
|
|
|
/** @var Blog $firstBlog */ |
153
|
|
|
$firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
154
|
|
|
$firstCategory = $firstBlog->Categories(false)->find('URLSegment', 'first-category'); |
155
|
|
|
|
156
|
|
|
$this->assertTrue($firstCategory->canDelete($admin), 'Admin should be able to delete category.'); |
157
|
|
|
$this->assertTrue($firstCategory->canDelete($editor), 'Editor should be able to category category.'); |
158
|
|
|
|
159
|
|
|
/** @var Blog $secondBlog */ |
160
|
|
|
$secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog'); |
161
|
|
|
$secondCategory = $secondBlog->Categories(false)->find('URLSegment', 'second-category'); |
162
|
|
|
|
163
|
|
|
$this->assertTrue($secondCategory->canDelete($admin), 'Admin should be able to delete category.'); |
164
|
|
|
$this->assertFalse($secondCategory->canDelete($editor), 'Editor should not be able to delete category.'); |
165
|
|
|
|
166
|
|
|
/** @var Blog $secondBlog */ |
167
|
|
|
$thirdBlog = $this->objFromFixture(Blog::class, 'ThirdBlog'); |
168
|
|
|
$thirdCategory = $thirdBlog->Categories(false)->find('URLSegment', 'third-category'); |
169
|
|
|
|
170
|
|
|
$this->assertTrue($thirdCategory->canDelete($admin), 'Admin should always be able to delete category.'); |
171
|
|
|
$this->assertTrue($thirdCategory->canDelete($editor), 'Editor should be able to delete category.'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testDuplicateCategories() |
175
|
|
|
{ |
176
|
|
|
$this->expectException(ValidationException::class); |
177
|
|
|
$this->expectExceptionMessage('A blog category already exists with that name.'); |
178
|
|
|
|
179
|
|
|
$blog = new Blog(); |
180
|
|
|
$blog->Title = 'Testing for duplicate categories'; |
181
|
|
|
$blog->write(); |
182
|
|
|
|
183
|
|
|
$category = new BlogCategory(); |
184
|
|
|
$category->Title = 'Test'; |
185
|
|
|
$category->URLSegment = 'test'; |
186
|
|
|
$category->write(); |
187
|
|
|
|
188
|
|
|
$category = new BlogCategory(); |
189
|
|
|
$category->Title = 'Test'; |
190
|
|
|
$category->URLSegment = 'test'; |
191
|
|
|
$category->write(); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.