diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2024-01-11 15:32:58 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2024-01-15 15:45:13 -0100 |
commit | f7d0c74b1003af6c47dd6ec74f4ef904a2681d62 (patch) | |
tree | a0bfe1934fbec0ccf6651824589a0e67ae4f9d9f /tests | |
parent | e5ef58b7b9b8f7a232666df17d286d43fb4d1201 (diff) | |
download | nextcloud-server-f7d0c74b1003af6c47dd6ec74f4ef904a2681d62.tar.gz nextcloud-server-f7d0c74b1003af6c47dd6ec74f4ef904a2681d62.zip |
lazy AppConfig
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Core/Command/Config/App/GetConfigTest.php | 23 | ||||
-rw-r--r-- | tests/Core/Command/Config/App/SetConfigTest.php | 44 | ||||
-rw-r--r-- | tests/lib/AppConfigTest.php | 69 | ||||
-rw-r--r-- | tests/lib/DB/QueryBuilder/FunctionBuilderTest.php | 1 |
4 files changed, 78 insertions, 59 deletions
diff --git a/tests/Core/Command/Config/App/GetConfigTest.php b/tests/Core/Command/Config/App/GetConfigTest.php index 521ecfbfb40..5988d8d867f 100644 --- a/tests/Core/Command/Config/App/GetConfigTest.php +++ b/tests/Core/Command/Config/App/GetConfigTest.php @@ -21,8 +21,9 @@ namespace Tests\Core\Command\Config\App; +use OC\AppConfig; use OC\Core\Command\Config\App\GetConfig; -use OCP\IConfig; +use OCP\Exceptions\AppConfigUnknownKeyException; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; @@ -42,13 +43,13 @@ class GetConfigTest extends TestCase { protected function setUp(): void { parent::setUp(); - $config = $this->config = $this->getMockBuilder(IConfig::class) + $config = $this->config = $this->getMockBuilder(AppConfig::class) ->disableOriginalConstructor() ->getMock(); $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); - /** @var \OCP\IConfig $config */ + /** @var \OCP\IAppConfig $config */ $this->command = new GetConfig($config); } @@ -108,20 +109,22 @@ class GetConfigTest extends TestCase { * @param string $expectedMessage */ public function testGet($configName, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage) { - $this->config->expects($this->atLeastOnce()) - ->method('getAppKeys') - ->with('app-name') - ->willReturn($configExists ? [$configName] : []); - if (!$expectedReturn) { if ($configExists) { $this->config->expects($this->once()) - ->method('getAppValue') + ->method('getDetails') ->with('app-name', $configName) - ->willReturn($value); + ->willReturn(['value' => $value]); } } + if (!$configExists) { + $this->config->expects($this->once()) + ->method('getDetails') + ->with('app-name', $configName) + ->willThrowException(new AppConfigUnknownKeyException()); + } + $this->consoleInput->expects($this->exactly(2)) ->method('getArgument') ->willReturnMap([ diff --git a/tests/Core/Command/Config/App/SetConfigTest.php b/tests/Core/Command/Config/App/SetConfigTest.php index 88053f8c189..4918053048a 100644 --- a/tests/Core/Command/Config/App/SetConfigTest.php +++ b/tests/Core/Command/Config/App/SetConfigTest.php @@ -21,8 +21,10 @@ namespace Tests\Core\Command\Config\App; +use OC\AppConfig; use OC\Core\Command\Config\App\SetConfig; -use OCP\IConfig; +use OCP\Exceptions\AppConfigUnknownKeyException; +use OCP\IAppConfig; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; @@ -42,13 +44,13 @@ class SetConfigTest extends TestCase { protected function setUp(): void { parent::setUp(); - $config = $this->config = $this->getMockBuilder(IConfig::class) + $config = $this->config = $this->getMockBuilder(AppConfig::class) ->disableOriginalConstructor() ->getMock(); $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock(); $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock(); - /** @var \OCP\IConfig $config */ + /** @var \OCP\IAppConfig $config */ $this->command = new SetConfig($config); } @@ -85,14 +87,24 @@ class SetConfigTest extends TestCase { * @param string $expectedMessage */ public function testSet($configName, $newValue, $configExists, $updateOnly, $updated, $expectedMessage) { - $this->config->expects($this->once()) - ->method('getAppKeys') - ->with('app-name') - ->willReturn($configExists ? [$configName] : []); + $this->config->expects($this->any()) + ->method('hasKey') + ->with('app-name', $configName) + ->willReturn($configExists); + + if (!$configExists) { + $this->config->expects($this->any()) + ->method('getValueType') + ->willThrowException(new AppConfigUnknownKeyException()); + } else { + $this->config->expects($this->any()) + ->method('getValueType') + ->willReturn(IAppConfig::VALUE_MIXED); + } if ($updated) { $this->config->expects($this->once()) - ->method('setAppValue') + ->method('setValueMixed') ->with('app-name', $configName, $newValue); } @@ -104,13 +116,19 @@ class SetConfigTest extends TestCase { ]); $this->consoleInput->expects($this->any()) ->method('getOption') - ->with('value') - ->willReturn($newValue); + ->willReturnMap([ + ['value', $newValue], + ['lazy', null], + ['sensitive', null], + ['no-interaction', true], + ]); $this->consoleInput->expects($this->any()) ->method('hasParameterOption') - ->with('--update-only') - ->willReturn($updateOnly); - + ->willReturnMap([ + ['--type', false, false], + ['--value', false, true], + ['--update-only', false, $updateOnly] + ]); $this->consoleOutput->expects($this->any()) ->method('writeln') ->with($this->stringContains($expectedMessage)); diff --git a/tests/lib/AppConfigTest.php b/tests/lib/AppConfigTest.php index d4ae66cb2f1..a4d4119ba96 100644 --- a/tests/lib/AppConfigTest.php +++ b/tests/lib/AppConfigTest.php @@ -10,8 +10,9 @@ namespace Test; use OC\AppConfig; -use OC\DB\Connection; use OCP\IConfig; +use OCP\IDBConnection; +use Psr\Log\LoggerInterface; /** * Class AppConfigTest @@ -24,15 +25,17 @@ class AppConfigTest extends TestCase { /** @var \OCP\IAppConfig */ protected $appConfig; - /** @var Connection */ - protected $connection; + protected IDBConnection $connection; + private LoggerInterface $logger; protected $originalConfig; protected function setUp(): void { parent::setUp(); - $this->connection = \OC::$server->get(Connection::class); + $this->connection = \OC::$server->get(IDBConnection::class); + $this->logger = \OC::$server->get(LoggerInterface::class); + $sql = $this->connection->getQueryBuilder(); $sql->select('*') ->from('appconfig'); @@ -44,20 +47,20 @@ class AppConfigTest extends TestCase { $sql->delete('appconfig'); $sql->execute(); - $this->overwriteService(AppConfig::class, new \OC\AppConfig($this->connection)); + $this->overwriteService(AppConfig::class, new \OC\AppConfig($this->connection, $this->logger)); $sql = $this->connection->getQueryBuilder(); $sql->insert('appconfig') ->values([ 'appid' => $sql->createParameter('appid'), 'configkey' => $sql->createParameter('configkey'), - 'configvalue' => $sql->createParameter('configvalue'), + 'configvalue' => $sql->createParameter('configvalue') ]); $sql->setParameters([ 'appid' => 'testapp', 'configkey' => 'enabled', - 'configvalue' => 'true', + 'configvalue' => 'true' ])->execute(); $sql->setParameters([ 'appid' => 'testapp', @@ -125,12 +128,16 @@ class AppConfigTest extends TestCase { 'appid' => $sql->createParameter('appid'), 'configkey' => $sql->createParameter('configkey'), 'configvalue' => $sql->createParameter('configvalue'), + 'lazy' => $sql->createParameter('lazy'), + 'type' => $sql->createParameter('type'), ]); foreach ($this->originalConfig as $configs) { $sql->setParameter('appid', $configs['appid']) ->setParameter('configkey', $configs['configkey']) - ->setParameter('configvalue', $configs['configvalue']); + ->setParameter('configvalue', $configs['configvalue']) + ->setParameter('lazy', ($configs['lazy'] === '1') ? '1' : '0') + ->setParameter('type', $configs['type']); $sql->execute(); } @@ -139,7 +146,7 @@ class AppConfigTest extends TestCase { } public function testGetApps() { - $config = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $this->assertEqualsCanonicalizing([ 'anotherapp', @@ -150,7 +157,7 @@ class AppConfigTest extends TestCase { } public function testGetKeys() { - $config = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $keys = $config->getKeys('testapp'); $this->assertEqualsCanonicalizing([ @@ -163,7 +170,7 @@ class AppConfigTest extends TestCase { } public function testGetValue() { - $config = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $value = $config->getValue('testapp', 'installed_version'); $this->assertConfigKey('testapp', 'installed_version', $value); @@ -176,7 +183,7 @@ class AppConfigTest extends TestCase { } public function testHasKey() { - $config = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $this->assertTrue($config->hasKey('testapp', 'installed_version')); $this->assertFalse($config->hasKey('testapp', 'nonexistant')); @@ -184,13 +191,13 @@ class AppConfigTest extends TestCase { } public function testSetValueUpdate() { - $config = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $this->assertEquals('1.2.3', $config->getValue('testapp', 'installed_version')); $this->assertConfigKey('testapp', 'installed_version', '1.2.3'); $wasModified = $config->setValue('testapp', 'installed_version', '1.2.3'); - if (!(\OC::$server->get(Connection::class) instanceof \OC\DB\OracleConnection)) { + if (!(\OC::$server->get(IDBConnection::class) instanceof \OC\DB\OracleConnection)) { $this->assertFalse($wasModified); } @@ -208,7 +215,7 @@ class AppConfigTest extends TestCase { } public function testSetValueInsert() { - $config = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $this->assertFalse($config->hasKey('someapp', 'somekey')); $this->assertNull($config->getValue('someapp', 'somekey')); @@ -220,13 +227,13 @@ class AppConfigTest extends TestCase { $this->assertConfigKey('someapp', 'somekey', 'somevalue'); $wasInserted = $config->setValue('someapp', 'somekey', 'somevalue'); - if (!(\OC::$server->get(Connection::class) instanceof \OC\DB\OracleConnection)) { + if (!(\OC::$server->get(IDBConnection::class) instanceof \OC\DB\OracleConnection)) { $this->assertFalse($wasInserted); } } public function testDeleteKey() { - $config = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $this->assertTrue($config->hasKey('testapp', 'deletethis')); @@ -248,7 +255,7 @@ class AppConfigTest extends TestCase { } public function testDeleteApp() { - $config = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $this->assertTrue($config->hasKey('someapp', 'otherkey')); @@ -268,7 +275,7 @@ class AppConfigTest extends TestCase { } public function testGetValuesNotAllowed() { - $config = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $this->assertFalse($config->getValues('testapp', 'enabled')); @@ -276,7 +283,7 @@ class AppConfigTest extends TestCase { } public function testGetValues() { - $config = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder(); $sql->select(['configkey', 'configvalue']) @@ -310,20 +317,10 @@ class AppConfigTest extends TestCase { } public function testGetFilteredValues() { - /** @var \OC\AppConfig|\PHPUnit\Framework\MockObject\MockObject $config */ - $config = $this->getMockBuilder(\OC\AppConfig::class) - ->setConstructorArgs([\OC::$server->get(Connection::class)]) - ->setMethods(['getValues']) - ->getMock(); - - $config->expects($this->once()) - ->method('getValues') - ->with('user_ldap', false) - ->willReturn([ - 'ldap_agent_password' => 'secret', - 's42ldap_agent_password' => 'secret', - 'ldap_dn' => 'dn', - ]); + $config = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); + $config->setValue('user_ldap', 'ldap_agent_password', 'secret'); + $config->setValue('user_ldap', 's42ldap_agent_password', 'secret'); + $config->setValue('user_ldap', 'ldap_dn', 'dn'); $values = $config->getFilteredValues('user_ldap'); $this->assertEquals([ @@ -334,8 +331,8 @@ class AppConfigTest extends TestCase { } public function testSettingConfigParallel() { - $appConfig1 = new \OC\AppConfig(\OC::$server->get(Connection::class)); - $appConfig2 = new \OC\AppConfig(\OC::$server->get(Connection::class)); + $appConfig1 = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); + $appConfig2 = new \OC\AppConfig(\OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); $appConfig1->getValue('testapp', 'foo', 'v1'); $appConfig2->getValue('testapp', 'foo', 'v1'); diff --git a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php index 08392b09d8d..430c7d24950 100644 --- a/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php +++ b/tests/lib/DB/QueryBuilder/FunctionBuilderTest.php @@ -50,6 +50,7 @@ class FunctionBuilderTest extends TestCase { if ($real) { $this->addDummyData(); $query->where($query->expr()->eq('appid', $query->createNamedParameter('group_concat'))); + $query->orderBy('configkey', 'asc'); } $query->select($query->func()->concat(...$arguments)); |