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
|
<?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\Tests\Unit;
use PHPUnit\Framework\TestCase;
use Piwik\Application\Kernel\GlobalSettingsProvider;
use Piwik\Config;
class DumpConfigTestMockIniFileChain extends Config\IniFileChain
{
public function __construct($settingsChain, $mergedSettings)
{
parent::__construct();
$this->settingsChain = $settingsChain;
$this->mergedSettings = $mergedSettings;
}
}
class MockIniSettingsProvider extends GlobalSettingsProvider
{
public function __construct($configLocal, $configGlobal, $configCommon, $configCache)
{
parent::__construct();
$this->iniFileChain = new DumpConfigTestMockIniFileChain(
array(
$this->pathGlobal => $configGlobal,
$this->pathCommon => $configCommon,
$this->pathLocal => $configLocal,
),
$configCache
);
}
}
class DumpConfigTestMockConfig extends Config
{
public function __construct($configLocal, $configGlobal, $configCommon, $configCache)
{
parent::__construct(new MockIniSettingsProvider($configLocal, $configGlobal, $configCommon, $configCache));
}
}
/**
* @group Core
*/
class ConfigTest extends TestCase
{
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 = new Config(new GlobalSettingsProvider($globalFile, $userFile, $commonFile));
$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']);
}
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 = new Config(new GlobalSettingsProvider($globalFile, $userFile, $commonFile));
$this->assertEquals("valueCommon", $config->Category['key2'], var_export($config->Category['key2'], true));
$this->assertEquals("test", $config->GeneralSection['password']);
$this->assertEquals("commonValue", $config->TestOnlyInCommon['value']);
}
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 = new Config(new GlobalSettingsProvider($globalFile, $userFile));
$stringWritten = '&6^ geagea\'\'\'";;&';
$config->Category = array('test' => $stringWritten);
$this->assertEquals($stringWritten, $config->Category['test']);
// This will write the file
$config->forceSave();
$config = new Config(new GlobalSettingsProvider($globalFile, $userFile));
$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']);
}
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 = new Config(new GlobalSettingsProvider($globalFile, $userFile));
$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']);
}
/**
* Dataprovider for testDumpConfig
*/
public function getDumpConfigData()
{
$header = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n" .
"; file automatically generated or modified by Matomo; 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), 'Tracker' => array('anonymize' => 1)),
$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),
'General' => array('debug' => 1)),
$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'),
'Tracker' => array('anonymize' => 1), // local
'General' => array('debug' => 1)),
$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'),
'General' => array('key' => 'value')),
$header . "[General]\nkey = \"value\"\n\n[CommonCategory]\nnewSetting = \"newValue\"\n\n",
)),
array('Converts Dollar Sign To Dollar Entity', array(
array('General' => array('key' => '$value', 'key2' => '${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'),
'General' => array('key' => '$value', 'key2' => '${value}')),
$header . "[General]\nkey = \"$value\"\nkey2 = \"${value}\"\n\n[CommonCategory]\nnewSetting = \"newValue\"\n\n",
)),
);
}
/**
* @dataProvider getDumpConfigData
*/
public function testDumpConfig($description, $test)
{
list($configLocal, $configGlobal, $configCommon, $configCache, $expected) = $test;
$config = new DumpConfigTestMockConfig($configLocal, $configGlobal, $configCommon, $configCache);
$output = $config->dumpConfig();
$this->assertEquals($expected, $output, $description);
}
public function testDollarEntityGetsConvertedToDollarSign()
{
$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 = new Config(new GlobalSettingsProvider($globalFile, $userFile, $commonFile));
$this->assertEquals('${@piwik(crash))}', $config->Category['key3']);
}
public function test_forceSave_writesNothingIfThereAreNoChanges()
{
$sourceConfigFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/config.ini.php';
$configFile = PIWIK_INCLUDE_PATH . '/tmp/tmp.config.ini.php';
if(file_exists($configFile)){
@unlink($configFile);
}
copy($sourceConfigFile, $configFile);
$config = new Config(new GlobalSettingsProvider($sourceConfigFile, $configFile));
$config->forceSave();
$this->assertEquals(file_get_contents($sourceConfigFile), file_get_contents($configFile));
if(file_exists($configFile)){
@unlink($configFile);
}
}
public function testFromGlobalConfig()
{
$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 = new Config(new GlobalSettingsProvider($globalFile, $userFile, $commonFile));
$configCategory = $config->getFromGlobalConfig('Category');
$this->assertEquals('value1', $configCategory['key1']);
$this->assertEquals('value2', $configCategory['key2']);
$this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $configCategory);
}
public function testFromCommonConfig()
{
$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 = new Config(new GlobalSettingsProvider($globalFile, $userFile, $commonFile));
$configCategory = $config->getFromCommonConfig('Category');
$this->assertEquals(array('key2' => 'valueCommon', 'key3' => '${@piwik(crash))}'), $configCategory);
}
public function testFromLocalConfig()
{
$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 = new Config(new GlobalSettingsProvider($globalFile, $userFile, $commonFile));
$configCategory = $config->getFromLocalConfig('Category');
$this->assertEquals(array('key1' => 'value_overwritten'), $configCategory);
}
}
|