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

ConfigTest.php « Core « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b52917827dc920caff38c5532717427e2b1c117f (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
use Piwik\Config;

/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
class ConfigTest extends PHPUnit_Framework_TestCase
{
    /**
     * @group Core
     */
    public function testUserConfigOverwritesSectionGlobalConfigValue()
    {
        $userFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/config.ini.php';
        $globalFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/global.ini.php';
        $commonFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/common.ini.php';
        $config = Config::getInstance();
        $config->setTestEnvironment($userFile, $globalFile, $commonFile);
        $config->init();

        $this->assertEquals("value_overwritten", $config->Category['key1']);
        $this->assertEquals("value2", $config->Category['key2']);
        $this->assertEquals('tes"t', $config->GeneralSection['login']);
        $this->assertEquals("value3", $config->CategoryOnlyInGlobalFile['key3']);
        $this->assertEquals("value4", $config->CategoryOnlyInGlobalFile['key4']);

        $expectedArray = array('plugin"1', 'plugin2', 'plugin3');
        $array = $config->TestArray;
        $this->assertEquals($expectedArray, $array['installed']);

        $expectedArray = array('value1', 'value2');
        $array = $config->TestArrayOnlyInGlobalFile;
        $this->assertEquals($expectedArray, $array['my_array']);

        $expectedArray = array('value1', 'value2');
        $array = $config->TestArrayOnlyInGlobalFile;
        $this->assertEquals($expectedArray, $array['my_array']);

    }

    /**
     * @group Core
     */
    public function test_CommonConfig_Overrides()
    {
        $userFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/config.ini.php';
        $globalFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/global.ini.php';
        $commonFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/common.config.ini.php';

        $config = Config::getInstance();
        $config->setTestEnvironment($userFile, $globalFile, $commonFile);
        $config->init();

        $this->assertEquals("valueCommon", $config->Category['key2'], var_export($config->Category['key2'], true));
        $this->assertEquals("test", $config->GeneralSection['password']);
        $this->assertEquals("commonValue", $config->TestOnlyInCommon['value']);

    }

    /**
     * @group Core
     */
    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 = Config::getInstance();
        $config->setTestEnvironment($userFile, $globalFile);
        $config->init();

        $stringWritten = '&6^ geagea\'\'\'";;&';
        $config->Category = array('test' => $stringWritten);
        $this->assertEquals($stringWritten, $config->Category['test']);

        // This will write the file
        $config->forceSave();

        $config = Config::getInstance();
        $config->setTestEnvironment($userFile, $globalFile);
        $config->init();

        $this->assertEquals($stringWritten, $config->Category['test']);
        $config->Category = array(
            'test'  => $config->Category['test'],
            'test2' => $stringWritten,
        );
        $this->assertEquals($stringWritten, $config->Category['test']);
        $this->assertEquals($stringWritten, $config->Category['test2']);
    }

    /**
     * @group Core
     */
    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 = Config::getInstance();
        $config->setTestEnvironment($userFile, $globalFile);

        $this->assertEquals("value_overwritten", $config->Category['key1']);
        $this->assertEquals("value2", $config->Category['key2']);
        $this->assertEquals("tes\"t", $config->GeneralSection['login']);
        $this->assertEquals("value3", $config->CategoryOnlyInGlobalFile['key3']);
        $this->assertEquals("value4", $config->CategoryOnlyInGlobalFile['key4']);

        $expectedArray = array('plugin"1', 'plugin2', 'plugin3');
        $array = $config->TestArray;
        $this->assertEquals($expectedArray, $array['installed']);

        $expectedArray = array('value1', 'value2');
        $array = $config->TestArrayOnlyInGlobalFile;
        $this->assertEquals($expectedArray, $array['my_array']);

        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
     * 
     * @dataProvider getCompareElementsData
     */
    public function testCompareElements($description, $test)
    {
        list($a, $b, $expected) = $test;

        $result = Config::compareElements($a, $b);
        $this->assertEquals($expected, $result, $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
     * 
     * @dataProvider getArrayUnmergeData
     */
    public function testArrayUnmerge($description, $test)
    {
        $configWriter = 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 = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n" .
            "; file automatically generated or modified by Piwik; you can manually override the default values in global.ini.php by redefining them in this file.\n";

        return array(
            // Test name, array(
            //   LOCAL
            //   GLOBAL
            //   COMMON
            //   CACHE
            //   --> EXPECTED <--
            array('global only, not cached', array(
                array(),                                    // local
                array('General' => array('debug' => '1')),  // global
                array(),                                    // common
                array(),
                false,
            )),

            array('global only, cached get', array(
                array(),                                    // local
                array('General' => array('debug' => '1')),  // global
                array(),                                    // common
                array('General' => array('debug' => '1')),
                false,
            )),

            array('global only, cached set', array(
                array(),                                    // local
                array('General' => array('debug' => '1')),  // global
                array(),                                    // common
                array('General' => array('debug' => '2')),
                $header . "[General]\ndebug = 2\n\n",
            )),

            array('local copy (same), not cached', array(
                array('General' => array('debug' => '1')),  // local
                array('General' => array('debug' => '1')),  // global
                array(),                                    // common
                array(),
                false,
            )),

            array('local copy (same), cached get', array(
                array('General' => array('debug' => '1')),  // local
                array('General' => array('debug' => '1')),  // global
                array(),                                    // common
                array('General' => array('debug' => '1')),
                false,
            )),

            array('local copy (same), cached set', array(
                array('General' => array('debug' => '1')),  // local
                array('General' => array('debug' => '1')),  // global
                array(),                                    // common
                array('General' => array('debug' => '2')),
                $header . "[General]\ndebug = 2\n\n",
            )),

            array('local copy (different), not cached', array(
                array('General' => array('debug' => '2')),  // local
                array('General' => array('debug' => '1')),  // global
                array(),                                    // common
                array(),
                false,
            )),

            array('local copy (different), cached get', array(
                array('General' => array('debug' => '2')),  // local
                array('General' => array('debug' => '1')),  // global
                array(),                                    // common
                array('General' => array('debug' => '2')),
                false,
            )),

            array('local copy (different), cached set', array(
                array('General' => array('debug' => '2')),  // local
                array('General' => array('debug' => '1')),  // global
                array(),                                    // common
                array('General' => array('debug' => '3')),
                $header . "[General]\ndebug = 3\n\n",
            )),

            array('local copy, not cached, new section', array(
                array('Tracker' => array('anonymize' => '1')),  // local
                array('General' => array('debug' => '1')),      // global
                array(),                                        // common
                array(),
                false,
            )),

            array('local copy, cached get, new section', array(
                array('Tracker' => array('anonymize' => '1')),  // local
                array('General' => array('debug' => '1')),      // global
                array(),                                        // common
                array('Tracker' => array('anonymize' => '1')),
                false,
            )),

            array('local copy, cached set local, new section', array(
                array('Tracker' => array('anonymize' => '1')),  // local
                array('General' => array('debug' => '1')),      // global
                array(),                                        // common
                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')),  // local
                array('General' => array('debug' => '1')),      // global
                array(),                                        // common
                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'),   // local
                      'General' => array('debug' => '1')),
                array('General' => array('debug' => '0'),       // global
                      'Tracker' => array('anonymize' => '0')),
                array(),                                        // common
                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'),   // local
                      'General' => array('debug' => '1')),
                array('General' => array('debug' => '0'),       // global
                      'Tracker' => array('anonymize' => '0')),
                array(),                                        // common
                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')),  // local
                array('Tracker' => array('anonymize' => '0'),   // global
                      'General' => array('debug' => '1')),
                array(),                                        // common
                array('Tracker' => array('anonymize' => '0')),
                $header
            )),

            array('[General] trusted_hosts has been updated and only this one is written', array(
                array('General' => array('trusted_hosts' => 'someRandomHostToOverwrite')),  // local
                array('General' => array('settingGlobal' => 'global',   // global
                                         'settingCommon' => 'global',
                                         'trusted_hosts' => 'none')),
                array('General' => array('settingCommon' => 'common',       // common
                                         'settingCommon2' => 'common')),
                array('General' => array('trusted_hosts' => 'works')),
                $header . "[General]\ntrusted_hosts = \"works\"\n\n",
            )),

            // Same as above but without trusted_hosts default value in global.ini.php
            // Also, settingCommon3 is the same in the local file as in common, so it is not written out
            array('trusted_hosts and settingCommon3 changed ', array(
                array('General' => array('trusted_hosts' => 'someRandomHostToOverwrite')), // local
                array('General' => array('settingGlobal' => 'global',                   // global
                                         'settingCommon' => 'global')),
                array('General' => array('settingCommon' => 'common',                   // common
                                         'settingCommon2' => 'common',
                                         'settingCommon3' => 'common3')),
                array('General' => array('trusted_hosts' => 'works',               // common
                                         'settingCommon2' => 'common', // will be cleared since it's same as in common
                                         'settingCommon3' => 'commonOverridenByLocal')),
                $header . "[General]\ntrusted_hosts = \"works\"\nsettingCommon3 = \"commonOverridenByLocal\"\n\n",
            )),

            // the value in [General]->key has changed
            // the value in [CommonCategory]->newSetting has changed,
            //         but  [CommonCategory]->settingCommon2 hasn't so it is not written
            array('Common tests file', array(
                array('General' => array('key' => 'value')),                            // local
                array('General' => array('key' => 'global'),                            // global
                      'CommonCategory' => array('settingGlobal' => 'valueGlobal')),
                array('CommonCategory' => array('settingCommon' => 'common',            // common
                                                'settingCommon2' => 'common2')),
                array('CommonCategory' => array('settingCommon2' => 'common2',
                                                'newSetting' => 'newValue')),
                $header . "[General]\nkey = \"value\"\n\n[CommonCategory]\nnewSetting = \"newValue\"\n\n",
            )),
        );

    }

    /**
     * @group Core
     * 
     * @dataProvider getDumpConfigData
     */
    public function testDumpConfig($description, $test)
    {
        $config = Config::getInstance();

        list($configLocal, $configGlobal, $configCommon, $configCache, $expected) = $test;

        $output = $config->dumpConfig($configLocal, $configGlobal, $configCommon, $configCache);
        $this->assertEquals($expected, $output, $description);
    }
}