Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SetConfigTest.php « Commands « Integration « tests « CoreAdminHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e98a5152ef11ffd0ca12525046f1bdb54ede7d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CoreAdminHome\tests\Integration\Commands;

use Psr\Container\ContainerInterface;
use Piwik\Application\Kernel\GlobalSettingsProvider;
use Piwik\Config;
use Piwik\Tests\Framework\TestCase\ConsoleCommandTestCase;

/**
 * @group Core
 * @group CoreAdminHome
 * @group Integration
 */
class SetConfigTest extends ConsoleCommandTestCase
{
    const TEST_CONFIG_PATH = '/tmp/test.config.ini.php';

    public static function setUpBeforeClass(): void
    {
        self::removeTestConfigFile();

        parent::setUpBeforeClass();
    }

    public function setUp(): void
    {
        self::removeTestConfigFile();

        parent::setUp();
    }

    public function tearDown(): void
    {
        parent::tearDown();
        $this->makeLocalConfigWritable();
    }

    public function test_Command_SucceedsWhenOptionsUsed()
    {
        $code = $this->applicationTester->run(array(
            'command' => 'config:set',
            '--section' => 'MySection',
            '--key' => 'setting',
            '--value' => 'myvalue',
            '-vvv' => false,
        ));

        $this->assertEquals(0, $code, $this->getCommandDisplayOutputErrorMessage());

        $config = $this->makeNewConfig();
        $this->assertEquals(array('setting' => 'myvalue'), $config->MySection);

        self::assertStringContainsString('Setting [MySection] setting = "myvalue"', $this->applicationTester->getDisplay());
    }

    /**
     * @dataProvider getInvalidArgumentsForTest
     */
    public function test_Command_FailsWhenInvalidArgumentsUsed($invalidArgument)
    {
        $code = $this->applicationTester->run(array(
            'command' => 'config:set',
            'assignment' => array($invalidArgument),
            '-vvv' => false,
        ));

        $this->assertNotEquals(0, $code, $this->getCommandDisplayOutputErrorMessage());
        self::assertStringContainsString('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_FailsWithMissingFilePermissionException_whenConfigFileNotWritable()
    {
        $this->makeLocalConfigNotWritable();

        $code = $this->applicationTester->run(array(
            'command' => 'config:set',
            'assignment' => array(
                'MySection.other_array_value=[]',
            ),
            '-vvv' => false,
        ));

        $this->assertNotEquals(0, $code, $this->getCommandDisplayOutputErrorMessage());
        self::assertStringContainsString('[Piwik\Exception\MissingFilePermissionException]', $this->applicationTester->getDisplay());
    }

    public function test_Command_SucceedsWhenArgumentsUsed()
    {
        $config = Config::getInstance();
        $config->General['trusted_hosts'] = array('www.trustedhost.com');
        $config->MySection['other_array_value'] = array('1', '2');
        $config->forceSave();

        $code = $this->applicationTester->run(array(
            'command' => 'config:set',
            'assignment' => array(
                'General.action_url_category_delimiter="+"',
                'General.trusted_hosts[]="www.trustedhost2.com"',
                'MySection.array_value=["abc","def"]',
                'MySection.object_value={"abc":"def"}',
                'MySection.other_array_value=[]',
            ),
            '-vvv' => false,
        ));

        $this->assertEquals(0, $code, $this->getCommandDisplayOutputErrorMessage());

        $config = self::makeNewConfig(); // create a new config instance so we read what's in the file

        $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->assertArrayNotHasKey('other_array_value', $config->MySection);

        self::assertStringContainsString("done.", $this->applicationTester->getDisplay());
    }

    /**
     * @dataProvider getOptionsForSettingValueToZeroTests
     */
    public function test_Command_SucceedsWhenSettingValueToZero($options)
    {
        $config = Config::getInstance();
        $config->Tracker['debug'] = 1;
        $config->forceSave();

        $code = $this->applicationTester->run($options);

        $this->assertEquals(0, $code, $this->getCommandDisplayOutputErrorMessage());

        $config = self::makeNewConfig();

        $this->assertEquals(0, $config->Tracker['debug']);
        self::assertStringContainsString("done.", $this->applicationTester->getDisplay());
    }

    public function getOptionsForSettingValueToZeroTests()
    {
        return array(
            array(
                array(
                    'command' => 'config:set',
                    '--section' => 'Tracker',
                    '--key' => 'debug',
                    '--value' => 0,
                ),
            ),
            array(
                array(
                    'command' => 'config:set',
                    'assignment' => array(
                        'Tracker.debug=0',
                    ),
                ),
            ),
        );
    }

    private static function getTestConfigFilePath()
    {
        return PIWIK_INCLUDE_PATH . self::TEST_CONFIG_PATH;
    }

    public static function provideContainerConfigBeforeClass()
    {
        return array(
            // use a config instance that will save to a test INI file
            'Piwik\Config' => function (ContainerInterface $c) {
                /** @var GlobalSettingsProvider $actualGlobalSettingsProvider */
                $actualGlobalSettingsProvider = $c->get('Piwik\Application\Kernel\GlobalSettingsProvider');

                $config = SetConfigTest::makeNewConfig();

                // copy over sections required for tests
                $config->tests = $actualGlobalSettingsProvider->getSection('tests');
                $config->database = $actualGlobalSettingsProvider->getSection('database_tests');

                return $config;
            },
        );
    }

    private static function makeNewConfig()
    {
        $settings = new GlobalSettingsProvider(null, SetConfigTest::getTestConfigFilePath());
        return new Config($settings);
    }

    private static function removeTestConfigFile()
    {
        $configPath = self::getTestConfigFilePath();
        if (file_exists($configPath)) {
            unlink($configPath);
        }
    }

    protected function makeLocalConfigNotWritable()
    {
        $local = Config::getInstance()->getLocalPath();
        touch($local);
        chmod($local, 0444);
        $this->assertFalse(is_writable($local));
    }

    protected function makeLocalConfigWritable()
    {
        $local = Config::getInstance()->getLocalPath();
        @chmod(dirname($local), 0755);
        @chmod($local, 0755);
        $this->assertTrue(is_writable(dirname($local)));
        if(file_exists($local)) {
            $this->assertTrue(is_writable($local));
        }
    }
}