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;
28 class CacheTest extends MockedTest
30 public function dataTests()
41 'array' => ['1', 2, '3', true, false],
51 private function assertConfigValues($data, Cache $configCache)
53 foreach ($data as $cat => $values) {
54 foreach ($values as $key => $value) {
55 $this->assertEquals($data[$cat][$key], $configCache->get($cat, $key));
61 * Test the loadConfigArray() method without override
62 * @dataProvider dataTests
64 public function testLoadConfigArray($data)
66 $configCache = new Cache();
67 $configCache->load($data);
69 $this->assertConfigValues($data, $configCache);
73 * Test the loadConfigArray() method with overrides
74 * @dataProvider dataTests
76 public function testLoadConfigArrayOverride($data)
85 $configCache = new Cache();
86 $configCache->load($data);
87 $configCache->load($override);
89 $this->assertConfigValues($data, $configCache);
92 $configCache->load($override, true);
94 $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
95 $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
99 * Test the loadConfigArray() method with wrong/empty datasets
101 public function testLoadConfigArrayWrong()
103 $configCache = new Cache();
106 $configCache->load([]);
107 $this->assertEmpty($configCache->getAll());
110 $configCache->load(['system' => 'not_array']);
111 $this->assertEmpty($configCache->getAll());
113 // incomplete dataset (key is integer ID of the array)
114 $configCache->load(['system' => ['value']]);
115 $this->assertEquals('value', $configCache->get('system', 0));
119 * Test the getAll() method
120 * @dataProvider dataTests
122 public function testGetAll($data)
124 $configCache = new Cache();
125 $configCache->load($data);
127 $all = $configCache->getAll();
129 $this->assertContains($data['system'], $all);
130 $this->assertContains($data['config'], $all);
134 * Test the set() and get() method
135 * @dataProvider dataTests
137 public function testSetGet($data)
139 $configCache = new Cache();
141 foreach ($data as $cat => $values) {
142 foreach ($values as $key => $value) {
143 $configCache->set($cat, $key, $value);
147 $this->assertConfigValues($data, $configCache);
151 * Test the get() method without a value
153 public function testGetEmpty()
155 $configCache = new Cache();
157 $this->assertNull($configCache->get('something', 'value'));
161 * Test the get() method with a category
163 public function testGetCat()
165 $configCache = new Cache([
175 $this->assertEquals([
178 ], $configCache->get('system'));
180 // explicit null as key
181 $this->assertEquals([
184 ], $configCache->get('system', null));
188 * Test the delete() method
189 * @dataProvider dataTests
191 public function testDelete($data)
193 $configCache = new Cache($data);
195 foreach ($data as $cat => $values) {
196 foreach ($values as $key => $value) {
197 $configCache->delete($cat, $key);
201 $this->assertEmpty($configCache->getAll());
205 * Test the keyDiff() method with result
206 * @dataProvider dataTests
208 public function testKeyDiffWithResult($data)
210 $configCache = new Cache($data);
214 'fakeKey' => 'value',
218 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
222 * Test the keyDiff() method without result
223 * @dataProvider dataTests
225 public function testKeyDiffWithoutResult($data)
227 $configCache = new Cache($data);
229 $diffConfig = $configCache->getAll();
231 $this->assertEmpty($configCache->keyDiff($diffConfig));
235 * Test the default hiding of passwords inside the cache
237 public function testPasswordHide()
239 $configCache = new Cache([
241 'password' => 'supersecure',
242 'username' => 'notsecured',
246 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
247 $this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
248 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
252 * Test disabling the hiding of passwords inside the cache
254 public function testPasswordShow()
256 $configCache = new Cache([
258 'password' => 'supersecure',
259 'username' => 'notsecured',
263 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
264 $this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
265 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
269 * Test a empty password
271 public function testEmptyPassword()
273 $configCache = new Cache([
280 $this->assertNotEmpty($configCache->get('database', 'password'));
281 $this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
282 $this->assertEmpty($configCache->get('database', 'username'));
285 public function testWrongTypePassword()
287 $configCache = new Cache([
289 'password' => new \stdClass(),
294 $this->assertNotEmpty($configCache->get('database', 'password'));
295 $this->assertEmpty($configCache->get('database', 'username'));
297 $configCache = new Cache([
304 $this->assertEquals(23, $configCache->get('database', 'password'));
305 $this->assertEmpty($configCache->get('database', 'username'));