3 namespace Friendica\Test\src\Core\Config\Cache;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Test\MockedTest;
7 use ParagonIE\HiddenString\HiddenString;
9 class ConfigCacheTest extends MockedTest
11 public function dataTests()
22 'array' => ['1', 2, '3', true, false],
32 private function assertConfigValues($data, ConfigCache $configCache, $uid = null)
34 foreach ($data as $cat => $values) {
35 foreach ($values as $key => $value) {
37 $this->assertEquals($data[$cat][$key], $configCache->getP($uid, $cat, $key));
39 $this->assertEquals($data[$cat][$key], $configCache->get($cat, $key));
46 * Test the loadConfigArray() method without override
47 * @dataProvider dataTests
49 public function testLoadConfigArray($data)
51 $configCache = new ConfigCache();
52 $configCache->load($data);
54 $this->assertConfigValues($data, $configCache);
58 * Test the loadConfigArray() method with overrides
59 * @dataProvider dataTests
61 public function testLoadConfigArrayOverride($data)
70 $configCache = new ConfigCache();
71 $configCache->load($data);
72 $configCache->load($override);
74 $this->assertConfigValues($data, $configCache);
77 $configCache->load($override, true);
79 $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
80 $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
84 * Test the loadConfigArray() method with wrong/empty datasets
86 public function testLoadConfigArrayWrong()
88 $configCache = new ConfigCache();
91 $configCache->load([]);
92 $this->assertEmpty($configCache->getAll());
95 $configCache->load(['system' => 'not_array']);
96 $this->assertEmpty($configCache->getAll());
98 // incomplete dataset (key is integer ID of the array)
99 $configCache->load(['system' => ['value']]);
100 $this->assertEquals('value', $configCache->get('system', 0));
104 * Test the getAll() method
105 * @dataProvider dataTests
107 public function testGetAll($data)
109 $configCache = new ConfigCache();
110 $configCache->load($data);
112 $all = $configCache->getAll();
114 $this->assertContains($data['system'], $all);
115 $this->assertContains($data['config'], $all);
119 * Test the set() and get() method
120 * @dataProvider dataTests
122 public function testSetGet($data)
124 $configCache = new ConfigCache();
126 foreach ($data as $cat => $values) {
127 foreach ($values as $key => $value) {
128 $configCache->set($cat, $key, $value);
132 $this->assertConfigValues($data, $configCache);
136 * Test the get() method without a value
138 public function testGetEmpty()
140 $configCache = new ConfigCache();
142 $this->assertNull($configCache->get('something', 'value'));
146 * Test the get() method with a category
148 public function testGetCat()
150 $configCache = new ConfigCache([
160 $this->assertEquals([
163 ], $configCache->get('system'));
167 * Test the delete() method
168 * @dataProvider dataTests
170 public function testDelete($data)
172 $configCache = new ConfigCache($data);
174 foreach ($data as $cat => $values) {
175 foreach ($values as $key => $value) {
176 $configCache->delete($cat, $key);
180 $this->assertEmpty($configCache->getAll());
184 * Test the setP() and getP() methods
185 * @dataProvider dataTests
187 public function testSetGetP($data)
189 $configCache = new ConfigCache();
192 foreach ($data as $cat => $values) {
193 foreach ($values as $key => $value) {
194 $configCache->setP($uid, $cat, $key, $value);
198 $this->assertConfigValues($data, $configCache, $uid);
203 * Test the getP() method with a category
205 public function testGetPCat()
207 $configCache = new ConfigCache();
210 $configCache->loadP($uid, [
220 $this->assertEquals([
223 ], $configCache->get($uid, 'system'));
227 * Test the deleteP() method
228 * @dataProvider dataTests
230 public function testDeleteP($data)
232 $configCache = new ConfigCache();
235 foreach ($data as $cat => $values) {
236 foreach ($values as $key => $value) {
237 $configCache->setP($uid, $cat, $key, $value);
241 foreach ($data as $cat => $values) {
242 foreach ($values as $key => $value) {
243 $configCache->deleteP($uid, $cat, $key);
247 $this->assertEmpty($configCache->getAll());
251 * Test the keyDiff() method with result
252 * @dataProvider dataTests
254 public function testKeyDiffWithResult($data)
256 $configCache = new ConfigCache($data);
260 'fakeKey' => 'value',
264 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
268 * Test the keyDiff() method without result
269 * @dataProvider dataTests
271 public function testKeyDiffWithoutResult($data)
273 $configCache = new ConfigCache($data);
275 $diffConfig = $configCache->getAll();
277 $this->assertEmpty($configCache->keyDiff($diffConfig));
281 * Test the default hiding of passwords inside the cache
283 public function testPasswordHide()
285 $configCache = new ConfigCache([
287 'password' => 'supersecure',
288 'username' => 'notsecured',
292 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
293 $this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
294 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
298 * Test disabling the hiding of passwords inside the cache
300 public function testPasswordShow()
302 $configCache = new ConfigCache([
304 'password' => 'supersecure',
305 'username' => 'notsecured',
309 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
310 $this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
311 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
315 * Test a empty password
317 public function testEmptyPassword()
319 $configCache = new ConfigCache([
326 $this->assertNotEmpty($configCache->get('database', 'password'));
327 $this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
328 $this->assertEmpty($configCache->get('database', 'username'));
331 public function testWrongTypePassword()
333 $configCache = new ConfigCache([
335 'password' => new \stdClass(),
340 $this->assertNotEmpty($configCache->get('database', 'password'));
341 $this->assertEmpty($configCache->get('database', 'username'));
343 $configCache = new ConfigCache([
350 $this->assertEquals(23, $configCache->get('database', 'password'));
351 $this->assertEmpty($configCache->get('database', 'username'));