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.

284 lines
5.9KB

  1. <?php
  2. namespace Friendica\Test\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->assertEquals('!<unset>!', $configCache->get('something', 'value'));
  119. }
  120. /**
  121. * Test the has() method
  122. */
  123. public function testHas()
  124. {
  125. $configCache = new ConfigCache();
  126. $this->assertFalse($configCache->has('system', 'test'));
  127. $this->assertFalse($configCache->has('system'));
  128. $configCache->set('system', 'test', 'it');
  129. $this->assertTrue($configCache->has('system', 'test'));
  130. $this->assertTrue($configCache->has('system'));
  131. }
  132. /**
  133. * Test the get() method with a category
  134. */
  135. public function testGetCat()
  136. {
  137. $configCache = new ConfigCache([
  138. 'system' => [
  139. 'key1' => 'value1',
  140. 'key2' => 'value2',
  141. ],
  142. 'config' => [
  143. 'key3' => 'value3',
  144. ],
  145. ]);
  146. $this->assertTrue($configCache->has('system'));
  147. $this->assertEquals([
  148. 'key1' => 'value1',
  149. 'key2' => 'value2',
  150. ], $configCache->get('system'));
  151. }
  152. /**
  153. * Test the delete() method
  154. * @dataProvider dataTests
  155. */
  156. public function testDelete($data)
  157. {
  158. $configCache = new ConfigCache($data);
  159. foreach ($data as $cat => $values) {
  160. foreach ($values as $key => $value) {
  161. $configCache->delete($cat, $key);
  162. }
  163. }
  164. $this->assertEmpty($configCache->getAll());
  165. }
  166. /**
  167. * Test the setP() and getP() methods
  168. * @dataProvider dataTests
  169. */
  170. public function testSetGetP($data)
  171. {
  172. $configCache = new ConfigCache();
  173. $uid = 345;
  174. foreach ($data as $cat => $values) {
  175. foreach ($values as $key => $value) {
  176. $configCache->setP($uid, $cat, $key, $value);
  177. }
  178. }
  179. $this->assertConfigValues($data, $configCache, $uid);
  180. }
  181. /**
  182. * Test the getP() method with a category
  183. */
  184. public function testGetPCat()
  185. {
  186. $configCache = new ConfigCache();
  187. $uid = 345;
  188. $configCache->loadP($uid, [
  189. 'system' => [
  190. 'key1' => 'value1',
  191. 'key2' => 'value2',
  192. ],
  193. 'config' => [
  194. 'key3' => 'value3',
  195. ],
  196. ]);
  197. $this->assertTrue($configCache->hasP($uid,'system'));
  198. $this->assertEquals([
  199. 'key1' => 'value1',
  200. 'key2' => 'value2',
  201. ], $configCache->get($uid, 'system'));
  202. }
  203. /**
  204. * Test the deleteP() method
  205. * @dataProvider dataTests
  206. */
  207. public function testDeleteP($data)
  208. {
  209. $configCache = new ConfigCache();
  210. $uid = 345;
  211. foreach ($data as $cat => $values) {
  212. foreach ($values as $key => $value) {
  213. $configCache->setP($uid, $cat, $key, $value);
  214. }
  215. }
  216. foreach ($data as $cat => $values) {
  217. foreach ($values as $key => $value) {
  218. $configCache->deleteP($uid, $cat, $key);
  219. }
  220. }
  221. $this->assertEmpty($configCache->getAll());
  222. }
  223. /**
  224. * Test the hasP() method
  225. */
  226. public function testHasP()
  227. {
  228. $configCache = new ConfigCache();
  229. $uid = 345;
  230. $this->assertFalse($configCache->hasP($uid, 'system', 'test'));
  231. $this->assertFalse($configCache->hasP($uid, 'system'));
  232. $configCache->setP($uid, 'system', 'test', 'it');
  233. $this->assertTrue($configCache->hasP($uid, 'system', 'test'));
  234. $this->assertTrue($configCache->hasP($uid, 'system'));
  235. }
  236. }