3 * @copyright Copyright (C) 2010-2021, the Friendica project
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 self::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 self::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
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 self::assertEmpty($configCache->getAll());
135 * Test the keyDiff() method with result
137 public function testKeyDiffWithResult()
139 $configCache = new Cache();
143 'fakeKey' => 'value',
147 self::assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
151 * Test the keyDiff() method without result
153 * @dataProvider dataTests
155 public function testKeyDiffWithoutResult($data)
157 $configCache = new Cache();
159 $configCache->load(1, $data);
161 $diffConfig = $configCache->getAll();
163 self::assertEmpty($configCache->keyDiff($diffConfig));
167 * Test the default hiding of passwords inside the cache
169 public function testPasswordHide()
171 $configCache = new Cache();
173 $configCache->load(1, [
175 'password' => 'supersecure',
176 'username' => 'notsecured',
180 self::assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
181 self::assertNotEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
182 self::assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
186 * Test disabling the hiding of passwords inside the cache
188 public function testPasswordShow()
190 $configCache = new Cache(false);
192 $configCache->load(1, [
194 'password' => 'supersecure',
195 'username' => 'notsecured',
199 self::assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
200 self::assertEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
201 self::assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
205 * Test a empty password
207 public function testEmptyPassword()
209 $configCache = new Cache();
211 $configCache->load(1, [
218 self::assertEmpty($configCache->get(1, 'database', 'password'));
219 self::assertEmpty($configCache->get(1, 'database', 'username'));
222 public function testWrongTypePassword()
224 $configCache = new Cache();
226 $configCache->load(1, [
228 'password' => new \stdClass(),
233 self::assertNotEmpty($configCache->get(1, 'database', 'password'));
234 self::assertEmpty($configCache->get(1, 'database', 'username'));
236 $configCache = new Cache();
238 $configCache->load(1, [
245 self::assertEquals(23, $configCache->get(1, 'database', 'password'));
246 self::assertEmpty($configCache->get(1, 'database', 'username'));
250 * Test two different UID configs and make sure that there is no overlapping possible
252 public function testTwoUid()
254 $configCache = new Cache();
256 $configCache->load(1, [
263 $configCache->load(2, [
269 self::assertEquals('value1', $configCache->get(1, 'cat1', 'key1'));
270 self::assertEquals('value2', $configCache->get(2, 'cat2', 'key2'));
272 self::assertNull($configCache->get(1, 'cat2', 'key2'));
273 self::assertNull($configCache->get(2, 'cat1', 'key1'));
277 * Test when using an invalid UID
278 * @todo check it the clean way before using the config class
280 public function testInvalidUid()
285 $configCache = new Cache();
287 self::assertNull($configCache->get($uid, 'cat1', 'cat2'));
289 self::assertFalse($configCache->set($uid, 'cat1', 'key1', 'doesn\'t matter!'));
290 self::assertFalse($configCache->delete($uid, 'cat1', 'key1'));