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\Blog\Model\BlogTag; |
9
|
|
|
use SilverStripe\Control\Controller; |
10
|
|
|
use SilverStripe\Dev\FunctionalTest; |
11
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
12
|
|
|
use SilverStripe\ORM\ValidationException; |
13
|
|
|
use SilverStripe\Security\Member; |
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 = Member::currentUser(); |
52
|
|
|
|
53
|
|
|
if ($member) { |
54
|
|
|
$member->logout(); |
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
|
|
|
$blog = $this->objFromFixture(Blog::class, 'FirstBlog'); |
73
|
|
|
$cat = new BlogCategory(); |
74
|
|
|
$cat->BlogID = $blog->ID; |
75
|
|
|
$cat->Title = 'تست'; |
76
|
|
|
$cat->write(); |
77
|
|
|
// urlencoded |
78
|
|
|
$this->assertEquals('%D8%AA%D8%B3%D8%AA', $cat->URLSegment); |
79
|
|
|
$link = Controller::join_links($cat->Blog()->Link(), 'category', '%D8%AA%D8%B3%D8%AA'); |
80
|
|
|
$this->assertEquals($link, $cat->getLink()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testCanView() |
84
|
|
|
{ |
85
|
|
|
$this->useDraftSite(); |
86
|
|
|
|
87
|
|
|
$this->objFromFixture(Member::class, 'Admin'); |
88
|
|
|
|
89
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor'); |
90
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'SecondCategory'); |
91
|
|
|
|
92
|
|
|
$this->assertFalse($category->canView($editor), 'Editor should not be able to view category.'); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* The first blog can be viewed by anybody. |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function testCanEdit() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$this->useDraftSite(); |
101
|
|
|
|
102
|
|
|
$admin = $this->objFromFixture(Member::class, 'Admin'); |
103
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor'); |
104
|
|
|
|
105
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'FirstCategory'); |
106
|
|
|
|
107
|
|
|
$this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.'); |
|
|
|
|
108
|
|
|
$this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.'); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'SecondCategory'); |
111
|
|
|
|
112
|
|
|
$this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.'); |
|
|
|
|
113
|
|
|
$this->assertFalse($category->canEdit($editor), 'Editor should not be able to edit category.'); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'ThirdCategory'); |
116
|
|
|
|
117
|
|
|
$this->assertTrue($category->canEdit($admin), 'Admin should always be able to edit category.'); |
|
|
|
|
118
|
|
|
$this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.'); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
View Code Duplication |
public function testCanCreate() |
|
|
|
|
122
|
|
|
{ |
123
|
|
|
$this->useDraftSite(); |
124
|
|
|
|
125
|
|
|
$admin = $this->objFromFixture(Member::class, 'Admin'); |
126
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor'); |
127
|
|
|
|
128
|
|
|
$category = singleton(BlogCategory::class); |
129
|
|
|
|
130
|
|
|
$this->assertTrue($category->canCreate($admin), 'Admin should be able to create category.'); |
131
|
|
|
$this->assertTrue($category->canCreate($editor), 'Editor should be able to create category.'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function testCanDelete() |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$this->useDraftSite(); |
137
|
|
|
|
138
|
|
|
$admin = $this->objFromFixture(Member::class, 'Admin'); |
139
|
|
|
$editor = $this->objFromFixture(Member::class, 'Editor'); |
140
|
|
|
|
141
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'FirstCategory'); |
142
|
|
|
|
143
|
|
|
$this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.'); |
|
|
|
|
144
|
|
|
$this->assertTrue($category->canDelete($editor), 'Editor should be able to category category.'); |
|
|
|
|
145
|
|
|
|
146
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'SecondCategory'); |
147
|
|
|
$this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.'); |
|
|
|
|
148
|
|
|
$this->assertFalse($category->canDelete($editor), 'Editor should not be able to delete category.'); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$category = $this->objFromFixture(BlogCategory::class, 'ThirdCategory'); |
151
|
|
|
$this->assertTrue($category->canDelete($admin), 'Admin should always be able to delete category.'); |
|
|
|
|
152
|
|
|
$this->assertTrue($category->canDelete($editor), 'Editor should be able to delete category.'); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
public function testDuplicateCategories() |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$blog = new Blog(); |
158
|
|
|
$blog->Title = 'Testing for duplicate categories'; |
159
|
|
|
$blog->write(); |
160
|
|
|
|
161
|
|
|
$category = new BlogCategory(); |
162
|
|
|
$category->Title = 'Test'; |
163
|
|
|
$category->BlogID = $blog->ID; |
164
|
|
|
$category->URLSegment = 'test'; |
165
|
|
|
$category->write(); |
166
|
|
|
|
167
|
|
|
$category = new BlogCategory(); |
168
|
|
|
$category->Title = 'Test'; |
169
|
|
|
$category->URLSegment = 'test'; |
170
|
|
|
$category->BlogID = $blog->ID; |
171
|
|
|
try { |
172
|
|
|
$category->write(); |
173
|
|
|
$this->fail('Duplicate BlogCategory written'); |
174
|
|
|
} catch (ValidationException $e) { |
175
|
|
|
$messages = $e->getResult()->getMessages(); |
176
|
|
|
$this->assertCount(1, $messages); |
177
|
|
|
$this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
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.