Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(migration): Don't redo app initialisation #214

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

## 1.20.0 – 2024-08-26
### Changed
- Nextcloud 30 compatibility
- Require Nextcloud 28

## 1.19.0 – 2024-03-29
### Changed
- Nextcloud 29 compatibility
Expand Down
4 changes: 2 additions & 2 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<description>This app sends notifications to users when they reached 85, 90 and 95% of their quota (checked once a day).
In addition an email can be sent to the users. The three percentages can be changed in the admin settings.
It is also possible to have a link in the email and the notification for upsell options.</description>
<version>1.20.0-dev.0</version>
<version>1.20.0</version>
<licence>agpl</licence>
<author>Joas Schilling</author>
<namespace>QuotaWarning</namespace>
Expand All @@ -23,7 +23,7 @@ It is also possible to have a link in the email and the notification for upsell
<screenshot>https://raw.githubusercontent.com/nextcloud/quota_warning/main/docs/email.png</screenshot>
<screenshot>https://raw.githubusercontent.com/nextcloud/quota_warning/main/docs/admin-settings.png</screenshot>
<dependencies>
<nextcloud min-version="28" max-version="29" />
<nextcloud min-version="28" max-version="30" />
</dependencies>
<repair-steps>
<install>
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"psalm:update-baseline": "psalm.phar --threads=1 --update-baseline --set-baseline=tests/psalm-baseline.xml",
"psalm:clear": "psalm.phar --clear-cache && psalm.phar --clear-global-cache",
"psalm:fix": "psalm.phar --alter --issues=InvalidReturnType,InvalidNullableReturnType,MissingParamType,InvalidFalsableReturnType",
"test:unit": "vendor/bin/phpunit -c tests/phpunit.xml"
"test:unit": "vendor/bin/phpunit --color -c tests/phpunit.xml"
},
"require-dev": {
"nextcloud/coding-standard": "^1.2",
Expand Down
18 changes: 16 additions & 2 deletions lib/Migration/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

namespace OCA\QuotaWarning\Migration;

use OCA\QuotaWarning\AppInfo\Application;
use OCA\QuotaWarning\Job\User;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Migration\IOutput;
Expand All @@ -37,11 +39,17 @@ class Install implements IRepairStep {

/** @var IJobList */
protected $jobList;
/** @var IConfig */
protected $config;

public function __construct(IUserManager $userManager,
IJobList $jobList) {
public function __construct(
IUserManager $userManager,
IJobList $jobList,
IConfig $config,
) {
$this->userManager = $userManager;
$this->jobList = $jobList;
$this->config = $config;
}

/**
Expand All @@ -59,6 +67,10 @@ public function getName(): string {
* @since 9.1.0
*/
public function run(IOutput $output): void {
if ($this->config->getAppValue(Application::APP_ID, 'initialised', 'no') === 'yes') {
return;
}

$output->startProgress();
$this->userManager->callForSeenUsers(function (IUser $user) use ($output) {
$this->jobList->add(
Expand All @@ -68,5 +80,7 @@ public function run(IOutput $output): void {
$output->advance();
});
$output->finishProgress();

$this->config->setAppValue(Application::APP_ID, 'initialised', 'yes');
}
}
11 changes: 6 additions & 5 deletions tests/AppInfo/ApplicationTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
*
Expand Down Expand Up @@ -53,12 +56,12 @@ protected function setUp(): void {
$this->container = $this->app->getContainer();
}

public function testContainerAppName() {
public function testContainerAppName(): void {
$this->app = new Application();
$this->assertEquals(Application::APP_ID, $this->container->getAppName());
}

public function dataContainerQuery() {
public function dataContainerQuery(): array {
return [
[Application::class, App::class],
[Notifier::class, INotifier::class],
Expand All @@ -70,10 +73,8 @@ public function dataContainerQuery() {

/**
* @dataProvider dataContainerQuery
* @param string $service
* @param string $expected
*/
public function testContainerQuery($service, $expected) {
public function testContainerQuery(string $service, string $expected): void {
$this->assertInstanceOf($expected, $this->container->query($service));
}
}
7 changes: 5 additions & 2 deletions tests/Job/UserTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
*
Expand Down Expand Up @@ -49,11 +52,11 @@ protected function setUp(): void {
);
}

public function testInterval() {
public function testInterval(): void {
$this->assertSame(86400, self::invokePrivate($this->job, 'interval'));
}

public function testRun() {
public function testRun(): void {
$this->checkQuota->expects($this->once())
->method('check')
->with('test1');
Expand Down
49 changes: 39 additions & 10 deletions tests/Migration/InstallTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
*
Expand Down Expand Up @@ -26,45 +28,50 @@
use OCA\QuotaWarning\Job\User;
use OCA\QuotaWarning\Migration\Install;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Migration\IOutput;
use PHPUnit\Framework\MockObject\MockObject;

class InstallTest extends \Test\TestCase {
/** @var Install */
protected $migration;

/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
/** @var IUserManager|MockObject */
protected $userManager;
/** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */
/** @var IJobList|MockObject */
protected $jobList;
/** @var IConfig|MockObject */
protected $config;
/** @var Install */
protected $migration;

protected function setUp(): void {
parent::setUp();

$this->userManager = $this->createMock(IUserManager::class);
$this->jobList = $this->createMock(IJobList::class);
$this->config = $this->createMock(IConfig::class);

$this->migration = new Install(
$this->userManager,
$this->jobList
$this->jobList,
$this->config
);
}

public function testGetName() {
public function testGetName(): void {
$this->assertIsString($this->migration->getName());
}

protected function getUser($uid) {
protected function getUser(string $uid): MockObject {
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn($uid);
return $user;
}

public function testRun() {
/** @var IOutput|\PHPUnit_Framework_MockObject_MockObject $output */
public function testRun(): void {
/** @var IOutput|MockObject $output */
$output = $this->createMock(IOutput::class);
$output->expects($this->once())
->method('startProgress');
Expand All @@ -89,4 +96,26 @@ public function testRun() {

$this->migration->run($output);
}

public function testRunSkipped(): void {
/** @var IOutput|MockObject $output */
$output = $this->createMock(IOutput::class);
$output->expects($this->never())
->method('startProgress');
$output->expects($this->never())
->method('finishProgress');

$this->config->expects($this->once())
->method('getAppValue')
->with('quota_warning', 'initialised', 'no')
->willReturn('yes');

$this->userManager->expects($this->never())
->method('callForSeenUsers');

$this->jobList->expects($this->never())
->method('add');

$this->migration->run($output);
}
}
31 changes: 15 additions & 16 deletions tests/Notification/NotifierTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
*
Expand Down Expand Up @@ -31,20 +33,21 @@
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Notification\INotification;
use PHPUnit\Framework\MockObject\MockObject;

class NotifierTest extends \Test\TestCase {
/** @var Notifier */
protected $notifier;

/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
/** @var IFactory|MockObject */
protected $factory;
/** @var CheckQuota|\PHPUnit_Framework_MockObject_MockObject */
/** @var CheckQuota|MockObject */
protected $checkQuota;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
/** @var IConfig|MockObject */
protected $config;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
/** @var IURLGenerator|MockObject */
protected $urlGenerator;
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
/** @var IL10N|MockObject */
protected $l;

protected function setUp(): void {
Expand Down Expand Up @@ -72,8 +75,8 @@ protected function setUp(): void {
);
}

public function testPrepareWrongApp() {
/** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
public function testPrepareWrongApp(): void {
/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);

$notification->expects($this->once())
Expand All @@ -87,8 +90,8 @@ public function testPrepareWrongApp() {
$this->notifier->prepare($notification, 'en');
}

public function testPrepareLessUsage() {
/** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
public function testPrepareLessUsage(): void {
/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);

$notification->expects($this->once())
Expand All @@ -114,7 +117,7 @@ public function testPrepareLessUsage() {
$this->notifier->prepare($notification, 'en');
}

public function dataPrepare() {
public function dataPrepare(): array {
return [
[85.1, ' 85% ', 'app-dark.svg'],
[94.9, ' 95% ', 'app-warning.svg'],
Expand All @@ -124,12 +127,8 @@ public function dataPrepare() {

/**
* @dataProvider dataPrepare
*
* @param float $quota
* @param string $stringContains
* @param string $image
*/
public function testPrepare($quota, $stringContains, $image) {
public function testPrepare(float $quota, string $stringContains, string $image): void {
$this->checkQuota->expects($this->once())
->method('getRelativeQuotaUsage')
->with('user')
Expand All @@ -143,7 +142,7 @@ public function testPrepare($quota, $stringContains, $image) {
['quota_warning', 'alert_percentage', '95', '95'],
]);

/** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);
$notification->expects($this->once())
->method('getApp')
Expand Down
Loading