123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691 |
- <?php
- declare(strict_types=1);
- /**
- * @copyright Copyright (c) 2024, Maxence Lange <maxence@artificial-owl.com
- *
- * @author Maxence Lange <maxence@artificial-owl.com>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * 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, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace Test\AppFramework\Services;
- use OC\AppConfig as AppConfigCore;
- use OC\AppFramework\Services\AppConfig;
- use OCP\Exceptions\AppConfigTypeConflictException;
- use OCP\Exceptions\AppConfigUnknownKeyException;
- use OCP\IAppConfig as IAppConfigCore;
- use OCP\IConfig;
- use PHPUnit\Framework\MockObject\MockObject;
- use Test\TestCase;
- class AppConfigTest extends TestCase {
- private IConfig|MockObject $config;
- private IAppConfigCore|MockObject $appConfigCore;
- private AppConfig $appConfig;
- private const TEST_APPID = 'appconfig-test';
- protected function setUp(): void {
- parent::setUp();
- $this->config = $this->createMock(IConfig::class);
- $this->appConfigCore = $this->createMock(AppConfigCore::class);
-
- $this->appConfig = new AppConfig($this->config, $this->appConfigCore, self::TEST_APPID);
- }
- public function testGetAppKeys(): void {
- $expected = ['key1', 'key2', 'key3', 'key4', 'key5', 'key6', 'key7', 'test8'];
- $this->appConfigCore->expects($this->once())
- ->method('getKeys')
- ->with(self::TEST_APPID)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->getAppKeys());
- }
- /**
- * @return array
- * @see testHasAppKey
- */
- public function providerHasAppKey(): array {
- return [
- // lazy, expected
- [false, true],
- [true, true],
- [false, false],
- [true, false],
- ];
- }
- /**
- * @dataProvider providerHasAppKey
- *
- * @param bool $lazy
- * @param bool $expected
- */
- public function testHasAppKey(bool $lazy, bool $expected): void {
- $key = 'key';
- $this->appConfigCore->expects($this->once())
- ->method('hasKey')
- ->with(self::TEST_APPID, $key, $lazy)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->hasAppKey($key, $lazy));
- }
- /**
- * @return array
- * @see testIsSensitive
- */
- public function providerIsSensitive(): array {
- return [
- // lazy, expected
- [false, true],
- [true, true],
- [false, false],
- [true, false],
- ];
- }
- /**
- * @dataProvider providerIsSensitive
- *
- * @param bool $lazy
- * @param bool $expected
- */
- public function testIsSensitive(bool $lazy, bool $expected): void {
- $key = 'key';
- $this->appConfigCore->expects($this->once())
- ->method('isSensitive')
- ->with(self::TEST_APPID, $key, $lazy)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->isSensitive($key, $lazy));
- }
- /**
- * @dataProvider providerIsSensitive
- *
- * @param bool $lazy
- * @param bool $expected
- */
- public function testIsSensitiveException(bool $lazy, bool $expected): void {
- $key = 'unknown-key';
- $this->appConfigCore->expects($this->once())
- ->method('isSensitive')
- ->with(self::TEST_APPID, $key, $lazy)
- ->willThrowException(new AppConfigUnknownKeyException());
- $this->expectException(AppConfigUnknownKeyException::class);
- $this->appConfig->isSensitive($key, $lazy);
- }
- /**
- * @return array
- * @see testIsLazy
- */
- public function providerIsLazy(): array {
- return [
- // expected
- [true],
- [false],
- ];
- }
- /**
- * @dataProvider providerIsLazy
- *
- * @param bool $expected
- */
- public function testIsLazy(bool $expected): void {
- $key = 'key';
- $this->appConfigCore->expects($this->once())
- ->method('isLazy')
- ->with(self::TEST_APPID, $key)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->isLazy($key));
- }
- public function testIsLazyException(): void {
- $key = 'unknown-key';
- $this->appConfigCore->expects($this->once())
- ->method('isLazy')
- ->with(self::TEST_APPID, $key)
- ->willThrowException(new AppConfigUnknownKeyException());
- $this->expectException(AppConfigUnknownKeyException::class);
- $this->appConfig->isLazy($key);
- }
- /**
- * @return array
- * @see testGetAllAppValues
- */
- public function providerGetAllAppValues(): array {
- return [
- // key, filtered
- ['', false],
- ['', true],
- ['key', false],
- ['key', true],
- ];
- }
- /**
- * @dataProvider providerGetAllAppValues
- *
- * @param string $key
- * @param bool $filtered
- */
- public function testGetAllAppValues(string $key, bool $filtered): void {
- $expected = [
- 'key1' => 'value1',
- 'key2' => 3,
- 'key3' => 3.14,
- 'key4' => true
- ];
- $this->appConfigCore->expects($this->once())
- ->method('getAllValues')
- ->with(self::TEST_APPID, $key, $filtered)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->getAllAppValues($key, $filtered));
- }
- public function testSetAppValue(): void {
- $key = 'key';
- $value = 'value';
- $this->appConfigCore->expects($this->once())
- ->method('setValueMixed')
- ->with(self::TEST_APPID, $key, $value);
- $this->appConfig->setAppValue($key, $value);
- }
- /**
- * @return array
- * @see testSetAppValueString
- * @see testSetAppValueStringException
- * @see testSetAppValueInt
- * @see testSetAppValueIntException
- * @see testSetAppValueFloat
- * @see testSetAppValueFloatException
- * @see testSetAppValueArray
- * @see testSetAppValueArrayException
- */
- public function providerSetAppValue(): array {
- return [
- // lazy, sensitive, expected
- [false, false, true],
- [false, true, true],
- [true, true, true],
- [true, false, true],
- [false, false, false],
- [false, true, false],
- [true, true, false],
- [true, false, false],
- ];
- }
- /**
- * @dataProvider providerSetAppValue
- *
- * @param bool $lazy
- * @param bool $sensitive
- * @param bool $expected
- */
- public function testSetAppValueString(bool $lazy, bool $sensitive, bool $expected): void {
- $key = 'key';
- $value = 'valueString';
- $this->appConfigCore->expects($this->once())
- ->method('setValueString')
- ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->setAppValueString($key, $value, $lazy, $sensitive));
- }
- /**
- * @dataProvider providerSetAppValue
- *
- * @param bool $lazy
- * @param bool $sensitive
- */
- public function testSetAppValueStringException(bool $lazy, bool $sensitive): void {
- $key = 'key';
- $value = 'valueString';
- $this->appConfigCore->expects($this->once())
- ->method('setValueString')
- ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
- ->willThrowException(new AppConfigTypeConflictException());
- $this->expectException(AppConfigTypeConflictException::class);
- $this->appConfig->setAppValueString($key, $value, $lazy, $sensitive);
- }
- /**
- * @dataProvider providerSetAppValue
- *
- * @param bool $lazy
- * @param bool $sensitive
- * @param bool $expected
- */
- public function testSetAppValueInt(bool $lazy, bool $sensitive, bool $expected): void {
- $key = 'key';
- $value = 42;
- $this->appConfigCore->expects($this->once())
- ->method('setValueInt')
- ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->setAppValueInt($key, $value, $lazy, $sensitive));
- }
- /**
- * @dataProvider providerSetAppValue
- *
- * @param bool $lazy
- * @param bool $sensitive
- */
- public function testSetAppValueIntException(bool $lazy, bool $sensitive): void {
- $key = 'key';
- $value = 42;
- $this->appConfigCore->expects($this->once())
- ->method('setValueInt')
- ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
- ->willThrowException(new AppConfigTypeConflictException());
- $this->expectException(AppConfigTypeConflictException::class);
- $this->appConfig->setAppValueInt($key, $value, $lazy, $sensitive);
- }
- /**
- * @dataProvider providerSetAppValue
- *
- * @param bool $lazy
- * @param bool $sensitive
- * @param bool $expected
- */
- public function testSetAppValueFloat(bool $lazy, bool $sensitive, bool $expected): void {
- $key = 'key';
- $value = 3.14;
- $this->appConfigCore->expects($this->once())
- ->method('setValueFloat')
- ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->setAppValueFloat($key, $value, $lazy, $sensitive));
- }
- /**
- * @dataProvider providerSetAppValue
- *
- * @param bool $lazy
- * @param bool $sensitive
- */
- public function testSetAppValueFloatException(bool $lazy, bool $sensitive): void {
- $key = 'key';
- $value = 3.14;
- $this->appConfigCore->expects($this->once())
- ->method('setValueFloat')
- ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
- ->willThrowException(new AppConfigTypeConflictException());
- $this->expectException(AppConfigTypeConflictException::class);
- $this->appConfig->setAppValueFloat($key, $value, $lazy, $sensitive);
- }
- /**
- * @return array
- * @see testSetAppValueBool
- */
- public function providerSetAppValueBool(): array {
- return [
- // lazy, expected
- [false, true],
- [false, false],
- [true, true],
- [true, false],
- ];
- }
- /**
- * @dataProvider providerSetAppValueBool
- *
- * @param bool $lazy
- * @param bool $expected
- */
- public function testSetAppValueBool(bool $lazy, bool $expected): void {
- $key = 'key';
- $value = true;
- $this->appConfigCore->expects($this->once())
- ->method('setValueBool')
- ->with(self::TEST_APPID, $key, $value, $lazy)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->setAppValueBool($key, $value, $lazy));
- }
- /**
- * @dataProvider providerSetAppValueBool
- *
- * @param bool $lazy
- */
- public function testSetAppValueBoolException(bool $lazy): void {
- $key = 'key';
- $value = true;
- $this->appConfigCore->expects($this->once())
- ->method('setValueBool')
- ->with(self::TEST_APPID, $key, $value, $lazy)
- ->willThrowException(new AppConfigTypeConflictException());
- $this->expectException(AppConfigTypeConflictException::class);
- $this->appConfig->setAppValueBool($key, $value, $lazy);
- }
- /**
- * @dataProvider providerSetAppValue
- *
- * @param bool $lazy
- * @param bool $sensitive
- * @param bool $expected
- */
- public function testSetAppValueArray(bool $lazy, bool $sensitive, bool $expected): void {
- $key = 'key';
- $value = ['item' => true];
- $this->appConfigCore->expects($this->once())
- ->method('setValueArray')
- ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->setAppValueArray($key, $value, $lazy, $sensitive));
- }
- /**
- * @dataProvider providerSetAppValue
- *
- * @param bool $lazy
- * @param bool $sensitive
- */
- public function testSetAppValueArrayException(bool $lazy, bool $sensitive): void {
- $key = 'key';
- $value = ['item' => true];
- $this->appConfigCore->expects($this->once())
- ->method('setValueArray')
- ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
- ->willThrowException(new AppConfigTypeConflictException());
- $this->expectException(AppConfigTypeConflictException::class);
- $this->appConfig->setAppValueArray($key, $value, $lazy, $sensitive);
- }
- public function testGetAppValue(): void {
- $key = 'key';
- $value = 'value';
- $default = 'default';
- $this->appConfigCore->expects($this->once())
- ->method('getValueMixed')
- ->with(self::TEST_APPID, $key, $default)
- ->willReturn($value);
- $this->assertSame($value, $this->appConfig->getAppValue($key, $default));
- }
- public function testGetAppValueDefault(): void {
- $key = 'key';
- $default = 'default';
- $this->appConfigCore->expects($this->once())
- ->method('getValueMixed')
- ->with(self::TEST_APPID, $key, $default)
- ->willReturn($default);
- $this->assertSame($default, $this->appConfig->getAppValue($key, $default));
- }
- /**
- * @return array
- * @see testGetAppValueString
- * @see testGetAppValueStringException
- * @see testGetAppValueInt
- * @see testGetAppValueIntException
- * @see testGetAppValueFloat
- * @see testGetAppValueFloatException
- * @see testGetAppValueBool
- * @see testGetAppValueBoolException
- * @see testGetAppValueArray
- * @see testGetAppValueArrayException
- */
- public function providerGetAppValue(): array {
- return [
- // lazy, exist
- [false, false],
- [false, true],
- [true, true],
- [true, false]
- ];
- }
- /**
- * @dataProvider providerGetAppValue
- *
- * @param bool $lazy
- * @param bool $exist
- */
- public function testGetAppValueString(bool $lazy, bool $exist): void {
- $key = 'key';
- $value = 'valueString';
- $default = 'default';
- $expected = ($exist) ? $value : $default;
- $this->appConfigCore->expects($this->once())
- ->method('getValueString')
- ->with(self::TEST_APPID, $key, $default, $lazy)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->getAppValueString($key, $default, $lazy));
- }
- /**
- * @dataProvider providerGetAppValue
- *
- * @param bool $lazy
- */
- public function testGetAppValueStringException(bool $lazy): void {
- $key = 'key';
- $default = 'default';
- $this->appConfigCore->expects($this->once())
- ->method('getValueString')
- ->with(self::TEST_APPID, $key, $default, $lazy)
- ->willThrowException(new AppConfigTypeConflictException());
- $this->expectException(AppConfigTypeConflictException::class);
- $this->appConfig->getAppValueString($key, $default, $lazy);
- }
- /**
- * @dataProvider providerGetAppValue
- *
- * @param bool $lazy
- * @param bool $exist
- */
- public function testGetAppValueInt(bool $lazy, bool $exist): void {
- $key = 'key';
- $value = 42;
- $default = 17;
- $expected = ($exist) ? $value : $default;
- $this->appConfigCore->expects($this->once())
- ->method('getValueInt')
- ->with(self::TEST_APPID, $key, $default, $lazy)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->getAppValueInt($key, $default, $lazy));
- }
- /**
- * @dataProvider providerGetAppValue
- *
- * @param bool $lazy
- */
- public function testGetAppValueIntException(bool $lazy): void {
- $key = 'key';
- $default = 17;
- $this->appConfigCore->expects($this->once())
- ->method('getValueInt')
- ->with(self::TEST_APPID, $key, $default, $lazy)
- ->willThrowException(new AppConfigTypeConflictException());
- $this->expectException(AppConfigTypeConflictException::class);
- $this->appConfig->getAppValueInt($key, $default, $lazy);
- }
- /**
- * @dataProvider providerGetAppValue
- *
- * @param bool $lazy
- * @param bool $exist
- */
- public function testGetAppValueFloat(bool $lazy, bool $exist): void {
- $key = 'key';
- $value = 3.14;
- $default = 17.04;
- $expected = ($exist) ? $value : $default;
- $this->appConfigCore->expects($this->once())
- ->method('getValueFloat')
- ->with(self::TEST_APPID, $key, $default, $lazy)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->getAppValueFloat($key, $default, $lazy));
- }
- /**
- * @dataProvider providerGetAppValue
- *
- * @param bool $lazy
- */
- public function testGetAppValueFloatException(bool $lazy): void {
- $key = 'key';
- $default = 17.04;
- $this->appConfigCore->expects($this->once())
- ->method('getValueFloat')
- ->with(self::TEST_APPID, $key, $default, $lazy)
- ->willThrowException(new AppConfigTypeConflictException());
- $this->expectException(AppConfigTypeConflictException::class);
- $this->appConfig->getAppValueFloat($key, $default, $lazy);
- }
- /**
- * @dataProvider providerGetAppValue
- *
- * @param bool $lazy
- * @param bool $exist
- */
- public function testGetAppValueBool(bool $lazy, bool $exist): void {
- $key = 'key';
- $value = true;
- $default = false;
- $expected = ($exist) ? $value : $default; // yes, it can be simplified
- $this->appConfigCore->expects($this->once())
- ->method('getValueBool')
- ->with(self::TEST_APPID, $key, $default, $lazy)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->getAppValueBool($key, $default, $lazy));
- }
- /**
- * @dataProvider providerGetAppValue
- *
- * @param bool $lazy
- */
- public function testGetAppValueBoolException(bool $lazy): void {
- $key = 'key';
- $default = false;
- $this->appConfigCore->expects($this->once())
- ->method('getValueBool')
- ->with(self::TEST_APPID, $key, $default, $lazy)
- ->willThrowException(new AppConfigTypeConflictException());
- $this->expectException(AppConfigTypeConflictException::class);
- $this->appConfig->getAppValueBool($key, $default, $lazy);
- }
- /**
- * @dataProvider providerGetAppValue
- *
- * @param bool $lazy
- * @param bool $exist
- */
- public function testGetAppValueArray(bool $lazy, bool $exist): void {
- $key = 'key';
- $value = ['item' => true];
- $default = [];
- $expected = ($exist) ? $value : $default;
- $this->appConfigCore->expects($this->once())
- ->method('getValueArray')
- ->with(self::TEST_APPID, $key, $default, $lazy)
- ->willReturn($expected);
- $this->assertSame($expected, $this->appConfig->getAppValueArray($key, $default, $lazy));
- }
- /**
- * @dataProvider providerGetAppValue
- *
- * @param bool $lazy
- */
- public function testGetAppValueArrayException(bool $lazy): void {
- $key = 'key';
- $default = [];
- $this->appConfigCore->expects($this->once())
- ->method('getValueArray')
- ->with(self::TEST_APPID, $key, $default, $lazy)
- ->willThrowException(new AppConfigTypeConflictException());
- $this->expectException(AppConfigTypeConflictException::class);
- $this->appConfig->getAppValueArray($key, $default, $lazy);
- }
- public function testDeleteAppValue(): void {
- $key = 'key';
- $this->appConfigCore->expects($this->once())
- ->method('deleteKey')
- ->with(self::TEST_APPID, $key);
- $this->appConfig->deleteAppValue($key);
- }
- public function testDeleteAppValues(): void {
- $this->appConfigCore->expects($this->once())
- ->method('deleteApp')
- ->with(self::TEST_APPID);
- $this->appConfig->deleteAppValues();
- }
- }
|