aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Repair/Owncloud
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Repair/Owncloud')
-rw-r--r--tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php237
-rw-r--r--tests/lib/Repair/Owncloud/CleanPreviewsTest.php110
-rw-r--r--tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php156
3 files changed, 503 insertions, 0 deletions
diff --git a/tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php b/tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php
new file mode 100644
index 00000000000..fc88ee5d226
--- /dev/null
+++ b/tests/lib/Repair/Owncloud/CleanPreviewsBackgroundJobTest.php
@@ -0,0 +1,237 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace Test\Repair\Owncloud;
+
+use OC\Repair\Owncloud\CleanPreviewsBackgroundJob;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\BackgroundJob\IJobList;
+use OCP\Files\Folder;
+use OCP\Files\IRootFolder;
+use OCP\Files\NotFoundException;
+use OCP\Files\NotPermittedException;
+use OCP\IUserManager;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
+use Test\TestCase;
+
+class CleanPreviewsBackgroundJobTest extends TestCase {
+
+ private IRootFolder&MockObject $rootFolder;
+ private LoggerInterface&MockObject $logger;
+ private IJobList&MockObject $jobList;
+ private ITimeFactory&MockObject $timeFactory;
+ private IUserManager&MockObject $userManager;
+ private CleanPreviewsBackgroundJob $job;
+
+ public function setUp(): void {
+ parent::setUp();
+
+ $this->rootFolder = $this->createMock(IRootFolder::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
+ $this->jobList = $this->createMock(IJobList::class);
+ $this->timeFactory = $this->createMock(ITimeFactory::class);
+ $this->userManager = $this->createMock(IUserManager::class);
+
+ $this->userManager->expects($this->any())->method('userExists')->willReturn(true);
+
+ $this->job = new CleanPreviewsBackgroundJob(
+ $this->rootFolder,
+ $this->logger,
+ $this->jobList,
+ $this->timeFactory,
+ $this->userManager
+ );
+ }
+
+ public function testCleanupPreviewsUnfinished(): void {
+ $userFolder = $this->createMock(Folder::class);
+ $userRoot = $this->createMock(Folder::class);
+ $thumbnailFolder = $this->createMock(Folder::class);
+
+ $this->rootFolder->method('getUserFolder')
+ ->with($this->equalTo('myuid'))
+ ->willReturn($userFolder);
+
+ $userFolder->method('getParent')->willReturn($userRoot);
+
+ $userRoot->method('get')
+ ->with($this->equalTo('thumbnails'))
+ ->willReturn($thumbnailFolder);
+
+ $previewFolder1 = $this->createMock(Folder::class);
+
+ $previewFolder1->expects($this->once())
+ ->method('delete');
+
+ $thumbnailFolder->method('getDirectoryListing')
+ ->willReturn([$previewFolder1]);
+ $thumbnailFolder->expects($this->never())
+ ->method('delete');
+
+ $this->timeFactory->method('getTime')->willReturnOnConsecutiveCalls(100, 200);
+
+ $this->jobList->expects($this->once())
+ ->method('add')
+ ->with(
+ $this->equalTo(CleanPreviewsBackgroundJob::class),
+ $this->equalTo(['uid' => 'myuid'])
+ );
+
+ $loggerCalls = [];
+ $this->logger->expects($this->exactly(2))
+ ->method('info')
+ ->willReturnCallback(function () use (&$loggerCalls): void {
+ $loggerCalls[] = func_get_args();
+ });
+
+ $this->job->run(['uid' => 'myuid']);
+ self::assertEquals([
+ ['Started preview cleanup for myuid', []],
+ ['New preview cleanup scheduled for myuid', []],
+ ], $loggerCalls);
+ }
+
+ public function testCleanupPreviewsFinished(): void {
+ $userFolder = $this->createMock(Folder::class);
+ $userRoot = $this->createMock(Folder::class);
+ $thumbnailFolder = $this->createMock(Folder::class);
+
+ $this->rootFolder->method('getUserFolder')
+ ->with($this->equalTo('myuid'))
+ ->willReturn($userFolder);
+
+ $userFolder->method('getParent')->willReturn($userRoot);
+
+ $userRoot->method('get')
+ ->with($this->equalTo('thumbnails'))
+ ->willReturn($thumbnailFolder);
+
+ $previewFolder1 = $this->createMock(Folder::class);
+
+ $previewFolder1->expects($this->once())
+ ->method('delete');
+
+ $thumbnailFolder->method('getDirectoryListing')
+ ->willReturn([$previewFolder1]);
+
+ $this->timeFactory->method('getTime')->willReturnOnConsecutiveCalls(100, 101);
+
+ $this->jobList->expects($this->never())
+ ->method('add');
+
+ $loggerCalls = [];
+ $this->logger->expects($this->exactly(2))
+ ->method('info')
+ ->willReturnCallback(function () use (&$loggerCalls): void {
+ $loggerCalls[] = func_get_args();
+ });
+
+ $thumbnailFolder->expects($this->once())
+ ->method('delete');
+
+ $this->job->run(['uid' => 'myuid']);
+ self::assertEquals([
+ ['Started preview cleanup for myuid', []],
+ ['Preview cleanup done for myuid', []],
+ ], $loggerCalls);
+ }
+
+
+ public function testNoUserFolder(): void {
+ $this->rootFolder->method('getUserFolder')
+ ->with($this->equalTo('myuid'))
+ ->willThrowException(new NotFoundException());
+
+ $loggerCalls = [];
+ $this->logger->expects($this->exactly(2))
+ ->method('info')
+ ->willReturnCallback(function () use (&$loggerCalls): void {
+ $loggerCalls[] = func_get_args();
+ });
+
+ $this->job->run(['uid' => 'myuid']);
+ self::assertEquals([
+ ['Started preview cleanup for myuid', []],
+ ['Preview cleanup done for myuid', []],
+ ], $loggerCalls);
+ }
+
+ public function testNoThumbnailFolder(): void {
+ $userFolder = $this->createMock(Folder::class);
+ $userRoot = $this->createMock(Folder::class);
+
+ $this->rootFolder->method('getUserFolder')
+ ->with($this->equalTo('myuid'))
+ ->willReturn($userFolder);
+
+ $userFolder->method('getParent')->willReturn($userRoot);
+
+ $userRoot->method('get')
+ ->with($this->equalTo('thumbnails'))
+ ->willThrowException(new NotFoundException());
+
+ $loggerCalls = [];
+ $this->logger->expects($this->exactly(2))
+ ->method('info')
+ ->willReturnCallback(function () use (&$loggerCalls): void {
+ $loggerCalls[] = func_get_args();
+ });
+
+ $this->job->run(['uid' => 'myuid']);
+ self::assertEquals([
+ ['Started preview cleanup for myuid', []],
+ ['Preview cleanup done for myuid', []],
+ ], $loggerCalls);
+ }
+
+ public function testNotPermittedToDelete(): void {
+ $userFolder = $this->createMock(Folder::class);
+ $userRoot = $this->createMock(Folder::class);
+ $thumbnailFolder = $this->createMock(Folder::class);
+
+ $this->rootFolder->method('getUserFolder')
+ ->with($this->equalTo('myuid'))
+ ->willReturn($userFolder);
+
+ $userFolder->method('getParent')->willReturn($userRoot);
+
+ $userRoot->method('get')
+ ->with($this->equalTo('thumbnails'))
+ ->willReturn($thumbnailFolder);
+
+ $previewFolder1 = $this->createMock(Folder::class);
+
+ $previewFolder1->expects($this->once())
+ ->method('delete')
+ ->willThrowException(new NotPermittedException());
+
+ $thumbnailFolder->method('getDirectoryListing')
+ ->willReturn([$previewFolder1]);
+
+ $this->timeFactory->method('getTime')->willReturnOnConsecutiveCalls(100, 101);
+
+ $this->jobList->expects($this->never())
+ ->method('add');
+
+ $thumbnailFolder->expects($this->once())
+ ->method('delete')
+ ->willThrowException(new NotPermittedException());
+
+ $loggerCalls = [];
+ $this->logger->expects($this->exactly(2))
+ ->method('info')
+ ->willReturnCallback(function () use (&$loggerCalls): void {
+ $loggerCalls[] = func_get_args();
+ });
+
+ $this->job->run(['uid' => 'myuid']);
+ self::assertEquals([
+ ['Started preview cleanup for myuid', []],
+ ['Preview cleanup done for myuid', []],
+ ], $loggerCalls);
+ }
+}
diff --git a/tests/lib/Repair/Owncloud/CleanPreviewsTest.php b/tests/lib/Repair/Owncloud/CleanPreviewsTest.php
new file mode 100644
index 00000000000..e5a4441a4fa
--- /dev/null
+++ b/tests/lib/Repair/Owncloud/CleanPreviewsTest.php
@@ -0,0 +1,110 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace Test\Repair\Owncloud;
+
+use OC\Repair\Owncloud\CleanPreviews;
+use OC\Repair\Owncloud\CleanPreviewsBackgroundJob;
+use OCP\BackgroundJob\IJobList;
+use OCP\IConfig;
+use OCP\IUser;
+use OCP\IUserManager;
+use OCP\Migration\IOutput;
+use PHPUnit\Framework\MockObject\MockObject;
+use Test\TestCase;
+
+class CleanPreviewsTest extends TestCase {
+
+ private IJobList&MockObject $jobList;
+ private IUserManager&MockObject $userManager;
+ private IConfig&MockObject $config;
+
+ /** @var CleanPreviews */
+ private $repair;
+
+ public function setUp(): void {
+ parent::setUp();
+
+ $this->jobList = $this->createMock(IJobList::class);
+ $this->userManager = $this->createMock(IUserManager::class);
+ $this->config = $this->createMock(IConfig::class);
+
+ $this->repair = new CleanPreviews(
+ $this->jobList,
+ $this->userManager,
+ $this->config
+ );
+ }
+
+ public function testGetName(): void {
+ $this->assertSame('Add preview cleanup background jobs', $this->repair->getName());
+ }
+
+ public function testRun(): void {
+ $user1 = $this->createMock(IUser::class);
+ $user1->method('getUID')
+ ->willReturn('user1');
+ $user2 = $this->createMock(IUser::class);
+ $user2->method('getUID')
+ ->willReturn('user2');
+
+ $this->userManager->expects($this->once())
+ ->method('callForSeenUsers')
+ ->willReturnCallback(function (\Closure $function) use (&$user1, $user2): void {
+ $function($user1);
+ $function($user2);
+ });
+
+ $jobListCalls = [];
+ $this->jobList->expects($this->exactly(2))
+ ->method('add')
+ ->willReturnCallback(function () use (&$jobListCalls): void {
+ $jobListCalls[] = func_get_args();
+ });
+
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->with(
+ $this->equalTo('core'),
+ $this->equalTo('previewsCleanedUp'),
+ $this->equalTo(false)
+ )->willReturn(false);
+ $this->config->expects($this->once())
+ ->method('setAppValue')
+ ->with(
+ $this->equalTo('core'),
+ $this->equalTo('previewsCleanedUp'),
+ $this->equalTo(1)
+ );
+
+ $this->repair->run($this->createMock(IOutput::class));
+ $this->assertEqualsCanonicalizing([
+ [CleanPreviewsBackgroundJob::class, ['uid' => 'user1']],
+ [CleanPreviewsBackgroundJob::class, ['uid' => 'user2']],
+ ], $jobListCalls);
+ }
+
+
+ public function testRunAlreadyDone(): void {
+ $this->userManager->expects($this->never())
+ ->method($this->anything());
+
+ $this->jobList->expects($this->never())
+ ->method($this->anything());
+
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->with(
+ $this->equalTo('core'),
+ $this->equalTo('previewsCleanedUp'),
+ $this->equalTo(false)
+ )->willReturn('1');
+ $this->config->expects($this->never())
+ ->method('setAppValue');
+
+ $this->repair->run($this->createMock(IOutput::class));
+ }
+}
diff --git a/tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php b/tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php
new file mode 100644
index 00000000000..a3eb163b0d6
--- /dev/null
+++ b/tests/lib/Repair/Owncloud/UpdateLanguageCodesTest.php
@@ -0,0 +1,156 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace Test\Repair\Owncloud;
+
+use OC\Repair\Owncloud\UpdateLanguageCodes;
+use OCP\DB\QueryBuilder\IQueryBuilder;
+use OCP\IConfig;
+use OCP\IDBConnection;
+use OCP\Migration\IOutput;
+use OCP\Server;
+use PHPUnit\Framework\MockObject\MockObject;
+use Test\TestCase;
+
+/**
+ * Class UpdateLanguageCodesTest
+ *
+ * @group DB
+ *
+ * @package Test\Repair
+ */
+class UpdateLanguageCodesTest extends TestCase {
+
+ protected IDBConnection $connection;
+ private IConfig&MockObject $config;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->connection = Server::get(IDBConnection::class);
+ $this->config = $this->createMock(IConfig::class);
+ }
+
+ public function testRun(): void {
+ $users = [
+ ['userid' => 'user1', 'configvalue' => 'fi_FI'],
+ ['userid' => 'user2', 'configvalue' => 'de'],
+ ['userid' => 'user3', 'configvalue' => 'fi'],
+ ['userid' => 'user4', 'configvalue' => 'ja'],
+ ['userid' => 'user5', 'configvalue' => 'bg_BG'],
+ ['userid' => 'user6', 'configvalue' => 'ja'],
+ ['userid' => 'user7', 'configvalue' => 'th_TH'],
+ ['userid' => 'user8', 'configvalue' => 'th_TH'],
+ ];
+
+ // insert test data
+ $qb = $this->connection->getQueryBuilder();
+ $qb->insert('preferences')
+ ->values([
+ 'userid' => $qb->createParameter('userid'),
+ 'appid' => $qb->createParameter('appid'),
+ 'configkey' => $qb->createParameter('configkey'),
+ 'configvalue' => $qb->createParameter('configvalue'),
+ ]);
+ foreach ($users as $user) {
+ $qb->setParameters([
+ 'userid' => $user['userid'],
+ 'appid' => 'core',
+ 'configkey' => 'lang',
+ 'configvalue' => $user['configvalue'],
+ ])->executeStatement();
+ }
+
+ // check if test data is written to DB
+ $qb = $this->connection->getQueryBuilder();
+ $result = $qb->select(['userid', 'configvalue'])
+ ->from('preferences')
+ ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
+ ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
+ ->orderBy('userid')
+ ->executeQuery();
+
+ $rows = $result->fetchAll();
+ $result->closeCursor();
+
+ $this->assertSame($users, $rows, 'Asserts that the entries are the ones from the test data set');
+
+ $expectedOutput = [
+ ['Changed 1 setting(s) from "bg_BG" to "bg" in preferences table.'],
+ ['Changed 0 setting(s) from "cs_CZ" to "cs" in preferences table.'],
+ ['Changed 1 setting(s) from "fi_FI" to "fi" in preferences table.'],
+ ['Changed 0 setting(s) from "hu_HU" to "hu" in preferences table.'],
+ ['Changed 0 setting(s) from "nb_NO" to "nb" in preferences table.'],
+ ['Changed 0 setting(s) from "sk_SK" to "sk" in preferences table.'],
+ ['Changed 2 setting(s) from "th_TH" to "th" in preferences table.'],
+ ];
+ $outputMessages = [];
+ /** @var IOutput&MockObject $outputMock */
+ $outputMock = $this->createMock(IOutput::class);
+ $outputMock->expects($this->exactly(7))
+ ->method('info')
+ ->willReturnCallback(function () use (&$outputMessages): void {
+ $outputMessages[] = func_get_args();
+ });
+
+ $this->config->expects($this->once())
+ ->method('getSystemValueString')
+ ->with('version', '0.0.0')
+ ->willReturn('12.0.0.13');
+
+ // run repair step
+ $repair = new UpdateLanguageCodes($this->connection, $this->config);
+ $repair->run($outputMock);
+
+ // check if test data is correctly modified in DB
+ $qb = $this->connection->getQueryBuilder();
+ $result = $qb->select(['userid', 'configvalue'])
+ ->from('preferences')
+ ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
+ ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
+ ->orderBy('userid')
+ ->executeQuery();
+
+ $rows = $result->fetchAll();
+ $result->closeCursor();
+
+ // value has changed for one user
+ $users[0]['configvalue'] = 'fi';
+ $users[4]['configvalue'] = 'bg';
+ $users[6]['configvalue'] = 'th';
+ $users[7]['configvalue'] = 'th';
+ $this->assertSame($users, $rows, 'Asserts that the entries are updated correctly.');
+
+ // remove test data
+ foreach ($users as $user) {
+ $qb = $this->connection->getQueryBuilder();
+ $qb->delete('preferences')
+ ->where($qb->expr()->eq('userid', $qb->createNamedParameter($user['userid'])))
+ ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter('core')))
+ ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang')))
+ ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($user['configvalue']), IQueryBuilder::PARAM_STR))
+ ->executeStatement();
+ }
+ self::assertEquals($expectedOutput, $outputMessages);
+ }
+
+ public function testSecondRun(): void {
+ /** @var IOutput&MockObject $outputMock */
+ $outputMock = $this->createMock(IOutput::class);
+ $outputMock->expects($this->never())
+ ->method('info');
+
+ $this->config->expects($this->once())
+ ->method('getSystemValueString')
+ ->with('version', '0.0.0')
+ ->willReturn('12.0.0.14');
+
+ // run repair step
+ $repair = new UpdateLanguageCodes($this->connection, $this->config);
+ $repair->run($outputMock);
+ }
+}