Skip to content
Extraits de code Groupes Projets
ConfigTest.php 12,5 ko
Newer Older
  • Learn to ignore specific revisions
  • <?php
    /**
     * Piwik - Open source web analytics
     *
     * @link http://piwik.org
     * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
     * @version $Id$
     */
    class ConfigTest extends PHPUnit_Framework_TestCase
    {
        /**
         * @group Core
         * @group Config
         */
        public function testUserConfigOverwritesSectionGlobalConfigValue()
        {
            $userFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/config.ini.php';
            $globalFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/global.ini.php';
    
            $config = Piwik_Config::getInstance();
            $config->setTestEnvironment($userFile, $globalFile);
            $config->init();
    
            $this->assertEquals($config->Category['key1'], "value_overwritten");
            $this->assertEquals($config->Category['key2'], "value2");
            $this->assertEquals($config->GeneralSection['login'], 'tes"t');
            $this->assertEquals($config->CategoryOnlyInGlobalFile['key3'], "value3");
            $this->assertEquals($config->CategoryOnlyInGlobalFile['key4'], "value4");
    
            $expectedArray = array('plugin"1', 'plugin2', 'plugin3');
            $array = $config->TestArray;
            $this->assertEquals($array['installed'], $expectedArray);
    
            $expectedArray = array('value1', 'value2');
            $array = $config->TestArrayOnlyInGlobalFile;
            $this->assertEquals($array['my_array'], $expectedArray);
        }
    
        /**
         * @group Core
         * @group Config
         */
        public function testWritingConfigWithSpecialCharacters()
        {
            $userFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/config.written.ini.php';
            $globalFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/global.ini.php';
    
            $config = Piwik_Config::getInstance();
            $config->setTestEnvironment($userFile, $globalFile);
            $config->init();
    
            $stringWritten = '&6^ geagea\'\'\'";;&';
            $config->Category = array('test' => $stringWritten);
            $this->assertEquals($config->Category['test'], $stringWritten);
            unset($config);
    
            $config = Piwik_Config::getInstance();
            $config->setTestEnvironment($userFile, $globalFile);
            $config->init();
    
            $this->assertEquals($config->Category['test'], $stringWritten);
            $config->Category = array(
                'test' => $config->Category['test'],
                'test2' => $stringWritten,
            );
            $this->assertEquals($config->Category['test'], $stringWritten);
            $this->assertEquals($config->Category['test2'], $stringWritten);
        }
    
        /**
         * @group Core
         * @group Config
         */
        public function testUserConfigOverwritesGlobalConfig()
        {
            $userFile = PIWIK_PATH_TEST_TO_ROOT . '/tests/resources/Config/config.ini.php';
            $globalFile = PIWIK_PATH_TEST_TO_ROOT . '/tests/resources/Config/global.ini.php';
    
            $config = Piwik_Config::getInstance();
            $config->setTestEnvironment($userFile, $globalFile);
    
            $this->assertEquals($config->Category['key1'], "value_overwritten");
            $this->assertEquals($config->Category['key2'], "value2");
            $this->assertEquals($config->GeneralSection['login'], "tes\"t");
            $this->assertEquals($config->CategoryOnlyInGlobalFile['key3'], "value3");
            $this->assertEquals($config->CategoryOnlyInGlobalFile['key4'], "value4");
    
            $expectedArray = array('plugin"1', 'plugin2', 'plugin3');
            $array = $config->TestArray;
            $this->assertEquals($array['installed'], $expectedArray);
    
            $expectedArray = array('value1', 'value2');
            $array = $config->TestArrayOnlyInGlobalFile;
            $this->assertEquals($array['my_array'], $expectedArray);
    
            Piwik_Config::getInstance()->clear();
        }
    
        /**
         * Dateprovider for testCompareElements
         */
        public function getCompareElementsData()
        {
            return array(
                array('string = string', array(
                    'a', 'a', 0,
                )),
                array('string > string', array(
                    'b', 'a', 1,
                )),
                array('string < string', array(
                    'a', 'b', -1,
                )),
                array('string vs array', array(
                    'a', array('a'), -1,
                )),
                array('array vs string', array(
                    array('a'), 'a', 1,
                )),
                array('array = array', array(
                    array('a'), array('a'), 0,
                )),
                array('array > array', array(
                    array('b'), array('a'), 1,
                )),
                array('array < array', array(
                    array('a'), array('b'), -1,
                )),
            );
        }
    
        /**
         * @group Core
         * @group Config
         * @dataProvider getCompareElementsData
         */
        public function testCompareElements($description, $test)
        {
            list($a, $b, $expected) = $test;
    
            $result = Piwik_Config::compareElements($a, $b);
            $this->assertEquals($result, $expected, $description);
        }
    
        /**
         * Dataprovider for testArrayUnmerge
         * @return array
         */
        public function getArrayUnmergeData() {
            return array(
                array('description of test', array(
                    array(),
                    array(),
                )),
                array('override with empty', array(
                    array('login' => 'root', 'password' => 'b33r'),
                    array('password' => ''),
                )),
                array('override with non-empty', array(
                    array('login' => 'root', 'password' => ''),
                    array('password' => 'b33r'),
                )),
                array('add element', array(
                    array('login' => 'root', 'password' => ''),
                    array('auth' => 'Login'),
                )),
                array('override with empty array', array(
                    array('headers' => ''),
                    array('headers' => array()),
                )),
                array('override with array', array(
                    array('headers' => ''),
                    array('headers' => array('Content-Length', 'Content-Type')),
                )),
                array('override an array', array(
                    array('headers' => array()),
                    array('headers' => array('Content-Length', 'Content-Type')),
                )),
                array('override similar arrays', array(
                    array('headers' => array('Content-Length', 'Set-Cookie')),
                    array('headers' => array('Content-Length', 'Content-Type')),
                )),
                array('override dyslexic arrays', array(
                    array('headers' => array('Content-Type', 'Content-Length')),
                    array('headers' => array('Content-Length', 'Content-Type')),
                )),
            );
        }
    
        /**
         * @group Core
         * @group Config
         * @dataProvider getArrayUnmergeData
         */
        public function testArrayUnmerge($description, $test)
        {
            $configWriter = Piwik_Config::getInstance();
    
            list($a, $b) = $test;
    
            $combined = array_merge($a, $b);
    
            $diff = $configWriter->array_unmerge($a, $combined);
    
            // expect $b == $diff
            $this->assertEquals(serialize($b), serialize($diff), $description);
        }
    
        /**
         * Dataprovider for testDumpConfig
         */
        public function getDumpConfigData()
        {
            $header = <<<END_OF_HEADER
    ; <?php exit; ?> DO NOT REMOVE THIS LINE
    ; file automatically generated or modified by Piwik; you can manually override the default values in global.ini.php by redefining them in this file.
    
    END_OF_HEADER;
    
            return array(
                array('global only, not cached', array(
                    array(),
                    array('General' => array('debug' => '1')),
                    array(),
                    false,
                )),
    
                array('global only, cached get', array(
                    array(),
                    array('General' => array('debug' => '1')),
                    array('General' => array('debug' => '1')),
                    false,
                )),
    
                array('global only, cached set', array(
                    array(),
                    array('General' => array('debug' => '1')),
                    array('General' => array('debug' => '2')),
                    $header . "[General]\ndebug = 2\n\n",
                )),
    
                array('local copy (same), not cached', array(
                    array('General' => array('debug' => '1')),
                    array('General' => array('debug' => '1')),
                    array(),
                    false,
                )),
    
                array('local copy (same), cached get', array(
                    array('General' => array('debug' => '1')),
                    array('General' => array('debug' => '1')),
                    array('General' => array('debug' => '1')),
                    false,
                )),
    
                array('local copy (same), cached set', array(
                    array('General' => array('debug' => '1')),
                    array('General' => array('debug' => '1')),
                    array('General' => array('debug' => '2')),
                    $header . "[General]\ndebug = 2\n\n",
                )),
    
                array('local copy (different), not cached', array(
                    array('General' => array('debug' => '2')),
                    array('General' => array('debug' => '1')),
                    array(),
                    false,
                )),
    
                array('local copy (different), cached get', array(
                    array('General' => array('debug' => '2')),
                    array('General' => array('debug' => '1')),
                    array('General' => array('debug' => '2')),
                    false,
                )),
    
                array('local copy (different), cached set', array(
                    array('General' => array('debug' => '2')),
                    array('General' => array('debug' => '1')),
                    array('General' => array('debug' => '3')),
                    $header . "[General]\ndebug = 3\n\n",
                )),
    
                array('local copy, not cached, new section', array(
                    array('Tracker' => array('anonymize' => '1')),
                    array('General' => array('debug' => '1')),
                    array(),
                    false,
                )),
    
                array('local copy, cached get, new section', array(
                    array('Tracker' => array('anonymize' => '1')),
                    array('General' => array('debug' => '1')),
                    array('Tracker' => array('anonymize' => '1')),
                    false,
                )),
    
                array('local copy, cached set local, new section', array(
                    array('Tracker' => array('anonymize' => '1')),
                    array('General' => array('debug' => '1')),
                    array('Tracker' => array('anonymize' => '2')),
                    $header . "[Tracker]\nanonymize = 2\n\n",
                )),
    
                array('local copy, cached set global, new section', array(
                    array('Tracker' => array('anonymize' => '1')),
                    array('General' => array('debug' => '1')),
                    array('General' => array('debug' => '2')),
                    $header . "[General]\ndebug = 2\n\n[Tracker]\nanonymize = 1\n\n",
                )),
    
                array('sort, common sections', array(
                    array('Tracker' => array('anonymize' => '1'),
                        'General' => array('debug' => '1')),
                    array('General' => array('debug' => '0'),
                        'Tracker' => array('anonymize' => '0')),
                    array('Tracker' => array('anonymize' => '2')),
                    $header . "[General]\ndebug = 1\n\n[Tracker]\nanonymize = 2\n\n",
                )),
    
                array('sort, common sections before new section', array(
                    array('Tracker' => array('anonymize' => '1'),
                        'General' => array('debug' => '1')),
                    array('General' => array('debug' => '0'),
                        'Tracker' => array('anonymize' => '0')),
                    array('Segment' => array('dimension' => 'foo')),
                    $header . "[General]\ndebug = 1\n\n[Tracker]\nanonymize = 1\n\n[Segment]\ndimension = \"foo\"\n\n",
                )),
    
                array('change back to default', array(
                    array('Tracker' => array('anonymize' => '1')),
                    array('Tracker' => array('anonymize' => '0'),
                        'General' => array('debug' => '1')),
                    array('Tracker' => array('anonymize' => '0')),
                    $header
                )),
            );
    
        }
    
        /**
         * @group Core
         * @group Config
         * @dataProvider getDumpConfigData
         */
        public function testDumpConfig($description, $test)
        {
            $config = Piwik_Config::getInstance();
    
            list($configLocal, $configGlobal, $configCache, $expected) = $test;
    
            $output = $config->dumpConfig($configLocal, $configGlobal, $configCache);
    
            $this->assertEquals($output, $expected, $description);
        }
    }