3 namespace Friendica\Test\src\Core\Config;
5 use Friendica\Core\Config\Cache;
6 use Friendica\Test\MockedTest;
7 use ParagonIE\HiddenString\HiddenString;
9 class CacheTest extends MockedTest
11 public function dataTests()
22 'array' => ['1', 2, '3', true, false],
32 private function assertConfigValues($data, Cache $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 Cache();
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 Cache();
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 Cache();
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 Cache();
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 Cache();
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 Cache();
138 $this->assertNull($configCache->get('something', 'value'));
142 * Test the get() method with a category
144 public function testGetCat()
146 $configCache = new Cache([
156 $this->assertEquals([
159 ], $configCache->get('system'));
161 // explicit null as key
162 $this->assertEquals([
165 ], $configCache->get('system', null));
169 * Test the delete() method
170 * @dataProvider dataTests
172 public function testDelete($data)
174 $configCache = new Cache($data);
176 foreach ($data as $cat => $values) {
177 foreach ($values as $key => $value) {
178 $configCache->delete($cat, $key);
182 $this->assertEmpty($configCache->getAll());
186 * Test the keyDiff() method with result
187 * @dataProvider dataTests
189 public function testKeyDiffWithResult($data)
191 $configCache = new Cache($data);
195 'fakeKey' => 'value',
199 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
203 * Test the keyDiff() method without result
204 * @dataProvider dataTests
206 public function testKeyDiffWithoutResult($data)
208 $configCache = new Cache($data);
210 $diffConfig = $configCache->getAll();
212 $this->assertEmpty($configCache->keyDiff($diffConfig));
216 * Test the default hiding of passwords inside the cache
218 public function testPasswordHide()
220 $configCache = new Cache([
222 'password' => 'supersecure',
223 'username' => 'notsecured',
227 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
228 $this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
229 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
233 * Test disabling the hiding of passwords inside the cache
235 public function testPasswordShow()
237 $configCache = new Cache([
239 'password' => 'supersecure',
240 'username' => 'notsecured',
244 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
245 $this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
246 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
250 * Test a empty password
252 public function testEmptyPassword()
254 $configCache = new Cache([
261 $this->assertNotEmpty($configCache->get('database', 'password'));
262 $this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
263 $this->assertEmpty($configCache->get('database', 'username'));
266 public function testWrongTypePassword()
268 $configCache = new Cache([
270 'password' => new \stdClass(),
275 $this->assertNotEmpty($configCache->get('database', 'password'));
276 $this->assertEmpty($configCache->get('database', 'username'));
278 $configCache = new Cache([
285 $this->assertEquals(23, $configCache->get('database', 'password'));
286 $this->assertEmpty($configCache->get('database', 'username'));