3 namespace Friendica\Test\src\Core\Config\Cache;
5 use Friendica\Core\Config\Cache\PConfigCache;
6 use Friendica\Test\MockedTest;
8 class PConfigCacheTest extends MockedTest
10 public function dataTests()
21 'array' => ['1', 2, '3', true, false],
31 private function assertConfigValues($data, PConfigCache $configCache, $uid)
33 foreach ($data as $cat => $values) {
34 foreach ($values as $key => $value) {
35 $this->assertEquals($data[$cat][$key], $configCache->get($uid, $cat, $key));
41 * Test the setP() and getP() methods
43 * @dataProvider dataTests
45 public function testSetGet($data)
47 $configCache = new PConfigCache();
50 foreach ($data as $cat => $values) {
51 foreach ($values as $key => $value) {
52 $configCache->set($uid, $cat, $key, $value);
56 $this->assertConfigValues($data, $configCache, $uid);
61 * Test the getP() method with a category
63 public function testGetCat()
65 $configCache = new PConfigCache();
68 $configCache->load($uid, [
81 ], $configCache->get($uid, 'system'));
83 // test explicit cat with null as key
87 ], $configCache->get($uid, 'system', null));
91 * Test the deleteP() method
93 * @dataProvider dataTests
95 public function testDelete($data)
97 $configCache = new PConfigCache();
100 foreach ($data as $cat => $values) {
101 foreach ($values as $key => $value) {
102 $configCache->set($uid, $cat, $key, $value);
106 foreach ($data as $cat => $values) {
107 foreach ($values as $key => $value) {
108 $configCache->delete($uid, $cat, $key);
112 $this->assertEmpty($configCache->getAll());
116 * Test the keyDiff() method with result
118 * @dataProvider dataTests
120 public function testKeyDiffWithResult($data)
122 $configCache = new PConfigCache();
126 'fakeKey' => 'value',
130 $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
134 * Test the keyDiff() method without result
136 * @dataProvider dataTests
138 public function testKeyDiffWithoutResult($data)
140 $configCache = new PConfigCache();
142 $configCache->load(1, $data);
144 $diffConfig = $configCache->getAll();
146 $this->assertEmpty($configCache->keyDiff($diffConfig));
150 * Test the default hiding of passwords inside the cache
152 public function testPasswordHide()
154 $configCache = new PConfigCache();
156 $configCache->load(1, [
158 'password' => 'supersecure',
159 'username' => 'notsecured',
163 $this->assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
164 $this->assertNotEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
165 $this->assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
169 * Test disabling the hiding of passwords inside the cache
171 public function testPasswordShow()
173 $configCache = new PConfigCache(false);
175 $configCache->load(1, [
177 'password' => 'supersecure',
178 'username' => 'notsecured',
182 $this->assertEquals('supersecure', $configCache->get(1, 'database', 'password'));
183 $this->assertEquals('supersecure', print_r($configCache->get(1, 'database', 'password'), true));
184 $this->assertEquals('notsecured', print_r($configCache->get(1, 'database', 'username'), true));
188 * Test a empty password
190 public function testEmptyPassword()
192 $configCache = new PConfigCache();
194 $configCache->load(1, [
201 $this->assertEmpty($configCache->get(1, 'database', 'password'));
202 $this->assertEmpty($configCache->get(1, 'database', 'username'));
205 public function testWrongTypePassword()
207 $configCache = new PConfigCache();
209 $configCache->load(1, [
211 'password' => new \stdClass(),
216 $this->assertNotEmpty($configCache->get(1, 'database', 'password'));
217 $this->assertEmpty($configCache->get(1, 'database', 'username'));
219 $configCache = new PConfigCache();
221 $configCache->load(1, [
228 $this->assertEquals(23, $configCache->get(1, 'database', 'password'));
229 $this->assertEmpty($configCache->get(1, 'database', 'username'));
233 * Test two different UID configs and make sure that there is no overlapping possible
235 public function testTwoUid()
237 $configCache = new PConfigCache();
239 $configCache->load(1, [
246 $configCache->load(2, [
252 $this->assertEquals('value1', $configCache->get(1, 'cat1', 'key1'));
253 $this->assertEquals('value2', $configCache->get(2, 'cat2', 'key2'));
255 $this->assertNull($configCache->get(1, 'cat2', 'key2'));
256 $this->assertNull($configCache->get(2, 'cat1', 'key1'));
260 * Test when using an invalid UID
261 * @todo check it the clean way before using the config class
263 public function testInvalidUid()
268 $configCache = new PConfigCache();
270 $this->assertNull($configCache->get($uid, 'cat1', 'cat2'));
272 $this->assertFalse($configCache->set($uid, 'cat1', 'key1', 'doesn\'t matter!'));
273 $this->assertFalse($configCache->delete($uid, 'cat1', 'key1'));