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)
34 foreach ($data as $cat => $values) {
35 foreach ($values as $key => $value) {
36 $this->assertEquals($data[$cat][$key], $configCache->get($cat, $key));
42 * Test the loadConfigArray() method without override
43 * @dataProvider dataTests
45 public function testLoadConfigArray($data)
47 $configCache = new ConfigCache();
48 $configCache->load($data);
50 $this->assertConfigValues($data, $configCache);
54 * Test the loadConfigArray() method with overrides
55 * @dataProvider dataTests
57 public function testLoadConfigArrayOverride($data)
66 $configCache = new ConfigCache();
67 $configCache->load($data);
68 $configCache->load($override);
70 $this->assertConfigValues($data, $configCache);
73 $configCache->load($override, true);
75 $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
76 $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
80 * Test the loadConfigArray() method with wrong/empty datasets
82 public function testLoadConfigArrayWrong()
84 $configCache = new ConfigCache();
87 $configCache->load([]);
88 $this->assertEmpty($configCache->getAll());
91 $configCache->load(['system' => 'not_array']);
92 $this->assertEmpty($configCache->getAll());
94 // incomplete dataset (key is integer ID of the array)
95 $configCache->load(['system' => ['value']]);
96 $this->assertEquals('value', $configCache->get('system', 0));
100 * Test the getAll() method
101 * @dataProvider dataTests
103 public function testGetAll($data)
105 $configCache = new ConfigCache();
106 $configCache->load($data);
108 $all = $configCache->getAll();
110 $this->assertContains($data['system'], $all);
111 $this->assertContains($data['config'], $all);
115 * Test the set() and get() method
116 * @dataProvider dataTests
118 public function testSetGet($data)
120 $configCache = new ConfigCache();
122 foreach ($data as $cat => $values) {
123 foreach ($values as $key => $value) {
124 $configCache->set($cat, $key, $value);
128 $this->assertConfigValues($data, $configCache);
132 * Test the get() method without a value
134 public function testGetEmpty()
136 $configCache = new ConfigCache();
138 $this->assertNull($configCache->get('something', 'value'));
142 * Test the get() method with a category
144 public function testGetCat()
146 $configCache = new ConfigCache([
156 $this->assertEquals([
159 ], $configCache->get('system'));
163 * Test the delete() method
164 * @dataProvider dataTests
166 public function testDelete($data)
168 $configCache = new ConfigCache($data);
170 foreach ($data as $cat => $values) {
171 foreach ($values as $key => $value) {
172 $configCache->delete($cat, $key);
176 $this->assertEmpty($configCache->getAll());
180 * Test the keyDiff() method with result
181 * @dataProvider dataTests
183 public function testKeyDiffWithResult($data)
185 $configCache = new ConfigCache($data);
189 'fakeKey' => 'value',
193 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
197 * Test the keyDiff() method without result
198 * @dataProvider dataTests
200 public function testKeyDiffWithoutResult($data)
202 $configCache = new ConfigCache($data);
204 $diffConfig = $configCache->getAll();
206 $this->assertEmpty($configCache->keyDiff($diffConfig));
210 * Test the default hiding of passwords inside the cache
212 public function testPasswordHide()
214 $configCache = new ConfigCache([
216 'password' => 'supersecure',
217 'username' => 'notsecured',
221 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
222 $this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
223 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
227 * Test disabling the hiding of passwords inside the cache
229 public function testPasswordShow()
231 $configCache = new ConfigCache([
233 'password' => 'supersecure',
234 'username' => 'notsecured',
238 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
239 $this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
240 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
244 * Test a empty password
246 public function testEmptyPassword()
248 $configCache = new ConfigCache([
255 $this->assertNotEmpty($configCache->get('database', 'password'));
256 $this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
257 $this->assertEmpty($configCache->get('database', 'username'));
260 public function testWrongTypePassword()
262 $configCache = new ConfigCache([
264 'password' => new \stdClass(),
269 $this->assertNotEmpty($configCache->get('database', 'password'));
270 $this->assertEmpty($configCache->get('database', 'username'));
272 $configCache = new ConfigCache([
279 $this->assertEquals(23, $configCache->get('database', 'password'));
280 $this->assertEmpty($configCache->get('database', 'username'));