Skip to content
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
17 changes: 6 additions & 11 deletions apps/files_versions/tests/BackgroundJob/ExpireVersionsTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
Expand All @@ -16,17 +18,10 @@
use Test\TestCase;

class ExpireVersionsTest extends TestCase {
/** @var IConfig|MockObject */
private $config;

/** @var IUserManager|MockObject */
private $userManager;

/** @var Expiration|MockObject */
private $expiration;

/** @var IJobList|MockObject */
private $jobList;
private IConfig&MockObject $config;
private IUserManager&MockObject $userManager;
private Expiration&MockObject $expiration;
private IJobList&MockObject $jobList;

protected function setUp(): void {
parent::setUp();
Expand Down
53 changes: 21 additions & 32 deletions apps/files_versions/tests/Command/CleanupTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand All @@ -8,11 +10,13 @@

use OC\User\Manager;
use OCA\Files_Versions\Command\CleanUp;
use OCA\Files_Versions\Db\VersionsMapper;
use OCP\Files\Cache\ICache;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Storage\IStorage;
use OCP\UserInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

/**
Expand All @@ -23,28 +27,17 @@
* @package OCA\Files_Versions\Tests\Command
*/
class CleanupTest extends TestCase {

/** @var CleanUp */
protected $cleanup;

/** @var \PHPUnit\Framework\MockObject\MockObject | Manager */
protected $userManager;

/** @var \PHPUnit\Framework\MockObject\MockObject | IRootFolder */
protected $rootFolder;

/** @var \PHPUnit\Framework\MockObject\MockObject | VersionsMapper */
protected $versionMapper;
protected Manager&MockObject $userManager;
protected IRootFolder&MockObject $rootFolder;
protected VersionsMapper&MockObject $versionMapper;
protected CleanUp $cleanup;

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

$this->rootFolder = $this->getMockBuilder('OCP\Files\IRootFolder')
->disableOriginalConstructor()->getMock();
$this->userManager = $this->getMockBuilder('OC\User\Manager')
->disableOriginalConstructor()->getMock();
$this->versionMapper = $this->getMockBuilder('OCA\Files_Versions\Db\VersionsMapper')
->disableOriginalConstructor()->getMock();
$this->rootFolder = $this->createMock(IRootFolder::class);
$this->userManager = $this->createMock(Manager::class);
$this->versionMapper = $this->createMock(VersionsMapper::class);

$this->cleanup = new CleanUp($this->rootFolder, $this->userManager, $this->versionMapper);
}
Expand All @@ -53,7 +46,7 @@ protected function setUp(): void {
* @dataProvider dataTestDeleteVersions
* @param boolean $nodeExists
*/
public function testDeleteVersions($nodeExists): void {
public function testDeleteVersions(bool $nodeExists): void {
$this->rootFolder->expects($this->once())
->method('nodeExists')
->with('/testUser/files_versions')
Expand Down Expand Up @@ -92,7 +85,7 @@ public function testDeleteVersions($nodeExists): void {
$this->invokePrivate($this->cleanup, 'deleteVersions', ['testUser']);
}

public function dataTestDeleteVersions() {
public static function dataTestDeleteVersions(): array {
return [
[true],
[false]
Expand All @@ -106,8 +99,8 @@ public function dataTestDeleteVersions() {
public function testExecuteDeleteListOfUsers(): void {
$userIds = ['user1', 'user2', 'user3'];

$instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
->setMethods(['deleteVersions'])
$instance = $this->getMockBuilder(CleanUp::class)
->onlyMethods(['deleteVersions'])
->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper])
->getMock();
$instance->expects($this->exactly(count($userIds)))
Expand All @@ -119,14 +112,12 @@ public function testExecuteDeleteListOfUsers(): void {
$this->userManager->expects($this->exactly(count($userIds)))
->method('userExists')->willReturn(true);

$inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
->disableOriginalConstructor()->getMock();
$inputInterface = $this->createMock(\Symfony\Component\Console\Input\InputInterface::class);
$inputInterface->expects($this->once())->method('getArgument')
->with('user_id')
->willReturn($userIds);

$outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
->disableOriginalConstructor()->getMock();
$outputInterface = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class);

$this->invokePrivate($instance, 'execute', [$inputInterface, $outputInterface]);
}
Expand All @@ -138,8 +129,8 @@ public function testExecuteAllUsers(): void {
$userIds = [];
$backendUsers = ['user1', 'user2'];

$instance = $this->getMockBuilder('OCA\Files_Versions\Command\CleanUp')
->setMethods(['deleteVersions'])
$instance = $this->getMockBuilder(CleanUp::class)
->onlyMethods(['deleteVersions'])
->setConstructorArgs([$this->rootFolder, $this->userManager, $this->versionMapper])
->getMock();

Expand All @@ -155,14 +146,12 @@ public function testExecuteAllUsers(): void {
$this->assertTrue(in_array($user, $backendUsers));
});

$inputInterface = $this->getMockBuilder('\Symfony\Component\Console\Input\InputInterface')
->disableOriginalConstructor()->getMock();
$inputInterface = $this->createMock(\Symfony\Component\Console\Input\InputInterface::class);
$inputInterface->expects($this->once())->method('getArgument')
->with('user_id')
->willReturn($userIds);

$outputInterface = $this->getMockBuilder('\Symfony\Component\Console\Output\OutputInterface')
->disableOriginalConstructor()->getMock();
$outputInterface = $this->createMock(\Symfony\Component\Console\Output\OutputInterface::class);

$this->userManager->expects($this->once())
->method('getBackends')
Expand Down
2 changes: 2 additions & 0 deletions apps/files_versions/tests/Command/ExpireTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand Down
30 changes: 8 additions & 22 deletions apps/files_versions/tests/Controller/PreviewControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
Expand All @@ -12,7 +14,6 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
Expand All @@ -25,29 +26,14 @@
use Test\TestCase;

class PreviewControllerTest extends TestCase {

/** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
private $rootFolder;

/** @var string */
private $userId;

/** @var IMimeTypeDetector|\PHPUnit\Framework\MockObject\MockObject */
private $mimeTypeDetector;

/** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
private $previewManager;

/** @var PreviewController|\PHPUnit\Framework\MockObject\MockObject */
private $controller;

/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
private $userSession;

/** @var IVersionManager|\PHPUnit\Framework\MockObject\MockObject */
private $versionManager;
private IRootFolder&MockObject $rootFolder;
private string $userId;
private IPreview&MockObject $previewManager;
private IUserSession&MockObject $userSession;
private IVersionManager&MockObject $versionManager;

private IMimeIconProvider&MockObject $mimeIconProvider;
private PreviewController $controller;

protected function setUp(): void {
parent::setUp();
Expand Down
24 changes: 6 additions & 18 deletions apps/files_versions/tests/ExpirationTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand All @@ -15,7 +17,7 @@
class ExpirationTest extends \Test\TestCase {
public const SECONDS_PER_DAY = 86400; //60*60*24

public function expirationData() {
public static function expirationData(): array {
$today = 100 * self::SECONDS_PER_DAY;
$back10Days = (100 - 10) * self::SECONDS_PER_DAY;
$back20Days = (100 - 20) * self::SECONDS_PER_DAY;
Expand Down Expand Up @@ -81,14 +83,8 @@ public function expirationData() {

/**
* @dataProvider expirationData
*
* @param string $retentionObligation
* @param int $timeNow
* @param int $timestamp
* @param bool $quotaExceeded
* @param string $expectedResult
*/
public function testExpiration($retentionObligation, $timeNow, $timestamp, $quotaExceeded, $expectedResult): void {
public function testExpiration(string $retentionObligation, int $timeNow, int $timestamp, bool $quotaExceeded, bool $expectedResult): void {
$mockedConfig = $this->getMockedConfig($retentionObligation);
$mockedTimeFactory = $this->getMockedTimeFactory($timeNow);
$mockedLogger = $this->createMock(LoggerInterface::class);
Expand All @@ -100,11 +96,7 @@ public function testExpiration($retentionObligation, $timeNow, $timestamp, $quot
}


/**
* @param int $time
* @return ITimeFactory|MockObject
*/
private function getMockedTimeFactory($time) {
private function getMockedTimeFactory(int $time): ITimeFactory&MockObject {
$mockedTimeFactory = $this->createMock(ITimeFactory::class);
$mockedTimeFactory->expects($this->any())
->method('getTime')
Expand All @@ -113,11 +105,7 @@ private function getMockedTimeFactory($time) {
return $mockedTimeFactory;
}

/**
* @param string $returnValue
* @return IConfig|MockObject
*/
private function getMockedConfig($returnValue) {
private function getMockedConfig(string $returnValue): IConfig&MockObject {
$mockedConfig = $this->createMock(IConfig::class);
$mockedConfig->expects($this->any())
->method('getSystemValue')
Expand Down
4 changes: 2 additions & 2 deletions apps/files_versions/tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class StorageTest extends TestCase {

private $versionsRoot;
private $userFolder;
private $expireTimestamp = 10;
private int $expireTimestamp = 10;

protected function setUp(): void {
parent::setUp();
Expand All @@ -46,7 +46,7 @@ protected function setUp(): void {
}


protected function createPastFile(string $path, int $mtime) {
protected function createPastFile(string $path, int $mtime): void {
try {
$file = $this->userFolder->get($path);
} catch (NotFoundException $e) {
Expand Down
29 changes: 14 additions & 15 deletions apps/files_versions/tests/VersioningTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
Expand Down Expand Up @@ -163,7 +165,7 @@ public function testGetExpireList($versions, $sizeOfAllDeletedFiles): void {
}
}

public function versionsProvider() {
public static function versionsProvider(): array {
return [
// first set of versions uniformly distributed versions
[
Expand Down Expand Up @@ -683,7 +685,7 @@ public function testRestoreNoPermission(): void {

$firstVersion = current($versions);

$this->assertFalse(Storage::rollback('folder/test.txt', $firstVersion['version'], $this->user2), 'Revert did not happen');
$this->assertFalse(Storage::rollback('folder/test.txt', (int)$firstVersion['version'], $this->user2), 'Revert did not happen');

$this->loginAsUser(self::TEST_VERSIONS_USER);

Expand Down Expand Up @@ -743,8 +745,8 @@ private function connectMockHooks($hookName, &$params) {
return;
}

$eventHandler = $this->getMockBuilder(\stdclass::class)
->setMethods(['callback'])
$eventHandler = $this->getMockBuilder(DummyHookListener::class)
->onlyMethods(['callback'])
->getMock();

$eventHandler->expects($this->any())
Expand All @@ -763,7 +765,7 @@ function ($p) use (&$params): void {
);
}

private function doTestRestore() {
private function doTestRestore(): void {
$filePath = self::TEST_VERSIONS_USER . '/files/sub/test.txt';
$this->rootView->file_put_contents($filePath, 'test file');

Expand Down Expand Up @@ -941,11 +943,7 @@ public function testStoreVersionAsAnonymous(): void {
);
}

/**
* @param View $view
* @param string $path
*/
private function createAndCheckVersions(View $view, $path) {
private function createAndCheckVersions(View $view, string $path): array {
$view->file_put_contents($path, 'test file');
$view->file_put_contents($path, 'version 1');
$view->file_put_contents($path, 'version 2');
Expand All @@ -967,11 +965,7 @@ private function createAndCheckVersions(View $view, $path) {
return $versions;
}

/**
* @param string $user
* @param bool $create
*/
public static function loginHelper($user, $create = false) {
public static function loginHelper(string $user, bool $create = false) {
if ($create) {
$backend = new \Test\Util\User\Dummy();
$backend->createUser($user, $user);
Expand All @@ -987,6 +981,11 @@ public static function loginHelper($user, $create = false) {
}
}

class DummyHookListener {
public function callback() {
}
}

// extend the original class to make it possible to test protected methods
class VersionStorageToTest extends Storage {

Expand Down
Loading
Loading