/tests/src/Core/Config/Cache/CacheTest.php

https://github.com/friendica/friendica · PHP · 345 lines · 211 code · 54 blank · 80 comment · 0 complexity · 6e6c5a5ceb64af0dca353ba54caae453 MD5 · raw file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2010-2022, the Friendica project
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Friendica\Test\src\Core\Config\Cache;
  22. use Friendica\Core\Config\Cache;
  23. use Friendica\Test\MockedTest;
  24. use ParagonIE\HiddenString\HiddenString;
  25. use stdClass;
  26. class CacheTest extends MockedTest
  27. {
  28. public function dataTests()
  29. {
  30. return [
  31. 'normal' => [
  32. 'data' => [
  33. 'system' => [
  34. 'test' => 'it',
  35. 'boolTrue' => true,
  36. 'boolFalse' => false,
  37. 'int' => 235,
  38. 'dec' => 2.456,
  39. 'array' => ['1', 2, '3', true, false],
  40. ],
  41. 'config' => [
  42. 'a' => 'value',
  43. ],
  44. ]
  45. ]
  46. ];
  47. }
  48. private function assertConfigValues($data, \Friendica\Core\Config\ValueObject\Cache $configCache)
  49. {
  50. foreach ($data as $cat => $values) {
  51. foreach ($values as $key => $value) {
  52. self::assertEquals($data[$cat][$key], $configCache->get($cat, $key));
  53. }
  54. }
  55. }
  56. /**
  57. * Test the loadConfigArray() method without override
  58. * @dataProvider dataTests
  59. */
  60. public function testLoadConfigArray($data)
  61. {
  62. $configCache = new \Friendica\Core\Config\ValueObject\Cache();
  63. $configCache->load($data);
  64. self::assertConfigValues($data, $configCache);
  65. }
  66. /**
  67. * Test the loadConfigArray() method with overrides
  68. * @dataProvider dataTests
  69. */
  70. public function testLoadConfigArrayOverride($data)
  71. {
  72. $override = [
  73. 'system' => [
  74. 'test' => 'not',
  75. 'boolTrue' => false,
  76. ]
  77. ];
  78. $configCache = new \Friendica\Core\Config\ValueObject\Cache();
  79. $configCache->load($data, \Friendica\Core\Config\ValueObject\Cache::SOURCE_DB);
  80. // doesn't override - Low Priority due Config file
  81. $configCache->load($override, \Friendica\Core\Config\ValueObject\Cache::SOURCE_FILE);
  82. self::assertConfigValues($data, $configCache);
  83. // override the value - High Prio due Server Env
  84. $configCache->load($override, \Friendica\Core\Config\ValueObject\Cache::SOURCE_ENV);
  85. self::assertEquals($override['system']['test'], $configCache->get('system', 'test'));
  86. self::assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
  87. // Don't overwrite server ENV variables - even in load mode
  88. $configCache->load($data, \Friendica\Core\Config\ValueObject\Cache::SOURCE_DB);
  89. self::assertEquals($override['system']['test'], $configCache->get('system', 'test'));
  90. self::assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
  91. // Overwrite ENV variables with ENV variables
  92. $configCache->load($data, \Friendica\Core\Config\ValueObject\Cache::SOURCE_ENV);
  93. self::assertConfigValues($data, $configCache);
  94. self::assertNotEquals($override['system']['test'], $configCache->get('system', 'test'));
  95. self::assertNotEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
  96. }
  97. /**
  98. * Test the loadConfigArray() method with wrong/empty datasets
  99. */
  100. public function testLoadConfigArrayWrong()
  101. {
  102. $configCache = new \Friendica\Core\Config\ValueObject\Cache();
  103. // empty dataset
  104. $configCache->load([]);
  105. self::assertEmpty($configCache->getAll());
  106. // wrong dataset
  107. $configCache->load(['system' => 'not_array']);
  108. self::assertEmpty($configCache->getAll());
  109. // incomplete dataset (key is integer ID of the array)
  110. $configCache->load(['system' => ['value']]);
  111. self::assertEquals('value', $configCache->get('system', 0));
  112. }
  113. /**
  114. * Test the getAll() method
  115. * @dataProvider dataTests
  116. */
  117. public function testGetAll($data)
  118. {
  119. $configCache = new \Friendica\Core\Config\ValueObject\Cache();
  120. $configCache->load($data);
  121. $all = $configCache->getAll();
  122. self::assertContains($data['system'], $all);
  123. self::assertContains($data['config'], $all);
  124. }
  125. /**
  126. * Test the set() and get() method
  127. * @dataProvider dataTests
  128. */
  129. public function testSetGet($data)
  130. {
  131. $configCache = new \Friendica\Core\Config\ValueObject\Cache();
  132. foreach ($data as $cat => $values) {
  133. foreach ($values as $key => $value) {
  134. $configCache->set($cat, $key, $value);
  135. }
  136. }
  137. self::assertConfigValues($data, $configCache);
  138. }
  139. /**
  140. * Test the get() method without a value
  141. */
  142. public function testGetEmpty()
  143. {
  144. $configCache = new \Friendica\Core\Config\ValueObject\Cache();
  145. self::assertNull($configCache->get('something', 'value'));
  146. }
  147. /**
  148. * Test the get() method with a category
  149. */
  150. public function testGetCat()
  151. {
  152. $configCache = new \Friendica\Core\Config\ValueObject\Cache([
  153. 'system' => [
  154. 'key1' => 'value1',
  155. 'key2' => 'value2',
  156. ],
  157. 'config' => [
  158. 'key3' => 'value3',
  159. ],
  160. ]);
  161. self::assertEquals([
  162. 'key1' => 'value1',
  163. 'key2' => 'value2',
  164. ], $configCache->get('system'));
  165. // explicit null as key
  166. self::assertEquals([
  167. 'key1' => 'value1',
  168. 'key2' => 'value2',
  169. ], $configCache->get('system', null));
  170. }
  171. /**
  172. * Test the delete() method
  173. * @dataProvider dataTests
  174. */
  175. public function testDelete($data)
  176. {
  177. $configCache = new \Friendica\Core\Config\ValueObject\Cache($data);
  178. foreach ($data as $cat => $values) {
  179. foreach ($values as $key => $value) {
  180. $configCache->delete($cat, $key);
  181. }
  182. }
  183. self::assertEmpty($configCache->getAll());
  184. }
  185. /**
  186. * Test the keyDiff() method with result
  187. * @dataProvider dataTests
  188. */
  189. public function testKeyDiffWithResult($data)
  190. {
  191. $configCache = new \Friendica\Core\Config\ValueObject\Cache($data);
  192. $diffConfig = [
  193. 'fakeCat' => [
  194. 'fakeKey' => 'value',
  195. ]
  196. ];
  197. self::assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
  198. }
  199. /**
  200. * Test the keyDiff() method without result
  201. * @dataProvider dataTests
  202. */
  203. public function testKeyDiffWithoutResult($data)
  204. {
  205. $configCache = new \Friendica\Core\Config\ValueObject\Cache($data);
  206. $diffConfig = $configCache->getAll();
  207. self::assertEmpty($configCache->keyDiff($diffConfig));
  208. }
  209. /**
  210. * Test the default hiding of passwords inside the cache
  211. */
  212. public function testPasswordHide()
  213. {
  214. $configCache = new \Friendica\Core\Config\ValueObject\Cache([
  215. 'database' => [
  216. 'password' => 'supersecure',
  217. 'username' => 'notsecured',
  218. ],
  219. ]);
  220. self::assertEquals('supersecure', $configCache->get('database', 'password'));
  221. self::assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
  222. self::assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
  223. }
  224. /**
  225. * Test disabling the hiding of passwords inside the cache
  226. */
  227. public function testPasswordShow()
  228. {
  229. $configCache = new \Friendica\Core\Config\ValueObject\Cache([
  230. 'database' => [
  231. 'password' => 'supersecure',
  232. 'username' => 'notsecured',
  233. ],
  234. ], false);
  235. self::assertEquals('supersecure', $configCache->get('database', 'password'));
  236. self::assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
  237. self::assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
  238. }
  239. /**
  240. * Test a empty password
  241. */
  242. public function testEmptyPassword()
  243. {
  244. $configCache = new \Friendica\Core\Config\ValueObject\Cache([
  245. 'database' => [
  246. 'password' => '',
  247. 'username' => '',
  248. ]
  249. ]);
  250. self::assertNotEmpty($configCache->get('database', 'password'));
  251. self::assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
  252. self::assertEmpty($configCache->get('database', 'username'));
  253. }
  254. public function testWrongTypePassword()
  255. {
  256. $configCache = new \Friendica\Core\Config\ValueObject\Cache([
  257. 'database' => [
  258. 'password' => new stdClass(),
  259. 'username' => '',
  260. ]
  261. ]);
  262. self::assertNotEmpty($configCache->get('database', 'password'));
  263. self::assertEmpty($configCache->get('database', 'username'));
  264. $configCache = new \Friendica\Core\Config\ValueObject\Cache([
  265. 'database' => [
  266. 'password' => 23,
  267. 'username' => '',
  268. ]
  269. ]);
  270. self::assertEquals(23, $configCache->get('database', 'password'));
  271. self::assertEmpty($configCache->get('database', 'username'));
  272. }
  273. /**
  274. * Test the set() method with overrides
  275. * @dataProvider dataTests
  276. */
  277. public function testSetOverrides($data)
  278. {
  279. $configCache = new \Friendica\Core\Config\ValueObject\Cache();
  280. $configCache->load($data, \Friendica\Core\Config\ValueObject\Cache::SOURCE_DB);
  281. // test with wrong override
  282. self::assertFalse($configCache->set('system', 'test', '1234567', \Friendica\Core\Config\ValueObject\Cache::SOURCE_FILE));
  283. self::assertEquals($data['system']['test'], $configCache->get('system', 'test'));
  284. // test with override (equal)
  285. self::assertTrue($configCache->set('system', 'test', '8910', \Friendica\Core\Config\ValueObject\Cache::SOURCE_DB));
  286. self::assertEquals('8910', $configCache->get('system', 'test'));
  287. // test with override (over)
  288. self::assertTrue($configCache->set('system', 'test', '111213', \Friendica\Core\Config\ValueObject\Cache::SOURCE_ENV));
  289. self::assertEquals('111213', $configCache->get('system', 'test'));
  290. }
  291. }