PageRenderTime 39ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/PHPUnit/Core/ConfigTest.php

https://github.com/CodeYellowBV/piwik
PHP | 440 lines | 402 code | 19 blank | 19 comment | 0 complexity | 253b0571c15589a13efb0c373f27379d MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. use Piwik\Config;
  3. /**
  4. * Piwik - free/libre analytics platform
  5. *
  6. * @link http://piwik.org
  7. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  8. */
  9. class ConfigTest extends PHPUnit_Framework_TestCase
  10. {
  11. /**
  12. * @group Core
  13. */
  14. public function testUserConfigOverwritesSectionGlobalConfigValue()
  15. {
  16. $userFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/config.ini.php';
  17. $globalFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/global.ini.php';
  18. $commonFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/common.ini.php';
  19. $config = Config::getInstance();
  20. $config->setTestEnvironment($userFile, $globalFile, $commonFile);
  21. $config->init();
  22. $this->assertEquals("value_overwritten", $config->Category['key1']);
  23. $this->assertEquals("value2", $config->Category['key2']);
  24. $this->assertEquals('tes"t', $config->GeneralSection['login']);
  25. $this->assertEquals("value3", $config->CategoryOnlyInGlobalFile['key3']);
  26. $this->assertEquals("value4", $config->CategoryOnlyInGlobalFile['key4']);
  27. $expectedArray = array('plugin"1', 'plugin2', 'plugin3');
  28. $array = $config->TestArray;
  29. $this->assertEquals($expectedArray, $array['installed']);
  30. $expectedArray = array('value1', 'value2');
  31. $array = $config->TestArrayOnlyInGlobalFile;
  32. $this->assertEquals($expectedArray, $array['my_array']);
  33. $expectedArray = array('value1', 'value2');
  34. $array = $config->TestArrayOnlyInGlobalFile;
  35. $this->assertEquals($expectedArray, $array['my_array']);
  36. }
  37. /**
  38. * @group Core
  39. */
  40. public function test_CommonConfig_Overrides()
  41. {
  42. $userFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/config.ini.php';
  43. $globalFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/global.ini.php';
  44. $commonFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/common.config.ini.php';
  45. $config = Config::getInstance();
  46. $config->setTestEnvironment($userFile, $globalFile, $commonFile);
  47. $config->init();
  48. $this->assertEquals("valueCommon", $config->Category['key2'], var_export($config->Category['key2'], true));
  49. $this->assertEquals("test", $config->GeneralSection['password']);
  50. $this->assertEquals("commonValue", $config->TestOnlyInCommon['value']);
  51. }
  52. /**
  53. * @group Core
  54. */
  55. public function testWritingConfigWithSpecialCharacters()
  56. {
  57. $userFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/config.written.ini.php';
  58. $globalFile = PIWIK_INCLUDE_PATH . '/tests/resources/Config/global.ini.php';
  59. $config = Config::getInstance();
  60. $config->setTestEnvironment($userFile, $globalFile);
  61. $config->init();
  62. $stringWritten = '&6^ geagea\'\'\'";;&';
  63. $config->Category = array('test' => $stringWritten);
  64. $this->assertEquals($stringWritten, $config->Category['test']);
  65. // This will write the file
  66. $config->forceSave();
  67. $config = Config::getInstance();
  68. $config->setTestEnvironment($userFile, $globalFile);
  69. $config->init();
  70. $this->assertEquals($stringWritten, $config->Category['test']);
  71. $config->Category = array(
  72. 'test' => $config->Category['test'],
  73. 'test2' => $stringWritten,
  74. );
  75. $this->assertEquals($stringWritten, $config->Category['test']);
  76. $this->assertEquals($stringWritten, $config->Category['test2']);
  77. }
  78. /**
  79. * @group Core
  80. */
  81. public function testUserConfigOverwritesGlobalConfig()
  82. {
  83. $userFile = PIWIK_PATH_TEST_TO_ROOT . '/tests/resources/Config/config.ini.php';
  84. $globalFile = PIWIK_PATH_TEST_TO_ROOT . '/tests/resources/Config/global.ini.php';
  85. $config = Config::getInstance();
  86. $config->setTestEnvironment($userFile, $globalFile);
  87. $this->assertEquals("value_overwritten", $config->Category['key1']);
  88. $this->assertEquals("value2", $config->Category['key2']);
  89. $this->assertEquals("tes\"t", $config->GeneralSection['login']);
  90. $this->assertEquals("value3", $config->CategoryOnlyInGlobalFile['key3']);
  91. $this->assertEquals("value4", $config->CategoryOnlyInGlobalFile['key4']);
  92. $expectedArray = array('plugin"1', 'plugin2', 'plugin3');
  93. $array = $config->TestArray;
  94. $this->assertEquals($expectedArray, $array['installed']);
  95. $expectedArray = array('value1', 'value2');
  96. $array = $config->TestArrayOnlyInGlobalFile;
  97. $this->assertEquals($expectedArray, $array['my_array']);
  98. Config::getInstance()->clear();
  99. }
  100. /**
  101. * Dateprovider for testCompareElements
  102. */
  103. public function getCompareElementsData()
  104. {
  105. return array(
  106. array('string = string', array(
  107. 'a', 'a', 0,
  108. )),
  109. array('string > string', array(
  110. 'b', 'a', 1,
  111. )),
  112. array('string < string', array(
  113. 'a', 'b', -1,
  114. )),
  115. array('string vs array', array(
  116. 'a', array('a'), -1,
  117. )),
  118. array('array vs string', array(
  119. array('a'), 'a', 1,
  120. )),
  121. array('array = array', array(
  122. array('a'), array('a'), 0,
  123. )),
  124. array('array > array', array(
  125. array('b'), array('a'), 1,
  126. )),
  127. array('array < array', array(
  128. array('a'), array('b'), -1,
  129. )),
  130. );
  131. }
  132. /**
  133. * @group Core
  134. *
  135. * @dataProvider getCompareElementsData
  136. */
  137. public function testCompareElements($description, $test)
  138. {
  139. list($a, $b, $expected) = $test;
  140. $result = Config::compareElements($a, $b);
  141. $this->assertEquals($expected, $result, $description);
  142. }
  143. /**
  144. * Dataprovider for testArrayUnmerge
  145. * @return array
  146. */
  147. public function getArrayUnmergeData()
  148. {
  149. return array(
  150. array('description of test', array(
  151. array(),
  152. array(),
  153. )),
  154. array('override with empty', array(
  155. array('login' => 'root', 'password' => 'b33r'),
  156. array('password' => ''),
  157. )),
  158. array('override with non-empty', array(
  159. array('login' => 'root', 'password' => ''),
  160. array('password' => 'b33r'),
  161. )),
  162. array('add element', array(
  163. array('login' => 'root', 'password' => ''),
  164. array('auth' => 'Login'),
  165. )),
  166. array('override with empty array', array(
  167. array('headers' => ''),
  168. array('headers' => array()),
  169. )),
  170. array('override with array', array(
  171. array('headers' => ''),
  172. array('headers' => array('Content-Length', 'Content-Type')),
  173. )),
  174. array('override an array', array(
  175. array('headers' => array()),
  176. array('headers' => array('Content-Length', 'Content-Type')),
  177. )),
  178. array('override similar arrays', array(
  179. array('headers' => array('Content-Length', 'Set-Cookie')),
  180. array('headers' => array('Content-Length', 'Content-Type')),
  181. )),
  182. array('override dyslexic arrays', array(
  183. array('headers' => array('Content-Type', 'Content-Length')),
  184. array('headers' => array('Content-Length', 'Content-Type')),
  185. )),
  186. );
  187. }
  188. /**
  189. * @group Core
  190. *
  191. * @dataProvider getArrayUnmergeData
  192. */
  193. public function testArrayUnmerge($description, $test)
  194. {
  195. $configWriter = Config::getInstance();
  196. list($a, $b) = $test;
  197. $combined = array_merge($a, $b);
  198. $diff = $configWriter->array_unmerge($a, $combined);
  199. // expect $b == $diff
  200. $this->assertEquals(serialize($b), serialize($diff), $description);
  201. }
  202. /**
  203. * Dataprovider for testDumpConfig
  204. */
  205. public function getDumpConfigData()
  206. {
  207. $header = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n" .
  208. "; 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";
  209. return array(
  210. // Test name, array(
  211. // LOCAL
  212. // GLOBAL
  213. // COMMON
  214. // CACHE
  215. // --> EXPECTED <--
  216. array('global only, not cached', array(
  217. array(), // local
  218. array('General' => array('debug' => '1')), // global
  219. array(), // common
  220. array(),
  221. false,
  222. )),
  223. array('global only, cached get', array(
  224. array(), // local
  225. array('General' => array('debug' => '1')), // global
  226. array(), // common
  227. array('General' => array('debug' => '1')),
  228. false,
  229. )),
  230. array('global only, cached set', array(
  231. array(), // local
  232. array('General' => array('debug' => '1')), // global
  233. array(), // common
  234. array('General' => array('debug' => '2')),
  235. $header . "[General]\ndebug = 2\n\n",
  236. )),
  237. array('local copy (same), not cached', array(
  238. array('General' => array('debug' => '1')), // local
  239. array('General' => array('debug' => '1')), // global
  240. array(), // common
  241. array(),
  242. false,
  243. )),
  244. array('local copy (same), cached get', array(
  245. array('General' => array('debug' => '1')), // local
  246. array('General' => array('debug' => '1')), // global
  247. array(), // common
  248. array('General' => array('debug' => '1')),
  249. false,
  250. )),
  251. array('local copy (same), cached set', array(
  252. array('General' => array('debug' => '1')), // local
  253. array('General' => array('debug' => '1')), // global
  254. array(), // common
  255. array('General' => array('debug' => '2')),
  256. $header . "[General]\ndebug = 2\n\n",
  257. )),
  258. array('local copy (different), not cached', array(
  259. array('General' => array('debug' => '2')), // local
  260. array('General' => array('debug' => '1')), // global
  261. array(), // common
  262. array(),
  263. false,
  264. )),
  265. array('local copy (different), cached get', array(
  266. array('General' => array('debug' => '2')), // local
  267. array('General' => array('debug' => '1')), // global
  268. array(), // common
  269. array('General' => array('debug' => '2')),
  270. false,
  271. )),
  272. array('local copy (different), cached set', array(
  273. array('General' => array('debug' => '2')), // local
  274. array('General' => array('debug' => '1')), // global
  275. array(), // common
  276. array('General' => array('debug' => '3')),
  277. $header . "[General]\ndebug = 3\n\n",
  278. )),
  279. array('local copy, not cached, new section', array(
  280. array('Tracker' => array('anonymize' => '1')), // local
  281. array('General' => array('debug' => '1')), // global
  282. array(), // common
  283. array(),
  284. false,
  285. )),
  286. array('local copy, cached get, new section', array(
  287. array('Tracker' => array('anonymize' => '1')), // local
  288. array('General' => array('debug' => '1')), // global
  289. array(), // common
  290. array('Tracker' => array('anonymize' => '1')),
  291. false,
  292. )),
  293. array('local copy, cached set local, new section', array(
  294. array('Tracker' => array('anonymize' => '1')), // local
  295. array('General' => array('debug' => '1')), // global
  296. array(), // common
  297. array('Tracker' => array('anonymize' => '2')),
  298. $header . "[Tracker]\nanonymize = 2\n\n",
  299. )),
  300. array('local copy, cached set global, new section', array(
  301. array('Tracker' => array('anonymize' => '1')), // local
  302. array('General' => array('debug' => '1')), // global
  303. array(), // common
  304. array('General' => array('debug' => '2')),
  305. $header . "[General]\ndebug = 2\n\n[Tracker]\nanonymize = 1\n\n",
  306. )),
  307. array('sort, common sections', array(
  308. array('Tracker' => array('anonymize' => '1'), // local
  309. 'General' => array('debug' => '1')),
  310. array('General' => array('debug' => '0'), // global
  311. 'Tracker' => array('anonymize' => '0')),
  312. array(), // common
  313. array('Tracker' => array('anonymize' => '2')),
  314. $header . "[General]\ndebug = 1\n\n[Tracker]\nanonymize = 2\n\n",
  315. )),
  316. array('sort, common sections before new section', array(
  317. array('Tracker' => array('anonymize' => '1'), // local
  318. 'General' => array('debug' => '1')),
  319. array('General' => array('debug' => '0'), // global
  320. 'Tracker' => array('anonymize' => '0')),
  321. array(), // common
  322. array('Segment' => array('dimension' => 'foo')),
  323. $header . "[General]\ndebug = 1\n\n[Tracker]\nanonymize = 1\n\n[Segment]\ndimension = \"foo\"\n\n",
  324. )),
  325. array('change back to default', array(
  326. array('Tracker' => array('anonymize' => '1')), // local
  327. array('Tracker' => array('anonymize' => '0'), // global
  328. 'General' => array('debug' => '1')),
  329. array(), // common
  330. array('Tracker' => array('anonymize' => '0')),
  331. $header
  332. )),
  333. array('[General] trusted_hosts has been updated and only this one is written', array(
  334. array('General' => array('trusted_hosts' => 'someRandomHostToOverwrite')), // local
  335. array('General' => array('settingGlobal' => 'global', // global
  336. 'settingCommon' => 'global',
  337. 'trusted_hosts' => 'none')),
  338. array('General' => array('settingCommon' => 'common', // common
  339. 'settingCommon2' => 'common')),
  340. array('General' => array('trusted_hosts' => 'works')),
  341. $header . "[General]\ntrusted_hosts = \"works\"\n\n",
  342. )),
  343. // Same as above but without trusted_hosts default value in global.ini.php
  344. // Also, settingCommon3 is the same in the local file as in common, so it is not written out
  345. array('trusted_hosts and settingCommon3 changed ', array(
  346. array('General' => array('trusted_hosts' => 'someRandomHostToOverwrite')), // local
  347. array('General' => array('settingGlobal' => 'global', // global
  348. 'settingCommon' => 'global')),
  349. array('General' => array('settingCommon' => 'common', // common
  350. 'settingCommon2' => 'common',
  351. 'settingCommon3' => 'common3')),
  352. array('General' => array('trusted_hosts' => 'works', // common
  353. 'settingCommon2' => 'common', // will be cleared since it's same as in common
  354. 'settingCommon3' => 'commonOverridenByLocal')),
  355. $header . "[General]\ntrusted_hosts = \"works\"\nsettingCommon3 = \"commonOverridenByLocal\"\n\n",
  356. )),
  357. // the value in [General]->key has changed
  358. // the value in [CommonCategory]->newSetting has changed,
  359. // but [CommonCategory]->settingCommon2 hasn't so it is not written
  360. array('Common tests file', array(
  361. array('General' => array('key' => 'value')), // local
  362. array('General' => array('key' => 'global'), // global
  363. 'CommonCategory' => array('settingGlobal' => 'valueGlobal')),
  364. array('CommonCategory' => array('settingCommon' => 'common', // common
  365. 'settingCommon2' => 'common2')),
  366. array('CommonCategory' => array('settingCommon2' => 'common2',
  367. 'newSetting' => 'newValue')),
  368. $header . "[General]\nkey = \"value\"\n\n[CommonCategory]\nnewSetting = \"newValue\"\n\n",
  369. )),
  370. );
  371. }
  372. /**
  373. * @group Core
  374. *
  375. * @dataProvider getDumpConfigData
  376. */
  377. public function testDumpConfig($description, $test)
  378. {
  379. $config = Config::getInstance();
  380. list($configLocal, $configGlobal, $configCommon, $configCache, $expected) = $test;
  381. $output = $config->dumpConfig($configLocal, $configGlobal, $configCommon, $configCache);
  382. $this->assertEquals($expected, $output, $description);
  383. }
  384. }