1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Blog\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Blog\Model\Blog; |
6
|
|
|
use SilverStripe\Blog\Model\BlogPost; |
7
|
|
|
use SilverStripe\Blog\Model\BlogTag; |
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
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @mixin PHPUnit_Framework_TestCase |
16
|
|
|
*/ |
17
|
|
|
class BlogTagTest extends FunctionalTest |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritDoc} |
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 $tag->BlogPosts() many_many are published, both by |
47
|
|
|
* 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('SiteTree', 'FirstBlogPost'); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var BlogTag $tag |
61
|
|
|
*/ |
62
|
|
|
$tag = $this->objFromFixture('BlogTag', 'FirstTag'); |
63
|
|
|
|
64
|
|
|
$this->assertEquals(1, $tag->BlogPosts()->count(), 'Tag 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('SiteTree', 'FirstBlog'); |
73
|
|
|
$tag = new BlogTag(); |
74
|
|
|
$tag->BlogID = $blog->ID; |
75
|
|
|
$tag->Title = 'تست'; |
76
|
|
|
$tag->write(); |
77
|
|
|
// urlencoded |
78
|
|
|
$this->assertEquals('%D8%AA%D8%B3%D8%AA', $tag->URLSegment); |
|
|
|
|
79
|
|
|
$link = Controller::join_links($tag->Blog()->Link(), 'tag', '%D8%AA%D8%B3%D8%AA'); |
80
|
|
|
$this->assertEquals($link, $tag->getLink()); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The first blog can be viewed by anybody. |
85
|
|
|
*/ |
86
|
|
|
public function testCanView() |
87
|
|
|
{ |
88
|
|
|
$this->useDraftSite(); |
89
|
|
|
|
90
|
|
|
$admin = $this->objFromFixture('Member', 'Admin'); |
91
|
|
|
$editor = $this->objFromFixture('Member', 'Editor'); |
92
|
|
|
|
93
|
|
|
$tag = $this->objFromFixture('BlogTag', 'FirstTag'); |
94
|
|
|
|
95
|
|
|
$this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.'); |
|
|
|
|
96
|
|
|
$this->assertTrue($tag->canView($editor), 'Editor should be able to view tag.'); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$tag = $this->objFromFixture('BlogTag', 'SecondTag'); |
99
|
|
|
|
100
|
|
|
$this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.'); |
|
|
|
|
101
|
|
|
$this->assertFalse($tag->canView($editor), 'Editor should not be able to view tag.'); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function testCanEdit() |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->useDraftSite(); |
107
|
|
|
|
108
|
|
|
$admin = $this->objFromFixture('Member', 'Admin'); |
109
|
|
|
$editor = $this->objFromFixture('Member', 'Editor'); |
110
|
|
|
|
111
|
|
|
$tag = $this->objFromFixture('BlogTag', 'FirstTag'); |
112
|
|
|
|
113
|
|
|
$this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.'); |
|
|
|
|
114
|
|
|
$this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.'); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$tag = $this->objFromFixture('BlogTag', 'SecondTag'); |
117
|
|
|
|
118
|
|
|
$this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.'); |
|
|
|
|
119
|
|
|
$this->assertFalse($tag->canEdit($editor), 'Editor should not be able to edit tag.'); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$tag = $this->objFromFixture('BlogTag', 'ThirdTag'); |
122
|
|
|
|
123
|
|
|
$this->assertTrue($tag->canEdit($admin), 'Admin should always be able to edit tags.'); |
|
|
|
|
124
|
|
|
$this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.'); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
View Code Duplication |
public function testCanCreate() |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$this->useDraftSite(); |
130
|
|
|
|
131
|
|
|
$admin = $this->objFromFixture('Member', 'Admin'); |
132
|
|
|
$editor = $this->objFromFixture('Member', 'Editor'); |
133
|
|
|
|
134
|
|
|
$tag = singleton(BlogTag::class); |
135
|
|
|
|
136
|
|
|
$this->assertTrue($tag->canCreate($admin), 'Admin should be able to create tag.'); |
|
|
|
|
137
|
|
|
$this->assertTrue($tag->canCreate($editor), 'Editor should be able to create tag.'); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
View Code Duplication |
public function testCanDelete() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$this->useDraftSite(); |
143
|
|
|
|
144
|
|
|
$admin = $this->objFromFixture('Member', 'Admin'); |
145
|
|
|
$editor = $this->objFromFixture('Member', 'Editor'); |
146
|
|
|
|
147
|
|
|
$tag = $this->objFromFixture('BlogTag', 'FirstTag'); |
148
|
|
|
|
149
|
|
|
$this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.'); |
|
|
|
|
150
|
|
|
$this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.'); |
|
|
|
|
151
|
|
|
|
152
|
|
|
$tag = $this->objFromFixture('BlogTag', 'SecondTag'); |
153
|
|
|
|
154
|
|
|
$this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.'); |
|
|
|
|
155
|
|
|
$this->assertFalse($tag->canDelete($editor), 'Editor should not be able to delete tag.'); |
|
|
|
|
156
|
|
|
|
157
|
|
|
$tag = $this->objFromFixture('BlogTag', 'ThirdTag'); |
158
|
|
|
|
159
|
|
|
$this->assertTrue($tag->canDelete($admin), 'Admin should always be able to delete tags.'); |
|
|
|
|
160
|
|
|
$this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.'); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testDuplicateTagsForURLSegment() |
164
|
|
|
{ |
165
|
|
|
$blog = new Blog(); |
166
|
|
|
$blog->Title = 'Testing for duplicates blog'; |
167
|
|
|
$blog->write(); |
168
|
|
|
$tag1 = new BlogTag(); |
169
|
|
|
$tag1->Title = 'cat-test'; |
170
|
|
|
$tag1->BlogID = $blog->ID; |
171
|
|
|
$tag1->write(); |
172
|
|
|
$this->assertEquals('cat-test', $tag1->URLSegment); |
|
|
|
|
173
|
|
|
|
174
|
|
|
$tag2 = new BlogTag(); |
175
|
|
|
$tag2->Title = 'cat test'; |
176
|
|
|
$tag2->BlogID = $blog->ID; |
177
|
|
|
$tag2->write(); |
178
|
|
|
$this->assertEquals('cat-test-1', $tag2->URLSegment); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
View Code Duplication |
public function testDuplicateTags() |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
$blog = new Blog(); |
184
|
|
|
$blog->Title = 'Testing for duplicate tags'; |
185
|
|
|
$blog->write(); |
186
|
|
|
|
187
|
|
|
$tag = new BlogTag(); |
188
|
|
|
$tag->Title = 'Test'; |
189
|
|
|
$tag->BlogID = $blog->ID; |
190
|
|
|
$tag->URLSegment = 'test'; |
191
|
|
|
$tag->write(); |
192
|
|
|
|
193
|
|
|
$tag = new BlogTag(); |
194
|
|
|
$tag->Title = 'Test'; |
195
|
|
|
$tag->URLSegment = 'test'; |
196
|
|
|
$tag->BlogID = $blog->ID; |
197
|
|
|
try { |
198
|
|
|
$tag->write(); |
199
|
|
|
$this->fail('Duplicate BlogTag written'); |
|
|
|
|
200
|
|
|
} catch (ValidationException $e) { |
201
|
|
|
$messages = $e->getResult()->getMessages(); |
202
|
|
|
$this->assertCount(1, $messages); |
|
|
|
|
203
|
|
|
$this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testBlogTagUrlSegmentsAreAutomaticallyUpdated() |
208
|
|
|
{ |
209
|
|
|
$tag = new BlogTag; |
210
|
|
|
$tag->Title = "a test"; |
211
|
|
|
$tag->write(); |
212
|
|
|
$this->assertEquals($tag->URLSegment, "a-test"); |
|
|
|
|
213
|
|
|
|
214
|
|
|
$tag->Title = "another test"; |
215
|
|
|
$tag->write(); |
216
|
|
|
$this->assertEquals($tag->URLSegment, "another-test"); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
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.