Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigCacheTest.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. namespace Friendica\Test\src\Core\Config\Cache;
  3. use Friendica\Core\Config\Cache\ConfigCache;
  4. use Friendica\Test\MockedTest;
  5. use ParagonIE\HiddenString\HiddenString;
  6. class ConfigCacheTest extends MockedTest
  7. {
  8. public function dataTests()
  9. {
  10. return [
  11. 'normal' => [
  12. 'data' => [
  13. 'system' => [
  14. 'test' => 'it',
  15. 'boolTrue' => true,
  16. 'boolFalse' => false,
  17. 'int' => 235,
  18. 'dec' => 2.456,
  19. 'array' => ['1', 2, '3', true, false],
  20. ],
  21. 'config' => [
  22. 'a' => 'value',
  23. ],
  24. ]
  25. ]
  26. ];
  27. }
  28. private function assertConfigValues($data, ConfigCache $configCache, $uid = null)
  29. {
  30. foreach ($data as $cat => $values) {
  31. foreach ($values as $key => $value) {
  32. if (isset($uid)) {
  33. $this->assertEquals($data[$cat][$key], $configCache->getP($uid, $cat, $key));
  34. } else {
  35. $this->assertEquals($data[$cat][$key], $configCache->get($cat, $key));
  36. }
  37. }
  38. }
  39. }
  40. /**
  41. * Test the loadConfigArray() method without override
  42. * @dataProvider dataTests
  43. */
  44. public function testLoadConfigArray($data)
  45. {
  46. $configCache = new ConfigCache();
  47. $configCache->load($data);
  48. $this->assertConfigValues($data, $configCache);
  49. }
  50. /**
  51. * Test the loadConfigArray() method with overrides
  52. * @dataProvider dataTests
  53. */
  54. public function testLoadConfigArrayOverride($data)
  55. {
  56. $override = [
  57. 'system' => [
  58. 'test' => 'not',
  59. 'boolTrue' => false,
  60. ]
  61. ];
  62. $configCache = new ConfigCache();
  63. $configCache->load($data);
  64. $configCache->load($override);
  65. $this->assertConfigValues($data, $configCache);
  66. // override the value
  67. $configCache->load($override, true);
  68. $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
  69. $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
  70. }
  71. /**
  72. * Test the loadConfigArray() method with wrong/empty datasets
  73. */
  74. public function testLoadConfigArrayWrong()
  75. {
  76. $configCache = new ConfigCache();
  77. // empty dataset
  78. $configCache->load([]);
  79. $this->assertEmpty($configCache->getAll());
  80. // wrong dataset
  81. $configCache->load(['system' => 'not_array']);
  82. $this->assertEmpty($configCache->getAll());
  83. // incomplete dataset (key is integer ID of the array)
  84. $configCache->load(['system' => ['value']]);
  85. $this->assertEquals('value', $configCache->get('system', 0));
  86. }
  87. /**
  88. * Test the getAll() method
  89. * @dataProvider dataTests
  90. */
  91. public function testGetAll($data)
  92. {
  93. $configCache = new ConfigCache();
  94. $configCache->load($data);
  95. $all = $configCache->getAll();
  96. $this->assertContains($data['system'], $all);
  97. $this->assertContains($data['config'], $all);
  98. }
  99. /**
  100. * Test the set() and get() method
  101. * @dataProvider dataTests
  102. */
  103. public function testSetGet($data)
  104. {
  105. $configCache = new ConfigCache();
  106. foreach ($data as $cat => $values) {
  107. foreach ($values as $key => $value) {
  108. $configCache->set($cat, $key, $value);
  109. }
  110. }
  111. $this->assertConfigValues($data, $configCache);
  112. }
  113. /**
  114. * Test the get() method without a value
  115. */
  116. public function testGetEmpty()
  117. {
  118. $configCache = new ConfigCache();
  119. $this->assertNull($configCache->get('something', 'value'));
  120. }
  121. /**
  122. * Test the get() method with a category
  123. */
  124. public function testGetCat()
  125. {
  126. $configCache = new ConfigCache([
  127. 'system' => [
  128. 'key1' => 'value1',
  129. 'key2' => 'value2',
  130. ],
  131. 'config' => [
  132. 'key3' => 'value3',
  133. ],
  134. ]);
  135. $this->assertEquals([
  136. 'key1' => 'value1',
  137. 'key2' => 'value2',
  138. ], $configCache->get('system'));
  139. }
  140. /**
  141. * Test the delete() method
  142. * @dataProvider dataTests
  143. */
  144. public function testDelete($data)
  145. {
  146. $configCache = new ConfigCache($data);
  147. foreach ($data as $cat => $values) {
  148. foreach ($values as $key => $value) {
  149. $configCache->delete($cat, $key);
  150. }
  151. }
  152. $this->assertEmpty($configCache->getAll());
  153. }
  154. /**
  155. * Test the setP() and getP() methods
  156. * @dataProvider dataTests
  157. */
  158. public function testSetGetP($data)
  159. {
  160. $configCache = new ConfigCache();
  161. $uid = 345;
  162. foreach ($data as $cat => $values) {
  163. foreach ($values as $key => $value) {
  164. $configCache->setP($uid, $cat, $key, $value);
  165. }
  166. }
  167. $this->assertConfigValues($data, $configCache, $uid);
  168. }
  169. /**
  170. * Test the getP() method with a category
  171. */
  172. public function testGetPCat()
  173. {
  174. $configCache = new ConfigCache();
  175. $uid = 345;
  176. $configCache->loadP($uid, [
  177. 'system' => [
  178. 'key1' => 'value1',
  179. 'key2' => 'value2',
  180. ],
  181. 'config' => [
  182. 'key3' => 'value3',
  183. ],
  184. ]);
  185. $this->assertEquals([
  186. 'key1' => 'value1',
  187. 'key2' => 'value2',
  188. ], $configCache->get($uid, 'system'));
  189. }
  190. /**
  191. * Test the deleteP() method
  192. * @dataProvider dataTests
  193. */
  194. public function testDeleteP($data)
  195. {
  196. $configCache = new ConfigCache();
  197. $uid = 345;
  198. foreach ($data as $cat => $values) {
  199. foreach ($values as $key => $value) {
  200. $configCache->setP($uid, $cat, $key, $value);
  201. }
  202. }
  203. foreach ($data as $cat => $values) {
  204. foreach ($values as $key => $value) {
  205. $configCache->deleteP($uid, $cat, $key);
  206. }
  207. }
  208. $this->assertEmpty($configCache->getAll());
  209. }
  210. /**
  211. * Test the keyDiff() method with result
  212. * @dataProvider dataTests
  213. */
  214. public function testKeyDiffWithResult($data)
  215. {
  216. $configCache = new ConfigCache($data);
  217. $diffConfig = [
  218. 'fakeCat' => [
  219. 'fakeKey' => 'value',
  220. ]
  221. ];
  222. $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
  223. }
  224. /**
  225. * Test the keyDiff() method without result
  226. * @dataProvider dataTests
  227. */
  228. public function testKeyDiffWithoutResult($data)
  229. {
  230. $configCache = new ConfigCache($data);
  231. $diffConfig = $configCache->getAll();
  232. $this->assertEmpty($configCache->keyDiff($diffConfig));
  233. }
  234. /**
  235. * Test the default hiding of passwords inside the cache
  236. */
  237. public function testPasswordHide()
  238. {
  239. $configCache = new ConfigCache([
  240. 'database' => [
  241. 'password' => 'supersecure',
  242. 'username' => 'notsecured',
  243. ],
  244. ]);
  245. $this->assertEquals('supersecure', $configCache->get('database', 'password'));
  246. $this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
  247. $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
  248. }
  249. /**
  250. * Test disabling the hiding of passwords inside the cache
  251. */
  252. public function testPasswordShow()
  253. {
  254. $configCache = new ConfigCache([
  255. 'database' => [
  256. 'password' => 'supersecure',
  257. 'username' => 'notsecured',
  258. ],
  259. ], false);
  260. $this->assertEquals('supersecure', $configCache->get('database', 'password'));
  261. $this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
  262. $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
  263. }
  264. /**
  265. * Test a empty password
  266. */
  267. public function testEmptyPassword()
  268. {
  269. $configCache = new ConfigCache([
  270. 'database' => [
  271. 'password' => '',
  272. 'username' => '',
  273. ]
  274. ]);
  275. $this->assertNotEmpty($configCache->get('database', 'password'));
  276. $this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
  277. $this->assertEmpty($configCache->get('database', 'username'));
  278. }
  279. public function testWrongTypePassword()
  280. {
  281. $configCache = new ConfigCache([
  282. 'database' => [
  283. 'password' => new \stdClass(),
  284. 'username' => '',
  285. ]
  286. ]);
  287. $this->assertNotEmpty($configCache->get('database', 'password'));
  288. $this->assertEmpty($configCache->get('database', 'username'));
  289. $configCache = new ConfigCache([
  290. 'database' => [
  291. 'password' => 23,
  292. 'username' => '',
  293. ]
  294. ]);
  295. $this->assertEquals(23, $configCache->get('database', 'password'));
  296. $this->assertEmpty($configCache->get('database', 'username'));
  297. }
  298. }