0
0
mirror of https://github.com/nextcloud/server.git synced 2024-12-29 16:38:28 +00:00
nextcloud_server/tests/lib/Files/Storage/CommonTest.php
Christoph Wurst 49dd79eabb
refactor: Add void return type to PHPUnit test methods
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2024-09-15 22:32:31 +02:00

137 lines
4.0 KiB
PHP

<?php
/**
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Files\Storage;
use OC\Files\Storage\Wrapper\Jail;
use OC\Files\Storage\Wrapper\Wrapper;
use OCP\Files\IFilenameValidator;
use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidPathException;
use OCP\ITempManager;
use PHPUnit\Framework\MockObject\MockObject;
/**
* Class CommonTest
*
* @group DB
*
* @package Test\Files\Storage
*/
class CommonTest extends Storage {
private string $tmpDir;
private IFilenameValidator&MockObject $filenameValidator;
protected function setUp(): void {
parent::setUp();
$this->filenameValidator = $this->createMock(IFilenameValidator::class);
$this->overwriteService(IFilenameValidator::class, $this->filenameValidator);
$this->tmpDir = \OCP\Server::get(ITempManager::class)->getTemporaryFolder();
$this->instance = new \OC\Files\Storage\CommonTest(['datadir' => $this->tmpDir]);
}
protected function tearDown(): void {
\OC_Helper::rmdirr($this->tmpDir);
$this->restoreService(IFilenameValidator::class);
parent::tearDown();
}
public function testVerifyPath(): void {
$this->filenameValidator
->expects($this->once())
->method('validateFilename')
->with('invalid:char.txt')
->willThrowException(new InvalidCharacterInPathException());
$this->expectException(InvalidPathException::class);
$this->instance->verifyPath('/', 'invalid:char.txt');
}
public function testVerifyPathSucceed(): void {
$this->filenameValidator
->expects($this->once())
->method('validateFilename')
->with('valid-char.txt');
$this->instance->verifyPath('/', 'valid-char.txt');
}
public function testMoveFromStorageWrapped(): void {
/** @var \OC\Files\Storage\CommonTest|MockObject $instance */
$instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
->onlyMethods(['copyFromStorage', 'rmdir', 'unlink'])
->setConstructorArgs([['datadir' => $this->tmpDir]])
->getMock();
$instance->method('copyFromStorage')
->willThrowException(new \Exception('copy'));
$source = new Wrapper([
'storage' => $instance,
]);
$instance->file_put_contents('foo.txt', 'bar');
$instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
$this->assertTrue($instance->file_exists('bar.txt'));
}
public function testMoveFromStorageJailed(): void {
/** @var \OC\Files\Storage\CommonTest|MockObject $instance */
$instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
->onlyMethods(['copyFromStorage', 'rmdir', 'unlink'])
->setConstructorArgs([['datadir' => $this->tmpDir]])
->getMock();
$instance->method('copyFromStorage')
->willThrowException(new \Exception('copy'));
$source = new Jail([
'storage' => $instance,
'root' => 'foo'
]);
$source = new Wrapper([
'storage' => $source
]);
$instance->mkdir('foo');
$instance->file_put_contents('foo/foo.txt', 'bar');
$instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
$this->assertTrue($instance->file_exists('bar.txt'));
}
public function testMoveFromStorageNestedJail(): void {
/** @var \OC\Files\Storage\CommonTest|MockObject $instance */
$instance = $this->getMockBuilder(\OC\Files\Storage\CommonTest::class)
->onlyMethods(['copyFromStorage', 'rmdir', 'unlink'])
->setConstructorArgs([['datadir' => $this->tmpDir]])
->getMock();
$instance->method('copyFromStorage')
->willThrowException(new \Exception('copy'));
$source = new Jail([
'storage' => $instance,
'root' => 'foo'
]);
$source = new Wrapper([
'storage' => $source
]);
$source = new Jail([
'storage' => $source,
'root' => 'bar'
]);
$source = new Wrapper([
'storage' => $source
]);
$instance->mkdir('foo');
$instance->mkdir('foo/bar');
$instance->file_put_contents('foo/bar/foo.txt', 'bar');
$instance->moveFromStorage($source, 'foo.txt', 'bar.txt');
$this->assertTrue($instance->file_exists('bar.txt'));
}
}