From 752d509d1e513d9b3c7066498b3859bf0fa3955f Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Tue, 25 Aug 2015 16:49:58 +1200 Subject: [PATCH] BUG Fix assignment of default values Fixes #130 --- code/extensions/CommentsExtension.php | 22 ++++++-- tests/CommentsTest.php | 75 ++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/code/extensions/CommentsExtension.php b/code/extensions/CommentsExtension.php index 626a4a8..fbfaea6 100644 --- a/code/extensions/CommentsExtension.php +++ b/code/extensions/CommentsExtension.php @@ -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() { + $defaults = $this->owner->config()->defaults; + // Set if comments should be enabled by default - $this->owner->ProvideComments = $this->owner->getCommentsOption('enabled') ? 1 : 0; + if(isset($defaults['ProvideComments'])) { + $this->owner->ProvideComments = $defaults['ProvideComments']; + } else { + $this->owner->ProvideComments = $this->owner->getCommentsOption('enabled') ? 1 : 0; + } // 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'; } elseif($this->owner->getCommentsOption('require_moderation_nonmembers')) { $this->owner->ModerationRequired = 'NonMembersOnly'; @@ -90,7 +99,12 @@ class CommentsExtension extends DataExtension { $this->owner->ModerationRequired = 'None'; } - $this->owner->CommentsRequireLogin = $this->owner->getCommentsOption('require_login') ? 1 : 0; + // Set login required + if(isset($defaults['CommentsRequireLogin'])) { + $this->owner->CommentsRequireLogin = $defaults['CommentsRequireLogin']; + } else { + $this->owner->CommentsRequireLogin = $this->owner->getCommentsOption('require_login') ? 1 : 0; + } } diff --git a/tests/CommentsTest.php b/tests/CommentsTest.php index ffd037f..ecc8c64 100644 --- a/tests/CommentsTest.php +++ b/tests/CommentsTest.php @@ -8,7 +8,9 @@ class CommentsTest extends FunctionalTest { public static $fixture_file = 'comments/tests/CommentsTest.yml'; protected $extraDataObjects = array( - 'CommentableItem' + 'CommentableItem', + 'CommentableItemEnabled', + 'CommentableItemDisabled' ); public function setUp() { @@ -504,6 +506,59 @@ class CommentsTest extends FunctionalTest { 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 { private static $db = array( - 'ProvideComments' => 'Boolean', '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 * @subpackage tests