123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
-
- namespace Friendica\Test\src\Core\Config\Cache;
-
- use Friendica\Core\Config\Cache\ConfigCache;
- use Friendica\Test\MockedTest;
- use ParagonIE\HiddenString\HiddenString;
-
- class ConfigCacheTest extends MockedTest
- {
- 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',
- ],
- ]
- ]
- ];
- }
-
- private function assertConfigValues($data, ConfigCache $configCache, $uid = null)
- {
- foreach ($data as $cat => $values) {
- foreach ($values as $key => $value) {
- if (isset($uid)) {
- $this->assertEquals($data[$cat][$key], $configCache->getP($uid, $cat, $key));
- } else {
- $this->assertEquals($data[$cat][$key], $configCache->get($cat, $key));
- }
- }
- }
- }
-
- /**
- * Test the loadConfigArray() method without override
- * @dataProvider dataTests
- */
- public function testLoadConfigArray($data)
- {
- $configCache = new ConfigCache();
- $configCache->load($data);
-
- $this->assertConfigValues($data, $configCache);
- }
-
- /**
- * Test the loadConfigArray() method with overrides
- * @dataProvider dataTests
- */
- public function testLoadConfigArrayOverride($data)
- {
- $override = [
- 'system' => [
- 'test' => 'not',
- 'boolTrue' => false,
- ]
- ];
-
- $configCache = new ConfigCache();
- $configCache->load($data);
- $configCache->load($override);
-
- $this->assertConfigValues($data, $configCache);
-
- // override the value
- $configCache->load($override, true);
-
- $this->assertEquals($override['system']['test'], $configCache->get('system', 'test'));
- $this->assertEquals($override['system']['boolTrue'], $configCache->get('system', 'boolTrue'));
- }
-
- /**
- * Test the loadConfigArray() method with wrong/empty datasets
- */
- public function testLoadConfigArrayWrong()
- {
- $configCache = new ConfigCache();
-
- // 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));
- }
-
- /**
- * Test the getAll() method
- * @dataProvider dataTests
- */
- public function testGetAll($data)
- {
- $configCache = new ConfigCache();
- $configCache->load($data);
-
- $all = $configCache->getAll();
-
- $this->assertContains($data['system'], $all);
- $this->assertContains($data['config'], $all);
- }
-
- /**
- * Test the set() and get() method
- * @dataProvider dataTests
- */
- public function testSetGet($data)
- {
- $configCache = new ConfigCache();
-
- foreach ($data as $cat => $values) {
- foreach ($values as $key => $value) {
- $configCache->set($cat, $key, $value);
- }
- }
-
- $this->assertConfigValues($data, $configCache);
- }
-
- /**
- * Test the get() method without a value
- */
- public function testGetEmpty()
- {
- $configCache = new ConfigCache();
-
- $this->assertNull($configCache->get('something', 'value'));
- }
-
- /**
- * Test the get() method with a category
- */
- public function testGetCat()
- {
- $configCache = new ConfigCache([
- 'system' => [
- 'key1' => 'value1',
- 'key2' => 'value2',
- ],
- 'config' => [
- 'key3' => 'value3',
- ],
- ]);
-
- $this->assertEquals([
- 'key1' => 'value1',
- 'key2' => 'value2',
- ], $configCache->get('system'));
- }
-
- /**
- * Test the delete() method
- * @dataProvider dataTests
- */
- public function testDelete($data)
- {
- $configCache = new ConfigCache($data);
-
- foreach ($data as $cat => $values) {
- foreach ($values as $key => $value) {
- $configCache->delete($cat, $key);
- }
- }
-
- $this->assertEmpty($configCache->getAll());
- }
-
- /**
- * Test the setP() and getP() methods
- * @dataProvider dataTests
- */
- public function testSetGetP($data)
- {
- $configCache = new ConfigCache();
- $uid = 345;
-
- foreach ($data as $cat => $values) {
- foreach ($values as $key => $value) {
- $configCache->setP($uid, $cat, $key, $value);
- }
- }
-
- $this->assertConfigValues($data, $configCache, $uid);
- }
-
-
- /**
- * Test the getP() method with a category
- */
- public function testGetPCat()
- {
- $configCache = new ConfigCache();
- $uid = 345;
-
- $configCache->loadP($uid, [
- 'system' => [
- 'key1' => 'value1',
- 'key2' => 'value2',
- ],
- 'config' => [
- 'key3' => 'value3',
- ],
- ]);
-
- $this->assertEquals([
- 'key1' => 'value1',
- 'key2' => 'value2',
- ], $configCache->get($uid, 'system'));
- }
-
- /**
- * Test the deleteP() method
- * @dataProvider dataTests
- */
- public function testDeleteP($data)
- {
- $configCache = new ConfigCache();
- $uid = 345;
-
- foreach ($data as $cat => $values) {
- foreach ($values as $key => $value) {
- $configCache->setP($uid, $cat, $key, $value);
- }
- }
-
- foreach ($data as $cat => $values) {
- foreach ($values as $key => $value) {
- $configCache->deleteP($uid, $cat, $key);
- }
- }
-
- $this->assertEmpty($configCache->getAll());
- }
-
- /**
- * Test the keyDiff() method with result
- * @dataProvider dataTests
- */
- public function testKeyDiffWithResult($data)
- {
- $configCache = new ConfigCache($data);
-
- $diffConfig = [
- 'fakeCat' => [
- 'fakeKey' => 'value',
- ]
- ];
-
- $this->assertEquals($diffConfig, $configCache->keyDiff($diffConfig));
- }
-
- /**
- * Test the keyDiff() method without result
- * @dataProvider dataTests
- */
- public function testKeyDiffWithoutResult($data)
- {
- $configCache = new ConfigCache($data);
-
- $diffConfig = $configCache->getAll();
-
- $this->assertEmpty($configCache->keyDiff($diffConfig));
- }
-
- /**
- * Test the default hiding of passwords inside the cache
- */
- public function testPasswordHide()
- {
- $configCache = new ConfigCache([
- '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()
- {
- $configCache = new ConfigCache([
- '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));
- }
-
- /**
- * Test a empty password
- */
- public function testEmptyPassword()
- {
- $configCache = new ConfigCache([
- 'database' => [
- 'password' => '',
- 'username' => '',
- ]
- ]);
-
- $this->assertNotEmpty($configCache->get('database', 'password'));
- $this->assertInstanceOf(HiddenString::class, $configCache->get('database', 'password'));
- $this->assertEmpty($configCache->get('database', 'username'));
- }
-
- public function testWrongTypePassword()
- {
- $configCache = new ConfigCache([
- 'database' => [
- 'password' => new \stdClass(),
- 'username' => '',
- ]
- ]);
-
- $this->assertNotEmpty($configCache->get('database', 'password'));
- $this->assertEmpty($configCache->get('database', 'username'));
-
- $configCache = new ConfigCache([
- 'database' => [
- 'password' => 23,
- 'username' => '',
- ]
- ]);
-
- $this->assertEquals(23, $configCache->get('database', 'password'));
- $this->assertEmpty($configCache->get('database', 'username'));
- }
- }
|