BUG Fix assignment of default values

Fixes #130
This commit is contained in:
Damian Mooyman 2015-08-25 16:49:58 +12:00
parent d3a9e78002
commit 752d509d1e
2 changed files with 91 additions and 6 deletions

View File

@ -75,14 +75,23 @@ class CommentsExtension extends DataExtension {
); );
/** /**
* CMS configurable options should default to the config values * CMS configurable options should default to the config values, but respect
* default values specified by the object
*/ */
public function populateDefaults() { public function populateDefaults() {
$defaults = $this->owner->config()->defaults;
// Set if comments should be enabled by default // Set if comments should be enabled by default
if(isset($defaults['ProvideComments'])) {
$this->owner->ProvideComments = $defaults['ProvideComments'];
} else {
$this->owner->ProvideComments = $this->owner->getCommentsOption('enabled') ? 1 : 0; $this->owner->ProvideComments = $this->owner->getCommentsOption('enabled') ? 1 : 0;
}
// If moderation options should be configurable via the CMS then // If moderation options should be configurable via the CMS then
if($this->owner->getCommentsOption('require_moderation')) { if(isset($defaults['ModerationRequired'])) {
$this->owner->ModerationRequired = $defaults['ModerationRequired'];
} elseif($this->owner->getCommentsOption('require_moderation')) {
$this->owner->ModerationRequired = 'Required'; $this->owner->ModerationRequired = 'Required';
} elseif($this->owner->getCommentsOption('require_moderation_nonmembers')) { } elseif($this->owner->getCommentsOption('require_moderation_nonmembers')) {
$this->owner->ModerationRequired = 'NonMembersOnly'; $this->owner->ModerationRequired = 'NonMembersOnly';
@ -90,8 +99,13 @@ class CommentsExtension extends DataExtension {
$this->owner->ModerationRequired = 'None'; $this->owner->ModerationRequired = 'None';
} }
// Set login required
if(isset($defaults['CommentsRequireLogin'])) {
$this->owner->CommentsRequireLogin = $defaults['CommentsRequireLogin'];
} else {
$this->owner->CommentsRequireLogin = $this->owner->getCommentsOption('require_login') ? 1 : 0; $this->owner->CommentsRequireLogin = $this->owner->getCommentsOption('require_login') ? 1 : 0;
} }
}
/** /**

View File

@ -8,7 +8,9 @@ class CommentsTest extends FunctionalTest {
public static $fixture_file = 'comments/tests/CommentsTest.yml'; public static $fixture_file = 'comments/tests/CommentsTest.yml';
protected $extraDataObjects = array( protected $extraDataObjects = array(
'CommentableItem' 'CommentableItem',
'CommentableItemEnabled',
'CommentableItemDisabled'
); );
public function setUp() { public function setUp() {
@ -504,6 +506,59 @@ class CommentsTest extends FunctionalTest {
Commenting::set_config_value('CommentableItem','html_allowed', $origAllowed); Commenting::set_config_value('CommentableItem','html_allowed', $origAllowed);
} }
/**
* Tests whether comments are enabled or disabled by default
*/
public function testDefaultEnabled() {
// Ensure values are set via cms (not via config)
Config::inst()->update('CommentableItem', 'comments', array(
'enabled_cms' => true,
'require_moderation_cms' => true,
'require_login_cms' => true
));
// With default = true
$obj = new CommentableItem();
$this->assertTrue((bool)$obj->getCommentsOption('enabled'), "Default setting is enabled");
$this->assertTrue((bool)$obj->ProvideComments);
$this->assertEquals('None', $obj->ModerationRequired);
$this->assertFalse((bool)$obj->CommentsRequireLogin);
$obj = new CommentableItemEnabled();
$this->assertTrue((bool)$obj->ProvideComments);
$this->assertEquals('Required', $obj->ModerationRequired);
$this->assertTrue((bool)$obj->CommentsRequireLogin);
$obj = new CommentableItemDisabled();
$this->assertFalse((bool)$obj->ProvideComments);
$this->assertEquals('None', $obj->ModerationRequired);
$this->assertFalse((bool)$obj->CommentsRequireLogin);
// With default = false
// Because of config rules about falsey values, apply config to object directly
Config::inst()->update('CommentableItem', 'comments', array(
'enabled' => false,
'require_login' => true,
'require_moderation' => true
));
$obj = new CommentableItem();
$this->assertFalse((bool)$obj->getCommentsOption('enabled'), "Default setting is disabled");
$this->assertFalse((bool)$obj->ProvideComments);
$this->assertEquals('Required', $obj->ModerationRequired);
$this->assertTrue((bool)$obj->CommentsRequireLogin);
$obj = new CommentableItemEnabled();
$this->assertTrue((bool)$obj->ProvideComments);
$this->assertEquals('Required', $obj->ModerationRequired);
$this->assertTrue((bool)$obj->CommentsRequireLogin);
$obj = new CommentableItemDisabled();
$this->assertFalse((bool)$obj->ProvideComments);
$this->assertEquals('None', $obj->ModerationRequired);
$this->assertFalse((bool)$obj->CommentsRequireLogin);
}
} }
@ -514,7 +569,6 @@ class CommentsTest extends FunctionalTest {
class CommentableItem extends DataObject implements TestOnly { class CommentableItem extends DataObject implements TestOnly {
private static $db = array( private static $db = array(
'ProvideComments' => 'Boolean',
'Title' => 'Varchar' 'Title' => 'Varchar'
); );
@ -539,6 +593,23 @@ class CommentableItem extends DataObject implements TestOnly {
} }
} }
class CommentableItemEnabled extends CommentableItem {
private static $defaults = array(
'ProvideComments' => true,
'ModerationRequired' => 'Required',
'CommentsRequireLogin' => true
);
}
class CommentableItemDisabled extends CommentableItem {
private static $defaults = array(
'ProvideComments' => false,
'ModerationRequired' => 'None',
'CommentsRequireLogin' => false
);
}
/** /**
* @package comments * @package comments
* @subpackage tests * @subpackage tests