diff options
author | Tim-Hinnerk Heuer <tim@innocraft.com> | 2021-08-19 10:23:59 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-19 10:23:59 +0300 |
commit | 9d26506c1d005f1c480325b0e5f2a5046a96f9dd (patch) | |
tree | 2c56974891fad18eb300eb72cb24cf07db8cfa8e /tests | |
parent | 0829d6e6ac487bf47e4197123d6683b271db8266 (diff) |
Test Config::getBool() for clarity of configuration options (#17865)
* relax force ssl if assuming secure protocol #13374
* avoid error ignoring on always defined config variables #13374
* solidify Config::getBool() with unit test #13374
* leave logic as was but introduce new method: Config::getBool() #13374
* ensure all possible true settings' values are reflected and documented in tests #13374
* show developers how boolean settings should be processed and what true means in config.ini.php files #13374
* remove edge cases and exception handling fixes #13374
* Update tests/PHPUnit/Unit/Config/ConfigTest.php
Co-authored-by: Stefan Giehl <stefan@matomo.org>
* beautify test cases with @dataProvider #13374
* Apply suggestions from code review
Co-authored-by: Stefan Giehl <stefan@matomo.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/PHPUnit/Unit/Config/ConfigTest.php | 64 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/Config/test_files/boolean_settings_test.ini.php | 14 |
2 files changed, 78 insertions, 0 deletions
diff --git a/tests/PHPUnit/Unit/Config/ConfigTest.php b/tests/PHPUnit/Unit/Config/ConfigTest.php new file mode 100644 index 0000000000..d351da3535 --- /dev/null +++ b/tests/PHPUnit/Unit/Config/ConfigTest.php @@ -0,0 +1,64 @@ +<?php + +namespace Piwik\Tests\Unit\Config; + +use Piwik\Application\Kernel\GlobalSettingsProvider; +use Piwik\Config; +use PHPUnit\Framework\TestCase; + +class ConfigTest extends TestCase +{ + + private $config; + + protected function setUp(): void + { + $userSettingsPath = __DIR__ . '/test_files/boolean_settings_test.ini.php'; + $settingsProvider = new GlobalSettingsProvider([], $userSettingsPath); + $this->config = new Config($settingsProvider); + } + + /** + * Returns a boolean variable setting for convenience + * when calling e.g. getBool('General', 'force_ssl') + * This also documents that a boolean is only true if + * it is equal to 1. + * @see https://github.com/matomo-org/matomo/pull/17865 + * return 1 === $value || '1' === $value || true === $value; // was suggested for future use by @sgiehl + * + * @param string $section Configuration section + * @param string $name variable name + * @return bool whether it is considered set true (== 1) + */ + private function getBool(string $section, string $name): bool + { + return $this->config->$section[$name] == 1; + } + + /** + * @dataProvider getTestCases + */ + public function testGetBool($expected, $setting) + { + $this->assertSame($expected, $this->getBool('BoolSettings', $setting)); + } + + public function getTestCases(): array + { + return [ + [true, 'one'], + [true, 'onestr'], + [true, 'truebool'], + [true, 'isyes'], + [true, 'ison'], + [false, 'truestr'], + [false, 'two'], + [false, 'twostr'], + [false, 'invalid'], + [false, 'oneinstr'], + [false, 'twoinstr'], + [false, 'isoff'], + [false, 'isno'], + ]; + } +} diff --git a/tests/PHPUnit/Unit/Config/test_files/boolean_settings_test.ini.php b/tests/PHPUnit/Unit/Config/test_files/boolean_settings_test.ini.php new file mode 100644 index 0000000000..8f825b4a2c --- /dev/null +++ b/tests/PHPUnit/Unit/Config/test_files/boolean_settings_test.ini.php @@ -0,0 +1,14 @@ +[BoolSettings] +one = 1 +oneinstr = 'te1st' +onestr = '1' +ison = on +isyes = yes +isoff = off +isno = no +two = 2 +twostr = '2' +twoinstr = 'te2st' +invalid = 'invalid' +truebool = true +truestr = 'true' |