aboutsummaryrefslogtreecommitdiffstats
path: root/apps/federation/tests/DbHandlerTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/federation/tests/DbHandlerTest.php')
-rw-r--r--apps/federation/tests/DbHandlerTest.php144
1 files changed, 52 insertions, 92 deletions
diff --git a/apps/federation/tests/DbHandlerTest.php b/apps/federation/tests/DbHandlerTest.php
index 56645b307d1..5452a48fc4a 100644
--- a/apps/federation/tests/DbHandlerTest.php
+++ b/apps/federation/tests/DbHandlerTest.php
@@ -1,28 +1,10 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- *
- * @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/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Federation\Tests;
@@ -30,30 +12,24 @@ use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
use OCP\IDBConnection;
use OCP\IL10N;
+use OCP\Server;
+use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
/**
* @group DB
*/
class DbHandlerTest extends TestCase {
-
- /** @var DbHandler */
- private $dbHandler;
-
- /** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
- private $il10n;
-
- /** @var IDBConnection */
- private $connection;
-
- /** @var string */
- private $dbTable = 'trusted_servers';
+ private DbHandler $dbHandler;
+ private IL10N&MockObject $il10n;
+ private IDBConnection $connection;
+ private string $dbTable = 'trusted_servers';
protected function setUp(): void {
parent::setUp();
- $this->connection = \OC::$server->getDatabaseConnection();
- $this->il10n = $this->getMockBuilder(IL10N::class)->getMock();
+ $this->connection = Server::get(IDBConnection::class);
+ $this->il10n = $this->createMock(IL10N::class);
$this->dbHandler = new DbHandler(
$this->connection,
@@ -62,26 +38,27 @@ class DbHandlerTest extends TestCase {
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
- $qResult = $query->execute();
+ $qResult = $query->executeQuery();
$result = $qResult->fetchAll();
$qResult->closeCursor();
$this->assertEmpty($result, 'we need to start with a empty trusted_servers table');
}
protected function tearDown(): void {
- parent::tearDown();
$query = $this->connection->getQueryBuilder()->delete($this->dbTable);
- $query->execute();
+ $query->executeStatement()
+ ;
+ parent::tearDown();
}
/**
- * @dataProvider dataTestAddServer
*
* @param string $url passed to the method
* @param string $expectedUrl the url we expect to be written to the db
* @param string $expectedHash the hash value we expect to be written to the db
*/
- public function testAddServer($url, $expectedUrl, $expectedHash) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestAddServer')]
+ public function testAddServer(string $url, string $expectedUrl, string $expectedHash): void {
$id = $this->dbHandler->addServer($url);
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
@@ -89,14 +66,14 @@ class DbHandlerTest extends TestCase {
$qResult = $query->execute();
$result = $qResult->fetchAll();
$qResult->closeCursor();
- $this->assertSame(1, count($result));
+ $this->assertCount(1, $result);
$this->assertSame($expectedUrl, $result[0]['url']);
$this->assertSame($id, (int)$result[0]['id']);
$this->assertSame($expectedHash, $result[0]['url_hash']);
$this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
}
- public function dataTestAddServer() {
+ public static function dataTestAddServer(): array {
return [
['http://owncloud.org', 'http://owncloud.org', sha1('owncloud.org')],
['https://owncloud.org', 'https://owncloud.org', sha1('owncloud.org')],
@@ -104,7 +81,7 @@ class DbHandlerTest extends TestCase {
];
}
- public function testRemove() {
+ public function testRemove(): void {
$id1 = $this->dbHandler->addServer('server1');
$id2 = $this->dbHandler->addServer('server2');
@@ -113,7 +90,7 @@ class DbHandlerTest extends TestCase {
$qResult = $query->execute();
$result = $qResult->fetchAll();
$qResult->closeCursor();
- $this->assertSame(2, count($result));
+ $this->assertCount(2, $result);
$this->assertSame('server1', $result[0]['url']);
$this->assertSame('server2', $result[1]['url']);
$this->assertSame($id1, (int)$result[0]['id']);
@@ -125,13 +102,13 @@ class DbHandlerTest extends TestCase {
$qResult = $query->execute();
$result = $qResult->fetchAll();
$qResult->closeCursor();
- $this->assertSame(1, count($result));
+ $this->assertCount(1, $result);
$this->assertSame('server1', $result[0]['url']);
$this->assertSame($id1, (int)$result[0]['id']);
}
- public function testGetServerById() {
+ public function testGetServerById(): void {
$this->dbHandler->addServer('server1');
$id = $this->dbHandler->addServer('server2');
@@ -139,7 +116,7 @@ class DbHandlerTest extends TestCase {
$this->assertSame('server2', $result['url']);
}
- public function testGetAll() {
+ public function testGetAll(): void {
$id1 = $this->dbHandler->addServer('server1');
$id2 = $this->dbHandler->addServer('server2');
@@ -151,21 +128,15 @@ class DbHandlerTest extends TestCase {
$this->assertSame($id2, (int)$result[1]['id']);
}
- /**
- * @dataProvider dataTestServerExists
- *
- * @param string $serverInTable
- * @param string $checkForServer
- * @param bool $expected
- */
- public function testServerExists($serverInTable, $checkForServer, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestServerExists')]
+ public function testServerExists(string $serverInTable, string $checkForServer, bool $expected): void {
$this->dbHandler->addServer($serverInTable);
$this->assertSame($expected,
$this->dbHandler->serverExists($checkForServer)
);
}
- public function dataTestServerExists() {
+ public static function dataTestServerExists(): array {
return [
['server1', 'server1', true],
['server1', 'http://server1', true],
@@ -177,22 +148,22 @@ class DbHandlerTest extends TestCase {
$this->dbHandler->addServer('server1');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
- $qResult = $query->execute();
+ $qResult = $query->executeQuery();
$result = $qResult->fetchAll();
$qResult->closeCursor();
- $this->assertSame(1, count($result));
+ $this->assertCount(1, $result);
$this->assertSame(null, $result[0]['token']);
$this->dbHandler->addToken('http://server1', 'token');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
- $qResult = $query->execute();
+ $qResult = $query->executeQuery();
$result = $qResult->fetchAll();
$qResult->closeCursor();
- $this->assertSame(1, count($result));
+ $this->assertCount(1, $result);
$this->assertSame('token', $result[0]['token']);
}
- public function testGetToken() {
+ public function testGetToken(): void {
$this->dbHandler->addServer('server1');
$this->dbHandler->addToken('http://server1', 'token');
$this->assertSame('token',
@@ -207,7 +178,7 @@ class DbHandlerTest extends TestCase {
$qResult = $query->execute();
$result = $qResult->fetchAll();
$qResult->closeCursor();
- $this->assertSame(1, count($result));
+ $this->assertCount(1, $result);
$this->assertSame(null, $result[0]['shared_secret']);
$this->dbHandler->addSharedSecret('http://server1', 'secret');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
@@ -215,11 +186,11 @@ class DbHandlerTest extends TestCase {
$qResult = $query->execute();
$result = $qResult->fetchAll();
$qResult->closeCursor();
- $this->assertSame(1, count($result));
+ $this->assertCount(1, $result);
$this->assertSame('secret', $result[0]['shared_secret']);
}
- public function testGetSharedSecret() {
+ public function testGetSharedSecret(): void {
$this->dbHandler->addServer('server1');
$this->dbHandler->addSharedSecret('http://server1', 'secret');
$this->assertSame('secret',
@@ -227,26 +198,26 @@ class DbHandlerTest extends TestCase {
);
}
- public function testSetServerStatus() {
+ public function testSetServerStatus(): void {
$this->dbHandler->addServer('server1');
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
- $qResult = $query->execute();
+ $qResult = $query->executeQuery();
$result = $qResult->fetchAll();
$qResult->closeCursor();
- $this->assertSame(1, count($result));
+ $this->assertCount(1, $result);
$this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
$this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
- $qResult = $query->execute();
+ $qResult = $query->executeQuery();
$result = $qResult->fetchAll();
$qResult->closeCursor();
- $this->assertSame(1, count($result));
+ $this->assertCount(1, $result);
$this->assertSame(TrustedServers::STATUS_OK, (int)$result[0]['status']);
}
- public function testGetServerStatus() {
+ public function testGetServerStatus(): void {
$this->dbHandler->addServer('server1');
$this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
$this->assertSame(TrustedServers::STATUS_OK,
@@ -261,19 +232,15 @@ class DbHandlerTest extends TestCase {
/**
* hash should always be computed with the normalized URL
- *
- * @dataProvider dataTestHash
- *
- * @param string $url
- * @param string $expected
*/
- public function testHash($url, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestHash')]
+ public function testHash(string $url, string $expected): void {
$this->assertSame($expected,
$this->invokePrivate($this->dbHandler, 'hash', [$url])
);
}
- public function dataTestHash() {
+ public static function dataTestHash(): array {
return [
['server1', sha1('server1')],
['http://server1', sha1('server1')],
@@ -282,19 +249,14 @@ class DbHandlerTest extends TestCase {
];
}
- /**
- * @dataProvider dataTestNormalizeUrl
- *
- * @param string $url
- * @param string $expected
- */
- public function testNormalizeUrl($url, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestNormalizeUrl')]
+ public function testNormalizeUrl(string $url, string $expected): void {
$this->assertSame($expected,
$this->invokePrivate($this->dbHandler, 'normalizeUrl', [$url])
);
}
- public function dataTestNormalizeUrl() {
+ public static function dataTestNormalizeUrl(): array {
return [
['owncloud.org', 'owncloud.org'],
['http://owncloud.org', 'owncloud.org'],
@@ -304,10 +266,8 @@ class DbHandlerTest extends TestCase {
];
}
- /**
- * @dataProvider providesAuth
- */
- public function testAuth($expectedResult, $user, $password) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('providesAuth')]
+ public function testAuth(bool $expectedResult, string $user, string $password): void {
if ($expectedResult) {
$this->dbHandler->addServer('url1');
$this->dbHandler->addSharedSecret('url1', $password);
@@ -316,7 +276,7 @@ class DbHandlerTest extends TestCase {
$this->assertEquals($expectedResult, $result);
}
- public function providesAuth() {
+ public static function providesAuth(): array {
return [
[false, 'foo', ''],
[true, 'system', '123456789'],