diff options
Diffstat (limited to 'tests/lib/AllConfigTest.php')
-rw-r--r-- | tests/lib/AllConfigTest.php | 142 |
1 files changed, 101 insertions, 41 deletions
diff --git a/tests/lib/AllConfigTest.php b/tests/lib/AllConfigTest.php index c703c47c94e..80a38d590f4 100644 --- a/tests/lib/AllConfigTest.php +++ b/tests/lib/AllConfigTest.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -14,17 +15,19 @@ namespace Test; * * @package Test */ - +use OC\AllConfig; use OC\SystemConfig; use OCP\IDBConnection; +use OCP\PreConditionNotMetException; +use OCP\Server; class AllConfigTest extends \Test\TestCase { - /** @var \OCP\IDBConnection */ + /** @var IDBConnection */ protected $connection; protected function getConfig($systemConfig = null, $connection = null) { if ($this->connection === null) { - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = Server::get(IDBConnection::class); } if ($connection === null) { $connection = $this->connection; @@ -34,16 +37,16 @@ class AllConfigTest extends \Test\TestCase { ->disableOriginalConstructor() ->getMock(); } - return new \OC\AllConfig($systemConfig, $connection); + return new AllConfig($systemConfig, $connection); } - public function testDeleteUserValue() { + public function testDeleteUserValue(): void { $config = $this->getConfig(); // preparation - add something to the database $this->connection->executeUpdate( - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' . - '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', ['userDelete', 'appDelete', 'keyDelete', 'valueDelete'] ); @@ -58,7 +61,7 @@ class AllConfigTest extends \Test\TestCase { $this->assertEquals(0, $actualCount, 'There was one value in the database and after the tests there should be no entry left.'); } - public function testSetUserValue() { + public function testSetUserValue(): void { $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; $config = $this->getConfig(); @@ -91,7 +94,28 @@ class AllConfigTest extends \Test\TestCase { $config->deleteUserValue('userSet', 'appSet', 'keySet'); } - public function testSetUserValueWithPreCondition() { + /** + * This test needs to stay! Emails are expected to be lowercase due to performance reasons. + * This way we can skip the expensive casing change on the database. + */ + public function testSetUserValueSettingsEmail(): void { + $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; + $config = $this->getConfig(); + + $config->setUserValue('userSet', 'settings', 'email', 'mixed.CASE@domain.COM'); + + $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAll(); + + $this->assertEquals(1, count($result)); + $this->assertEquals([ + 'userid' => 'userSet', + 'appid' => 'settings', + 'configkey' => 'email', + 'configvalue' => 'mixed.case@domain.com' + ], $result[0]); + } + + public function testSetUserValueWithPreCondition(): void { $config = $this->getConfig(); $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; @@ -125,7 +149,7 @@ class AllConfigTest extends \Test\TestCase { $config->deleteUserValue('userPreCond', 'appPreCond', 'keyPreCond'); } - public function dataSetUserValueUnexpectedValue() { + public static function dataSetUserValueUnexpectedValue(): array { return [ [true], [false], @@ -135,10 +159,10 @@ class AllConfigTest extends \Test\TestCase { } /** - * @dataProvider dataSetUserValueUnexpectedValue * @param mixed $value */ - public function testSetUserValueUnexpectedValue($value) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSetUserValueUnexpectedValue')] + public function testSetUserValueUnexpectedValue($value): void { $this->expectException(\UnexpectedValueException::class); $config = $this->getConfig(); @@ -146,8 +170,8 @@ class AllConfigTest extends \Test\TestCase { } - public function testSetUserValueWithPreConditionFailure() { - $this->expectException(\OCP\PreConditionNotMetException::class); + public function testSetUserValueWithPreConditionFailure(): void { + $this->expectException(PreConditionNotMetException::class); $config = $this->getConfig(); @@ -182,7 +206,43 @@ class AllConfigTest extends \Test\TestCase { $config->deleteUserValue('userPreCond1', 'appPreCond', 'keyPreCond'); } - public function testSetUserValueUnchanged() { + public function testSetUserValueWithPreConditionFailureWhenResultStillMatches(): void { + $this->expectException(PreConditionNotMetException::class); + + $config = $this->getConfig(); + + $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?'; + + $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond'); + + $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAll(); + + $this->assertCount(1, $result); + $this->assertEquals([ + 'userid' => 'userPreCond1', + 'appid' => 'appPreCond', + 'configkey' => 'keyPreCond', + 'configvalue' => 'valuePreCond' + ], $result[0]); + + // test if the method throws with invalid precondition when the value is the same + $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond', 'valuePreCond3'); + + $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAll(); + + $this->assertCount(1, $result); + $this->assertEquals([ + 'userid' => 'userPreCond1', + 'appid' => 'appPreCond', + 'configkey' => 'keyPreCond', + 'configvalue' => 'valuePreCond' + ], $result[0]); + + // cleanup + $config->deleteUserValue('userPreCond1', 'appPreCond', 'keyPreCond'); + } + + public function testSetUserValueUnchanged(): void { // TODO - FIXME until the dependency injection is handled properly (in AllConfig) $this->markTestSkipped('Skipped because this is just testable if database connection can be injected'); @@ -195,8 +255,8 @@ class AllConfigTest extends \Test\TestCase { $connectionMock = $this->createMock(IDBConnection::class); $connectionMock->expects($this->once()) ->method('executeQuery') - ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` '. - 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'), + ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` ' + . 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'), $this->equalTo(['userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged'])) ->willReturn($resultMock); $connectionMock->expects($this->never()) @@ -207,7 +267,7 @@ class AllConfigTest extends \Test\TestCase { $config->setUserValue('userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged', 'valueSetUnchanged'); } - public function testGetUserValue() { + public function testGetUserValue(): void { $config = $this->getConfig(); // setup - it therefore relies on the successful execution of the previous test @@ -245,7 +305,7 @@ class AllConfigTest extends \Test\TestCase { $this->assertEquals(0, count($result)); } - public function testGetUserKeys() { + public function testGetUserKeys(): void { $config = $this->getConfig(); // preparation - add something to the database @@ -260,8 +320,8 @@ class AllConfigTest extends \Test\TestCase { ]; foreach ($data as $entry) { $this->connection->executeUpdate( - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' . - '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', $entry ); } @@ -276,23 +336,23 @@ class AllConfigTest extends \Test\TestCase { $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); } - public function testGetUserKeysAllInts() { + public function testGetUserKeysAllInts(): void { $config = $this->getConfig(); // preparation - add something to the database $data = [ - ['userFetch', 'appFetch1', '123', 'value'], - ['userFetch', 'appFetch1', '456', 'value'], + ['userFetch8', 'appFetch1', '123', 'value'], + ['userFetch8', 'appFetch1', '456', 'value'], ]; foreach ($data as $entry) { $this->connection->executeUpdate( - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' . - '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', $entry ); } - $value = $config->getUserKeys('userFetch', 'appFetch1'); + $value = $config->getUserKeys('userFetch8', 'appFetch1'); $this->assertEquals(['123', '456'], $value); $this->assertIsString($value[0]); $this->assertIsString($value[1]); @@ -301,7 +361,7 @@ class AllConfigTest extends \Test\TestCase { $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); } - public function testGetUserValueDefault() { + public function testGetUserValueDefault(): void { $config = $this->getConfig(); $this->assertEquals('', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset')); @@ -309,7 +369,7 @@ class AllConfigTest extends \Test\TestCase { $this->assertEquals('foobar', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', 'foobar')); } - public function testGetUserValueForUsers() { + public function testGetUserValueForUsers(): void { $config = $this->getConfig(); // preparation - add something to the database @@ -324,8 +384,8 @@ class AllConfigTest extends \Test\TestCase { ]; foreach ($data as $entry) { $this->connection->executeUpdate( - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' . - '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', $entry ); } @@ -350,7 +410,7 @@ class AllConfigTest extends \Test\TestCase { $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); } - public function testDeleteAllUserValues() { + public function testDeleteAllUserValues(): void { $config = $this->getConfig(); // preparation - add something to the database @@ -365,8 +425,8 @@ class AllConfigTest extends \Test\TestCase { ]; foreach ($data as $entry) { $this->connection->executeUpdate( - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' . - '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', $entry ); } @@ -384,7 +444,7 @@ class AllConfigTest extends \Test\TestCase { $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); } - public function testDeleteAppFromAllUsers() { + public function testDeleteAppFromAllUsers(): void { $config = $this->getConfig(); // preparation - add something to the database @@ -399,8 +459,8 @@ class AllConfigTest extends \Test\TestCase { ]; foreach ($data as $entry) { $this->connection->executeUpdate( - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' . - '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', $entry ); } @@ -427,7 +487,7 @@ class AllConfigTest extends \Test\TestCase { $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); } - public function testGetUsersForUserValue() { + public function testGetUsersForUserValue(): void { // mock the check for the database to run the correct SQL statements for each database type $systemConfig = $this->getMockBuilder('\OC\SystemConfig') ->disableOriginalConstructor() @@ -445,8 +505,8 @@ class AllConfigTest extends \Test\TestCase { ]; foreach ($data as $entry) { $this->connection->executeUpdate( - 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' . - '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', + 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' + . '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)', $entry ); } @@ -458,7 +518,7 @@ class AllConfigTest extends \Test\TestCase { $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`'); } - public function testGetUsersForUserValueCaseInsensitive() { + public function testGetUsersForUserValueCaseInsensitive(): void { // mock the check for the database to run the correct SQL statements for each database type $systemConfig = $this->createMock(SystemConfig::class); $config = $this->getConfig($systemConfig); |