Skip to content

Commit af17da7

Browse files
committed
FIX Validate Category / Tag Titles
Closes silverstripe#695
1 parent 6c0ae1f commit af17da7

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

src/Model/BlogCategory.php

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class BlogCategory extends DataObject implements CategorisationObject
2727
*/
2828
const DUPLICATE_EXCEPTION = 'DUPLICATE';
2929

30+
const EMPTY_TITLE_EXCEPTION = 'EMPTY_TITLE';
31+
3032
/**
3133
* {@inheritDoc}
3234
* @var string

src/Model/BlogObject.php

+14
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function validate()
7272
$validation->addError($this->getDuplicateError(), self::DUPLICATE_EXCEPTION);
7373
}
7474

75+
if(empty($this->Title)) {
76+
$validation->addError($this->getEmptyTitleError(), self::EMPTY_TITLE_EXCEPTION);
77+
}
78+
7579
return $validation;
7680
}
7781

@@ -244,4 +248,14 @@ abstract protected function getListUrlSegment();
244248
* @return string
245249
*/
246250
abstract protected function getDuplicateError();
251+
252+
/**
253+
* Returns an error message for this object when it tries to write with an empty title.
254+
*
255+
* @return string
256+
*/
257+
protected function getEmptyTitleError()
258+
{
259+
return _t(__CLASS__ . '.EmptyTitle', 'Title must not be empty');
260+
}
247261
}

src/Model/BlogTag.php

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class BlogTag extends DataObject implements CategorisationObject
2727
*/
2828
const DUPLICATE_EXCEPTION = 'DUPLICATE';
2929

30+
const EMPTY_TITLE_EXCEPTION = 'EMPTY_TITLE';
31+
3032
/**
3133
* {@inheritDoc}
3234
* @var string

tests/php/BlogCategoryTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,23 @@ public function testDuplicateCategories()
167167
$this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']);
168168
}
169169
}
170+
171+
public function testEmptyTitle()
172+
{
173+
$blog = $this->objFromFixture(Blog::class, 'FirstBlog');
174+
175+
$category = new BlogCategory();
176+
$category->Title = '';
177+
$category->BlogID = $blog->ID;
178+
$category->URLSegment = 'test';
179+
180+
try {
181+
$category->write();
182+
$this->fail('BlogCategory with empty title is written');
183+
} catch (ValidationException $e) {
184+
$messages = $e->getResult()->getMessages();
185+
$this->assertCount(1, $messages);
186+
$this->assertEquals(BlogCategory::EMPTY_TITLE_EXCEPTION, $messages[0]['messageType']);
187+
}
188+
}
170189
}

tests/php/BlogTagTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,23 @@ public function testBlogTagUrlSegmentsAreAutomaticallyUpdated()
195195
$tag->write();
196196
$this->assertEquals($tag->URLSegment, "another-test");
197197
}
198+
199+
public function testEmptyTitle()
200+
{
201+
$blog = $this->objFromFixture(Blog::class, 'FirstBlog');
202+
203+
$tag = new BlogTag();
204+
$tag->Title = '';
205+
$tag->BlogID = $blog->ID;
206+
$tag->URLSegment = 'test';
207+
208+
try {
209+
$tag->write();
210+
$this->fail('BlogTag with empty title is written');
211+
} catch (ValidationException $e) {
212+
$messages = $e->getResult()->getMessages();
213+
$this->assertCount(1, $messages);
214+
$this->assertEquals(BlogTag::EMPTY_TITLE_EXCEPTION, $messages[0]['messageType']);
215+
}
216+
}
198217
}

0 commit comments

Comments
 (0)