Skip to content

Commit

Permalink
RecentBlogPostsBlock - check if Blog exists
Browse files Browse the repository at this point in the history
fixes dynamic#6
  • Loading branch information
jsirish committed Sep 30, 2016
1 parent 343ae23 commit f66e623
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
45 changes: 41 additions & 4 deletions code/blocks/RecentBlogPostsBlock.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?php

if (!class_exists('Blog')) {
return;
}

/**
* Class RecentBlogPostsBlock
*/
class RecentBlogPostsBlock extends Block
{
/**
Expand Down Expand Up @@ -34,18 +41,48 @@ class RecentBlogPostsBlock extends Block
);

/**
*
* @return FieldList
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields = singleton('Block')->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();
}
}
44 changes: 44 additions & 0 deletions tests/RecentBlogPostsBlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

class RecentBlogPostsBlockTest extends SapphireTest
{
/**
* @var string
*/
protected static $fixture_file = 'dynamic-blocks/tests/Fixtures.yml';

/**
*
*/
public function testGetCMSFields()
{
$object = $this->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));
}
}

0 comments on commit f66e623

Please sign in to comment.