diff options
Diffstat (limited to 'apps/updatenotification/tests')
-rw-r--r-- | apps/updatenotification/tests/BackgroundJob/ResetTokenTest.php | 93 | ||||
-rw-r--r-- | apps/updatenotification/tests/BackgroundJob/UpdateAvailableNotificationsTest.php (renamed from apps/updatenotification/tests/Notification/BackgroundJobTest.php) | 257 | ||||
-rw-r--r-- | apps/updatenotification/tests/Controller/APIControllerTest.php | 160 | ||||
-rw-r--r-- | apps/updatenotification/tests/Controller/AdminControllerTest.php | 60 | ||||
-rw-r--r-- | apps/updatenotification/tests/Notification/NotifierTest.php | 81 | ||||
-rw-r--r-- | apps/updatenotification/tests/ResetTokenBackgroundJobTest.php | 86 | ||||
-rw-r--r-- | apps/updatenotification/tests/Settings/AdminTest.php | 354 | ||||
-rw-r--r-- | apps/updatenotification/tests/UpdateCheckerTest.php | 75 |
8 files changed, 605 insertions, 561 deletions
diff --git a/apps/updatenotification/tests/BackgroundJob/ResetTokenTest.php b/apps/updatenotification/tests/BackgroundJob/ResetTokenTest.php new file mode 100644 index 00000000000..c03d4c48827 --- /dev/null +++ b/apps/updatenotification/tests/BackgroundJob/ResetTokenTest.php @@ -0,0 +1,93 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OCA\UpdateNotification\Tests; + +use OCA\UpdateNotification\BackgroundJob\ResetToken as BackgroundJobResetToken; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\IAppConfig; +use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class ResetTokenTest extends TestCase { + private IConfig&MockObject $config; + private IAppConfig&MockObject $appConfig; + private ITimeFactory&MockObject $timeFactory; + private BackgroundJobResetToken $resetTokenBackgroundJob; + + protected function setUp(): void { + parent::setUp(); + $this->appConfig = $this->createMock(IAppConfig::class); + $this->config = $this->createMock(IConfig::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + $this->resetTokenBackgroundJob = new BackgroundJobResetToken( + $this->timeFactory, + $this->config, + $this->appConfig, + ); + } + + public function testRunWithNotExpiredToken(): void { + $this->timeFactory + ->expects($this->atLeastOnce()) + ->method('getTime') + ->willReturn(123); + $this->appConfig + ->expects($this->once()) + ->method('getValueInt') + ->with('core', 'updater.secret.created', 123); + $this->config + ->expects($this->once()) + ->method('getSystemValueBool') + ->with('config_is_read_only') + ->willReturn(false); + $this->config + ->expects($this->never()) + ->method('deleteSystemValue'); + + static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); + } + + public function testRunWithExpiredToken(): void { + $this->timeFactory + ->expects($this->once()) + ->method('getTime') + ->willReturn(1455045234); + $this->appConfig + ->expects($this->once()) + ->method('getValueInt') + ->with('core', 'updater.secret.created', 1455045234) + ->willReturn(2 * 24 * 60 * 60 + 1); // over 2 days + $this->config + ->expects($this->once()) + ->method('deleteSystemValue') + ->with('updater.secret'); + + static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); + } + + public function testRunWithExpiredTokenAndReadOnlyConfigFile(): void { + $this->timeFactory + ->expects($this->never()) + ->method('getTime'); + $this->appConfig + ->expects($this->never()) + ->method('getValueInt'); + $this->config + ->expects($this->once()) + ->method('getSystemValueBool') + ->with('config_is_read_only') + ->willReturn(true); + $this->config + ->expects($this->never()) + ->method('deleteSystemValue'); + + static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); + } +} diff --git a/apps/updatenotification/tests/Notification/BackgroundJobTest.php b/apps/updatenotification/tests/BackgroundJob/UpdateAvailableNotificationsTest.php index d4d7a543e56..26a5ecde052 100644 --- a/apps/updatenotification/tests/Notification/BackgroundJobTest.php +++ b/apps/updatenotification/tests/BackgroundJob/UpdateAvailableNotificationsTest.php @@ -3,100 +3,88 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @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> - * - * @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 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ -namespace OCA\UpdateNotification\Tests\Notification; +namespace OCA\UpdateNotification\Tests\BackgroundJob; use OC\Installer; use OC\Updater\VersionCheck; -use OCA\UpdateNotification\Notification\BackgroundJob; +use OCA\UpdateNotification\BackgroundJob\UpdateAvailableNotifications; use OCP\App\IAppManager; -use OCP\Http\Client\IClientService; +use OCP\AppFramework\Services\IAppConfig; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IConfig; use OCP\IGroup; use OCP\IGroupManager; use OCP\IUser; use OCP\Notification\IManager; use OCP\Notification\INotification; +use OCP\ServerVersion; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; -class BackgroundJobTest extends TestCase { - - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - protected $config; - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $notificationManager; - /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $groupManager; - /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $appManager; - /** @var IClientService|\PHPUnit\Framework\MockObject\MockObject */ - protected $client; - /** @var Installer|\PHPUnit\Framework\MockObject\MockObject */ - protected $installer; +class UpdateAvailableNotificationsTest extends TestCase { + private ServerVersion&MockObject $serverVersion; + private IConfig&MockObject $config; + private IManager&MockObject $notificationManager; + private IGroupManager&MockObject $groupManager; + private IAppManager&MockObject $appManager; + private IAppConfig&MockObject $appConfig; + private ITimeFactory&MockObject $timeFactory; + private Installer&MockObject $installer; + private VersionCheck&MockObject $versionCheck; protected function setUp(): void { parent::setUp(); + $this->serverVersion = $this->createMock(ServerVersion::class); $this->config = $this->createMock(IConfig::class); + $this->appConfig = $this->createMock(IAppConfig::class); $this->notificationManager = $this->createMock(IManager::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->appManager = $this->createMock(IAppManager::class); - $this->client = $this->createMock(IClientService::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); $this->installer = $this->createMock(Installer::class); + $this->versionCheck = $this->createMock(VersionCheck::class); } /** - * @param array $methods - * @return BackgroundJob|\PHPUnit\Framework\MockObject\MockObject + * @return UpdateAvailableNotifications|MockObject */ - protected function getJob(array $methods = []) { + protected function getJob(array $methods = []): UpdateAvailableNotifications { if (empty($methods)) { - return new BackgroundJob( + return new UpdateAvailableNotifications( + $this->timeFactory, + $this->serverVersion, $this->config, + $this->appConfig, $this->notificationManager, $this->groupManager, $this->appManager, - $this->client, - $this->installer + $this->installer, + $this->versionCheck, ); } { - return $this->getMockBuilder(BackgroundJob::class) + return $this->getMockBuilder(UpdateAvailableNotifications::class) ->setConstructorArgs([ + $this->timeFactory, + $this->serverVersion, $this->config, + $this->appConfig, $this->notificationManager, $this->groupManager, $this->appManager, - $this->client, $this->installer, + $this->versionCheck, ]) - ->setMethods($methods) + ->onlyMethods($methods) ->getMock(); } } - public function testRun() { + public function testRun(): void { $job = $this->getJob([ 'checkCoreUpdate', 'checkAppUpdates', @@ -107,14 +95,36 @@ class BackgroundJobTest extends TestCase { $job->expects($this->once()) ->method('checkAppUpdates'); - $this->config->method('getSystemValueBool') - ->with('debug', false) - ->willReturn(true); + $this->config->expects(self::exactly(2)) + ->method('getSystemValueBool') + ->willReturnMap([ + ['debug', false, true], + ['has_internet_connection', true, true], + ]); + self::invokePrivate($job, 'run', [null]); + } + + public function testRunNoInternet(): void { + $job = $this->getJob([ + 'checkCoreUpdate', + 'checkAppUpdates', + ]); + + $job->expects($this->never()) + ->method('checkCoreUpdate'); + $job->expects($this->never()) + ->method('checkAppUpdates'); + + $this->config + ->expects(self::once()) + ->method('getSystemValueBool') + ->with('has_internet_connection', true) + ->willReturn(false); self::invokePrivate($job, 'run', [null]); } - public function dataCheckCoreUpdate(): array { + public static function dataCheckCoreUpdate(): array { return [ ['daily', null, null, null, null], ['git', null, null, null, null], @@ -142,40 +152,25 @@ class BackgroundJobTest extends TestCase { ]; } - /** - * @dataProvider dataCheckCoreUpdate - * - * @param string $channel - * @param mixed $versionCheck - * @param null|string $version - * @param null|string $readableVersion - * @param null|int $errorDays - */ - public function testCheckCoreUpdate(string $channel, $versionCheck, $version, $readableVersion, $errorDays) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataCheckCoreUpdate')] + public function testCheckCoreUpdate(string $channel, mixed $versionCheck, mixed $version, ?string $readableVersion, ?int $errorDays): void { $job = $this->getJob([ - 'getChannel', - 'createVersionCheck', 'createNotifications', 'clearErrorNotifications', 'sendErrorNotifications', ]); - $job->expects($this->once()) + $this->serverVersion->expects($this->once()) ->method('getChannel') ->willReturn($channel); if ($versionCheck === null) { - $job->expects($this->never()) - ->method('createVersionCheck'); + $this->versionCheck->expects($this->never()) + ->method('check'); } else { - $check = $this->createMock(VersionCheck::class); - $check->expects($this->once()) + $this->versionCheck->expects($this->once()) ->method('check') ->willReturn($versionCheck); - - $job->expects($this->once()) - ->method('createVersionCheck') - ->willReturn($check); } if ($version === null) { @@ -189,19 +184,19 @@ class BackgroundJobTest extends TestCase { $job->expects($this->never()) ->method('clearErrorNotifications'); - $this->config->expects($this->once()) - ->method('getAppValue') - ->willReturn($errorDays); - $this->config->expects($this->once()) - ->method('setAppValue') - ->with('updatenotification', 'update_check_errors', $errorDays + 1); + $this->appConfig->expects($this->once()) + ->method('getAppValueInt') + ->willReturn($errorDays ?? 0); + $this->appConfig->expects($this->once()) + ->method('setAppValueInt') + ->with('update_check_errors', $errorDays + 1); $job->expects($errorDays !== null ? $this->once() : $this->never()) ->method('sendErrorNotifications') ->with($errorDays + 1); } else { - $this->config->expects($this->once()) - ->method('setAppValue') - ->with('updatenotification', 'update_check_errors', 0); + $this->appConfig->expects($this->once()) + ->method('setAppValueInt') + ->with('update_check_errors', 0); $job->expects($this->once()) ->method('clearErrorNotifications'); $job->expects($this->once()) @@ -209,10 +204,17 @@ class BackgroundJobTest extends TestCase { ->with('core', $version, $readableVersion); } + $this->config->expects(self::any()) + ->method('getSystemValueBool') + ->willReturnMap([ + ['updatechecker', true, true], + ['has_internet_connection', true, true], + ]); + self::invokePrivate($job, 'checkCoreUpdate'); } - public function dataCheckAppUpdates(): array { + public static function dataCheckAppUpdates(): array { return [ [ ['app1', 'app2'], @@ -221,41 +223,40 @@ class BackgroundJobTest extends TestCase { ['app2', '1.9.2'], ], [ - ['app2', '1.9.2'], + ['app2', '1.9.2', ''], ], ], ]; } - /** - * @dataProvider dataCheckAppUpdates - * - * @param string[] $apps - * @param array $isUpdateAvailable - * @param array $notifications - */ - public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array $notifications) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataCheckAppUpdates')] + public function testCheckAppUpdates(array $apps, array $isUpdateAvailable, array $notifications): void { $job = $this->getJob([ 'isUpdateAvailable', 'createNotifications', ]); $this->appManager->expects($this->once()) - ->method('getInstalledApps') + ->method('getEnabledApps') ->willReturn($apps); $job->expects($this->exactly(\count($apps))) ->method('isUpdateAvailable') ->willReturnMap($isUpdateAvailable); - $mockedMethod = $job->expects($this->exactly(\count($notifications))) - ->method('createNotifications'); - \call_user_func_array([$mockedMethod, 'withConsecutive'], $notifications); + $i = 0; + $job->expects($this->exactly(\count($notifications))) + ->method('createNotifications') + ->willReturnCallback(function () use ($notifications, &$i): void { + $this->assertEquals($notifications[$i], func_get_args()); + $i++; + }); + self::invokePrivate($job, 'checkAppUpdates'); } - public function dataCreateNotifications(): array { + public static function dataCreateNotifications(): array { return [ ['app1', '1.0.0', '1.0.0', false, false, null, null], ['app2', '1.0.1', '1.0.0', '1.0.0', true, ['user1'], [['user1']]], @@ -263,32 +264,22 @@ class BackgroundJobTest extends TestCase { ]; } - /** - * @dataProvider dataCreateNotifications - * - * @param string $app - * @param string $version - * @param string|false $lastNotification - * @param string|false $callDelete - * @param bool $createNotification - * @param string[]|null $users - * @param array|null $userNotifications - */ - public function testCreateNotifications(string $app, string $version, $lastNotification, $callDelete, $createNotification, $users, $userNotifications) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataCreateNotifications')] + public function testCreateNotifications(string $app, string $version, string|false $lastNotification, string|false $callDelete, bool $createNotification, ?array $users, ?array $userNotifications): void { $job = $this->getJob([ 'deleteOutdatedNotifications', 'getUsersToNotify', ]); - $this->config->expects($this->once()) - ->method('getAppValue') - ->with('updatenotification', $app, false) - ->willReturn($lastNotification); + $this->appConfig->expects($this->once()) + ->method('getAppValueString') + ->with($app, '') + ->willReturn($lastNotification ?: ''); if ($lastNotification !== $version) { - $this->config->expects($this->once()) - ->method('setAppValue') - ->with('updatenotification', $app, $version); + $this->appConfig->expects($this->once()) + ->method('setAppValueString') + ->with($app, $version); } if ($callDelete === false) { @@ -328,10 +319,9 @@ class BackgroundJobTest extends TestCase { ->willReturnSelf(); if ($userNotifications !== null) { - $mockedMethod = $notification->expects($this->exactly(\count($userNotifications))) + $notification->expects($this->exactly(\count($userNotifications))) ->method('setUser') ->willReturnSelf(); - \call_user_func_array([$mockedMethod, 'withConsecutive'], $userNotifications); $this->notificationManager->expects($this->exactly(\count($userNotifications))) ->method('notify'); @@ -348,26 +338,21 @@ class BackgroundJobTest extends TestCase { self::invokePrivate($job, 'createNotifications', [$app, $version]); } - public function dataGetUsersToNotify(): array { + public static function dataGetUsersToNotify(): array { return [ [['g1', 'g2'], ['g1' => null, 'g2' => ['u1', 'u2']], ['u1', 'u2']], [['g3', 'g4'], ['g3' => ['u1', 'u2'], 'g4' => ['u2', 'u3']], ['u1', 'u2', 'u3']], ]; } - /** - * @dataProvider dataGetUsersToNotify - * @param string[] $groups - * @param array $groupUsers - * @param string[] $expected - */ - public function testGetUsersToNotify(array $groups, array $groupUsers, array $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetUsersToNotify')] + public function testGetUsersToNotify(array $groups, array $groupUsers, array $expected): void { $job = $this->getJob(); - $this->config->expects($this->once()) - ->method('getAppValue') - ->with('updatenotification', 'notify_groups', '["admin"]') - ->willReturn(json_encode($groups)); + $this->appConfig->expects($this->once()) + ->method('getAppValueArray') + ->with('notify_groups', ['admin']) + ->willReturn($groups); $groupMap = []; foreach ($groupUsers as $gid => $uids) { @@ -393,7 +378,7 @@ class BackgroundJobTest extends TestCase { $this->assertEquals($expected, $result); } - public function dataDeleteOutdatedNotifications(): array { + public static function dataDeleteOutdatedNotifications(): array { return [ ['app1', '1.1.0'], ['app2', '1.2.0'], @@ -401,11 +386,11 @@ class BackgroundJobTest extends TestCase { } /** - * @dataProvider dataDeleteOutdatedNotifications * @param string $app * @param string $version */ - public function testDeleteOutdatedNotifications(string $app, string $version) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataDeleteOutdatedNotifications')] + public function testDeleteOutdatedNotifications(string $app, string $version): void { $notification = $this->createMock(INotification::class); $notification->expects($this->once()) ->method('setApp') @@ -429,7 +414,7 @@ class BackgroundJobTest extends TestCase { /** * @param string[] $userIds - * @return IUser[]|\PHPUnit\Framework\MockObject\MockObject[] + * @return IUser[]|MockObject[] */ protected function getUsers(array $userIds): array { $users = []; @@ -445,7 +430,7 @@ class BackgroundJobTest extends TestCase { /** * @param string $gid - * @return \OCP\IGroup|\PHPUnit\Framework\MockObject\MockObject + * @return IGroup|MockObject */ protected function getGroup(string $gid) { $group = $this->createMock(IGroup::class); diff --git a/apps/updatenotification/tests/Controller/APIControllerTest.php b/apps/updatenotification/tests/Controller/APIControllerTest.php new file mode 100644 index 00000000000..84973e64c22 --- /dev/null +++ b/apps/updatenotification/tests/Controller/APIControllerTest.php @@ -0,0 +1,160 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\UpdateNotification\Tests\Controller; + +use OC\App\AppStore\Fetcher\AppFetcher; +use OCA\UpdateNotification\AppInfo\Application; +use OCA\UpdateNotification\Controller\APIController; +use OCA\UpdateNotification\Manager; +use OCP\App\IAppManager; +use OCP\AppFramework\Http; +use OCP\IConfig; +use OCP\IRequest; +use OCP\IUserSession; +use OCP\L10N\IFactory; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class APIControllerTest extends TestCase { + private IRequest&MockObject $request; + private IConfig&MockObject $config; + private IAppManager&MockObject $appManager; + private AppFetcher&MockObject $appFetcher; + private IFactory&MockObject $l10nFactory; + private IUserSession&MockObject $userSession; + private Manager&MockObject $manager; + + private APIController $apiController; + + protected function setUp(): void { + parent::setUp(); + + $this->request = $this->createMock(IRequest::class); + $this->config = $this->createMock(IConfig::class); + $this->appManager = $this->createMock(IAppManager::class); + $this->appFetcher = $this->createMock(AppFetcher::class); + $this->l10nFactory = $this->createMock(IFactory::class); + $this->userSession = $this->createMock(IUserSession::class); + $this->manager = $this->createMock(Manager::class); + + $this->apiController = new APIController( + Application::APP_NAME, + $this->request, + $this->config, + $this->appManager, + $this->appFetcher, + $this->l10nFactory, + $this->userSession, + $this->manager, + ); + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetAppChangelog')] + public function testGetAppChangelogEntry( + array $params, + bool $hasChanges, + array $appInfo, + array $expected, + ): void { + $this->appManager->method('getAppInfo') + ->with('the-app') + ->willReturn($appInfo); + $this->appManager->method('getAppVersion') + ->with('the-app') + ->willReturn($appInfo['version']); + $this->manager->method('getChangelog') + ->with('the-app', self::anything()) + ->willReturnCallback(fn ($app, $version) => $hasChanges ? "$app v$version" : null); + + $result = $this->apiController->getAppChangelogEntry(...$params); + $this->assertEquals($result->getStatus(), $expected['status']); + $this->assertEquals($result->getData(), $expected['data']); + } + + public static function dataGetAppChangelog(): array { + return [ + 'no changes found' => [ + ['the-app', null], + false, + [ + 'name' => 'Localized name', + 'version' => '1.0.0', + ], + [ + 'status' => Http::STATUS_NOT_FOUND, + 'data' => [], + ] + ], + 'changes with version parameter' => [ + ['the-app', '1.0.0'], + true, + [ + 'name' => 'Localized name', + 'version' => '1.2.0', // installed version + ], + [ + 'status' => Http::STATUS_OK, + 'data' => [ + 'appName' => 'Localized name', + 'content' => 'the-app v1.0.0', + 'version' => '1.0.0', + ], + ] + ], + 'changes without version parameter' => [ + ['the-app', null], + true, + [ + 'name' => 'Localized name', + 'version' => '1.2.0', + ], + [ + 'status' => Http::STATUS_OK, + 'data' => [ + 'appName' => 'Localized name', + 'content' => 'the-app v1.2.0', + 'version' => '1.2.0', + ], + ] + ], + 'changes of pre-release version' => [ + ['the-app', null], + true, + [ + 'name' => 'Localized name', + 'version' => '1.2.0-alpha.1', + ], + [ + 'status' => Http::STATUS_OK, + 'data' => [ + 'appName' => 'Localized name', + 'content' => 'the-app v1.2.0', + 'version' => '1.2.0-alpha.1', + ], + ] + ], + 'changes of pre-release version as parameter' => [ + ['the-app', '1.2.0-alpha.2'], + true, + [ + 'name' => 'Localized name', + 'version' => '1.2.0-beta.3', + ], + [ + 'status' => Http::STATUS_OK, + 'data' => [ + 'appName' => 'Localized name', + 'content' => 'the-app v1.2.0', + 'version' => '1.2.0-alpha.2', + ], + ] + ], + ]; + } +} diff --git a/apps/updatenotification/tests/Controller/AdminControllerTest.php b/apps/updatenotification/tests/Controller/AdminControllerTest.php index 836dc25f4aa..2263495fc14 100644 --- a/apps/updatenotification/tests/Controller/AdminControllerTest.php +++ b/apps/updatenotification/tests/Controller/AdminControllerTest.php @@ -3,56 +3,34 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @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> - * - * @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 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\UpdateNotification\Tests\Controller; +use OCA\UpdateNotification\BackgroundJob\ResetToken; use OCA\UpdateNotification\Controller\AdminController; -use OCA\UpdateNotification\ResetTokenBackgroundJob; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJobList; +use OCP\IAppConfig; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; use OCP\Security\ISecureRandom; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class AdminControllerTest extends TestCase { - /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */ - private $request; - /** @var IJobList|\PHPUnit\Framework\MockObject\MockObject */ - private $jobList; - /** @var ISecureRandom|\PHPUnit\Framework\MockObject\MockObject */ - private $secureRandom; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - private $config; - /** @var AdminController */ - private $adminController; - /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ - private $timeFactory; - /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */ - private $l10n; + private IRequest&MockObject $request; + private IJobList&MockObject $jobList; + private ISecureRandom&MockObject $secureRandom; + private IConfig&MockObject $config; + private ITimeFactory&MockObject $timeFactory; + private IL10N&MockObject $l10n; + private IAppConfig&MockObject $appConfig; + + private AdminController $adminController; protected function setUp(): void { parent::setUp(); @@ -61,6 +39,7 @@ class AdminControllerTest extends TestCase { $this->jobList = $this->createMock(IJobList::class); $this->secureRandom = $this->createMock(ISecureRandom::class); $this->config = $this->createMock(IConfig::class); + $this->appConfig = $this->createMock(IAppConfig::class); $this->timeFactory = $this->createMock(ITimeFactory::class); $this->l10n = $this->createMock(IL10N::class); @@ -70,16 +49,17 @@ class AdminControllerTest extends TestCase { $this->jobList, $this->secureRandom, $this->config, + $this->appConfig, $this->timeFactory, $this->l10n ); } - public function testCreateCredentials() { + public function testCreateCredentials(): void { $this->jobList ->expects($this->once()) ->method('add') - ->with(ResetTokenBackgroundJob::class); + ->with(ResetToken::class); $this->secureRandom ->expects($this->once()) ->method('generate') @@ -93,9 +73,9 @@ class AdminControllerTest extends TestCase { ->expects($this->once()) ->method('getTime') ->willReturn(12345); - $this->config + $this->appConfig ->expects($this->once()) - ->method('setAppValue') + ->method('setValueInt') ->with('core', 'updater.secret.created', 12345); $expected = new DataResponse('MyGeneratedToken'); diff --git a/apps/updatenotification/tests/Notification/NotifierTest.php b/apps/updatenotification/tests/Notification/NotifierTest.php index f5d98e88e23..aab5a84d6f3 100644 --- a/apps/updatenotification/tests/Notification/NotifierTest.php +++ b/apps/updatenotification/tests/Notification/NotifierTest.php @@ -3,32 +3,15 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @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> - * - * @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 ownCloud, Inc. + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\UpdateNotification\Tests\Notification; use OCA\UpdateNotification\Notification\Notifier; -use OCP\IConfig; +use OCP\App\IAppManager; +use OCP\AppFramework\Services\IAppConfig; use OCP\IGroupManager; use OCP\IURLGenerator; use OCP\IUserSession; @@ -36,65 +19,69 @@ use OCP\L10N\IFactory; use OCP\Notification\AlreadyProcessedException; use OCP\Notification\IManager; use OCP\Notification\INotification; +use OCP\ServerVersion; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class NotifierTest extends TestCase { - /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ - protected $urlGenerator; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - protected $config; - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $notificationManager; - /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */ - protected $l10nFactory; - /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ - protected $userSession; - /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $groupManager; + protected IURLGenerator&MockObject $urlGenerator; + protected IAppConfig&MockObject $appConfig; + protected IManager&MockObject $notificationManager; + protected IFactory&MockObject $l10nFactory; + protected IUserSession&MockObject $userSession; + protected IGroupManager&MockObject $groupManager; + protected IAppManager&MockObject $appManager; + protected ServerVersion&MockObject $serverVersion; protected function setUp(): void { parent::setUp(); $this->urlGenerator = $this->createMock(IURLGenerator::class); - $this->config = $this->createMock(IConfig::class); + $this->appConfig = $this->createMock(IAppConfig::class); $this->notificationManager = $this->createMock(IManager::class); $this->l10nFactory = $this->createMock(IFactory::class); $this->userSession = $this->createMock(IUserSession::class); $this->groupManager = $this->createMock(IGroupManager::class); + $this->appManager = $this->createMock(IAppManager::class); + $this->serverVersion = $this->createMock(ServerVersion::class); } /** * @param array $methods - * @return Notifier|\PHPUnit\Framework\MockObject\MockObject + * @return Notifier|MockObject */ - protected function getNotifier(array $methods = []) { + protected function getNotifier(array $methods = []): Notifier { if (empty($methods)) { return new Notifier( $this->urlGenerator, - $this->config, + $this->appConfig, $this->notificationManager, $this->l10nFactory, $this->userSession, - $this->groupManager + $this->groupManager, + $this->appManager, + $this->serverVersion, ); } { return $this->getMockBuilder(Notifier::class) ->setConstructorArgs([ $this->urlGenerator, - $this->config, + $this->appConfig, $this->notificationManager, $this->l10nFactory, $this->userSession, $this->groupManager, + $this->appManager, + $this->serverVersion, ]) - ->setMethods($methods) + ->onlyMethods($methods) ->getMock(); } } - public function dataUpdateAlreadyInstalledCheck(): array { + public static function dataUpdateAlreadyInstalledCheck(): array { return [ ['1.1.0', '1.0.0', false], ['1.1.0', '1.1.0', true], @@ -102,14 +89,8 @@ class NotifierTest extends TestCase { ]; } - /** - * @dataProvider dataUpdateAlreadyInstalledCheck - * - * @param string $versionNotification - * @param string $versionInstalled - * @param bool $exception - */ - public function testUpdateAlreadyInstalledCheck(string $versionNotification, string $versionInstalled, bool $exception) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataUpdateAlreadyInstalledCheck')] + public function testUpdateAlreadyInstalledCheck(string $versionNotification, string $versionInstalled, bool $exception): void { $notifier = $this->getNotifier(); $notification = $this->createMock(INotification::class); diff --git a/apps/updatenotification/tests/ResetTokenBackgroundJobTest.php b/apps/updatenotification/tests/ResetTokenBackgroundJobTest.php deleted file mode 100644 index 129ba370980..00000000000 --- a/apps/updatenotification/tests/ResetTokenBackgroundJobTest.php +++ /dev/null @@ -1,86 +0,0 @@ -<?php - -declare(strict_types=1); - -/** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @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> - * - * @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\UpdateNotification\Tests; - -use OCA\UpdateNotification\ResetTokenBackgroundJob; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\IConfig; -use Test\TestCase; - -class ResetTokenBackgroundJobTest extends TestCase { - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - private $config; - /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ - private $timeFactory; - /** @var ResetTokenBackgroundJob */ - private $resetTokenBackgroundJob; - - protected function setUp(): void { - parent::setUp(); - $this->config = $this->createMock(IConfig::class); - $this->timeFactory = $this->createMock(ITimeFactory::class); - $this->resetTokenBackgroundJob = new ResetTokenBackgroundJob($this->config, $this->timeFactory); - } - - public function testRunWithNotExpiredToken() { - $this->timeFactory - ->expects($this->atLeastOnce()) - ->method('getTime') - ->willReturn(123); - $this->config - ->expects($this->once()) - ->method('getAppValue') - ->with('core', 'updater.secret.created', 123); - $this->config - ->expects($this->never()) - ->method('deleteSystemValue'); - - static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); - } - - public function testRunWithExpiredToken() { - $this->timeFactory - ->expects($this->at(0)) - ->method('getTime') - ->willReturn(1455131633); - $this->timeFactory - ->expects($this->at(1)) - ->method('getTime') - ->willReturn(1455045234); - $this->config - ->expects($this->once()) - ->method('getAppValue') - ->with('core', 'updater.secret.created', 1455045234); - $this->config - ->expects($this->once()) - ->method('deleteSystemValue') - ->with('updater.secret'); - - static::invokePrivate($this->resetTokenBackgroundJob, 'run', [null]); - } -} diff --git a/apps/updatenotification/tests/Settings/AdminTest.php b/apps/updatenotification/tests/Settings/AdminTest.php index 852504fb45a..0429c628049 100644 --- a/apps/updatenotification/tests/Settings/AdminTest.php +++ b/apps/updatenotification/tests/Settings/AdminTest.php @@ -3,71 +3,50 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Julius Härtl <jus@bitgrid.net> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Vincent Petry <vincent@nextcloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\UpdateNotification\Tests\Settings; +use OCA\UpdateNotification\AppInfo\Application; use OCA\UpdateNotification\Settings\Admin; use OCA\UpdateNotification\UpdateChecker; use OCP\AppFramework\Http\TemplateResponse; +use OCP\AppFramework\Services\IInitialState; +use OCP\IAppConfig; use OCP\IConfig; use OCP\IDateTimeFormatter; use OCP\IGroup; use OCP\IGroupManager; +use OCP\IUserManager; use OCP\L10N\IFactory; use OCP\L10N\ILanguageIterator; +use OCP\ServerVersion; use OCP\Support\Subscription\IRegistry; -use OCP\Util; -use Test\TestCase; -use OCP\IUserManager; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; +use Test\TestCase; class AdminTest extends TestCase { - /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */ - protected $l10nFactory; - /** @var Admin */ - private $admin; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - private $config; - /** @var UpdateChecker|\PHPUnit\Framework\MockObject\MockObject */ - private $updateChecker; - /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - private $groupManager; - /** @var IDateTimeFormatter|\PHPUnit\Framework\MockObject\MockObject */ - private $dateTimeFormatter; - /** @var IRegistry|\PHPUnit\Framework\MockObject\MockObject */ - private $subscriptionRegistry; - /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */ - private $userManager; - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ - private $logger; + private IFactory&MockObject $l10nFactory; + private IConfig&MockObject $config; + private IAppConfig&MockObject $appConfig; + private UpdateChecker&MockObject $updateChecker; + private IGroupManager&MockObject $groupManager; + private IDateTimeFormatter&MockObject $dateTimeFormatter; + private IRegistry&MockObject $subscriptionRegistry; + private IUserManager&MockObject $userManager; + private LoggerInterface&MockObject $logger; + private IInitialState&MockObject $initialState; + private ServerVersion&MockObject $serverVersion; + private Admin $admin; protected function setUp(): void { parent::setUp(); $this->config = $this->createMock(IConfig::class); + $this->appConfig = $this->createMock(IAppConfig::class); $this->updateChecker = $this->createMock(UpdateChecker::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->dateTimeFormatter = $this->createMock(IDateTimeFormatter::class); @@ -75,84 +54,63 @@ class AdminTest extends TestCase { $this->subscriptionRegistry = $this->createMock(IRegistry::class); $this->userManager = $this->createMock(IUserManager::class); $this->logger = $this->createMock(LoggerInterface::class); + $this->initialState = $this->createMock(IInitialState::class); + $this->serverVersion = $this->createMock(ServerVersion::class); $this->admin = new Admin( - $this->config, - $this->updateChecker, - $this->groupManager, - $this->dateTimeFormatter, - $this->l10nFactory, + $this->config, + $this->appConfig, + $this->updateChecker, + $this->groupManager, + $this->dateTimeFormatter, + $this->l10nFactory, $this->subscriptionRegistry, $this->userManager, - $this->logger + $this->logger, + $this->initialState, + $this->serverVersion, ); } - public function testGetFormWithUpdate() { - $backend1 = $this->createMock(UserInterface::class); - $backend2 = $this->createMock(UserInterface::class); - $backend3 = $this->createMock(UserInterface::class); - $backend1 - ->expects($this->once()) - ->method('implementsActions') - ->with(Backend::COUNT_USERS) - ->willReturn(false); - $backend2 - ->expects($this->once()) - ->method('implementsActions') - ->with(Backend::COUNT_USERS) - ->willReturn(true); - $backend3 - ->expects($this->once()) - ->method('implementsActions') - ->with(Backend::COUNT_USERS) - ->willReturn(true); - $backend1 - ->expects($this->never()) - ->method('countUsers'); - $backend2 - ->expects($this->once()) - ->method('countUsers') - ->with() - ->willReturn(false); - $backend3 - ->expects($this->once()) - ->method('countUsers') - ->with() - ->willReturn(5); + public function testGetFormWithUpdate(): void { + $this->serverVersion->expects(self::atLeastOnce()) + ->method('getChannel') + ->willReturn('daily'); $this->userManager ->expects($this->once()) - ->method('getBackends') - ->with() - ->willReturn([$backend1, $backend2, $backend3]); + ->method('countUsersTotal') + ->willReturn(5); $channels = [ 'daily', 'beta', 'stable', 'production', ]; - $currentChannel = Util::getChannel(); - if ($currentChannel === 'git') { - $channels[] = 'git'; - } - - $this->config - ->expects($this->exactly(2)) - ->method('getAppValue') - ->willReturnMap([ - ['core', 'lastupdatedat', '', '12345'], - ['updatenotification', 'notify_groups', '["admin"]', '["admin"]'], - ]); + $this->appConfig + ->expects($this->once()) + ->method('getValueInt') + ->with('core', 'lastupdatedat', 0) + ->willReturn(12345); + $this->appConfig + ->expects($this->once()) + ->method('getValueArray') + ->with(Application::APP_NAME, 'notify_groups', ['admin']) + ->willReturn(['admin']); $this->config ->method('getSystemValue') ->willReturnMap([ ['updater.server.url', 'https://updates.nextcloud.com/updater_server/', 'https://updates.nextcloud.com/updater_server/'], ['upgrade.disable-web', false, false], ]); + $this->config + ->expects(self::any()) + ->method('getSystemValueBool') + ->with('updatechecker', true) + ->willReturn(true); $this->dateTimeFormatter ->expects($this->once()) ->method('formatDateTime') - ->with('12345') + ->with(12345) ->willReturn('LastCheckedReturnValue'); $this->updateChecker ->expects($this->once()) @@ -184,12 +142,13 @@ class AdminTest extends TestCase { ->method('delegateHasValidSubscription') ->willReturn(true); - $params = [ - 'json' => json_encode([ + $this->initialState->expects($this->once()) + ->method('provideInitialState') + ->with('data', [ 'isNewVersionAvailable' => true, 'isUpdateChecked' => true, 'lastChecked' => 'LastCheckedReturnValue', - 'currentChannel' => Util::getChannel(), + 'currentChannel' => 'daily', 'channels' => $channels, 'newVersion' => '8.1.2', 'newVersionString' => 'Nextcloud 8.1.2', @@ -202,71 +161,45 @@ class AdminTest extends TestCase { 'isDefaultUpdateServerURL' => true, 'updateServerURL' => 'https://updates.nextcloud.com/updater_server/', 'notifyGroups' => [ - ['value' => 'admin', 'label' => 'Administrators'], + ['id' => 'admin', 'displayname' => 'Administrators'], ], 'hasValidSubscription' => true, - ]), - ]; + ]); - $expected = new TemplateResponse('updatenotification', 'admin', $params, ''); + $expected = new TemplateResponse(Application::APP_NAME, 'admin', [], ''); $this->assertEquals($expected, $this->admin->getForm()); } - public function testGetFormWithUpdateAndChangedUpdateServer() { - $backend1 = $this->createMock(UserInterface::class); - $backend2 = $this->createMock(UserInterface::class); - $backend3 = $this->createMock(UserInterface::class); - $backend1 - ->expects($this->once()) - ->method('implementsActions') - ->with(Backend::COUNT_USERS) - ->willReturn(false); - $backend2 - ->expects($this->once()) - ->method('implementsActions') - ->with(Backend::COUNT_USERS) - ->willReturn(true); - $backend3 - ->expects($this->once()) - ->method('implementsActions') - ->with(Backend::COUNT_USERS) - ->willReturn(true); - $backend1 - ->expects($this->never()) - ->method('countUsers'); - $backend2 - ->expects($this->once()) - ->method('countUsers') - ->with() - ->willReturn(false); - $backend3 - ->expects($this->once()) - ->method('countUsers') - ->with() - ->willReturn(5); + public function testGetFormWithUpdateAndChangedUpdateServer(): void { + $this->serverVersion->expects(self::atLeastOnce()) + ->method('getChannel') + ->willReturn('beta'); $this->userManager ->expects($this->once()) - ->method('getBackends') - ->with() - ->willReturn([$backend1, $backend2, $backend3]); + ->method('countUsersTotal') + ->willReturn(5); $channels = [ 'daily', 'beta', 'stable', 'production', ]; - $currentChannel = Util::getChannel(); - if ($currentChannel === 'git') { - $channels[] = 'git'; - } + $this->appConfig + ->expects($this->once()) + ->method('getValueInt') + ->with('core', 'lastupdatedat', 0) + ->willReturn(12345); $this->config - ->expects($this->exactly(2)) - ->method('getAppValue') - ->willReturnMap([ - ['core', 'lastupdatedat', '', '12345'], - ['updatenotification', 'notify_groups', '["admin"]', '["admin"]'], - ]); + ->expects(self::any()) + ->method('getSystemValueBool') + ->with('updatechecker', true) + ->willReturn(true); + $this->appConfig + ->expects($this->once()) + ->method('getValueArray') + ->with(Application::APP_NAME, 'notify_groups', ['admin']) + ->willReturn(['admin']); $this->config ->method('getSystemValue') ->willReturnMap([ @@ -308,12 +241,13 @@ class AdminTest extends TestCase { ->method('delegateHasValidSubscription') ->willReturn(true); - $params = [ - 'json' => json_encode([ + $this->initialState->expects($this->once()) + ->method('provideInitialState') + ->with('data', [ 'isNewVersionAvailable' => true, 'isUpdateChecked' => true, 'lastChecked' => 'LastCheckedReturnValue', - 'currentChannel' => Util::getChannel(), + 'currentChannel' => 'beta', 'channels' => $channels, 'newVersion' => '8.1.2', 'newVersionString' => 'Nextcloud 8.1.2', @@ -326,71 +260,45 @@ class AdminTest extends TestCase { 'isDefaultUpdateServerURL' => false, 'updateServerURL' => 'https://updates.nextcloud.com/updater_server_changed/', 'notifyGroups' => [ - ['value' => 'admin', 'label' => 'Administrators'], + ['id' => 'admin', 'displayname' => 'Administrators'], ], 'hasValidSubscription' => true, - ]), - ]; + ]); - $expected = new TemplateResponse('updatenotification', 'admin', $params, ''); + $expected = new TemplateResponse(Application::APP_NAME, 'admin', [], ''); $this->assertEquals($expected, $this->admin->getForm()); } - public function testGetFormWithUpdateAndCustomersUpdateServer() { - $backend1 = $this->createMock(UserInterface::class); - $backend2 = $this->createMock(UserInterface::class); - $backend3 = $this->createMock(UserInterface::class); - $backend1 - ->expects($this->once()) - ->method('implementsActions') - ->with(Backend::COUNT_USERS) - ->willReturn(false); - $backend2 - ->expects($this->once()) - ->method('implementsActions') - ->with(Backend::COUNT_USERS) - ->willReturn(true); - $backend3 - ->expects($this->once()) - ->method('implementsActions') - ->with(Backend::COUNT_USERS) - ->willReturn(true); - $backend1 - ->expects($this->never()) - ->method('countUsers'); - $backend2 - ->expects($this->once()) - ->method('countUsers') - ->with() - ->willReturn(false); - $backend3 - ->expects($this->once()) - ->method('countUsers') - ->with() - ->willReturn(5); + public function testGetFormWithUpdateAndCustomersUpdateServer(): void { + $this->serverVersion->expects(self::atLeastOnce()) + ->method('getChannel') + ->willReturn('production'); $this->userManager ->expects($this->once()) - ->method('getBackends') - ->with() - ->willReturn([$backend1, $backend2, $backend3]); + ->method('countUsersTotal') + ->willReturn(5); $channels = [ 'daily', 'beta', 'stable', 'production', ]; - $currentChannel = Util::getChannel(); - if ($currentChannel === 'git') { - $channels[] = 'git'; - } + $this->appConfig + ->expects($this->once()) + ->method('getValueInt') + ->with('core', 'lastupdatedat', 0) + ->willReturn(12345); $this->config - ->expects($this->exactly(2)) - ->method('getAppValue') - ->willReturnMap([ - ['core', 'lastupdatedat', '', '12345'], - ['updatenotification', 'notify_groups', '["admin"]', '["admin"]'], - ]); + ->expects(self::any()) + ->method('getSystemValueBool') + ->with('updatechecker', true) + ->willReturn(true); + $this->appConfig + ->expects(self::once()) + ->method('getValueArray') + ->with(Application::APP_NAME, 'notify_groups', ['admin']) + ->willReturn(['admin']); $this->config ->method('getSystemValue') ->willReturnMap([ @@ -432,12 +340,13 @@ class AdminTest extends TestCase { ->method('delegateHasValidSubscription') ->willReturn(true); - $params = [ - 'json' => json_encode([ + $this->initialState->expects($this->once()) + ->method('provideInitialState') + ->with('data', [ 'isNewVersionAvailable' => true, 'isUpdateChecked' => true, 'lastChecked' => 'LastCheckedReturnValue', - 'currentChannel' => Util::getChannel(), + 'currentChannel' => 'production', 'channels' => $channels, 'newVersion' => '8.1.2', 'newVersionString' => 'Nextcloud 8.1.2', @@ -450,26 +359,41 @@ class AdminTest extends TestCase { 'isDefaultUpdateServerURL' => true, 'updateServerURL' => 'https://updates.nextcloud.com/customers/ABC-DEF/', 'notifyGroups' => [ - ['value' => 'admin', 'label' => 'Administrators'], + ['id' => 'admin', 'displayname' => 'Administrators'], ], 'hasValidSubscription' => true, - ]), - ]; + ]); - $expected = new TemplateResponse('updatenotification', 'admin', $params, ''); + $expected = new TemplateResponse(Application::APP_NAME, 'admin', [], ''); $this->assertEquals($expected, $this->admin->getForm()); } - public function testGetSection() { + public function testGetSection(): void { + $this->config + ->expects(self::atLeastOnce()) + ->method('getSystemValueBool') + ->with('updatechecker', true) + ->willReturn(true); + $this->assertSame('overview', $this->admin->getSection()); } - public function testGetPriority() { + public function testGetSectionDisabled(): void { + $this->config + ->expects(self::atLeastOnce()) + ->method('getSystemValueBool') + ->with('updatechecker', true) + ->willReturn(false); + + $this->assertNull($this->admin->getSection()); + } + + public function testGetPriority(): void { $this->assertSame(11, $this->admin->getPriority()); } - public function changesProvider() { + public static function changesProvider(): array { return [ [ #0, all info, en [ @@ -524,10 +448,8 @@ class AdminTest extends TestCase { ]; } - /** - * @dataProvider changesProvider - */ - public function testFilterChanges($changes, $userLang, $expectation) { + #[\PHPUnit\Framework\Attributes\DataProvider('changesProvider')] + public function testFilterChanges($changes, $userLang, $expectation): void { $iterator = $this->createMock(ILanguageIterator::class); $iterator->expects($this->any()) ->method('current') diff --git a/apps/updatenotification/tests/UpdateCheckerTest.php b/apps/updatenotification/tests/UpdateCheckerTest.php index 778b747e1fc..cffdc25d3e4 100644 --- a/apps/updatenotification/tests/UpdateCheckerTest.php +++ b/apps/updatenotification/tests/UpdateCheckerTest.php @@ -3,54 +3,39 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @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> - * - * @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 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\UpdateNotification\Tests; use OC\Updater\ChangesCheck; use OC\Updater\VersionCheck; use OCA\UpdateNotification\UpdateChecker; +use OCP\AppFramework\Services\IInitialState; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class UpdateCheckerTest extends TestCase { - /** @var ChangesCheck|\PHPUnit\Framework\MockObject\MockObject */ - protected $changesChecker; - /** @var VersionCheck|\PHPUnit\Framework\MockObject\MockObject */ - private $updater; - /** @var UpdateChecker */ - private $updateChecker; + + private ChangesCheck&MockObject $changesChecker; + private VersionCheck&MockObject $updater; + private IInitialState&MockObject $initialState; + private UpdateChecker $updateChecker; protected function setUp(): void { parent::setUp(); $this->updater = $this->createMock(VersionCheck::class); $this->changesChecker = $this->createMock(ChangesCheck::class); - $this->updateChecker = new UpdateChecker($this->updater, $this->changesChecker); + $this->initialState = $this->createMock(IInitialState::class); + $this->updateChecker = new UpdateChecker( + $this->updater, + $this->changesChecker, + $this->initialState, + ); } - public function testGetUpdateStateWithUpdateAndInvalidLink() { + public function testGetUpdateStateWithUpdateAndInvalidLink(): void { $this->updater ->expects($this->once()) ->method('check') @@ -74,7 +59,7 @@ class UpdateCheckerTest extends TestCase { $this->assertSame($expected, $this->updateChecker->getUpdateState()); } - public function testGetUpdateStateWithUpdateAndValidLink() { + public function testGetUpdateStateWithUpdateAndValidLink(): void { $changes = [ 'changelog' => 'https://nextcloud.com/changelog/#123-0-0', 'whatsNew' => [ @@ -122,7 +107,7 @@ class UpdateCheckerTest extends TestCase { $this->assertSame($expected, $this->updateChecker->getUpdateState()); } - public function testGetUpdateStateWithoutUpdate() { + public function testGetUpdateStateWithoutUpdate(): void { $this->updater ->expects($this->once()) ->method('check') @@ -131,4 +116,28 @@ class UpdateCheckerTest extends TestCase { $expected = []; $this->assertSame($expected, $this->updateChecker->getUpdateState()); } + + public function testSetInitialState(): void { + $this->updater + ->expects($this->once()) + ->method('check') + ->willReturn([ + 'version' => '1.2.3', + 'versionstring' => 'Nextcloud 1.2.3', + 'web' => 'https://docs.nextcloud.com/myUrl', + 'url' => 'https://downloads.nextcloud.org/server', + 'changes' => 'https://updates.nextcloud.com/changelog_server/?version=123.0.0', + 'autoupdater' => '1', + 'eol' => '0', + ]); + + $this->initialState->expects(self::once()) + ->method('provideInitialState') + ->with('updateState', [ + 'updateVersion' => 'Nextcloud 1.2.3', + 'updateLink' => 'https://docs.nextcloud.com/myUrl', + ]); + + $this->updateChecker->setInitialState(); + } } |