Fix BlogCategoryTest

This commit is contained in:
Damian Mooyman 2019-10-16 15:39:24 +13:00
parent 4da2e164ee
commit 82b7dab574
No known key found for this signature in database
GPG Key ID: 19B1752E86A700BB
3 changed files with 47 additions and 50 deletions

View File

@ -97,11 +97,6 @@ trait BlogObject
return $validation; return $validation;
} }
$blog = $this->Blog();
if (!$blog || !$blog->exists()) {
return $validation;
}
if ($this->getDuplicatesByField('Title')->count() > 0) { if ($this->getDuplicatesByField('Title')->count() > 0) {
$validation->addError($this->getDuplicateError(), self::DUPLICATE_EXCEPTION); $validation->addError($this->getDuplicateError(), self::DUPLICATE_EXCEPTION);
} }

View File

@ -5,7 +5,6 @@ namespace SilverStripe\Blog\Tests;
use SilverStripe\Blog\Model\Blog; use SilverStripe\Blog\Model\Blog;
use SilverStripe\Blog\Model\BlogCategory; use SilverStripe\Blog\Model\BlogCategory;
use SilverStripe\Blog\Model\BlogPost; use SilverStripe\Blog\Model\BlogPost;
use SilverStripe\Blog\Model\BlogTag;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;
use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\FieldType\DBDatetime;
@ -70,15 +69,19 @@ class BlogCategoryTest extends FunctionalTest
*/ */
public function testAllowMultibyteUrlSegment() public function testAllowMultibyteUrlSegment()
{ {
/** @var Blog $blog */
$blog = $this->objFromFixture(Blog::class, 'FirstBlog'); $blog = $this->objFromFixture(Blog::class, 'FirstBlog');
$cat = new BlogCategory(); $cat = new BlogCategory();
$cat->BlogID = $blog->ID;
$cat->Title = 'تست'; $cat->Title = 'تست';
$cat->write(); $cat->write();
// urlencoded // urlencoded
$this->assertEquals('%D8%AA%D8%B3%D8%AA', $cat->URLSegment); $this->assertEquals('%D8%AA%D8%B3%D8%AA', $cat->URLSegment);
$link = Controller::join_links($cat->Blog()->Link(), 'category', '%D8%AA%D8%B3%D8%AA'); $expectedLink = Controller::join_links($blog->Link('category'), '%D8%AA%D8%B3%D8%AA');
$this->assertEquals($link, $cat->getLink()); $actualLink = $blog->Categories(false)->byID($cat->ID)->getLink();
$this->assertEquals($expectedLink, $actualLink);
} }
public function testCanView() public function testCanView()
@ -88,8 +91,9 @@ class BlogCategoryTest extends FunctionalTest
$this->objFromFixture(Member::class, 'Admin'); $this->objFromFixture(Member::class, 'Admin');
$editor = $this->objFromFixture(Member::class, 'Editor'); $editor = $this->objFromFixture(Member::class, 'Editor');
$category = $this->objFromFixture(BlogCategory::class, 'SecondCategory'); /** @var Blog $secondBlog */
$secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog');
$category = $secondBlog->Categories(false)->find('URLSegment', 'second-category');
$this->assertFalse($category->canView($editor), 'Editor should not be able to view category.'); $this->assertFalse($category->canView($editor), 'Editor should not be able to view category.');
} }
@ -103,20 +107,26 @@ class BlogCategoryTest extends FunctionalTest
$admin = $this->objFromFixture(Member::class, 'Admin'); $admin = $this->objFromFixture(Member::class, 'Admin');
$editor = $this->objFromFixture(Member::class, 'Editor'); $editor = $this->objFromFixture(Member::class, 'Editor');
$category = $this->objFromFixture(BlogCategory::class, 'FirstCategory'); /** @var Blog $firstBlog */
$firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog');
$firstCategory = $firstBlog->Categories(false)->find('URLSegment', 'first-category');
$this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.'); $this->assertTrue($firstCategory->canEdit($admin), 'Admin should be able to edit category.');
$this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.'); $this->assertTrue($firstCategory->canEdit($editor), 'Editor should be able to edit category.');
$category = $this->objFromFixture(BlogCategory::class, 'SecondCategory'); /** @var Blog $secondBlog */
$secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog');
$secondCategory = $secondBlog->Categories(false)->find('URLSegment', 'second-category');
$this->assertTrue($category->canEdit($admin), 'Admin should be able to edit category.'); $this->assertTrue($secondCategory->canEdit($admin), 'Admin should be able to edit category.');
$this->assertFalse($category->canEdit($editor), 'Editor should not be able to edit category.'); $this->assertFalse($secondCategory->canEdit($editor), 'Editor should not be able to edit category.');
$category = $this->objFromFixture(BlogCategory::class, 'ThirdCategory'); /** @var Blog $secondBlog */
$thirdBlog = $this->objFromFixture(Blog::class, 'ThirdBlog');
$thirdCategory = $thirdBlog->Categories(false)->find('URLSegment', 'third-category');
$this->assertTrue($category->canEdit($admin), 'Admin should always be able to edit category.'); $this->assertTrue($thirdCategory->canEdit($admin), 'Admin should always be able to edit category.');
$this->assertTrue($category->canEdit($editor), 'Editor should be able to edit category.'); $this->assertTrue($thirdCategory->canEdit($editor), 'Editor should be able to edit category.');
} }
public function testCanCreate() public function testCanCreate()
@ -126,7 +136,7 @@ class BlogCategoryTest extends FunctionalTest
$admin = $this->objFromFixture(Member::class, 'Admin'); $admin = $this->objFromFixture(Member::class, 'Admin');
$editor = $this->objFromFixture(Member::class, 'Editor'); $editor = $this->objFromFixture(Member::class, 'Editor');
$category = singleton(BlogCategory::class); $category = BlogCategory::singleton();
$this->assertTrue($category->canCreate($admin), 'Admin should be able to create category.'); $this->assertTrue($category->canCreate($admin), 'Admin should be able to create category.');
$this->assertTrue($category->canCreate($editor), 'Editor should be able to create category.'); $this->assertTrue($category->canCreate($editor), 'Editor should be able to create category.');
@ -139,43 +149,45 @@ class BlogCategoryTest extends FunctionalTest
$admin = $this->objFromFixture(Member::class, 'Admin'); $admin = $this->objFromFixture(Member::class, 'Admin');
$editor = $this->objFromFixture(Member::class, 'Editor'); $editor = $this->objFromFixture(Member::class, 'Editor');
$category = $this->objFromFixture(BlogCategory::class, 'FirstCategory'); /** @var Blog $firstBlog */
$firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog');
$firstCategory = $firstBlog->Categories(false)->find('URLSegment', 'first-category');
$this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.'); $this->assertTrue($firstCategory->canDelete($admin), 'Admin should be able to delete category.');
$this->assertTrue($category->canDelete($editor), 'Editor should be able to category category.'); $this->assertTrue($firstCategory->canDelete($editor), 'Editor should be able to category category.');
$category = $this->objFromFixture(BlogCategory::class, 'SecondCategory'); /** @var Blog $secondBlog */
$this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.'); $secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog');
$this->assertFalse($category->canDelete($editor), 'Editor should not be able to delete category.'); $secondCategory = $secondBlog->Categories(false)->find('URLSegment', 'second-category');
$category = $this->objFromFixture(BlogCategory::class, 'ThirdCategory'); $this->assertTrue($secondCategory->canDelete($admin), 'Admin should be able to delete category.');
$this->assertTrue($category->canDelete($admin), 'Admin should always be able to delete category.'); $this->assertFalse($secondCategory->canDelete($editor), 'Editor should not be able to delete category.');
$this->assertTrue($category->canDelete($editor), 'Editor should be able to delete category.');
/** @var Blog $secondBlog */
$thirdBlog = $this->objFromFixture(Blog::class, 'ThirdBlog');
$thirdCategory = $thirdBlog->Categories(false)->find('URLSegment', 'third-category');
$this->assertTrue($thirdCategory->canDelete($admin), 'Admin should always be able to delete category.');
$this->assertTrue($thirdCategory->canDelete($editor), 'Editor should be able to delete category.');
} }
public function testDuplicateCategories() public function testDuplicateCategories()
{ {
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('A blog category already exists with that name.');
$blog = new Blog(); $blog = new Blog();
$blog->Title = 'Testing for duplicate categories'; $blog->Title = 'Testing for duplicate categories';
$blog->write(); $blog->write();
$category = new BlogCategory(); $category = new BlogCategory();
$category->Title = 'Test'; $category->Title = 'Test';
$category->BlogID = $blog->ID;
$category->URLSegment = 'test'; $category->URLSegment = 'test';
$category->write(); $category->write();
$category = new BlogCategory(); $category = new BlogCategory();
$category->Title = 'Test'; $category->Title = 'Test';
$category->URLSegment = 'test'; $category->URLSegment = 'test';
$category->BlogID = $blog->ID; $category->write();
try {
$category->write();
$this->fail('Duplicate BlogCategory written');
} catch (ValidationException $e) {
$messages = $e->getResult()->getMessages();
$this->assertCount(1, $messages);
$this->assertEquals(BlogTag::DUPLICATE_EXCEPTION, $messages[0]['messageType']);
}
} }
} }

View File

@ -93,47 +93,37 @@ SilverStripe\Blog\Model\BlogCategory:
FirstCategory: FirstCategory:
Title: 'First Category' Title: 'First Category'
URLSegment: 'first-category' URLSegment: 'first-category'
BlogID: =>SilverStripe\Blog\Model\Blog.FirstBlog
SecondCategory: SecondCategory:
Title: 'Second Category' Title: 'Second Category'
URLSegment: 'second-category' URLSegment: 'second-category'
BlogID: =>SilverStripe\Blog\Model\Blog.SecondBlog
ThirdCategory: ThirdCategory:
Title: 'Third Category' Title: 'Third Category'
URLSegment: 'third-category' URLSegment: 'third-category'
BlogID: =>SilverStripe\Blog\Model\Blog.ThirdBlog
SilverStripe\Blog\Model\BlogTag: SilverStripe\Blog\Model\BlogTag:
FirstTag: FirstTag:
Title: 'First Tag' Title: 'First Tag'
URLSegment: 'first-tag' URLSegment: 'first-tag'
BlogID: =>SilverStripe\Blog\Model\Blog.FirstBlog
SecondTag: SecondTag:
Title: 'Second Tag' Title: 'Second Tag'
URLSegment: 'second-tag' URLSegment: 'second-tag'
BlogID: =>SilverStripe\Blog\Model\Blog.SecondBlog
ThirdTag: ThirdTag:
Title: 'Third Tag' Title: 'Third Tag'
URLSegment: 'third-tag' URLSegment: 'third-tag'
BlogID: =>SilverStripe\Blog\Model\Blog.ThirdBlog
#Tags for Tag Cloud widget #Tags for Tag Cloud widget
PopularTag: PopularTag:
Title: 'Popular' Title: 'Popular'
URLSegment: 'popular' URLSegment: 'popular'
BlogID: =>SilverStripe\Blog\Model\Blog.FourthBlog
CoolTag: CoolTag:
Title: 'Cool' Title: 'Cool'
URLSegment: 'cool' URLSegment: 'cool'
BlogID: =>SilverStripe\Blog\Model\Blog.FourthBlog
CatTag: CatTag:
Title: 'Cat' Title: 'Cat'
URLSegment: 'cat' URLSegment: 'cat'
BlogID: =>SilverStripe\Blog\Model\Blog.FourthBlog
KiwiTag: KiwiTag:
Title: 'Kiwi' Title: 'Kiwi'
URLSegment: 'kiwi' URLSegment: 'kiwi'
BlogID: =>SilverStripe\Blog\Model\Blog.FourthBlog
SilverStripe\Blog\Model\BlogPost: SilverStripe\Blog\Model\BlogPost:
FirstBlogPost: FirstBlogPost: