Fix blog controller tests

Add blog setter / getter to tags and categories
Fix remaining tests
This commit is contained in:
Damian Mooyman 2019-10-25 10:49:05 +13:00
parent 82b7dab574
commit 91f89637bb
No known key found for this signature in database
GPG Key ID: 19B1752E86A700BB
4 changed files with 72 additions and 34 deletions

View File

@ -177,7 +177,9 @@ class BlogController extends PageController
return (int)$this->urlParams['Year'];
}
if ($this->urlParams['Action'] === 'archive') {
if (empty($this->urlParams['Year']) &&
$this->urlParams['Action'] === 'archive'
) {
return DBDatetime::now()->Year();
}
@ -197,6 +199,10 @@ class BlogController extends PageController
? $this->urlParams['Month']
: null;
if (!$month) {
return null;
}
if (preg_match('/^[0-9]{1,2}$/', $month)
&& $month > 0
&& $month < 13
@ -220,6 +226,10 @@ class BlogController extends PageController
? $this->urlParams['Day']
: null;
if (!$day) {
return null;
}
// Cannot calculate day without month and year
$month = $this->getArchiveMonth();
$year = $this->getArchiveYear();

View File

@ -39,7 +39,7 @@ trait BlogObject
*/
public function Blog()
{
$blogID = $this->getSourceQueryParam('BlogID');
$blogID = $this->getBlogID();
if ($blogID) {
/** @var Blog $blog */
$blog = Blog::get()->byID($blogID);
@ -195,6 +195,26 @@ trait BlogObject
return $blog && $blog->canEdit($member);
}
/**
* @return mixed
*/
public function getBlogID()
{
return $this->getSourceQueryParam('BlogID');
}
/**
* Set a blog ID for this record
*
* @param int $id
* @return $this
*/
public function setBlogID($id)
{
$this->setSourceQueryParam('BlogID', $id);
return $this;
}
protected function onBeforeWrite()
{
parent::onBeforeWrite();

View File

@ -82,15 +82,19 @@ class BlogTagTest extends FunctionalTest
$admin = $this->objFromFixture(Member::class, 'Admin');
$editor = $this->objFromFixture(Member::class, 'Editor');
$tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
/** @var Blog $firstBlog */
$firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog');
$firstTag = $firstBlog->Tags(false)->find('URLSegment', 'first-tag');
$this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.');
$this->assertTrue($tag->canView($editor), 'Editor should be able to view tag.');
$this->assertTrue($firstTag->canView($admin), 'Admin should be able to view tag.');
$this->assertTrue($firstTag->canView($editor), 'Editor should be able to view tag.');
$tag = $this->objFromFixture(BlogTag::class, 'SecondTag');
/** @var Blog $secondBlog */
$secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog');
$secondTag = $secondBlog->Tags(false)->find('URLSegment', 'second-tag');
$this->assertTrue($tag->canView($admin), 'Admin should be able to view tag.');
$this->assertFalse($tag->canView($editor), 'Editor should not be able to view tag.');
$this->assertTrue($secondTag->canView($admin), 'Admin should be able to view tag.');
$this->assertFalse($secondTag->canView($editor), 'Editor should not be able to view tag.');
}
public function testCanEdit()
@ -100,20 +104,26 @@ class BlogTagTest extends FunctionalTest
$admin = $this->objFromFixture(Member::class, 'Admin');
$editor = $this->objFromFixture(Member::class, 'Editor');
$tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
/** @var Blog $firstBlog */
$firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog');
$firstTag = $firstBlog->Tags(false)->find('URLSegment', 'first-tag');
$this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.');
$this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.');
$this->assertTrue($firstTag->canEdit($admin), 'Admin should be able to edit tag.');
$this->assertTrue($firstTag->canEdit($editor), 'Editor should be able to edit tag.');
$tag = $this->objFromFixture(BlogTag::class, 'SecondTag');
/** @var Blog $secondBlog */
$secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog');
$secondTag = $secondBlog->Tags(false)->find('URLSegment', 'second-tag');
$this->assertTrue($tag->canEdit($admin), 'Admin should be able to edit tag.');
$this->assertFalse($tag->canEdit($editor), 'Editor should not be able to edit tag.');
$this->assertTrue($secondTag->canEdit($admin), 'Admin should be able to edit tag.');
$this->assertFalse($secondTag->canEdit($editor), 'Editor should not be able to edit tag.');
$tag = $this->objFromFixture(BlogTag::class, 'ThirdTag');
/** @var Blog $thirdBlog */
$thirdBlog = $this->objFromFixture(Blog::class, 'ThirdBlog');
$thirdTag = $thirdBlog->Tags(false)->find('URLSegment', 'third-tag');
$this->assertTrue($tag->canEdit($admin), 'Admin should always be able to edit tags.');
$this->assertTrue($tag->canEdit($editor), 'Editor should be able to edit tag.');
$this->assertTrue($thirdTag->canEdit($admin), 'Admin should always be able to edit tags.');
$this->assertTrue($thirdTag->canEdit($editor), 'Editor should be able to edit tag.');
}
public function testCanCreate()
@ -136,20 +146,26 @@ class BlogTagTest extends FunctionalTest
$admin = $this->objFromFixture(Member::class, 'Admin');
$editor = $this->objFromFixture(Member::class, 'Editor');
$tag = $this->objFromFixture(BlogTag::class, 'FirstTag');
/** @var Blog $firstBlog */
$firstBlog = $this->objFromFixture(Blog::class, 'FirstBlog');
$firstTag = $firstBlog->Tags(false)->find('URLSegment', 'first-tag');
$this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.');
$this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.');
$this->assertTrue($firstTag->canDelete($admin), 'Admin should be able to delete tag.');
$this->assertTrue($firstTag->canDelete($editor), 'Editor should be able to delete tag.');
$tag = $this->objFromFixture(BlogTag::class, 'SecondTag');
/** @var Blog $secondBlog */
$secondBlog = $this->objFromFixture(Blog::class, 'SecondBlog');
$secondTag = $secondBlog->Tags(false)->find('URLSegment', 'second-tag');
$this->assertTrue($tag->canDelete($admin), 'Admin should be able to delete tag.');
$this->assertFalse($tag->canDelete($editor), 'Editor should not be able to delete tag.');
$this->assertTrue($secondTag->canDelete($admin), 'Admin should be able to delete tag.');
$this->assertFalse($secondTag->canDelete($editor), 'Editor should not be able to delete tag.');
$tag = $this->objFromFixture(BlogTag::class, 'ThirdTag');
/** @var Blog $thirdBlog */
$thirdBlog = $this->objFromFixture(Blog::class, 'ThirdBlog');
$thirdTag = $thirdBlog->Tags(false)->find('URLSegment', 'third-tag');
$this->assertTrue($tag->canDelete($admin), 'Admin should always be able to delete tags.');
$this->assertTrue($tag->canDelete($editor), 'Editor should be able to delete tag.');
$this->assertTrue($thirdTag->canDelete($admin), 'Admin should always be able to delete tags.');
$this->assertTrue($thirdTag->canDelete($editor), 'Editor should be able to delete tag.');
}
public function testDuplicateTagsForURLSegment()

View File

@ -12,10 +12,6 @@ SilverStripe\Blog\Model\Blog:
blog_a:
URLSegment: my-blog
Title: My Blog
Categories:
- =>SilverStripe\Blog\Model\BlogCategory.category_a
Tags:
- =>SilverStripe\Blog\Model\BlogTag.tag_a
SilverStripe\Blog\Model\BlogPost:
blogpost_a:
@ -23,7 +19,3 @@ SilverStripe\Blog\Model\BlogPost:
URLSegment: آبیدآبید
PublishDate: 2017-08-01 00:00:00
Parent: =>SilverStripe\Blog\Model\Blog.blog_a
Categories:
- =>SilverStripe\Blog\Model\BlogCategory.category_a
Tags:
- =>SilverStripe\Blog\Model\BlogTag.tag_a