3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Core\Config;
24 use Friendica\Core\Config\Cache;
25 use Friendica\Test\MockedTest;
26 use ParagonIE\HiddenString\HiddenString;
29 class CacheTest extends MockedTest
31 public function dataTests()
42 'array' => ['1', 2, '3', true, false],
52 private function assertConfigValues($data, Cache $configCache)
54 foreach ($data as $cat => $values) {
55 foreach ($values as $key => $value) {
56 self::assertEquals($data[$cat][$key], $configCache->get($cat, $key));
62 * Test the loadConfigArray() method without override
63 * @dataProvider dataTests
65 public function testLoadConfigArray($data)
67 $configCache = new Cache();
68 $configCache->load($data);
70 self::assertConfigValues($data, $configCache);
74 * Test the loadConfigArray() method with overrides
75 * @dataProvider dataTests
77 public function testLoadConfigArrayOverride($data)
86 $configCache = new Cache();
87 $configCache->load($data, Cache::SOURCE_DB);
88 // doesn't override - Low Priority due Config file
89 $configCache->load($override, Cache::SOURCE_FILE);
91 self::assertConfigValues($data, $configCache);
93 // override the value - High Prio due Server Env
94 $configCache->load($override, Cache::SOURCE_ENV);
96 self::assertEquals($override['system']['test'], $configCache->get('system', 'test'));
97 self::assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
99 // Don't overwrite server ENV variables - even in load mode
100 $configCache->load($data, Cache::SOURCE_DB);
102 self::assertEquals($override['system']['test'], $configCache->get('system', 'test'));
103 self::assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
105 // Overwrite ENV variables with ENV variables
106 $configCache->load($data, Cache::SOURCE_ENV);
108 self::assertConfigValues($data, $configCache);
109 self::assertNotEquals($override['system']['test'], $configCache->get('system', 'test'));
110 self::assertNotEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
114 * Test the loadConfigArray() method with wrong/empty datasets
116 public function testLoadConfigArrayWrong()
118 $configCache = new Cache();
121 $configCache->load([]);
122 self::assertEmpty($configCache->getAll());
125 $configCache->load(['system' => 'not_array']);
126 self::assertEmpty($configCache->getAll());
128 // incomplete dataset (key is integer ID of the array)
129 $configCache->load(['system' => ['value']]);
130 self::assertEquals('value', $configCache->get('system', 0));
134 * Test the getAll() method
135 * @dataProvider dataTests
137 public function testGetAll($data)
139 $configCache = new Cache();
140 $configCache->load($data);
142 $all = $configCache->getAll();
144 self::assertContains($data['system'], $all);
145 self::assertContains($data['config'], $all);
149 * Test the set() and get() method
150 * @dataProvider dataTests
152 public function testSetGet($data)
154 $configCache = new Cache();
156 foreach ($data as $cat => $values) {
157 foreach ($values as $key => $value) {
158 $configCache->set($cat, $key, $value);
162 self::assertConfigValues($data, $configCache);
166 * Test the get() method without a value
168 public function testGetEmpty()
170 $configCache = new Cache();
172 self::assertNull($configCache->get('something', 'value'));
176 * Test the get() method with a category
178 public function testGetCat()
180 $configCache = new Cache([
193 ], $configCache->get('system'));
195 // explicit null as key
199 ], $configCache->get('system', null));
203 * Test the delete() method
204 * @dataProvider dataTests
206 public function testDelete($data)
208 $configCache = new Cache($data);
210 foreach ($data as $cat => $values) {
211 foreach ($values as $key => $value) {
212 $configCache->delete($cat, $key);
216 self::assertEmpty($configCache->getAll());
220 * Test the keyDiff() method with result
221 * @dataProvider dataTests
223 public function testKeyDiffWithResult($data)
225 $configCache = new Cache($data);
229 'fakeKey' => 'value',
233 self::assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
237 * Test the keyDiff() method without result
238 * @dataProvider dataTests
240 public function testKeyDiffWithoutResult($data)
242 $configCache = new Cache($data);
244 $diffConfig = $configCache->getAll();
246 self::assertEmpty($configCache->keyDiff($diffConfig));
250 * Test the default hiding of passwords inside the cache
252 public function testPasswordHide()
254 $configCache = new Cache([
256 'password' => 'supersecure',
257 'username' => 'notsecured',
261 self::assertEquals('supersecure', $configCache->get('database', 'password'));
262 self::assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
263 self::assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
267 * Test disabling the hiding of passwords inside the cache
269 public function testPasswordShow()
271 $configCache = new Cache([
273 'password' => 'supersecure',
274 'username' => 'notsecured',
278 self::assertEquals('supersecure', $configCache->get('database', 'password'));
279 self::assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
280 self::assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
284 * Test a empty password
286 public function testEmptyPassword()
288 $configCache = new Cache([
295 self::assertNotEmpty($configCache->get('database', 'password'));
296 self::assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
297 self::assertEmpty($configCache->get('database', 'username'));
300 public function testWrongTypePassword()
302 $configCache = new Cache([
304 'password' => new stdClass(),
309 self::assertNotEmpty($configCache->get('database', 'password'));
310 self::assertEmpty($configCache->get('database', 'username'));
312 $configCache = new Cache([
319 self::assertEquals(23, $configCache->get('database', 'password'));
320 self::assertEmpty($configCache->get('database', 'username'));
324 * Test the set() method with overrides
325 * @dataProvider dataTests
327 public function testSetOverrides($data)
330 $configCache = new Cache();
331 $configCache->load($data, Cache::SOURCE_DB);
333 // test with wrong override
334 self::assertFalse($configCache->set('system', 'test', '1234567', Cache::SOURCE_FILE));
335 self::assertEquals($data['system']['test'], $configCache->get('system', 'test'));
337 // test with override (equal)
338 self::assertTrue($configCache->set('system', 'test', '8910', Cache::SOURCE_DB));
339 self::assertEquals('8910', $configCache->get('system', 'test'));
341 // test with override (over)
342 self::assertTrue($configCache->set('system', 'test', '111213', Cache::SOURCE_ENV));
343 self::assertEquals('111213', $configCache->get('system', 'test'));