Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Validate Category / Tag Titles #700

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Model/BlogCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class BlogCategory extends DataObject implements CategorisationObject
*/
const DUPLICATE_EXCEPTION = 'DUPLICATE';

const EMPTY_TITLE_EXCEPTION = 'EMPTY_TITLE';

/**
* {@inheritDoc}
* @var string
Expand Down
14 changes: 14 additions & 0 deletions src/Model/BlogObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$validation->addError($this->getEmptyTitleError(), self::EMPTY_TITLE_EXCEPTION);
$validation->addError($this->getEmptyTitleError(), self::EMPTY_TITLE_EXCEPTION ?? 'EMPTY_TITLE');

The constant won't be declared on any custom classes that use this trait, and requiring it to be added is technically a breaking change.

Note: Please test that this actually works - I've not tried the null coalescing operator in this context before. I think it'll work but it'll pay to check.

Copy link
Member

@emteknetnz emteknetnz Apr 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yikes, shouldn't be calling self::<const> on a trait. Not a great choice when this was made back in the day

I don't think null coalescing works here

class C { function F(){ var_dump(self::Z ?? 'default'); } }; (new C)->F();
// PHP Warning:  Uncaught Error: Undefined constant C::Z in php shell cod

}

return $validation;
}

Expand Down Expand Up @@ -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');
}
}
2 changes: 2 additions & 0 deletions src/Model/BlogTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class BlogTag extends DataObject implements CategorisationObject
*/
const DUPLICATE_EXCEPTION = 'DUPLICATE';

const EMPTY_TITLE_EXCEPTION = 'EMPTY_TITLE';

/**
* {@inheritDoc}
* @var string
Expand Down
19 changes: 19 additions & 0 deletions tests/php/BlogCategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
}
19 changes: 19 additions & 0 deletions tests/php/BlogTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
}