Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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 6.6KB

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