From f66e623af3cc8aaa773545393ebab4ca2017e77c Mon Sep 17 00:00:00 2001 From: Jason Irish Date: Fri, 30 Sep 2016 12:00:18 -0500 Subject: [PATCH] RecentBlogPostsBlock - check if Blog exists fixes #6 --- code/blocks/RecentBlogPostsBlock.php | 45 +++++++++++++++++++++++++--- tests/RecentBlogPostsBlockTest.php | 44 +++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 tests/RecentBlogPostsBlockTest.php diff --git a/code/blocks/RecentBlogPostsBlock.php b/code/blocks/RecentBlogPostsBlock.php index 6e4f390..169a29a 100644 --- a/code/blocks/RecentBlogPostsBlock.php +++ b/code/blocks/RecentBlogPostsBlock.php @@ -1,5 +1,12 @@ getCMSFields(); $fields->addFieldsToTab('Root.Main', array( NumericField::create('Limit'), - DropdownField::create('BlogID', 'Featured Blog', Blog::get()->map()) - ->setEmptyString(''), )); + if (class_exists('Blog')) { + $fields->addFieldToTab( + 'Root.Main', + DropdownField::create('BlogID', 'Featured Blog', Blog::get()->map()) + ->setEmptyString('') + ); + } + return $fields; } + + /** + * @param null $member + * @return bool + */ + public function canCreate($member = null) + { + if (!class_exists('Blog')) { + return false; + } + return parent::canCreate(); + } + + /** + * @param null $member + * @return bool + */ + public function canView($member = null) + { + if (!class_exists('Blog')) { + return false; + } + return parent::canView(); + } } \ No newline at end of file diff --git a/tests/RecentBlogPostsBlockTest.php b/tests/RecentBlogPostsBlockTest.php new file mode 100644 index 0000000..cd1321d --- /dev/null +++ b/tests/RecentBlogPostsBlockTest.php @@ -0,0 +1,44 @@ +objFromFixture('RecentBlogPostsBlock', 'one'); + $fields = $object->getCMSFields(); + $this->assertInstanceOf('FieldList', $fields); + $this->assertNull($fields->dataFieldByName('SortOrder')); + } + + /** + * + */ + public function testCanView() + { + $object = $this->objFromFixture('RecentBlogPostsBlock', 'one'); + $admin = $this->objFromFixture('Member', 'admin'); + $this->assertFalse($object->canView($admin)); + $member = $this->objFromFixture('Member', 'default'); + $this->assertFalse($object->canView($member)); + } + + /** + * + */ + public function testCanCreate() + { + $object = $this->objFromFixture('RecentBlogPostsBlock', 'one'); + $admin = $this->objFromFixture('Member', 'admin'); + $this->assertFalse($object->canCreate($admin)); + $member = $this->objFromFixture('Member', 'default'); + $this->assertFalse($object->canCreate($member)); + } +} \ No newline at end of file