diff options
author | diosmosis <benaka@piwik.pro> | 2015-10-29 22:02:32 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-12-17 18:47:44 +0300 |
commit | 7e1e0bd2e759cbd33a7b836a82d169ea6a72a873 (patch) | |
tree | 25e9fd8725b732b548c6dd8ec8ae73d7fa99009a /plugins/CoreAdminHome/tests | |
parent | e9bef01209bc161021a8cf790b4f786575d73521 (diff) |
Adding new command core:set-config command to set INI config setting an instance via command. Includes tests.
Diffstat (limited to 'plugins/CoreAdminHome/tests')
-rw-r--r-- | plugins/CoreAdminHome/tests/Integration/SetConfigTest.php | 90 | ||||
-rw-r--r-- | plugins/CoreAdminHome/tests/Unit/SetConfig/ConfigSettingManipulationTest.php | 160 |
2 files changed, 250 insertions, 0 deletions
diff --git a/plugins/CoreAdminHome/tests/Integration/SetConfigTest.php b/plugins/CoreAdminHome/tests/Integration/SetConfigTest.php new file mode 100644 index 0000000000..68fa001caf --- /dev/null +++ b/plugins/CoreAdminHome/tests/Integration/SetConfigTest.php @@ -0,0 +1,90 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\CoreAdminHome\tests\Integration\Commands; + +use Piwik\Config; +use Piwik\Tests\Framework\TestCase\ConsoleCommandTestCase; +use Piwik\Url; + +/** + * @group CoreAdminHome + * @group CoreAdminHome_Integration + */ +class SetConfigTest extends ConsoleCommandTestCase +{ + public function test_Command_SucceedsWhenOptionsUsed() + { + $code = $this->applicationTester->run(array( + 'command' => 'core:set-config', + '--section' => 'MySection', + '--key' => 'setting', + '--value' => 'myvalue', + '-vvv' => false, + )); + + $this->assertEquals(0, $code, $this->getCommandDisplayOutputErrorMessage()); + + $config = Config::getInstance(); + $this->assertEquals(array('setting' => 'myvalue'), $config->MySection); + + $this->assertContains('Setting [MySection] setting = "myvalue"', $this->applicationTester->getDisplay()); + } + + /** + * @dataProvider getInvalidArgumentsForTest + */ + public function test_Command_FailsWhenInvalidArgumentsUsed($invalidArgument) + { + $code = $this->applicationTester->run(array( + 'command' => 'core:set-config', + 'assignment' => array($invalidArgument), + '-vvv' => false, + )); + + $this->assertNotEquals(0, $code, $this->getCommandDisplayOutputErrorMessage()); + $this->assertContains('Invalid assignment string', $this->applicationTester->getDisplay()); + } + + public function getInvalidArgumentsForTest() + { + return array( + array("garbage"), + array("ab&cd.ghi=23"), + array("section.value = 34"), + array("section.value = notjson"), + array("section.array[0]=23"), + ); + } + + public function test_Command_SucceedsWhenArgumentsUsed() + { + $config = Config::getInstance(); + $config->General['trusted_hosts'] = array('www.trustedhost.com'); + + $code = $this->applicationTester->run(array( + 'command' => 'core:set-config', + 'assignment' => array( + 'General.action_url_category_delimiter="+"', + 'General.trusted_hosts[]="www.trustedhost2.com"', + 'MySection.array_value=["abc","def"]', + 'MySection.object_value={"abc":"def"}', + ), + '-vvv' => false, + )); + + $this->assertEquals(0, $code, $this->getCommandDisplayOutputErrorMessage()); + + $this->assertEquals('+', $config->General['action_url_category_delimiter']); + $this->assertEquals(array('www.trustedhost.com', 'www.trustedhost2.com'), $config->General['trusted_hosts']); + $this->assertEquals(array('abc', 'def'), $config->MySection['array_value']); + $this->assertEquals(array('abc' => 'def'), $config->MySection['object_value']); + + $this->assertContains("Done.", $this->applicationTester->getDisplay()); + } +} diff --git a/plugins/CoreAdminHome/tests/Unit/SetConfig/ConfigSettingManipulationTest.php b/plugins/CoreAdminHome/tests/Unit/SetConfig/ConfigSettingManipulationTest.php new file mode 100644 index 0000000000..3f713d5ccf --- /dev/null +++ b/plugins/CoreAdminHome/tests/Unit/SetConfig/ConfigSettingManipulationTest.php @@ -0,0 +1,160 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +namespace Piwik\Plugins\CoreAdminHome\tests\Unit\Commands\SetConfig; + +use Piwik\Config; +use Piwik\Plugins\CoreAdminHome\Commands\SetConfig\ConfigSettingManipulation; + +// phpunit mocks can't return references, so we need a manual one +class DumbMockConfig extends \Piwik\Config +{ + /** + * @var array + */ + public $mockConfigData; + + public function __construct() + { + // empty + } + + public function &__get($sectionName) + { + if (!isset($this->mockConfigData[$sectionName])) { + $this->mockConfigData[$sectionName] = array(); + } + + $result =& $this->mockConfigData[$sectionName]; + return $result; + } +} + +/** + * @group CoreAdminHome + * @group CoreAdminHome_Unit + */ +class ConfigSettingManipulationTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Config + */ + private $mockConfig; + + protected function setUp() + { + $this->mockConfig = new DumbMockConfig(); + $this->mockConfigData = array(); + } + + /** + * @dataProvider getTestDataForMake + */ + public function test_make_CreatesCorrectManipulation($assignmentString, $expectedSectionName, $expectedSettingName, + $expectedSettingValue, $expectedIsArrayAppend) + { + $manipulation = ConfigSettingManipulation::make($assignmentString); + + $this->assertEquals($expectedSectionName, $manipulation->getSectionName()); + $this->assertEquals($expectedSettingName, $manipulation->getName()); + $this->assertEquals($expectedSettingValue, $manipulation->getValue()); + $this->assertEquals($expectedIsArrayAppend, $manipulation->isArrayAppend()); + } + + public function getTestDataForMake() + { + return array( + // normal assign + array("General.myconfig=0", "General", "myconfig", 0, false), + + // array append + array("General.myconfig444[]=5", "General", "myconfig444", 5, true), + + // assign array + array("1General1.2config2=[\"abc\",\"def\"]", "1General1", "2config2", array('abc', 'def'), false), + + // assign string + array("MySection.value=\"ghi\"", "MySection", "value", "ghi", false), + + // assign boolean + array("MySection.value=false", "MySection", "value", false, false), + array("MySection.value=true", "MySection", "value", true, false), + ); + } + + /** + * @dataProvider getFailureTestDataForMake + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Invalid assignment string + */ + public function test_make_ThrowsWhenInvalidAssignmentStringSupplied($assignmentString) + { + ConfigSettingManipulation::make($assignmentString); + } + + public function getFailureTestDataForMake() + { + return array( + array("General&.value=1"), + array("General.val&*ue=12"), + array("General.value=[notjson]"), + array("General.value=notjson"), + array("General.array[abc]=\"def\""), + ); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Trying to append to non-array setting value + */ + public function test_manipulate_ThrowsIfAppendingNonArraySetting() + { + $this->mockConfig->mockConfigData['General']['config'] = "5"; + + $manipulation = new ConfigSettingManipulation("General", "config", "10", true); + $manipulation->manipulate($this->mockConfig); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Trying to set non-array value to array setting + */ + public function test_manipulate_ThrowsIfAssigningNonArrayValue_ToArraySetting() + { + $this->mockConfig->mockConfigData['General']['config'] = array("5"); + + $manipulation = new ConfigSettingManipulation("General", "config", "10", false); + $manipulation->manipulate($this->mockConfig); + } + + /** + * @dataProvider getTestDataForManipulate + */ + public function test_manipulate_CorrectlyManipulatesConfig($sectionName, $name, $value, $isArrayAppend, $expectedConfig) + { + $manipulation = new ConfigSettingManipulation($sectionName, $name, $value, $isArrayAppend); + $manipulation->manipulate($this->mockConfig); + + $this->assertEquals($expectedConfig, $this->mockConfig->mockConfigData); + } + + public function getTestDataForManipulate() + { + return array( + // normal assign (string, int, array, bool) + array("Section", "config_setting", "stringvalue", false, array("Section" => array("config_setting" => "stringvalue"))), + array("Section", "config_setting", 25, false, array("Section" => array("config_setting" => 25))), + array("Section", "config_setting", array('a' => 'b'), false, array("Section" => array("config_setting" => array('a' => 'b')))), + array("Section", "config_setting", false, false, array("Section" => array("config_setting" => false))), + + // array append + array("Section", "config_setting", "value", true, array("Section" => array("config_setting" => array('value')))), + array("Section", "config_setting", array(1,2), true, array("Section" => array("config_setting" => array(array(1,2))))), + ); + } +}
\ No newline at end of file |