aboutsummaryrefslogtreecommitdiffstats
path: root/apps/federation/tests
diff options
context:
space:
mode:
Diffstat (limited to 'apps/federation/tests')
-rw-r--r--apps/federation/tests/BackgroundJob/GetSharedSecretTest.php142
-rw-r--r--apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php159
-rw-r--r--apps/federation/tests/Controller/OCSAuthAPIControllerTest.php84
-rw-r--r--apps/federation/tests/Controller/SettingsControllerTest.php90
-rw-r--r--apps/federation/tests/DAV/FedAuthTest.php43
-rw-r--r--apps/federation/tests/DbHandlerTest.php144
-rw-r--r--apps/federation/tests/Middleware/AddServerMiddlewareTest.php100
-rw-r--r--apps/federation/tests/Settings/AdminTest.php39
-rw-r--r--apps/federation/tests/SyncFederationAddressbooksTest.php138
-rw-r--r--apps/federation/tests/TrustedServersTest.php236
10 files changed, 440 insertions, 735 deletions
diff --git a/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php b/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php
index 55db5cb744f..943bdf352de 100644
--- a/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php
+++ b/apps/federation/tests/BackgroundJob/GetSharedSecretTest.php
@@ -1,28 +1,10 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @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\BackgroundJob;
@@ -36,8 +18,10 @@ use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
+use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\OCS\IDiscoveryService;
+use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
/**
@@ -49,32 +33,16 @@ use Psr\Log\LoggerInterface;
*/
class GetSharedSecretTest extends TestCase {
- /** @var \PHPUnit\Framework\MockObject\MockObject|IClient */
- private $httpClient;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|IClientService */
- private $httpClientService;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|IJobList */
- private $jobList;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */
- private $urlGenerator;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */
- private $trustedServers;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */
- private $logger;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|IResponse */
- private $response;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|IDiscoveryService */
- private $discoverService;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
- private $timeFactory;
+ private MockObject&IClient $httpClient;
+ private MockObject&IClientService $httpClientService;
+ private MockObject&IJobList $jobList;
+ private MockObject&IURLGenerator $urlGenerator;
+ private MockObject&TrustedServers $trustedServers;
+ private MockObject&LoggerInterface $logger;
+ private MockObject&IResponse $response;
+ private MockObject&IDiscoveryService $discoverService;
+ private MockObject&ITimeFactory $timeFactory;
+ private MockObject&IConfig $config;
private GetSharedSecret $getSharedSecret;
@@ -91,6 +59,7 @@ class GetSharedSecretTest extends TestCase {
$this->response = $this->getMockBuilder(IResponse::class)->getMock();
$this->discoverService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
$this->timeFactory = $this->createMock(ITimeFactory::class);
+ $this->config = $this->createMock(IConfig::class);
$this->discoverService->expects($this->any())->method('discover')->willReturn([]);
$this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
@@ -102,18 +71,14 @@ class GetSharedSecretTest extends TestCase {
$this->trustedServers,
$this->logger,
$this->discoverService,
- $this->timeFactory
+ $this->timeFactory,
+ $this->config,
);
}
- /**
- * @dataProvider dataTestExecute
- *
- * @param bool $isTrustedServer
- * @param bool $retainBackgroundJob
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')]
public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): void {
- /** @var GetSharedSecret |\PHPUnit\Framework\MockObject\MockObject $getSharedSecret */
+ /** @var GetSharedSecret&MockObject $getSharedSecret */
$getSharedSecret = $this->getMockBuilder(GetSharedSecret::class)
->setConstructorArgs(
[
@@ -123,10 +88,13 @@ class GetSharedSecretTest extends TestCase {
$this->trustedServers,
$this->logger,
$this->discoverService,
- $this->timeFactory
+ $this->timeFactory,
+ $this->config,
]
- )->setMethods(['parentStart'])->getMock();
- $this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
+ )
+ ->onlyMethods(['parentStart'])
+ ->getMock();
+ self::invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
$this->trustedServers->expects($this->once())->method('isTrustedServer')
->with('url')->willReturn($isTrustedServer);
@@ -135,7 +103,7 @@ class GetSharedSecretTest extends TestCase {
} else {
$getSharedSecret->expects($this->never())->method('parentStart');
}
- $this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
+ self::invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
$this->jobList->expects($this->once())->method('remove');
$this->timeFactory->method('getTime')->willReturn(42);
@@ -158,7 +126,7 @@ class GetSharedSecretTest extends TestCase {
$getSharedSecret->start($this->jobList);
}
- public function dataTestExecute() {
+ public static function dataTestExecute(): array {
return [
[true, true],
[true, false],
@@ -166,12 +134,8 @@ class GetSharedSecretTest extends TestCase {
];
}
- /**
- * @dataProvider dataTestRun
- *
- * @param int $statusCode
- */
- public function testRun($statusCode) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestRun')]
+ public function testRun(int $statusCode): void {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
@@ -187,14 +151,14 @@ class GetSharedSecretTest extends TestCase {
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/shared-secret',
[
- 'query' =>
- [
- 'url' => $source,
- 'token' => $token,
- 'format' => 'json',
- ],
+ 'query' => [
+ 'url' => $source,
+ 'token' => $token,
+ 'format' => 'json',
+ ],
'timeout' => 3,
'connect_timeout' => 3,
+ 'verify' => true,
]
)->willReturn($this->response);
@@ -210,18 +174,18 @@ class GetSharedSecretTest extends TestCase {
$this->trustedServers->expects($this->never())->method('addSharedSecret');
}
- $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
+ self::invokePrivate($this->getSharedSecret, 'run', [$argument]);
if (
$statusCode !== Http::STATUS_OK
&& $statusCode !== Http::STATUS_FORBIDDEN
) {
- $this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
+ $this->assertTrue(self::invokePrivate($this->getSharedSecret, 'retainJob'));
} else {
- $this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob'));
+ $this->assertFalse(self::invokePrivate($this->getSharedSecret, 'retainJob'));
}
}
- public function dataTestRun() {
+ public static function dataTestRun(): array {
return [
[Http::STATUS_OK],
[Http::STATUS_FORBIDDEN],
@@ -229,7 +193,7 @@ class GetSharedSecretTest extends TestCase {
];
}
- public function testRunExpired() {
+ public function testRunExpired(): void {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
@@ -256,10 +220,10 @@ class GetSharedSecretTest extends TestCase {
TrustedServers::STATUS_FAILURE
);
- $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
+ self::invokePrivate($this->getSharedSecret, 'run', [$argument]);
}
- public function testRunConnectionError() {
+ public function testRunConnectionError(): void {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
@@ -278,21 +242,21 @@ class GetSharedSecretTest extends TestCase {
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/shared-secret',
[
- 'query' =>
- [
- 'url' => $source,
- 'token' => $token,
- 'format' => 'json',
- ],
+ 'query' => [
+ 'url' => $source,
+ 'token' => $token,
+ 'format' => 'json',
+ ],
'timeout' => 3,
'connect_timeout' => 3,
+ 'verify' => true,
]
)->willThrowException($this->createMock(ConnectException::class));
$this->trustedServers->expects($this->never())->method('addSharedSecret');
- $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
+ self::invokePrivate($this->getSharedSecret, 'run', [$argument]);
- $this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
+ $this->assertTrue(self::invokePrivate($this->getSharedSecret, 'retainJob'));
}
}
diff --git a/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php b/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php
index 059348aa8ab..6ef579c7483 100644
--- a/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php
+++ b/apps/federation/tests/BackgroundJob/RequestSharedSecretTest.php
@@ -1,27 +1,10 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @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\BackgroundJob;
@@ -34,6 +17,7 @@ use OCP\BackgroundJob\IJobList;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
+use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\OCS\IDiscoveryService;
use PHPUnit\Framework\MockObject\MockObject;
@@ -41,49 +25,31 @@ use Psr\Log\LoggerInterface;
use Test\TestCase;
class RequestSharedSecretTest extends TestCase {
- /** @var MockObject|IClientService */
- private $httpClientService;
-
- /** @var MockObject|IClient */
- private $httpClient;
-
- /** @var MockObject|IJobList */
- private $jobList;
-
- /** @var MockObject|IURLGenerator */
- private $urlGenerator;
-
- /** @var MockObject|TrustedServers */
- private $trustedServers;
-
- /** @var MockObject|IResponse */
- private $response;
-
- /** @var MockObject|IDiscoveryService */
- private $discoveryService;
-
- /** @var MockObject|LoggerInterface */
- private $logger;
-
- /** @var MockObject|ITimeFactory */
- private $timeFactory;
-
- /** @var RequestSharedSecret */
- private $requestSharedSecret;
+ private IClientService&MockObject $httpClientService;
+ private IClient&MockObject $httpClient;
+ private IJobList&MockObject $jobList;
+ private IURLGenerator&MockObject $urlGenerator;
+ private TrustedServers&MockObject $trustedServers;
+ private IResponse&MockObject $response;
+ private IDiscoveryService&MockObject $discoveryService;
+ private LoggerInterface&MockObject $logger;
+ private ITimeFactory&MockObject $timeFactory;
+ private IConfig&MockObject $config;
+ private RequestSharedSecret $requestSharedSecret;
protected function setUp(): void {
parent::setUp();
$this->httpClientService = $this->createMock(IClientService::class);
- $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
- $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
- $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
- $this->trustedServers = $this->getMockBuilder(TrustedServers::class)
- ->disableOriginalConstructor()->getMock();
- $this->response = $this->getMockBuilder(IResponse::class)->getMock();
- $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
+ $this->httpClient = $this->createMock(IClient::class);
+ $this->jobList = $this->createMock(IJobList::class);
+ $this->urlGenerator = $this->createMock(IURLGenerator::class);
+ $this->trustedServers = $this->createMock(TrustedServers::class);
+ $this->response = $this->createMock(IResponse::class);
+ $this->discoveryService = $this->createMock(IDiscoveryService::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
+ $this->config = $this->createMock(IConfig::class);
$this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
$this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
@@ -95,19 +61,15 @@ class RequestSharedSecretTest extends TestCase {
$this->trustedServers,
$this->discoveryService,
$this->logger,
- $this->timeFactory
+ $this->timeFactory,
+ $this->config,
);
}
- /**
- * @dataProvider dataTestStart
- *
- * @param bool $isTrustedServer
- * @param bool $retainBackgroundJob
- */
- public function testStart($isTrustedServer, $retainBackgroundJob) {
- /** @var RequestSharedSecret |MockObject $requestSharedSecret */
- $requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret')
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestStart')]
+ public function testStart(bool $isTrustedServer, bool $retainBackgroundJob): void {
+ /** @var RequestSharedSecret&MockObject $requestSharedSecret */
+ $requestSharedSecret = $this->getMockBuilder(RequestSharedSecret::class)
->setConstructorArgs(
[
$this->httpClientService,
@@ -116,10 +78,13 @@ class RequestSharedSecretTest extends TestCase {
$this->trustedServers,
$this->discoveryService,
$this->logger,
- $this->timeFactory
+ $this->timeFactory,
+ $this->config,
]
- )->setMethods(['parentStart'])->getMock();
- $this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
+ )
+ ->onlyMethods(['parentStart'])
+ ->getMock();
+ self::invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
$this->trustedServers->expects($this->once())->method('isTrustedServer')
->with('url')->willReturn($isTrustedServer);
@@ -128,7 +93,7 @@ class RequestSharedSecretTest extends TestCase {
} else {
$requestSharedSecret->expects($this->never())->method('parentStart');
}
- $this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]);
+ self::invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]);
$this->jobList->expects($this->once())->method('remove');
$this->timeFactory->method('getTime')->willReturn(42);
@@ -152,7 +117,7 @@ class RequestSharedSecretTest extends TestCase {
$requestSharedSecret->start($this->jobList);
}
- public function dataTestStart() {
+ public static function dataTestStart(): array {
return [
[true, true],
[true, false],
@@ -160,11 +125,7 @@ class RequestSharedSecretTest extends TestCase {
];
}
- /**
- * @dataProvider dataTestRun
- *
- * @param int $statusCode
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestRun')]
public function testRun(int $statusCode, int $attempt = 0): void {
$target = 'targetURL';
$source = 'sourceURL';
@@ -180,32 +141,32 @@ class RequestSharedSecretTest extends TestCase {
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret',
[
- 'body' =>
- [
- 'url' => $source,
- 'token' => $token,
- 'format' => 'json',
- ],
+ 'body' => [
+ 'url' => $source,
+ 'token' => $token,
+ 'format' => 'json',
+ ],
'timeout' => 3,
'connect_timeout' => 3,
+ 'verify' => true,
]
)->willReturn($this->response);
$this->response->expects($this->once())->method('getStatusCode')
->willReturn($statusCode);
- $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
+ self::invokePrivate($this->requestSharedSecret, 'run', [$argument]);
if (
$statusCode !== Http::STATUS_OK
&& ($statusCode !== Http::STATUS_FORBIDDEN || $attempt < 5)
) {
- $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
+ $this->assertTrue(self::invokePrivate($this->requestSharedSecret, 'retainJob'));
} else {
- $this->assertFalse($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
+ $this->assertFalse(self::invokePrivate($this->requestSharedSecret, 'retainJob'));
}
}
- public function dataTestRun() {
+ public static function dataTestRun(): array {
return [
[Http::STATUS_OK],
[Http::STATUS_FORBIDDEN, 5],
@@ -214,7 +175,7 @@ class RequestSharedSecretTest extends TestCase {
];
}
- public function testRunExpired() {
+ public function testRunExpired(): void {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
@@ -241,10 +202,10 @@ class RequestSharedSecretTest extends TestCase {
TrustedServers::STATUS_FAILURE
);
- $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
+ self::invokePrivate($this->requestSharedSecret, 'run', [$argument]);
}
- public function testRunConnectionError() {
+ public function testRunConnectionError(): void {
$target = 'targetURL';
$source = 'sourceURL';
$token = 'token';
@@ -265,18 +226,18 @@ class RequestSharedSecretTest extends TestCase {
->with(
$target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret',
[
- 'body' =>
- [
- 'url' => $source,
- 'token' => $token,
- 'format' => 'json',
- ],
+ 'body' => [
+ 'url' => $source,
+ 'token' => $token,
+ 'format' => 'json',
+ ],
'timeout' => 3,
'connect_timeout' => 3,
+ 'verify' => true,
]
)->willThrowException($this->createMock(ConnectException::class));
- $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
- $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
+ self::invokePrivate($this->requestSharedSecret, 'run', [$argument]);
+ $this->assertTrue(self::invokePrivate($this->requestSharedSecret, 'retainJob'));
}
}
diff --git a/apps/federation/tests/Controller/OCSAuthAPIControllerTest.php b/apps/federation/tests/Controller/OCSAuthAPIControllerTest.php
index 17125246088..a054277c5cd 100644
--- a/apps/federation/tests/Controller/OCSAuthAPIControllerTest.php
+++ b/apps/federation/tests/Controller/OCSAuthAPIControllerTest.php
@@ -1,32 +1,15 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @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\Controller;
use OC\BackgroundJob\JobList;
+use OCA\Federation\BackgroundJob\GetSharedSecret;
use OCA\Federation\Controller\OCSAuthAPIController;
use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
@@ -35,34 +18,19 @@ use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IRequest;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Security\ISecureRandom;
+use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class OCSAuthAPIControllerTest extends TestCase {
- /** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
- private $request;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|ISecureRandom */
- private $secureRandom;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|JobList */
- private $jobList;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */
- private $trustedServers;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|DbHandler */
- private $dbHandler;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */
- private $logger;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
- private $timeFactory;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|IThrottler */
- private $throttler;
-
+ private IRequest&MockObject $request;
+ private ISecureRandom&MockObject $secureRandom;
+ private JobList&MockObject $jobList;
+ private TrustedServers&MockObject $trustedServers;
+ private DbHandler&MockObject $dbHandler;
+ private LoggerInterface&MockObject $logger;
+ private ITimeFactory&MockObject $timeFactory;
+ private IThrottler&MockObject $throttler;
private OCSAuthAPIController $ocsAuthApi;
/** @var int simulated timestamp */
@@ -96,9 +64,7 @@ class OCSAuthAPIControllerTest extends TestCase {
->willReturn($this->currentTime);
}
- /**
- * @dataProvider dataTestRequestSharedSecret
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestRequestSharedSecret')]
public function testRequestSharedSecret(string $token, string $localToken, bool $isTrustedServer, bool $ok): void {
$url = 'url';
@@ -110,7 +76,7 @@ class OCSAuthAPIControllerTest extends TestCase {
if ($ok) {
$this->jobList->expects($this->once())->method('add')
- ->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
+ ->with(GetSharedSecret::class, ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
} else {
$this->jobList->expects($this->never())->method('add');
$this->jobList->expects($this->never())->method('remove');
@@ -130,7 +96,7 @@ class OCSAuthAPIControllerTest extends TestCase {
}
}
- public function dataTestRequestSharedSecret() {
+ public static function dataTestRequestSharedSecret(): array {
return [
['token2', 'token1', true, true],
['token1', 'token2', false, false],
@@ -138,15 +104,13 @@ class OCSAuthAPIControllerTest extends TestCase {
];
}
- /**
- * @dataProvider dataTestGetSharedSecret
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetSharedSecret')]
public function testGetSharedSecret(bool $isTrustedServer, bool $isValidToken, bool $ok): void {
$url = 'url';
$token = 'token';
- /** @var OCSAuthAPIController | \PHPUnit\Framework\MockObject\MockObject $ocsAuthApi */
- $ocsAuthApi = $this->getMockBuilder('OCA\Federation\Controller\OCSAuthAPIController')
+ /** @var OCSAuthAPIController&MockObject $ocsAuthApi */
+ $ocsAuthApi = $this->getMockBuilder(OCSAuthAPIController::class)
->setConstructorArgs(
[
'federation',
@@ -159,7 +123,9 @@ class OCSAuthAPIControllerTest extends TestCase {
$this->timeFactory,
$this->throttler
]
- )->setMethods(['isValidToken'])->getMock();
+ )
+ ->onlyMethods(['isValidToken'])
+ ->getMock();
$this->trustedServers
->expects($this->any())
@@ -190,7 +156,7 @@ class OCSAuthAPIControllerTest extends TestCase {
}
}
- public function dataTestGetSharedSecret() {
+ public static function dataTestGetSharedSecret(): array {
return [
[true, true, true],
[false, true, false],
diff --git a/apps/federation/tests/Controller/SettingsControllerTest.php b/apps/federation/tests/Controller/SettingsControllerTest.php
index a3c66159147..b0a7a5e30c9 100644
--- a/apps/federation/tests/Controller/SettingsControllerTest.php
+++ b/apps/federation/tests/Controller/SettingsControllerTest.php
@@ -1,61 +1,46 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Carl Schwan <carl@carlschwan.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\Controller;
use OCA\Federation\Controller\SettingsController;
use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http\DataResponse;
+use OCP\AppFramework\OCS\OCSException;
+use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\IL10N;
use OCP\IRequest;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
use Test\TestCase;
class SettingsControllerTest extends TestCase {
private SettingsController $controller;
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IRequest */
- private $request;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IL10N */
- private $l10n;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Federation\TrustedServers */
- private $trustedServers;
+ private IRequest&MockObject $request;
+ private IL10N&MockObject $l10n;
+ private TrustedServers&MockObject $trustedServers;
+ private LoggerInterface&MockObject $logger;
protected function setUp(): void {
parent::setUp();
- $this->request = $this->getMockBuilder(IRequest::class)->getMock();
- $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
- $this->trustedServers = $this->getMockBuilder(TrustedServers::class)
- ->disableOriginalConstructor()->getMock();
+ $this->request = $this->createMock(IRequest::class);
+ $this->l10n = $this->createMock(IL10N::class);
+ $this->trustedServers = $this->createMock(TrustedServers::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
$this->controller = new SettingsController(
'SettingsControllerTest',
$this->request,
$this->l10n,
- $this->trustedServers
+ $this->trustedServers,
+ $this->logger,
);
}
@@ -72,7 +57,7 @@ class SettingsControllerTest extends TestCase {
->willReturn(true);
$result = $this->controller->addServer('url');
- $this->assertTrue($result instanceof DataResponse);
+ $this->assertInstanceOf(DataResponse::class, $result);
$data = $result->getData();
$this->assertSame(200, $result->getStatus());
@@ -80,12 +65,8 @@ class SettingsControllerTest extends TestCase {
$this->assertArrayHasKey('id', $data);
}
- /**
- * @dataProvider checkServerFails
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('checkServerFails')]
public function testAddServerFail(bool $isTrustedServer, bool $isNextcloud): void {
- $this->expectException(\OCP\HintException::class);
-
$this->trustedServers
->expects($this->any())
->method('isTrustedServer')
@@ -97,6 +78,12 @@ class SettingsControllerTest extends TestCase {
->with('url')
->willReturn($isNextcloud);
+ if ($isTrustedServer) {
+ $this->expectException(OCSException::class);
+ } else {
+ $this->expectException(OCSNotFoundException::class);
+ }
+
$this->controller->addServer('url');
}
@@ -121,17 +108,13 @@ class SettingsControllerTest extends TestCase {
->with('url')
->willReturn(true);
- $this->assertTrue(
- $this->invokePrivate($this->controller, 'checkServer', ['url'])
+ $this->assertNull(
+ self::invokePrivate($this->controller, 'checkServer', ['url'])
);
}
- /**
- * @dataProvider checkServerFails
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('checkServerFails')]
public function testCheckServerFail(bool $isTrustedServer, bool $isNextcloud): void {
- $this->expectException(\OCP\HintException::class);
-
$this->trustedServers
->expects($this->any())
->method('isTrustedServer')
@@ -143,15 +126,18 @@ class SettingsControllerTest extends TestCase {
->with('url')
->willReturn($isNextcloud);
+ if ($isTrustedServer) {
+ $this->expectException(OCSException::class);
+ } else {
+ $this->expectException(OCSNotFoundException::class);
+ }
+
$this->assertTrue(
- $this->invokePrivate($this->controller, 'checkServer', ['url'])
+ self::invokePrivate($this->controller, 'checkServer', ['url'])
);
}
- /**
- * Data to simulate checkServer fails
- */
- public function checkServerFails(): array {
+ public static function checkServerFails(): array {
return [
[true, true],
[false, false]
diff --git a/apps/federation/tests/DAV/FedAuthTest.php b/apps/federation/tests/DAV/FedAuthTest.php
index 3a801766d85..c95d3852b48 100644
--- a/apps/federation/tests/DAV/FedAuthTest.php
+++ b/apps/federation/tests/DAV/FedAuthTest.php
@@ -1,50 +1,31 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Morris Jobke <hey@morrisjobke.de>
- * @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: 2019-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Federation\Tests\DAV;
use OCA\Federation\DAV\FedAuth;
use OCA\Federation\DbHandler;
+use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class FedAuthTest extends TestCase {
- /**
- * @dataProvider providesUser
- *
- * @param array $expected
- * @param string $user
- * @param string $password
- */
- public function testFedAuth($expected, $user, $password) {
- /** @var DbHandler | \PHPUnit\Framework\MockObject\MockObject $db */
- $db = $this->getMockBuilder('OCA\Federation\DbHandler')->disableOriginalConstructor()->getMock();
+ #[\PHPUnit\Framework\Attributes\DataProvider('providesUser')]
+ public function testFedAuth(bool $expected, string $user, string $password): void {
+ /** @var DbHandler&MockObject $db */
+ $db = $this->createMock(DbHandler::class);
$db->method('auth')->willReturn(true);
$auth = new FedAuth($db);
- $result = $this->invokePrivate($auth, 'validateUserPass', [$user, $password]);
+ $result = self::invokePrivate($auth, 'validateUserPass', [$user, $password]);
$this->assertEquals($expected, $result);
}
- public function providesUser() {
+ public static function providesUser(): array {
return [
[true, 'system', '123456']
];
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'],
diff --git a/apps/federation/tests/Middleware/AddServerMiddlewareTest.php b/apps/federation/tests/Middleware/AddServerMiddlewareTest.php
deleted file mode 100644
index 43333137e65..00000000000
--- a/apps/federation/tests/Middleware/AddServerMiddlewareTest.php
+++ /dev/null
@@ -1,100 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @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/>
- *
- */
-namespace OCA\Federation\Tests\Middleware;
-
-use OCA\Federation\Controller\SettingsController;
-use OCA\Federation\Middleware\AddServerMiddleware;
-use OCP\AppFramework\Http;
-use OCP\HintException;
-use OCP\IL10N;
-use Psr\Log\LoggerInterface;
-use Test\TestCase;
-
-class AddServerMiddlewareTest extends TestCase {
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | LoggerInterface */
- private $logger;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IL10N */
- private $l10n;
-
- private AddServerMiddleware $middleware;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | SettingsController */
- private $controller;
-
- protected function setUp(): void {
- parent::setUp();
-
- $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
- $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
- $this->controller = $this->getMockBuilder(SettingsController::class)
- ->disableOriginalConstructor()->getMock();
-
- $this->middleware = new AddServerMiddleware(
- 'AddServerMiddlewareTest',
- $this->l10n,
- $this->logger
- );
- }
-
- /**
- * @dataProvider dataTestAfterException
- *
- * @param \Exception $exception
- * @param string $hint
- */
- public function testAfterException($exception, $hint) {
- $this->logger->expects($this->once())->method('error');
-
- $this->l10n->expects($this->any())->method('t')
- ->willReturnCallback(
- function (string $message): string {
- return $message;
- }
- );
-
- $result = $this->middleware->afterException($this->controller, 'method', $exception);
-
- $this->assertSame(Http::STATUS_BAD_REQUEST,
- $result->getStatus()
- );
-
- $data = $result->getData();
-
- $this->assertSame($hint,
- $data['message']
- );
- }
-
- public function dataTestAfterException() {
- return [
- [new HintException('message', 'hint'), 'hint'],
- [new \Exception('message'), 'message'],
- ];
- }
-}
diff --git a/apps/federation/tests/Settings/AdminTest.php b/apps/federation/tests/Settings/AdminTest.php
index 3d124d5c65a..b879547a8cd 100644
--- a/apps/federation/tests/Settings/AdminTest.php
+++ b/apps/federation/tests/Settings/AdminTest.php
@@ -1,25 +1,9 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
- *
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * 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
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Federation\Tests\Settings;
@@ -27,24 +11,23 @@ use OCA\Federation\Settings\Admin;
use OCA\Federation\TrustedServers;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IL10N;
+use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class AdminTest extends TestCase {
- /** @var Admin */
- private $admin;
- /** @var TrustedServers */
- private $trustedServers;
+ private TrustedServers&MockObject $trustedServers;
+ private Admin $admin;
protected function setUp(): void {
parent::setUp();
- $this->trustedServers = $this->getMockBuilder('\OCA\Federation\TrustedServers')->disableOriginalConstructor()->getMock();
+ $this->trustedServers = $this->createMock(TrustedServers::class);
$this->admin = new Admin(
$this->trustedServers,
$this->createMock(IL10N::class)
);
}
- public function testGetForm() {
+ public function testGetForm(): void {
$this->trustedServers
->expects($this->once())
->method('getServers')
@@ -57,11 +40,11 @@ class AdminTest extends TestCase {
$this->assertEquals($expected, $this->admin->getForm());
}
- public function testGetSection() {
+ public function testGetSection(): void {
$this->assertSame('sharing', $this->admin->getSection());
}
- public function testGetPriority() {
+ public function testGetPriority(): void {
$this->assertSame(30, $this->admin->getPriority());
}
}
diff --git a/apps/federation/tests/SyncFederationAddressbooksTest.php b/apps/federation/tests/SyncFederationAddressbooksTest.php
index 782ca52322a..ff03f5cf442 100644
--- a/apps/federation/tests/SyncFederationAddressbooksTest.php
+++ b/apps/federation/tests/SyncFederationAddressbooksTest.php
@@ -1,114 +1,108 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @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: 2017-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Federation\Tests;
use OC\OCS\DiscoveryService;
+use OCA\DAV\CardDAV\SyncService;
use OCA\Federation\DbHandler;
use OCA\Federation\SyncFederationAddressBooks;
+use OCA\Federation\TrustedServers;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
class SyncFederationAddressbooksTest extends \Test\TestCase {
-
- /** @var array */
- private $callBacks = [];
-
- /** @var MockObject | DiscoveryService */
- private $discoveryService;
-
- /** @var MockObject|LoggerInterface */
- private $logger;
+ private array $callBacks = [];
+ private DiscoveryService&MockObject $discoveryService;
+ private LoggerInterface&MockObject $logger;
protected function setUp(): void {
parent::setUp();
- $this->discoveryService = $this->getMockBuilder(DiscoveryService::class)
- ->disableOriginalConstructor()->getMock();
+ $this->discoveryService = $this->createMock(DiscoveryService::class);
$this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
$this->logger = $this->createMock(LoggerInterface::class);
}
- public function testSync() {
- /** @var DbHandler | MockObject $dbHandler */
- $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')
- ->disableOriginalConstructor()
- ->getMock();
+ public function testSync(): void {
+ /** @var DbHandler&MockObject $dbHandler */
+ $dbHandler = $this->createMock(DbHandler::class);
$dbHandler->method('getAllServer')
->willReturn([
[
- 'url' => 'https://cloud.drop.box',
+ 'url' => 'https://cloud.example.org',
'url_hash' => 'sha1',
- 'shared_secret' => 'iloveowncloud',
+ 'shared_secret' => 'ilovenextcloud',
'sync_token' => '0'
]
]);
- $dbHandler->expects($this->once())->method('setServerStatus')->
- with('https://cloud.drop.box', 1, '1');
- $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
- ->disableOriginalConstructor()
- ->getMock();
+ $dbHandler->expects($this->once())->method('setServerStatus')
+ ->with('https://cloud.example.org', 1, '1');
+ $syncService = $this->createMock(SyncService::class);
$syncService->expects($this->once())->method('syncRemoteAddressBook')
- ->willReturn('1');
+ ->willReturn(['1', false]);
- /** @var \OCA\DAV\CardDAV\SyncService $syncService */
+ /** @var SyncService $syncService */
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
- $s->syncThemAll(function ($url, $ex) {
+ $s->syncThemAll(function ($url, $ex): void {
$this->callBacks[] = [$url, $ex];
});
- $this->assertEquals('1', count($this->callBacks));
+ $this->assertCount(1, $this->callBacks);
}
- public function testException() {
- /** @var DbHandler | MockObject $dbHandler */
- $dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')->
- disableOriginalConstructor()->
- getMock();
- $dbHandler->method('getAllServer')->
- willReturn([
- [
- 'url' => 'https://cloud.drop.box',
- 'url_hash' => 'sha1',
- 'shared_secret' => 'iloveowncloud',
- 'sync_token' => '0'
- ]
- ]);
- $syncService = $this->getMockBuilder('OCA\DAV\CardDAV\SyncService')
- ->disableOriginalConstructor()
- ->getMock();
+ public function testException(): void {
+ /** @var DbHandler&MockObject $dbHandler */
+ $dbHandler = $this->createMock(DbHandler::class);
+ $dbHandler->method('getAllServer')
+ ->willReturn([
+ [
+ 'url' => 'https://cloud.example.org',
+ 'url_hash' => 'sha1',
+ 'shared_secret' => 'ilovenextcloud',
+ 'sync_token' => '0'
+ ]
+ ]);
+ $syncService = $this->createMock(SyncService::class);
$syncService->expects($this->once())->method('syncRemoteAddressBook')
->willThrowException(new \Exception('something did not work out'));
- /** @var \OCA\DAV\CardDAV\SyncService $syncService */
+ /** @var SyncService $syncService */
+ $s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
+ $s->syncThemAll(function ($url, $ex): void {
+ $this->callBacks[] = [$url, $ex];
+ });
+ $this->assertCount(2, $this->callBacks);
+ }
+
+ public function testSuccessfulSyncWithoutChangesAfterFailure(): void {
+ /** @var DbHandler&MockObject $dbHandler */
+ $dbHandler = $this->createMock(DbHandler::class);
+ $dbHandler->method('getAllServer')
+ ->willReturn([
+ [
+ 'url' => 'https://cloud.example.org',
+ 'url_hash' => 'sha1',
+ 'shared_secret' => 'ilovenextcloud',
+ 'sync_token' => '0'
+ ]
+ ]);
+ $dbHandler->method('getServerStatus')->willReturn(TrustedServers::STATUS_FAILURE);
+ $dbHandler->expects($this->once())->method('setServerStatus')
+ ->with('https://cloud.example.org', 1);
+ $syncService = $this->createMock(SyncService::class);
+ $syncService->expects($this->once())->method('syncRemoteAddressBook')
+ ->willReturn(['0', false]);
+
+ /** @var SyncService $syncService */
$s = new SyncFederationAddressBooks($dbHandler, $syncService, $this->discoveryService, $this->logger);
- $s->syncThemAll(function ($url, $ex) {
+ $s->syncThemAll(function ($url, $ex): void {
$this->callBacks[] = [$url, $ex];
});
- $this->assertEquals(2, count($this->callBacks));
+ $this->assertCount(1, $this->callBacks);
}
}
diff --git a/apps/federation/tests/TrustedServersTest.php b/apps/federation/tests/TrustedServersTest.php
index c5c056c7031..0c900f6edf7 100644
--- a/apps/federation/tests/TrustedServersTest.php
+++ b/apps/federation/tests/TrustedServersTest.php
@@ -1,93 +1,55 @@
<?php
+
+declare(strict_types=1);
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @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;
+use OCA\Federation\BackgroundJob\RequestSharedSecret;
use OCA\Federation\DbHandler;
use OCA\Federation\TrustedServers;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\IJobList;
use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Federation\Events\TrustedServerRemovedEvent;
+use OCP\HintException;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\IConfig;
use OCP\Security\ISecureRandom;
+use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class TrustedServersTest extends TestCase {
- /** @var \PHPUnit\Framework\MockObject\MockObject | TrustedServers */
- private $trustedServers;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | DbHandler */
- private $dbHandler;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | IClientService */
- private $httpClientService;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | IClient */
- private $httpClient;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | IResponse */
- private $response;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | LoggerInterface */
- private $logger;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | IJobList */
- private $jobList;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | ISecureRandom */
- private $secureRandom;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | IConfig */
- private $config;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | IEventDispatcher */
- private $dispatcher;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
- private $timeFactory;
+ private TrustedServers $trustedServers;
+ private DbHandler&MockObject $dbHandler;
+ private IClientService&MockObject $httpClientService;
+ private IClient&MockObject $httpClient;
+ private IResponse&MockObject $response;
+ private LoggerInterface&MockObject $logger;
+ private IJobList&MockObject $jobList;
+ private ISecureRandom&MockObject $secureRandom;
+ private IConfig&MockObject $config;
+ private IEventDispatcher&MockObject $dispatcher;
+ private ITimeFactory&MockObject $timeFactory;
protected function setUp(): void {
parent::setUp();
- $this->dbHandler = $this->getMockBuilder(DbHandler::class)
- ->disableOriginalConstructor()->getMock();
- $this->dispatcher = $this->getMockBuilder(IEventDispatcher::class)
- ->disableOriginalConstructor()->getMock();
- $this->httpClientService = $this->getMockBuilder(IClientService::class)->getMock();
- $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
- $this->response = $this->getMockBuilder(IResponse::class)->getMock();
- $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
- $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
- $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock();
- $this->config = $this->getMockBuilder(IConfig::class)->getMock();
+ $this->dbHandler = $this->createMock(DbHandler::class);
+ $this->dispatcher = $this->createMock(IEventDispatcher::class);
+ $this->httpClientService = $this->createMock(IClientService::class);
+ $this->httpClient = $this->createMock(IClient::class);
+ $this->response = $this->createMock(IResponse::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
+ $this->jobList = $this->createMock(IJobList::class);
+ $this->secureRandom = $this->createMock(ISecureRandom::class);
+ $this->config = $this->createMock(IConfig::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->trustedServers = new TrustedServers(
@@ -103,8 +65,8 @@ class TrustedServersTest extends TestCase {
}
public function testAddServer(): void {
- /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers $trustedServers */
- $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
+ /** @var TrustedServers&MockObject $trustedServers */
+ $trustedServers = $this->getMockBuilder(TrustedServers::class)
->setConstructorArgs(
[
$this->dbHandler,
@@ -117,10 +79,10 @@ class TrustedServersTest extends TestCase {
$this->timeFactory
]
)
- ->setMethods(['normalizeUrl', 'updateProtocol'])
+ ->onlyMethods(['updateProtocol'])
->getMock();
$trustedServers->expects($this->once())->method('updateProtocol')
- ->with('url')->willReturn('https://url');
+ ->with('url')->willReturn('https://url');
$this->timeFactory->method('getTime')
->willReturn(1234567);
$this->dbHandler->expects($this->once())->method('addServer')->with('https://url')
@@ -130,12 +92,12 @@ class TrustedServersTest extends TestCase {
->willReturn('token');
$this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token');
$this->jobList->expects($this->once())->method('add')
- ->with('OCA\Federation\BackgroundJob\RequestSharedSecret',
+ ->with(RequestSharedSecret::class,
['url' => 'https://url', 'token' => 'token', 'created' => 1234567]);
$this->assertSame(
- $trustedServers->addServer('url'),
- 1
+ 1,
+ $trustedServers->addServer('url')
);
}
@@ -147,9 +109,9 @@ class TrustedServersTest extends TestCase {
public function testGetSharedSecret(): void {
$this->dbHandler->expects($this->once())
- ->method('getSharedSecret')
- ->with('url')
- ->willReturn('secret');
+ ->method('getSharedSecret')
+ ->with('url')
+ ->willReturn('secret');
$this->assertSame(
$this->trustedServers->getSharedSecret('url'),
'secret'
@@ -164,8 +126,8 @@ class TrustedServersTest extends TestCase {
->willReturn($server);
$this->dispatcher->expects($this->once())->method('dispatchTyped')
->willReturnCallback(
- function ($event) {
- $this->assertSame(get_class($event), \OCP\Federation\Events\TrustedServerRemovedEvent::class);
+ function ($event): void {
+ $this->assertSame(get_class($event), TrustedServerRemovedEvent::class);
/** @var \OCP\Federated\Events\TrustedServerRemovedEvent $event */
$this->assertSame('url_hash', $event->getUrlHash());
}
@@ -182,6 +144,64 @@ class TrustedServersTest extends TestCase {
);
}
+ public static function dataTestGetServer() {
+ return [
+ [
+ 15,
+ [
+ 'id' => 15,
+ 'otherData' => 'first server',
+ ]
+ ],
+ [
+ 16,
+ [
+ 'id' => 16,
+ 'otherData' => 'second server',
+ ]
+ ],
+ [
+ 42,
+ [
+ 'id' => 42,
+ 'otherData' => 'last server',
+ ]
+ ],
+ [
+ 108,
+ null
+ ],
+ ];
+ }
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetServer')]
+ public function testGetServer(int $id, ?array $expectedServer): void {
+ $servers = [
+ [
+ 'id' => 15,
+ 'otherData' => 'first server',
+ ],
+ [
+ 'id' => 16,
+ 'otherData' => 'second server',
+ ],
+ [
+ 'id' => 42,
+ 'otherData' => 'last server',
+ ],
+ ];
+ $this->dbHandler->expects($this->once())->method('getAllServer')->willReturn($servers);
+
+ if ($expectedServer === null) {
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessage('No server found with ID: ' . $id);
+ }
+
+ $this->assertEquals(
+ $expectedServer,
+ $this->trustedServers->getServer($id)
+ );
+ }
public function testIsTrustedServer(): void {
$this->dbHandler->expects($this->once())
@@ -193,13 +213,13 @@ class TrustedServersTest extends TestCase {
);
}
- public function testSetServerStatus() {
+ public function testSetServerStatus(): void {
$this->dbHandler->expects($this->once())->method('setServerStatus')
->with('url', 1);
$this->trustedServers->setServerStatus('url', 1);
}
- public function testGetServerStatus() {
+ public function testGetServerStatus(): void {
$this->dbHandler->expects($this->once())->method('getServerStatus')
->with('url')->willReturn(1);
$this->assertSame(
@@ -208,14 +228,12 @@ class TrustedServersTest extends TestCase {
);
}
- /**
- * @dataProvider dataTestIsNextcloudServer
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestIsNextcloudServer')]
public function testIsNextcloudServer(int $statusCode, bool $isValidNextcloudVersion, bool $expected): void {
$server = 'server1';
- /** @var \PHPUnit\Framework\MockObject\MockObject | TrustedServers $trustedServers */
- $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
+ /** @var TrustedServers&MockObject $trustedServers */
+ $trustedServers = $this->getMockBuilder(TrustedServers::class)
->setConstructorArgs(
[
$this->dbHandler,
@@ -228,7 +246,7 @@ class TrustedServersTest extends TestCase {
$this->timeFactory
]
)
- ->setMethods(['checkNextcloudVersion'])
+ ->onlyMethods(['checkNextcloudVersion'])
->getMock();
$this->httpClientService->expects($this->once())->method('newClient')
@@ -254,7 +272,7 @@ class TrustedServersTest extends TestCase {
);
}
- public function dataTestIsNextcloudServer(): array {
+ public static function dataTestIsNextcloudServer(): array {
return [
[200, true, true],
[200, false, false],
@@ -262,63 +280,55 @@ class TrustedServersTest extends TestCase {
];
}
- /**
- * @expectedExceptionMessage simulated exception
- */
public function testIsNextcloudServerFail(): void {
$server = 'server1';
- $this->httpClientService->expects($this->once())->method('newClient')
+ $this->httpClientService->expects($this->once())
+ ->method('newClient')
->willReturn($this->httpClient);
- $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
- ->willReturnCallback(function () {
- throw new \Exception('simulated exception');
- });
+ $this->httpClient->expects($this->once())
+ ->method('get')
+ ->with($server . '/status.php')
+ ->willThrowException(new \Exception('simulated exception'));
$this->assertFalse($this->trustedServers->isNextcloudServer($server));
}
- /**
- * @dataProvider dataTestCheckNextcloudVersion
- */
- public function testCheckNextcloudVersion($status): void {
- $this->assertTrue($this->invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]));
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestCheckNextcloudVersion')]
+ public function testCheckNextcloudVersion(string $status): void {
+ $this->assertTrue(self::invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]));
}
- public function dataTestCheckNextcloudVersion(): array {
+ public static function dataTestCheckNextcloudVersion(): array {
return [
['{"version":"9.0.0"}'],
['{"version":"9.1.0"}']
];
}
- /**
- * @dataProvider dataTestCheckNextcloudVersionTooLow
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestCheckNextcloudVersionTooLow')]
public function testCheckNextcloudVersionTooLow(string $status): void {
- $this->expectException(\OCP\HintException::class);
+ $this->expectException(HintException::class);
$this->expectExceptionMessage('Remote server version is too low. 9.0 is required.');
- $this->invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]);
+ self::invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]);
}
- public function dataTestCheckNextcloudVersionTooLow(): array {
+ public static function dataTestCheckNextcloudVersionTooLow(): array {
return [
['{"version":"8.2.3"}'],
];
}
- /**
- * @dataProvider dataTestUpdateProtocol
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestUpdateProtocol')]
public function testUpdateProtocol(string $url, string $expected): void {
$this->assertSame($expected,
- $this->invokePrivate($this->trustedServers, 'updateProtocol', [$url])
+ self::invokePrivate($this->trustedServers, 'updateProtocol', [$url])
);
}
- public function dataTestUpdateProtocol(): array {
+ public static function dataTestUpdateProtocol(): array {
return [
['http://owncloud.org', 'http://owncloud.org'],
['https://owncloud.org', 'https://owncloud.org'],