From af17da7ecca6d7391df230f75f17b3ba5c416872 Mon Sep 17 00:00:00 2001 From: Nick Lamprecht Date: Tue, 31 Jan 2023 19:16:54 +0100 Subject: [PATCH 1/2] FIX Validate Category / Tag Titles Closes #695 --- src/Model/BlogCategory.php | 2 ++ src/Model/BlogObject.php | 14 ++++++++++++++ src/Model/BlogTag.php | 2 ++ tests/php/BlogCategoryTest.php | 19 +++++++++++++++++++ tests/php/BlogTagTest.php | 19 +++++++++++++++++++ 5 files changed, 56 insertions(+) diff --git a/src/Model/BlogCategory.php b/src/Model/BlogCategory.php index ae69c7de1..94590a35f 100644 --- a/src/Model/BlogCategory.php +++ b/src/Model/BlogCategory.php @@ -27,6 +27,8 @@ class BlogCategory extends DataObject implements CategorisationObject */ const DUPLICATE_EXCEPTION = 'DUPLICATE'; + const EMPTY_TITLE_EXCEPTION = 'EMPTY_TITLE'; + /** * {@inheritDoc} * @var string diff --git a/src/Model/BlogObject.php b/src/Model/BlogObject.php index d067fb392..93b127ccf 100644 --- a/src/Model/BlogObject.php +++ b/src/Model/BlogObject.php @@ -72,6 +72,10 @@ public function validate() $validation->addError($this->getDuplicateError(), self::DUPLICATE_EXCEPTION); } + if(empty($this->Title)) { + $validation->addError($this->getEmptyTitleError(), self::EMPTY_TITLE_EXCEPTION); + } + return $validation; } @@ -244,4 +248,14 @@ abstract protected function getListUrlSegment(); * @return string */ abstract protected function getDuplicateError(); + + /** + * Returns an error message for this object when it tries to write with an empty title. + * + * @return string + */ + protected function getEmptyTitleError() + { + return _t(__CLASS__ . '.EmptyTitle', 'Title must not be empty'); + } } diff --git a/src/Model/BlogTag.php b/src/Model/BlogTag.php index 4180f67da..66bf3fbc4 100644 --- a/src/Model/BlogTag.php +++ b/src/Model/BlogTag.php @@ -27,6 +27,8 @@ class BlogTag extends DataObject implements CategorisationObject */ const DUPLICATE_EXCEPTION = 'DUPLICATE'; + const EMPTY_TITLE_EXCEPTION = 'EMPTY_TITLE'; + /** * {@inheritDoc} * @var string diff --git a/tests/php/BlogCategoryTest.php b/tests/php/BlogCategoryTest.php index c189e32da..50bfd0986 100755 --- a/tests/php/BlogCategoryTest.php +++ b/tests/php/BlogCategoryTest.php @@ -167,4 +167,23 @@ public function testDuplicateCategories() $this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']); } } + + public function testEmptyTitle() + { + $blog = $this->objFromFixture(Blog::class, 'FirstBlog'); + + $category = new BlogCategory(); + $category->Title = ''; + $category->BlogID = $blog->ID; + $category->URLSegment = 'test'; + + try { + $category->write(); + $this->fail('BlogCategory with empty title is written'); + } catch (ValidationException $e) { + $messages = $e->getResult()->getMessages(); + $this->assertCount(1, $messages); + $this->assertEquals(BlogCategory::EMPTY_TITLE_EXCEPTION, $messages[0]['messageType']); + } + } } diff --git a/tests/php/BlogTagTest.php b/tests/php/BlogTagTest.php index 754b338ce..28ef254fc 100755 --- a/tests/php/BlogTagTest.php +++ b/tests/php/BlogTagTest.php @@ -195,4 +195,23 @@ public function testBlogTagUrlSegmentsAreAutomaticallyUpdated() $tag->write(); $this->assertEquals($tag->URLSegment, "another-test"); } + + public function testEmptyTitle() + { + $blog = $this->objFromFixture(Blog::class, 'FirstBlog'); + + $tag = new BlogTag(); + $tag->Title = ''; + $tag->BlogID = $blog->ID; + $tag->URLSegment = 'test'; + + try { + $tag->write(); + $this->fail('BlogTag with empty title is written'); + } catch (ValidationException $e) { + $messages = $e->getResult()->getMessages(); + $this->assertCount(1, $messages); + $this->assertEquals(BlogTag::EMPTY_TITLE_EXCEPTION, $messages[0]['messageType']); + } + } } From 6c9ab8058faece09a32c3b36013a50c0c1c893da Mon Sep 17 00:00:00 2001 From: Nick Lamprecht <27819706+freezernick@users.noreply.github.com> Date: Tue, 31 Jan 2023 22:22:56 +0100 Subject: [PATCH 2/2] Update src/Model/BlogObject.php Co-authored-by: Michal Kleiner --- src/Model/BlogObject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/BlogObject.php b/src/Model/BlogObject.php index 93b127ccf..aa46a271f 100644 --- a/src/Model/BlogObject.php +++ b/src/Model/BlogObject.php @@ -72,7 +72,7 @@ public function validate() $validation->addError($this->getDuplicateError(), self::DUPLICATE_EXCEPTION); } - if(empty($this->Title)) { + if (empty($this->Title)) { $validation->addError($this->getEmptyTitleError(), self::EMPTY_TITLE_EXCEPTION); }