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\PConfig;
24 use Friendica\Core\PConfig\Cache;
25 use Friendica\Test\MockedTest;
27 class CacheTest extends MockedTest
29 public function dataTests()
40 'array' => ['1', 2, '3', true, false],
50 private function assertConfigValues($data, Cache $configCache, $uid)
52 foreach ($data as $cat => $values) {
53 foreach ($values as $key => $value) {
54 $this->assertEquals($data[$cat][$key], $configCache->get($uid, $cat, $key));
60 * Test the setP() and getP() methods
62 * @dataProvider dataTests
64 public function testSetGet($data)
66 $configCache = new Cache();
69 foreach ($data as $cat => $values) {
70 foreach ($values as $key => $value) {
71 $configCache->set($uid, $cat, $key, $value);
75 $this->assertConfigValues($data, $configCache, $uid);
80 * Test the getP() method with a category
82 public function testGetCat()
84 $configCache = new Cache();
87 $configCache->load($uid, [
100 ], $configCache->get($uid, 'system'));
102 // test explicit cat with null as key
103 $this->assertEquals([
106 ], $configCache->get($uid, 'system', null));
110 * Test the deleteP() method
112 * @dataProvider dataTests
114 public function testDelete($data)
116 $configCache = new Cache();
119 foreach ($data as $cat => $values) {
120 foreach ($values as $key => $value) {
121 $configCache->set($uid, $cat, $key, $value);
125 foreach ($data as $cat => $values) {
126 foreach ($values as $key => $value) {
127 $configCache->delete($uid, $cat, $key);
131 $this->assertEmpty($configCache->getAll());
135 * Test the keyDiff() method with result
137 * @dataProvider dataTests
139 public function testKeyDiffWithResult($data)
141 $configCache = new Cache();
145 'fakeKey' => 'value',
149 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
153 * Test the keyDiff() method without result
155 * @dataProvider dataTests
157 public function testKeyDiffWithoutResult($data)
159 $configCache = new Cache();
161 $configCache->load(1, $data);
163 $diffConfig = $configCache->getAll();
165 $this->assertEmpty($configCache->keyDiff($diffConfig));
169 * Test the default hiding of passwords inside the cache
171 public function testPasswordHide()
173 $configCache = new Cache();
175 $configCache->load(1, [
177 'password' => 'supersecure',
178 'username' => 'notsecured',
182 $this->assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
183 $this->assertNotEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
184 $this->assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
188 * Test disabling the hiding of passwords inside the cache
190 public function testPasswordShow()
192 $configCache = new Cache(false);
194 $configCache->load(1, [
196 'password' => 'supersecure',
197 'username' => 'notsecured',
201 $this->assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
202 $this->assertEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
203 $this->assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
207 * Test a empty password
209 public function testEmptyPassword()
211 $configCache = new Cache();
213 $configCache->load(1, [
220 $this->assertEmpty($configCache->get(1, 'database', 'password'));
221 $this->assertEmpty($configCache->get(1, 'database', 'username'));
224 public function testWrongTypePassword()
226 $configCache = new Cache();
228 $configCache->load(1, [
230 'password' => new \stdClass(),
235 $this->assertNotEmpty($configCache->get(1, 'database', 'password'));
236 $this->assertEmpty($configCache->get(1, 'database', 'username'));
238 $configCache = new Cache();
240 $configCache->load(1, [
247 $this->assertEquals(23, $configCache->get(1, 'database', 'password'));
248 $this->assertEmpty($configCache->get(1, 'database', 'username'));
252 * Test two different UID configs and make sure that there is no overlapping possible
254 public function testTwoUid()
256 $configCache = new Cache();
258 $configCache->load(1, [
265 $configCache->load(2, [
271 $this->assertEquals('value1', $configCache->get(1, 'cat1', 'key1'));
272 $this->assertEquals('value2', $configCache->get(2, 'cat2', 'key2'));
274 $this->assertNull($configCache->get(1, 'cat2', 'key2'));
275 $this->assertNull($configCache->get(2, 'cat1', 'key1'));
279 * Test when using an invalid UID
280 * @todo check it the clean way before using the config class
282 public function testInvalidUid()
287 $configCache = new Cache();
289 $this->assertNull($configCache->get($uid, 'cat1', 'cat2'));
291 $this->assertFalse($configCache->set($uid, 'cat1', 'key1', 'doesn\'t matter!'));
292 $this->assertFalse($configCache->delete($uid, 'cat1', 'key1'));