diff --git a/src/Model/BlogObject.php b/src/Model/BlogObject.php index 30c9b2a..8b0738e 100644 --- a/src/Model/BlogObject.php +++ b/src/Model/BlogObject.php @@ -97,11 +97,6 @@ trait BlogObject return $validation; } - $blog = $this->Blog(); - if (!$blog || !$blog->exists()) { - return $validation; - } - if ($this->getDuplicatesByField('Title')->count() > 0) { $validation->addError($this->getDuplicateError(), self::DUPLICATE_EXCEPTION); } diff --git a/tests/BlogCategoryTest.php b/tests/BlogCategoryTest.php index 1cc4aa0..a730123 100755 --- a/tests/BlogCategoryTest.php +++ b/tests/BlogCategoryTest.php @@ -5,7 +5,6 @@ namespace SilverStripe\Blog\Tests; use SilverStripe\Blog\Model\Blog; use SilverStripe\Blog\Model\BlogCategory; use SilverStripe\Blog\Model\BlogPost; -use SilverStripe\Blog\Model\BlogTag; use SilverStripe\Control\Controller; use SilverStripe\Dev\FunctionalTest; use SilverStripe\ORM\FieldType\DBDatetime; @@ -70,15 +69,19 @@ class BlogCategoryTest extends FunctionalTest */ public function testAllowMultibyteUrlSegment() { + /** @var Blog $blog */ $blog = $this->objFromFixture(Blog::class, 'FirstBlog'); + $cat = new BlogCategory(); - $cat->BlogID = $blog->ID; $cat->Title = 'تست'; $cat->write(); + + // urlencoded $this->assertEquals('%D8%AA%D8%B3%D8%AA', $cat->URLSegment); - $link = Controller::join_links($cat->Blog()->Link(), 'category', '%D8%AA%D8%B3%D8%AA'); - $this->assertEquals($link, $cat->getLink()); + $expectedLink = Controller::join_links($blog->Link('category'), '%D8%AA%D8%B3%D8%AA'); + $actualLink = $blog->Categories(false)->byID($cat->ID)->getLink(); + $this->assertEquals($expectedLink, $actualLink); } public function testCanView() @@ -88,8 +91,9 @@ class BlogCategoryTest extends FunctionalTest $this->objFromFixture(Member::class, 'Admin'); $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.'); } @@ -103,20 +107,26 @@ class BlogCategoryTest extends FunctionalTest $admin = $this->objFromFixture(Member::class, 'Admin'); $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($category->canEdit($editor), 'Editor should be able to edit category.'); + $this->assertTrue($firstCategory->canEdit($admin), 'Admin 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->assertFalse($category->canEdit($editor), 'Editor should not be able to edit category.'); + $this->assertTrue($secondCategory->canEdit($admin), 'Admin should 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($category->canEdit($editor), 'Editor should be able to edit category.'); + $this->assertTrue($thirdCategory->canEdit($admin), 'Admin should always be able to edit category.'); + $this->assertTrue($thirdCategory->canEdit($editor), 'Editor should be able to edit category.'); } public function testCanCreate() @@ -126,7 +136,7 @@ class BlogCategoryTest extends FunctionalTest $admin = $this->objFromFixture(Member::class, 'Admin'); $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($editor), 'Editor should be able to create category.'); @@ -139,43 +149,45 @@ class BlogCategoryTest extends FunctionalTest $admin = $this->objFromFixture(Member::class, 'Admin'); $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($category->canDelete($editor), 'Editor should be able to category category.'); + $this->assertTrue($firstCategory->canDelete($admin), 'Admin should be able to delete category.'); + $this->assertTrue($firstCategory->canDelete($editor), 'Editor should be able to category category.'); - $category = $this->objFromFixture(BlogCategory::class, 'SecondCategory'); - $this->assertTrue($category->canDelete($admin), 'Admin should be able to delete category.'); - $this->assertFalse($category->canDelete($editor), 'Editor should not be able to delete category.'); + /** @var Blog $secondBlog */ + $secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog'); + $secondCategory = $secondBlog->Categories(false)->find('URLSegment', 'second-category'); - $category = $this->objFromFixture(BlogCategory::class, 'ThirdCategory'); - $this->assertTrue($category->canDelete($admin), 'Admin should always be able to delete category.'); - $this->assertTrue($category->canDelete($editor), 'Editor should be able to delete category.'); + $this->assertTrue($secondCategory->canDelete($admin), 'Admin should be able to delete category.'); + $this->assertFalse($secondCategory->canDelete($editor), 'Editor should not 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() { + $this->expectException(ValidationException::class); + $this->expectExceptionMessage('A blog category already exists with that name.'); + $blog = new Blog(); $blog->Title = 'Testing for duplicate categories'; $blog->write(); $category = new BlogCategory(); $category->Title = 'Test'; - $category->BlogID = $blog->ID; $category->URLSegment = 'test'; $category->write(); $category = new BlogCategory(); $category->Title = 'Test'; $category->URLSegment = 'test'; - $category->BlogID = $blog->ID; - 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']); - } + $category->write(); } } diff --git a/tests/blog.yml b/tests/blog.yml index 699395e..c7cd2f6 100755 --- a/tests/blog.yml +++ b/tests/blog.yml @@ -93,47 +93,37 @@ SilverStripe\Blog\Model\BlogCategory: FirstCategory: Title: 'First Category' URLSegment: 'first-category' - BlogID: =>SilverStripe\Blog\Model\Blog.FirstBlog SecondCategory: Title: 'Second Category' URLSegment: 'second-category' - BlogID: =>SilverStripe\Blog\Model\Blog.SecondBlog ThirdCategory: Title: 'Third Category' URLSegment: 'third-category' - BlogID: =>SilverStripe\Blog\Model\Blog.ThirdBlog SilverStripe\Blog\Model\BlogTag: FirstTag: Title: 'First Tag' URLSegment: 'first-tag' - BlogID: =>SilverStripe\Blog\Model\Blog.FirstBlog SecondTag: Title: 'Second Tag' URLSegment: 'second-tag' - BlogID: =>SilverStripe\Blog\Model\Blog.SecondBlog ThirdTag: Title: 'Third Tag' URLSegment: 'third-tag' - BlogID: =>SilverStripe\Blog\Model\Blog.ThirdBlog #Tags for Tag Cloud widget PopularTag: Title: 'Popular' URLSegment: 'popular' - BlogID: =>SilverStripe\Blog\Model\Blog.FourthBlog CoolTag: Title: 'Cool' URLSegment: 'cool' - BlogID: =>SilverStripe\Blog\Model\Blog.FourthBlog CatTag: Title: 'Cat' URLSegment: 'cat' - BlogID: =>SilverStripe\Blog\Model\Blog.FourthBlog KiwiTag: Title: 'Kiwi' URLSegment: 'kiwi' - BlogID: =>SilverStripe\Blog\Model\Blog.FourthBlog SilverStripe\Blog\Model\BlogPost: FirstBlogPost: