2019-02-10 12:30:13 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-02-10 12:30:13 +01:00
|
|
|
|
2020-01-19 22:38:33 +01:00
|
|
|
namespace Friendica\Test\src\Core\Config;
|
2019-02-10 12:30:13 +01:00
|
|
|
|
2020-01-19 22:23:44 +01:00
|
|
|
use Friendica\Core\Config\Cache;
|
2019-02-10 12:30:13 +01:00
|
|
|
use Friendica\Test\MockedTest;
|
2019-07-09 22:38:54 +02:00
|
|
|
use ParagonIE\HiddenString\HiddenString;
|
2019-02-10 12:30:13 +01:00
|
|
|
|
2020-01-19 22:38:33 +01:00
|
|
|
class CacheTest extends MockedTest
|
2019-02-10 12:30:13 +01:00
|
|
|
{
|
|
|
|
public function dataTests()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'normal' => [
|
|
|
|
'data' => [
|
|
|
|
'system' => [
|
|
|
|
'test' => 'it',
|
|
|
|
'boolTrue' => true,
|
|
|
|
'boolFalse' => false,
|
|
|
|
'int' => 235,
|
|
|
|
'dec' => 2.456,
|
|
|
|
'array' => ['1', 2, '3', true, false],
|
|
|
|
],
|
|
|
|
'config' => [
|
|
|
|
'a' => 'value',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-01-19 22:23:44 +01:00
|
|
|
private function assertConfigValues($data, Cache $configCache)
|
2019-02-10 12:30:13 +01:00
|
|
|
{
|
|
|
|
foreach ($data as $cat => $values) {
|
|
|
|
foreach ($values as $key => $value) {
|
2019-07-12 22:38:50 +02:00
|
|
|
$this->assertEquals($data[$cat][$key], $configCache->get($cat, $key));
|
2019-02-10 12:30:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the loadConfigArray() method without override
|
|
|
|
* @dataProvider dataTests
|
|
|
|
*/
|
|
|
|
public function testLoadConfigArray($data)
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache();
|
2019-02-10 19:52:21 +01:00
|
|
|
$configCache->load($data);
|
2019-02-10 12:30:13 +01:00
|
|
|
|
|
|
|
$this->assertConfigValues($data, $configCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the loadConfigArray() method with overrides
|
|
|
|
* @dataProvider dataTests
|
|
|
|
*/
|
|
|
|
public function testLoadConfigArrayOverride($data)
|
|
|
|
{
|
|
|
|
$override = [
|
|
|
|
'system' => [
|
|
|
|
'test' => 'not',
|
|
|
|
'boolTrue' => false,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache();
|
2019-02-10 19:52:21 +01:00
|
|
|
$configCache->load($data);
|
|
|
|
$configCache->load($override);
|
2019-02-10 12:30:13 +01:00
|
|
|
|
|
|
|
$this->assertConfigValues($data, $configCache);
|
|
|
|
|
|
|
|
// override the value
|
2019-02-10 19:52:21 +01:00
|
|
|
$configCache->load($override, true);
|
2019-02-10 12:30:13 +01:00
|
|
|
|
|
|
|
$this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
|
|
|
|
$this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:52:21 +01:00
|
|
|
/**
|
|
|
|
* Test the loadConfigArray() method with wrong/empty datasets
|
|
|
|
*/
|
|
|
|
public function testLoadConfigArrayWrong()
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache();
|
2019-02-10 19:52:21 +01:00
|
|
|
|
|
|
|
// empty dataset
|
|
|
|
$configCache->load([]);
|
|
|
|
$this->assertEmpty($configCache->getAll());
|
|
|
|
|
|
|
|
// wrong dataset
|
|
|
|
$configCache->load(['system' => 'not_array']);
|
|
|
|
$this->assertEmpty($configCache->getAll());
|
|
|
|
|
|
|
|
// incomplete dataset (key is integer ID of the array)
|
|
|
|
$configCache->load(['system' => ['value']]);
|
|
|
|
$this->assertEquals('value', $configCache->get('system', 0));
|
|
|
|
}
|
|
|
|
|
2019-02-10 12:30:13 +01:00
|
|
|
/**
|
|
|
|
* Test the getAll() method
|
|
|
|
* @dataProvider dataTests
|
|
|
|
*/
|
|
|
|
public function testGetAll($data)
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache();
|
2019-02-10 19:52:21 +01:00
|
|
|
$configCache->load($data);
|
2019-02-10 12:30:13 +01:00
|
|
|
|
|
|
|
$all = $configCache->getAll();
|
|
|
|
|
|
|
|
$this->assertContains($data['system'], $all);
|
2019-02-10 19:52:21 +01:00
|
|
|
$this->assertContains($data['config'], $all);
|
2019-02-10 12:30:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the set() and get() method
|
|
|
|
* @dataProvider dataTests
|
|
|
|
*/
|
|
|
|
public function testSetGet($data)
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache();
|
2019-02-10 12:30:13 +01:00
|
|
|
|
|
|
|
foreach ($data as $cat => $values) {
|
|
|
|
foreach ($values as $key => $value) {
|
|
|
|
$configCache->set($cat, $key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertConfigValues($data, $configCache);
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:52:21 +01:00
|
|
|
/**
|
|
|
|
* Test the get() method without a value
|
|
|
|
*/
|
|
|
|
public function testGetEmpty()
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache();
|
2019-02-10 19:52:21 +01:00
|
|
|
|
2019-02-23 00:09:57 +01:00
|
|
|
$this->assertNull($configCache->get('something', 'value'));
|
2019-02-11 21:13:53 +01:00
|
|
|
}
|
2019-02-10 19:52:21 +01:00
|
|
|
|
2019-02-11 21:13:53 +01:00
|
|
|
/**
|
|
|
|
* Test the get() method with a category
|
|
|
|
*/
|
|
|
|
public function testGetCat()
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache([
|
2019-02-11 21:13:53 +01:00
|
|
|
'system' => [
|
|
|
|
'key1' => 'value1',
|
|
|
|
'key2' => 'value2',
|
|
|
|
],
|
|
|
|
'config' => [
|
|
|
|
'key3' => 'value3',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'key1' => 'value1',
|
|
|
|
'key2' => 'value2',
|
|
|
|
], $configCache->get('system'));
|
2019-07-14 23:23:20 +02:00
|
|
|
|
|
|
|
// explicit null as key
|
|
|
|
$this->assertEquals([
|
|
|
|
'key1' => 'value1',
|
|
|
|
'key2' => 'value2',
|
|
|
|
], $configCache->get('system', null));
|
2019-02-10 19:52:21 +01:00
|
|
|
}
|
|
|
|
|
2019-02-10 12:30:13 +01:00
|
|
|
/**
|
|
|
|
* Test the delete() method
|
|
|
|
* @dataProvider dataTests
|
|
|
|
*/
|
|
|
|
public function testDelete($data)
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache($data);
|
2019-02-10 12:30:13 +01:00
|
|
|
|
|
|
|
foreach ($data as $cat => $values) {
|
|
|
|
foreach ($values as $key => $value) {
|
|
|
|
$configCache->delete($cat, $key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEmpty($configCache->getAll());
|
|
|
|
}
|
|
|
|
|
2019-03-30 19:08:47 +01:00
|
|
|
/**
|
|
|
|
* Test the keyDiff() method with result
|
|
|
|
* @dataProvider dataTests
|
|
|
|
*/
|
2019-03-30 18:57:54 +01:00
|
|
|
public function testKeyDiffWithResult($data)
|
2019-03-30 19:08:47 +01:00
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache($data);
|
2019-03-30 19:08:47 +01:00
|
|
|
|
|
|
|
$diffConfig = [
|
|
|
|
'fakeCat' => [
|
|
|
|
'fakeKey' => 'value',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the keyDiff() method without result
|
|
|
|
* @dataProvider dataTests
|
|
|
|
*/
|
|
|
|
public function testKeyDiffWithoutResult($data)
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache($data);
|
2019-03-30 19:08:47 +01:00
|
|
|
|
|
|
|
$diffConfig = $configCache->getAll();
|
|
|
|
|
|
|
|
$this->assertEmpty($configCache->keyDiff($diffConfig));
|
|
|
|
}
|
2019-06-10 14:43:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the default hiding of passwords inside the cache
|
|
|
|
*/
|
|
|
|
public function testPasswordHide()
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache([
|
2019-06-10 14:43:25 +02:00
|
|
|
'database' => [
|
|
|
|
'password' => 'supersecure',
|
|
|
|
'username' => 'notsecured',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals('supersecure', $configCache->get('database', 'password'));
|
|
|
|
$this->assertNotEquals('supersecure', print_r($configCache->get('database', 'password'), true));
|
|
|
|
$this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test disabling the hiding of passwords inside the cache
|
|
|
|
*/
|
|
|
|
public function testPasswordShow()
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache([
|
2019-06-10 14:43:25 +02:00
|
|
|
'database' => [
|
|
|
|
'password' => 'supersecure',
|
|
|
|
'username' => 'notsecured',
|
|
|
|
],
|
|
|
|
], false);
|
|
|
|
|
|
|
|
$this->assertEquals('supersecure', $configCache->get('database', 'password'));
|
|
|
|
$this->assertEquals('supersecure', print_r($configCache->get('database', 'password'), true));
|
|
|
|
$this->assertEquals('notsecured', print_r($configCache->get('database', 'username'), true));
|
|
|
|
}
|
2019-06-10 15:39:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a empty password
|
|
|
|
*/
|
|
|
|
public function testEmptyPassword()
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache([
|
2019-06-10 15:39:21 +02:00
|
|
|
'database' => [
|
|
|
|
'password' => '',
|
|
|
|
'username' => '',
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2019-07-09 22:38:54 +02:00
|
|
|
$this->assertNotEmpty($configCache->get('database', 'password'));
|
|
|
|
$this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
|
2019-06-10 15:46:34 +02:00
|
|
|
$this->assertEmpty($configCache->get('database', 'username'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWrongTypePassword()
|
|
|
|
{
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache([
|
2019-06-10 15:46:34 +02:00
|
|
|
'database' => [
|
|
|
|
'password' => new \stdClass(),
|
|
|
|
'username' => '',
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertNotEmpty($configCache->get('database', 'password'));
|
|
|
|
$this->assertEmpty($configCache->get('database', 'username'));
|
|
|
|
|
2020-01-19 22:23:44 +01:00
|
|
|
$configCache = new Cache([
|
2019-06-10 15:46:34 +02:00
|
|
|
'database' => [
|
|
|
|
'password' => 23,
|
|
|
|
'username' => '',
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(23, $configCache->get('database', 'password'));
|
|
|
|
$this->assertEmpty($configCache->get('database', 'username'));
|
2019-06-10 15:39:21 +02:00
|
|
|
}
|
2019-02-10 12:30:13 +01:00
|
|
|
}
|