Added tests for CheckAppSettings migration

Signed-off-by: Marius David Wieschollek's avatarMarius David Wieschollek <passwords.public@mdns.eu>
parent 9a04d925
Pipeline #3173 passed with stages
in 3 minutes and 35 seconds
......@@ -7,10 +7,12 @@
namespace OCA\Passwords\Migration;
use OC\User\User;
use OCA\Passwords\Helper\AppSettings\ServiceSettingsHelper;
use OCA\Passwords\Services\ConfigurationService;
use OCA\Passwords\Services\HelperService;
use OCA\Passwords\Services\NotificationService;
use OCP\IGroupManager;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
......@@ -42,17 +44,34 @@ class CheckAppSettings implements IRepairStep {
*/
protected $notifications;
/**
* @var IGroupManager
*/
protected $groupManager;
/**
* @var null|User[]
*/
protected $admins = null;
/**
* CheckAppSettings constructor.
*
* @param ServiceSettingsHelper $serviceSettings
* @param NotificationService $notifications
* @param ConfigurationService $config
* @param IGroupManager $groupManager
* @param NotificationService $notifications
* @param ServiceSettingsHelper $serviceSettings
*/
public function __construct(ServiceSettingsHelper $serviceSettings, NotificationService $notifications, ConfigurationService $config) {
$this->serviceSettings = $serviceSettings;
$this->notifications = $notifications;
public function __construct(
IGroupManager $groupManager,
ConfigurationService $config,
NotificationService $notifications,
ServiceSettingsHelper $serviceSettings
) {
$this->config = $config;
$this->groupManager = $groupManager;
$this->notifications = $notifications;
$this->serviceSettings = $serviceSettings;
}
/**
......@@ -102,8 +121,7 @@ class CheckAppSettings implements IRepairStep {
* @param string $setting
*/
protected function sendEmptySettingNotification(string $setting): void {
$adminGroup = \OC::$server->getGroupManager()->get('admin');
foreach($adminGroup->getUsers() as $admin) {
foreach($this->getAdmins() as $admin) {
$this->notifications->sendEmptyRequiredSettingNotification($admin->getUID(), $setting);
}
}
......@@ -112,8 +130,7 @@ class CheckAppSettings implements IRepairStep {
*
*/
protected function sendBesticonApiNotification(): void {
$adminGroup = \OC::$server->getGroupManager()->get('admin');
foreach($adminGroup->getUsers() as $admin) {
foreach($this->getAdmins() as $admin) {
$this->notifications->sendBesticonApiNotification($admin->getUID());
}
}
......@@ -122,8 +139,7 @@ class CheckAppSettings implements IRepairStep {
*
*/
protected function sendDeprecatedPlatformNotification(): void {
$adminGroup = \OC::$server->getGroupManager()->get('admin');
foreach($adminGroup->getUsers() as $admin) {
foreach($this->getAdmins() as $admin) {
$this->notifications->sendUpgradeRequiredNotification(
$admin->getUID(),
self::APP_BC_BREAK_VERSION,
......@@ -132,4 +148,16 @@ class CheckAppSettings implements IRepairStep {
);
}
}
/**
* @return User[]
*/
protected function getAdmins(): array {
if($this->admins === null) {
$adminGroup = $this->groupManager->get('admin');
$this->admins = $adminGroup->getUsers();
}
return $this->admins;
}
}
\ No newline at end of file
<?php
namespace OCP\AppFramework;
use OCP\Route\IRouter;
class App {
public static function buildAppNamespace(string $appId, string $topNamespace = 'OCA\\'): string {
return 'OCA\\Passwords\\';
}
public function __construct(string $appName, array $urlParams = []) {
}
public function getContainer(): IAppContainer {
return new IAppContainer();
}
public function registerRoutes(IRouter $router, array $routes) {
}
public function dispatch(string $controllerName, string $methodName) {
}
}
<?php
/**
* Created by PhpStorm.
* User: marius
* Date: 14.01.18
* Time: 20:15
*/
namespace OCP\AppFramework\Db;
class Entity {
......
<?php
/**
* Created by PhpStorm.
* User: marius
* Date: 23.01.18
* Time: 18:43
*/
namespace OCP\AppFramework;
......
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Joas Schilling <coding@schilljs.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/>
*
*/
namespace OCP\L10N;
use OCP\IUser;
/**
* @since 8.2.0
*/
interface IFactory {
public function get($app, $lang = null, $locale = null);
public function findLanguage($app = null);
public function findLocale($lang = null);
public function findLanguageFromLocale(string $app = 'core', string $locale = null);
public function findAvailableLanguages($app = null);
public function findAvailableLocales();
public function languageExists($app, $lang);
public function localeExists($locale);
public function createPluralFunction($string);
public function getLanguageIterator(IUser $user = null): ILanguageIterator;
}
<?php
namespace OCP;
class IGroup {
public function getUsers() { return []; }
}
<?php
namespace OCP;
class IGroupManager{
public function get(string $gid) { return new IGroup(); }
}
\ No newline at end of file
<?php
namespace OCP\Notification;
interface INotification {
public function setApp(string $app): INotification;
public function getApp(): string;
public function setUser(string $user): INotification;
public function getUser(): string;
public function setDateTime(\DateTime $dateTime): INotification;
public function getDateTime(): \DateTime;
public function setObject(string $type, string $id): INotification;
public function getObjectType(): string;
public function getObjectId(): string;
public function setSubject(string $subject, array $parameters = []): INotification;
public function getSubject(): string;
public function getSubjectParameters(): array;
public function setParsedSubject(string $subject): INotification;
public function getParsedSubject(): string;
public function setRichSubject(string $subject, array $parameters = []): INotification;
public function getRichSubject(): string;
public function getRichSubjectParameters(): array;
public function setMessage(string $message, array $parameters = []): INotification;
public function getMessage(): string;
public function getMessageParameters(): array;
public function setParsedMessage(string $message): INotification;
public function getParsedMessage(): string;
public function setRichMessage(string $message, array $parameters = []): INotification;
public function getRichMessage(): string;
public function getRichMessageParameters(): array;
public function setLink(string $link): INotification;
public function getLink(): string;
public function setIcon(string $icon): INotification;
public function getIcon(): string;
public function createAction(): IAction;
public function addAction(IAction $action): INotification;
public function getActions(): array;
public function addParsedAction(IAction $action): INotification;
public function getParsedActions(): array;
public function isValid(): bool;
public function isValidParsed(): bool;
}
<?php
namespace OCP\Notification;
interface INotifier {
public function getID(): string;
public function getName(): string;
public function prepare(INotification $notification, string $languageCode): INotification;
}
<?php
namespace OCP\Migration;
class IOutput {
public function info($message) {
}
public function warning($message) {
}
public function startProgress($max = 0) {
}
public function advance($step = 1, $description = '') {
}
public function finishProgress() {
}
}
<?php
namespace OCP\Migration;
interface IRepairStep {
public function getName();
public function run(IOutput $output);
}
<?php
namespace OCP;
class IUser {
public function getUID(): string {
return '';
}
}
\ No newline at end of file
<?php
namespace OCA\Passwords\Migration;
use Exception;
use OCA\Passwords\Helper\AppSettings\ServiceSettingsHelper;
use OCA\Passwords\Services\ConfigurationService;
use OCA\Passwords\Services\HelperService;
use OCA\Passwords\Services\NotificationService;
use OCA\Passwords\Services\ValidationService;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\Migration\IOutput;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class CheckAppSettingsTest extends TestCase {
/**
* @var ValidationService
*/
protected $checkAppSettings;
/**
* @var MockObject|IGroupManager
*/
protected $groupManager;
/**
* @var MockObject|ServiceSettingsHelper
*/
protected $settingsHelper;
/**
* @var MockObject|NotificationService
*/
protected $notificationService;
/**
* @var MockObject|ConfigurationService
*/
protected $configurationService;
/**
* @throws \ReflectionException
*/
protected function setUp(): void {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->settingsHelper = $this->createMock(ServiceSettingsHelper::class);
$this->notificationService = $this->createMock(NotificationService::class);
$this->configurationService = $this->createMock(ConfigurationService::class);
$this->checkAppSettings = new CheckAppSettings($this->groupManager, $this->configurationService, $this->notificationService, $this->settingsHelper);
}
/**
*
*/
public function testGetName(): void {
$this->assertEquals('Check app settings', $this->checkAppSettings->getName());
}
/**
*
*/
public function testSendNotificationIfFaviconApiMissing(): void {
$this->setUpGroupManager();
$this->notificationService->expects($this->once())->method('sendEmptyRequiredSettingNotification')->with('admin', 'favicon');
$this->settingsHelper->method('get')->willReturnMap(
[
['favicon', ['value' => HelperService::FAVICON_BESTICON]],
['favicon.api', ['value' => '']],
['preview', ['value' => '']],
['preview.api', ['value' => 'value']],
]
);
$this->configurationService->method('getSystemValue')->with('version')->willReturn(CheckAppSettings::NEXTCLOUD_RECOMMENDED_VERSION.'.0.0.0');
try {
$this->checkAppSettings->run(new IOutput());
} catch(Exception $e) {
$this->fail($e->getMessage());
}
}
/**
*
*/
public function testSendNoNotificationIfFaviconApiPresent(): void {
$this->setUpGroupManager();
$this->notificationService->expects($this->never())->method('sendEmptyRequiredSettingNotification');
$this->settingsHelper->method('get')->willReturnMap(
[
['favicon', ['value' => HelperService::FAVICON_BESTICON]],
['favicon.api', ['value' => 'https://api.example.com']],
['preview', ['value' => '']],
['preview.api', ['value' => 'value']],
]
);
$this->configurationService->method('getSystemValue')->with('version')->willReturn(CheckAppSettings::NEXTCLOUD_RECOMMENDED_VERSION.'.0.0.0');
try {
$this->checkAppSettings->run(new IOutput());
} catch(Exception $e) {
$this->fail($e->getMessage());
}
}
/**
*
*/
public function testSendNoNotificationIfFaviconApiNotRequired(): void {
$this->setUpGroupManager();
$this->notificationService->expects($this->never())->method('sendEmptyRequiredSettingNotification');
$this->settingsHelper->method('get')->willReturnMap(
[
['favicon', ['value' => 'none']],
['favicon.api', ['value' => '']],
['preview', ['value' => '']],
['preview.api', ['value' => 'value']],
]
);
$this->configurationService->method('getSystemValue')->with('version')->willReturn(CheckAppSettings::NEXTCLOUD_RECOMMENDED_VERSION.'.0.0.0');
try {
$this->checkAppSettings->run(new IOutput());
} catch(Exception $e) {
$this->fail($e->getMessage());
}
}
/**
*
*/
public function testSendNotificationIfPreviewApiMissing(): void {
$this->setUpGroupManager();
$this->notificationService->expects($this->once())->method('sendEmptyRequiredSettingNotification')->with('admin', 'preview');
$this->settingsHelper->method('get')->willReturnMap(
[
['favicon', ['value' => 'none']],
['favicon.api', ['value' => '']],
['preview', ['value' => 'test']],
['preview.api', ['value' => '', 'depends' => ['service.preview' => ['test']]]],
]
);
$this->configurationService->method('getSystemValue')->with('version')->willReturn(CheckAppSettings::NEXTCLOUD_RECOMMENDED_VERSION.'.0.0.0');
try {
$this->checkAppSettings->run(new IOutput());
} catch(Exception $e) {
$this->fail($e->getMessage());
}
}
/**
*
*/
public function testSendNoNotificationIfPreviewApiPresent(): void {
$this->setUpGroupManager();
$this->notificationService->expects($this->never())->method('sendEmptyRequiredSettingNotification');
$this->settingsHelper->method('get')->willReturnMap(
[
['favicon', ['value' => 'none']],
['favicon.api', ['value' => '']],
['preview', ['value' => 'test']],
['preview.api', ['value' => 'key', 'depends' => ['service.preview' => ['test']]]],
]
);
$this->configurationService->method('getSystemValue')->with('version')->willReturn(CheckAppSettings::NEXTCLOUD_RECOMMENDED_VERSION.'.0.0.0');
try {
$this->checkAppSettings->run(new IOutput());
} catch(Exception $e) {
$this->fail($e->getMessage());
}
}
/**
*
*/
public function testSendNotificationIfNextcloudOutdated(): void {
$this->setUpGroupManager();
$this->notificationService
->expects($this->once())
->method('sendUpgradeRequiredNotification')
->with('admin', CheckAppSettings::APP_BC_BREAK_VERSION, CheckAppSettings::NEXTCLOUD_RECOMMENDED_VERSION, CheckAppSettings::PHP_RECOMMENDED_VERSION);
$this->settingsHelper->method('get')->willReturnMap(
[
['favicon', ['value' => '']],
['favicon.api', ['value' => '']],
['preview', ['value' => '']],
['preview.api', ['value' => 'none']],
]
);
$this->configurationService->method('getSystemValue')->with('version')->willReturn('0.0.0.0');
try {
$this->checkAppSettings->run(new IOutput());
} catch(Exception $e) {
$this->fail($e->getMessage());
}
}
/**
*
*/
public function testSendNoNotificationIfNextcloudCurrent(): void {
$this->setUpGroupManager();
$this->notificationService
->expects($this->never())
->method('sendUpgradeRequiredNotification');
$this->settingsHelper->method('get')->willReturnMap(
[
['favicon', ['value' => '']],
['favicon.api', ['value' => '']],
['preview', ['value' => '']],
['preview.api', ['value' => 'none']],
]
);
$this->configurationService->method('getSystemValue')->with('version')->willReturn(CheckAppSettings::NEXTCLOUD_RECOMMENDED_VERSION.'.0.0.0');
try {
$this->checkAppSettings->run(new IOutput());
} catch(Exception $e) {
$this->fail($e->getMessage());
}
}
/**
*
*/
public function testSendNoNotificationIfPreviewApiNotRequired(): void {
$this->setUpGroupManager();
$this->notificationService->expects($this->never())->method('sendEmptyRequiredSettingNotification');
$this->settingsHelper->method('get')->willReturnMap(
[
['favicon', ['value' => 'none']],
['favicon.api', ['value' => '']],
['preview', ['value' => 'none']],
['preview.api', ['value' => '', 'depends' => ['service.preview' => ['test']]]],
]
);
$this->configurationService->method('getSystemValue')->with('version')->willReturn(CheckAppSettings::NEXTCLOUD_RECOMMENDED_VERSION.'.0.0.0');
try {
$this->checkAppSettings->run(new IOutput());
} catch(Exception $e) {
$this->fail($e->getMessage());
}
}
/**
*
*/
protected function setUpGroupManager(): void {
$admin = $this->createMock(IUser::class);
$admin->method('getUID')->willReturn('admin');
$group = $this->createMock(IGroup::class);
$group->method('getUsers')->willReturn([$admin]);
$this->groupManager->method('get')->with('admin')->willReturn($group);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment