3 namespace Friendica\Test\src\Core\Config\Cache;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Test\MockedTest;
8 class ConfigCacheTest extends MockedTest
10 public function dataTests()
21 'array' => ['1', 2, '3', true, false],
31 private function assertConfigValues($data, ConfigCache $configCache, $uid = null)
33 foreach ($data as $cat => $values) {
34 foreach ($values as $key => $value) {
36 $this->assertEquals($data[$cat][$key], $configCache->getP($uid, $cat, $key));
38 $this->assertEquals($data[$cat][$key], $configCache->get($cat, $key));
45 * Test the loadConfigArray() method without override
46 * @dataProvider dataTests
48 public function testLoadConfigArray($data)
50 $configCache = new ConfigCache();
51 $configCache->load($data);
53 $this->assertConfigValues($data, $configCache);
57 * Test the loadConfigArray() method with overrides
58 * @dataProvider dataTests
60 public function testLoadConfigArrayOverride($data)
69 $configCache = new ConfigCache();
70 $configCache->load($data);
71 $configCache->load($override);
73 $this->assertConfigValues($data, $configCache);
76 $configCache->load($override, true);
78 $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
79 $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
83 * Test the loadConfigArray() method with wrong/empty datasets
85 public function testLoadConfigArrayWrong()
87 $configCache = new ConfigCache();
90 $configCache->load([]);
91 $this->assertEmpty($configCache->getAll());
94 $configCache->load(['system' => 'not_array']);
95 $this->assertEmpty($configCache->getAll());
97 // incomplete dataset (key is integer ID of the array)
98 $configCache->load(['system' => ['value']]);
99 $this->assertEquals('value', $configCache->get('system', 0));
103 * Test the getAll() method
104 * @dataProvider dataTests
106 public function testGetAll($data)
108 $configCache = new ConfigCache();
109 $configCache->load($data);
111 $all = $configCache->getAll();
113 $this->assertContains($data['system'], $all);
114 $this->assertContains($data['config'], $all);
118 * Test the set() and get() method
119 * @dataProvider dataTests
121 public function testSetGet($data)
123 $configCache = new ConfigCache();
125 foreach ($data as $cat => $values) {
126 foreach ($values as $key => $value) {
127 $configCache->set($cat, $key, $value);
131 $this->assertConfigValues($data, $configCache);
135 * Test the get() method without a value
137 public function testGetEmpty()
139 $configCache = new ConfigCache();
141 $this->assertNull($configCache->get('something', 'value'));
145 * Test the get() method with a category
147 public function testGetCat()
149 $configCache = new ConfigCache([
159 $this->assertEquals([
162 ], $configCache->get('system'));
166 * Test the delete() method
167 * @dataProvider dataTests
169 public function testDelete($data)
171 $configCache = new ConfigCache($data);
173 foreach ($data as $cat => $values) {
174 foreach ($values as $key => $value) {
175 $configCache->delete($cat, $key);
179 $this->assertEmpty($configCache->getAll());
183 * Test the setP() and getP() methods
184 * @dataProvider dataTests
186 public function testSetGetP($data)
188 $configCache = new ConfigCache();
191 foreach ($data as $cat => $values) {
192 foreach ($values as $key => $value) {
193 $configCache->setP($uid, $cat, $key, $value);
197 $this->assertConfigValues($data, $configCache, $uid);
202 * Test the getP() method with a category
204 public function testGetPCat()
206 $configCache = new ConfigCache();
209 $configCache->loadP($uid, [
219 $this->assertEquals([
222 ], $configCache->get($uid, 'system'));
226 * Test the deleteP() method
227 * @dataProvider dataTests
229 public function testDeleteP($data)
231 $configCache = new ConfigCache();
234 foreach ($data as $cat => $values) {
235 foreach ($values as $key => $value) {
236 $configCache->setP($uid, $cat, $key, $value);
240 foreach ($data as $cat => $values) {
241 foreach ($values as $key => $value) {
242 $configCache->deleteP($uid, $cat, $key);
246 $this->assertEmpty($configCache->getAll());
250 * Test the keyDiff() method with result
251 * @dataProvider dataTests
253 public function testKeyDiffWithResult($data)
255 $configCache = new ConfigCache($data);
259 'fakeKey' => 'value',
263 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
267 * Test the keyDiff() method without result
268 * @dataProvider dataTests
270 public function testKeyDiffWithoutResult($data)
272 $configCache = new ConfigCache($data);
274 $diffConfig = $configCache->getAll();
276 $this->assertEmpty($configCache->keyDiff($diffConfig));
280 * Test the default hiding of passwords inside the cache
282 public function testPasswordHide()
284 $configCache = new ConfigCache([
286 'password' => 'supersecure',
287 'username' => 'notsecured',
291 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
292 $this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
293 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
297 * Test disabling the hiding of passwords inside the cache
299 public function testPasswordShow()
301 $configCache = new ConfigCache([
303 'password' => 'supersecure',
304 'username' => 'notsecured',
308 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
309 $this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
310 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
314 * Test a empty password
316 public function testEmptyPassword()
318 $configCache = new ConfigCache([
325 $this->assertEmpty($configCache->get('database', 'password'));
326 $this->assertEmpty($configCache->get('database', 'username'));
329 public function testWrongTypePassword()
331 $configCache = new ConfigCache([
333 'password' => new \stdClass(),
338 $this->assertNotEmpty($configCache->get('database', 'password'));
339 $this->assertEmpty($configCache->get('database', 'username'));
341 $configCache = new ConfigCache([
348 $this->assertEquals(23, $configCache->get('database', 'password'));
349 $this->assertEmpty($configCache->get('database', 'username'));