1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SilverStripe\Blog\Model\Blog; |
4
|
|
|
use SilverStripe\Blog\Model\BlogCategory; |
5
|
|
|
use SilverStripe\Blog\Model\BlogTag; |
6
|
|
|
use SilverStripe\Dev\FunctionalTest; |
7
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
8
|
|
|
use SilverStripe\ORM\ValidationException; |
9
|
|
|
use SilverStripe\Security\Member; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @mixin PHPUnit_Framework_TestCase |
13
|
|
|
*/ |
14
|
|
|
class BlogCategoryTest extends FunctionalTest |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public static $fixture_file = 'blog.yml'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
parent::setUp(); |
27
|
|
|
|
28
|
|
|
DBDatetime::set_mock_now('2013-10-10 20:00:00'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function tearDown() |
35
|
|
|
{ |
36
|
|
|
DBDatetime::clear_mock_now(); |
37
|
|
|
|
38
|
|
|
parent::tearDown(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Tests that any blog posts returned from $category->BlogPosts() many_many are published, |
43
|
|
|
* both by normal 'save & publish' functionality and by publish date. |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function testBlogPosts() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$member = Member::currentUser(); |
48
|
|
|
|
49
|
|
|
if ($member) { |
50
|
|
|
$member->logout(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->objFromFixture('SilverStripe\\Blog\\Model\\BlogPost', 'FirstBlogPost'); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var BlogCategory $category |
57
|
|
|
*/ |
58
|
|
|
$category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'FirstCategory'); |
59
|
|
|
|
60
|
|
|
$this->assertEquals(5, $category->BlogPosts()->count(), 'Category blog post count'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @see https://github.com/silverstripe/silverstripe-blog/issues/376 |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function testAllowMultibyteUrlSegment() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$blog = $this->objFromFixture('Blog', 'FirstBlog'); |
69
|
|
|
$cat = new BlogCategory(); |
70
|
|
|
$cat->BlogID = $blog->ID; |
71
|
|
|
$cat->Title = 'تست'; |
72
|
|
|
$cat->write(); |
73
|
|
|
// urlencoded |
74
|
|
|
$this->assertEquals('%D8%AA%D8%B3%D8%AA', $cat->URLSegment); |
75
|
|
|
$link = Controller::join_links($cat->Blog()->Link(), 'category', '%D8%AA%D8%B3%D8%AA'); |
76
|
|
|
$this->assertEquals($link, $cat->getLink()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testCanView() |
80
|
|
|
{ |
81
|
|
|
$this->useDraftSite(); |
82
|
|
|
|
83
|
|
|
$this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); |
84
|
|
|
|
85
|
|
|
$editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); |
86
|
|
|
$category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'SecondCategory'); |
87
|
|
|
|
88
|
|
|
$this->assertFalse($category->canView($editor), 'Editor should not be able to view category.'); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* The first blog can be viewed by anybody. |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function testCanEdit() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$this->useDraftSite(); |
97
|
|
|
|
98
|
|
|
$admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); |
99
|
|
|
$editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); |
100
|
|
|
|
101
|
|
|
$category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'FirstCategory'); |
102
|
|
|
|
103
|
|
|
$this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.'); |
|
|
|
|
104
|
|
|
$this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.'); |
|
|
|
|
105
|
|
|
|
106
|
|
|
$category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'SecondCategory'); |
107
|
|
|
|
108
|
|
|
$this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.'); |
|
|
|
|
109
|
|
|
$this->assertFalse($category->canEdit($editor), 'Editor should not be able to edit category.'); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'ThirdCategory'); |
112
|
|
|
|
113
|
|
|
$this->assertTrue($category->canEdit($admin), 'Admin should always be able to edit category.'); |
|
|
|
|
114
|
|
|
$this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.'); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
public function testCanCreate() |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$this->useDraftSite(); |
120
|
|
|
|
121
|
|
|
$admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); |
122
|
|
|
$editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); |
123
|
|
|
|
124
|
|
|
$category = singleton('SilverStripe\\Blog\\Model\\BlogCategory'); |
125
|
|
|
|
126
|
|
|
$this->assertTrue($category->canCreate($admin), 'Admin should be able to create category.'); |
127
|
|
|
$this->assertTrue($category->canCreate($editor), 'Editor should be able to create category.'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
public function testCanDelete() |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$this->useDraftSite(); |
133
|
|
|
|
134
|
|
|
$admin = $this->objFromFixture('SilverStripe\\Security\\Member', 'Admin'); |
135
|
|
|
$editor = $this->objFromFixture('SilverStripe\\Security\\Member', 'Editor'); |
136
|
|
|
|
137
|
|
|
$category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'FirstCategory'); |
138
|
|
|
|
139
|
|
|
$this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.'); |
|
|
|
|
140
|
|
|
$this->assertTrue($category->canDelete($editor), 'Editor should be able to category category.'); |
|
|
|
|
141
|
|
|
|
142
|
|
|
$category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'SecondCategory'); |
143
|
|
|
$this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.'); |
|
|
|
|
144
|
|
|
$this->assertFalse($category->canDelete($editor), 'Editor should not be able to delete category.'); |
|
|
|
|
145
|
|
|
|
146
|
|
|
$category = $this->objFromFixture('SilverStripe\\Blog\\Model\\BlogCategory', 'ThirdCategory'); |
147
|
|
|
$this->assertTrue($category->canDelete($admin), 'Admin should always be able to delete category.'); |
|
|
|
|
148
|
|
|
$this->assertTrue($category->canDelete($editor), 'Editor should be able to delete category.'); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
View Code Duplication |
public function testDuplicateCategories() |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$blog = new Blog(); |
154
|
|
|
$blog->Title = 'Testing for duplicate categories'; |
155
|
|
|
$blog->write(); |
156
|
|
|
|
157
|
|
|
$category = new BlogCategory(); |
158
|
|
|
$category->Title = 'Test'; |
159
|
|
|
$category->BlogID = $blog->ID; |
160
|
|
|
$category->URLSegment = 'test'; |
161
|
|
|
$category->write(); |
162
|
|
|
|
163
|
|
|
$category = new BlogCategory(); |
164
|
|
|
$category->Title = 'Test'; |
165
|
|
|
$category->URLSegment = 'test'; |
166
|
|
|
$category->BlogID = $blog->ID; |
167
|
|
|
try { |
168
|
|
|
$category->write(); |
169
|
|
|
$this->fail('Duplicate BlogCategory written'); |
170
|
|
|
} catch (ValidationException $e) { |
171
|
|
|
$messages = $e->getResult()->getMessages(); |
172
|
|
|
$this->assertCount(1, $messages); |
173
|
|
|
$this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.