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, Cache::SOURCE_DB);
87 // doesn't override - Low Priority due Config file
88 $configCache->load($override, Cache::SOURCE_FILE);
90 $this->assertConfigValues($data, $configCache);
92 // override the value - High Prio due Server Env
93 $configCache->load($override, Cache::SOURCE_ENV);
95 $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
96 $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
98 // Don't overwrite server ENV variables - even in load mode
99 $configCache->load($data, Cache::SOURCE_DB);
101 $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
102 $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
104 // Overwrite ENV variables with ENV variables
105 $configCache->load($data, Cache::SOURCE_ENV);
107 $this->assertConfigValues($data, $configCache);
108 $this->assertNotEquals($override['system']['test'], $configCache->get('system', 'test'));
109 $this->assertNotEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
113 * Test the loadConfigArray() method with wrong/empty datasets
115 public function testLoadConfigArrayWrong()
117 $configCache = new Cache();
120 $configCache->load([]);
121 $this->assertEmpty($configCache->getAll());
124 $configCache->load(['system' => 'not_array']);
125 $this->assertEmpty($configCache->getAll());
127 // incomplete dataset (key is integer ID of the array)
128 $configCache->load(['system' => ['value']]);
129 $this->assertEquals('value', $configCache->get('system', 0));
133 * Test the getAll() method
134 * @dataProvider dataTests
136 public function testGetAll($data)
138 $configCache = new Cache();
139 $configCache->load($data);
141 $all = $configCache->getAll();
143 $this->assertContains($data['system'], $all);
144 $this->assertContains($data['config'], $all);
148 * Test the set() and get() method
149 * @dataProvider dataTests
151 public function testSetGet($data)
153 $configCache = new Cache();
155 foreach ($data as $cat => $values) {
156 foreach ($values as $key => $value) {
157 $configCache->set($cat, $key, $value);
161 $this->assertConfigValues($data, $configCache);
165 * Test the get() method without a value
167 public function testGetEmpty()
169 $configCache = new Cache();
171 $this->assertNull($configCache->get('something', 'value'));
175 * Test the get() method with a category
177 public function testGetCat()
179 $configCache = new Cache([
189 $this->assertEquals([
192 ], $configCache->get('system'));
194 // explicit null as key
195 $this->assertEquals([
198 ], $configCache->get('system', null));
202 * Test the delete() method
203 * @dataProvider dataTests
205 public function testDelete($data)
207 $configCache = new Cache($data);
209 foreach ($data as $cat => $values) {
210 foreach ($values as $key => $value) {
211 $configCache->delete($cat, $key);
215 $this->assertEmpty($configCache->getAll());
219 * Test the keyDiff() method with result
220 * @dataProvider dataTests
222 public function testKeyDiffWithResult($data)
224 $configCache = new Cache($data);
228 'fakeKey' => 'value',
232 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
236 * Test the keyDiff() method without result
237 * @dataProvider dataTests
239 public function testKeyDiffWithoutResult($data)
241 $configCache = new Cache($data);
243 $diffConfig = $configCache->getAll();
245 $this->assertEmpty($configCache->keyDiff($diffConfig));
249 * Test the default hiding of passwords inside the cache
251 public function testPasswordHide()
253 $configCache = new Cache([
255 'password' => 'supersecure',
256 'username' => 'notsecured',
260 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
261 $this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
262 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
266 * Test disabling the hiding of passwords inside the cache
268 public function testPasswordShow()
270 $configCache = new Cache([
272 'password' => 'supersecure',
273 'username' => 'notsecured',
277 $this->assertEquals('supersecure', $configCache->get('database', 'password'));
278 $this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
279 $this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
283 * Test a empty password
285 public function testEmptyPassword()
287 $configCache = new Cache([
294 $this->assertNotEmpty($configCache->get('database', 'password'));
295 $this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
296 $this->assertEmpty($configCache->get('database', 'username'));
299 public function testWrongTypePassword()
301 $configCache = new Cache([
303 'password' => new \stdClass(),
308 $this->assertNotEmpty($configCache->get('database', 'password'));
309 $this->assertEmpty($configCache->get('database', 'username'));
311 $configCache = new Cache([
318 $this->assertEquals(23, $configCache->get('database', 'password'));
319 $this->assertEmpty($configCache->get('database', 'username'));