diff options
Diffstat (limited to 'apps/dav/tests/unit/Files')
3 files changed, 206 insertions, 132 deletions
diff --git a/apps/dav/tests/unit/Files/FileSearchBackendTest.php b/apps/dav/tests/unit/Files/FileSearchBackendTest.php index aaa3d8c147e..c6d6f85347b 100644 --- a/apps/dav/tests/unit/Files/FileSearchBackendTest.php +++ b/apps/dav/tests/unit/Files/FileSearchBackendTest.php @@ -1,10 +1,12 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\DAV\Tests\Files; +namespace OCA\DAV\Tests\unit\Files; use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchQuery; @@ -13,6 +15,7 @@ use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Connector\Sabre\File; use OCA\DAV\Connector\Sabre\FilesPlugin; use OCA\DAV\Connector\Sabre\ObjectTree; +use OCA\DAV\Connector\Sabre\Server; use OCA\DAV\Files\FileSearchBackend; use OCP\Files\FileInfo; use OCP\Files\Folder; @@ -23,42 +26,27 @@ use OCP\Files\Search\ISearchQuery; use OCP\FilesMetadata\IFilesMetadataManager; use OCP\IUser; use OCP\Share\IManager; +use PHPUnit\Framework\MockObject\MockObject; use SearchDAV\Backend\SearchPropertyDefinition; use SearchDAV\Query\Limit; +use SearchDAV\Query\Literal; use SearchDAV\Query\Operator; use SearchDAV\Query\Query; +use SearchDAV\Query\Scope; use Test\TestCase; class FileSearchBackendTest extends TestCase { - /** @var ObjectTree|\PHPUnit\Framework\MockObject\MockObject */ - private $tree; - - /** @var IUser */ - private $user; - - /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */ - private $rootFolder; - - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ - private $shareManager; - - /** @var View|\PHPUnit\Framework\MockObject\MockObject */ - private $view; - - /** @var Folder|\PHPUnit\Framework\MockObject\MockObject */ - private $searchFolder; - - /** @var FileSearchBackend */ - private $search; - - /** @var Directory|\PHPUnit\Framework\MockObject\MockObject */ - private $davFolder; + private ObjectTree&MockObject $tree; + private Server&MockObject $server; + private IUser&MockObject $user; + private IRootFolder&MockObject $rootFolder; + private IManager&MockObject $shareManager; + private View&MockObject $view; + private Folder&MockObject $searchFolder; + private Directory&MockObject $davFolder; + private FileSearchBackend $search; protected function setUp(): void { - if (PHP_VERSION_ID >= 80400) { - $this->markTestSkipped('SearchDAV is not yet PHP 8.4 compatible'); - } - parent::setUp(); $this->user = $this->createMock(IUser::class); @@ -66,11 +54,14 @@ class FileSearchBackendTest extends TestCase { ->method('getUID') ->willReturn('test'); - $this->tree = $this->getMockBuilder(ObjectTree::class) - ->disableOriginalConstructor() - ->getMock(); - + $this->tree = $this->createMock(ObjectTree::class); + $this->server = $this->createMock(Server::class); $this->view = $this->createMock(View::class); + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->shareManager = $this->createMock(IManager::class); + $this->searchFolder = $this->createMock(Folder::class); + $fileInfo = $this->createMock(FileInfo::class); + $this->davFolder = $this->createMock(Directory::class); $this->view->expects($this->any()) ->method('getRoot') @@ -80,16 +71,6 @@ class FileSearchBackendTest extends TestCase { ->method('getRelativePath') ->willReturnArgument(0); - $this->rootFolder = $this->createMock(IRootFolder::class); - - $this->shareManager = $this->createMock(IManager::class); - - $this->searchFolder = $this->createMock(Folder::class); - - $fileInfo = $this->createMock(FileInfo::class); - - $this->davFolder = $this->createMock(Directory::class); - $this->davFolder->expects($this->any()) ->method('getFileInfo') ->willReturn($fileInfo); @@ -100,7 +81,7 @@ class FileSearchBackendTest extends TestCase { $filesMetadataManager = $this->createMock(IFilesMetadataManager::class); - $this->search = new FileSearchBackend($this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager); + $this->search = new FileSearchBackend($this->server, $this->tree, $this->user, $this->rootFolder, $this->shareManager, $this->view, $filesMetadataManager); } public function testSearchFilename(): void { @@ -263,8 +244,8 @@ class FileSearchBackendTest extends TestCase { $this->search->search($query); } - private function getBasicQuery($type, $property, $value = null) { - $scope = new \SearchDAV\Query\Scope('/', 'infinite'); + private function getBasicQuery(string $type, string $property, int|string|null $value = null) { + $scope = new Scope('/', 'infinite'); $scope->path = '/'; $from = [$scope]; $orderBy = []; @@ -272,12 +253,12 @@ class FileSearchBackendTest extends TestCase { if (is_null($value)) { $where = new Operator( $type, - [new \SearchDAV\Query\Literal($property)] + [new Literal($property)] ); } else { $where = new Operator( $type, - [new SearchPropertyDefinition($property, true, true, true), new \SearchDAV\Query\Literal($value)] + [new SearchPropertyDefinition($property, true, true, true), new Literal($value)] ); } $limit = new Limit(); @@ -350,11 +331,11 @@ class FileSearchBackendTest extends TestCase { [ new Operator( Operator::OPERATION_EQUAL, - [new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), new \SearchDAV\Query\Literal('image/png')] + [new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), new Literal('image/png')] ), new Operator( Operator::OPERATION_EQUAL, - [new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, true, true, true), new \SearchDAV\Query\Literal($this->user->getUID())] + [new SearchPropertyDefinition(FilesPlugin::OWNER_ID_PROPERTYNAME, true, true, true), new Literal($this->user->getUID())] ), ] ); @@ -383,7 +364,7 @@ class FileSearchBackendTest extends TestCase { $innerOperator = new Operator( Operator::OPERATION_EQUAL, - [new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), new \SearchDAV\Query\Literal('image/png')] + [new SearchPropertyDefinition('{DAV:}getcontenttype', true, true, true), new Literal('image/png')] ); // 5 child operators $level1Operator = new Operator( @@ -424,4 +405,17 @@ class FileSearchBackendTest extends TestCase { $this->expectException(\InvalidArgumentException::class); $this->search->search($query); } + + public function testPreloadPropertyFor(): void { + $node1 = $this->createMock(File::class); + $node2 = $this->createMock(Directory::class); + $nodes = [$node1, $node2]; + $requestProperties = ['{DAV:}getcontenttype', '{DAV:}getlastmodified']; + + $this->server->expects($this->once()) + ->method('emit') + ->with('preloadProperties', [$nodes, $requestProperties]); + + $this->search->preloadPropertyFor($nodes, $requestProperties); + } } diff --git a/apps/dav/tests/unit/Files/MultipartRequestParserTest.php b/apps/dav/tests/unit/Files/MultipartRequestParserTest.php index 40880bdca9c..dc0e884f07c 100644 --- a/apps/dav/tests/unit/Files/MultipartRequestParserTest.php +++ b/apps/dav/tests/unit/Files/MultipartRequestParserTest.php @@ -1,10 +1,12 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-only */ -namespace OCA\DAV\Tests\unit\DAV; +namespace OCA\DAV\Tests\unit\Files; use OCA\DAV\BulkUpload\MultipartRequestParser; use PHPUnit\Framework\MockObject\MockObject; @@ -17,10 +19,11 @@ class MultipartRequestParserTest extends TestCase { protected LoggerInterface&MockObject $logger; protected function setUp(): void { + parent::setUp(); $this->logger = $this->createMock(LoggerInterface::class); } - private function getValidBodyObject() { + private static function getValidBodyObject(): array { return [ [ 'headers' => [ @@ -99,7 +102,7 @@ class MultipartRequestParserTest extends TestCase { * - valid file path */ public function testValidRequest(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); unset($bodyObject['0']['headers']['X-File-MD5']); $multipartParser = $this->getMultipartParser($bodyObject); @@ -122,7 +125,7 @@ class MultipartRequestParserTest extends TestCase { * - valid file path */ public function testValidRequestWithMd5(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); unset($bodyObject['0']['headers']['OC-Checksum']); $multipartParser = $this->getMultipartParser($bodyObject); @@ -140,7 +143,7 @@ class MultipartRequestParserTest extends TestCase { * Test with invalid hash. */ public function testInvalidHash(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); $bodyObject['0']['headers']['OC-Checksum'] = 'md5:f2377b4d911f7ec46325fe603c3af03'; unset($bodyObject['0']['headers']['X-File-MD5']); $multipartParser = $this->getMultipartParser( @@ -155,7 +158,7 @@ class MultipartRequestParserTest extends TestCase { * Test with invalid md5 hash. */ public function testInvalidMd5Hash(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); unset($bodyObject['0']['headers']['OC-Checksum']); $bodyObject['0']['headers']['X-File-MD5'] = 'f2377b4d911f7ec46325fe603c3af03'; $multipartParser = $this->getMultipartParser( @@ -170,7 +173,7 @@ class MultipartRequestParserTest extends TestCase { * Test with a null hash headers. */ public function testNullHash(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); unset($bodyObject['0']['headers']['OC-Checksum']); unset($bodyObject['0']['headers']['X-File-MD5']); $multipartParser = $this->getMultipartParser( @@ -185,7 +188,7 @@ class MultipartRequestParserTest extends TestCase { * Test with a null Content-Length. */ public function testNullContentLength(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); unset($bodyObject['0']['headers']['Content-Length']); $multipartParser = $this->getMultipartParser( $bodyObject @@ -199,7 +202,7 @@ class MultipartRequestParserTest extends TestCase { * Test with a lower Content-Length. */ public function testLowerContentLength(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); $bodyObject['0']['headers']['Content-Length'] = 6; $multipartParser = $this->getMultipartParser( $bodyObject @@ -213,7 +216,7 @@ class MultipartRequestParserTest extends TestCase { * Test with a higher Content-Length. */ public function testHigherContentLength(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); $bodyObject['0']['headers']['Content-Length'] = 8; $multipartParser = $this->getMultipartParser( $bodyObject @@ -227,7 +230,7 @@ class MultipartRequestParserTest extends TestCase { * Test with wrong boundary in body. */ public function testWrongBoundary(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); $multipartParser = $this->getMultipartParser( $bodyObject, ['Content-Type' => 'multipart/related; boundary=boundary_poiuytreza'] @@ -241,7 +244,7 @@ class MultipartRequestParserTest extends TestCase { * Test with no boundary in request headers. */ public function testNoBoundaryInHeader(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); $this->expectExceptionMessage('Error while parsing boundary in Content-Type header.'); $this->getMultipartParser( $bodyObject, @@ -253,7 +256,7 @@ class MultipartRequestParserTest extends TestCase { * Test with no boundary in the request's headers. */ public function testNoBoundaryInBody(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); $multipartParser = $this->getMultipartParser( $bodyObject, ['Content-Type' => 'multipart/related; boundary=boundary_azertyuiop'], @@ -268,7 +271,7 @@ class MultipartRequestParserTest extends TestCase { * Test with a boundary with quotes in the request's headers. */ public function testBoundaryWithQuotes(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); $multipartParser = $this->getMultipartParser( $bodyObject, ['Content-Type' => 'multipart/related; boundary="boundary_azertyuiop"'], @@ -284,7 +287,7 @@ class MultipartRequestParserTest extends TestCase { * Test with a wrong Content-Type in the request's headers. */ public function testWrongContentType(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); $this->expectExceptionMessage('Content-Type must be multipart/related'); $this->getMultipartParser( $bodyObject, @@ -296,7 +299,7 @@ class MultipartRequestParserTest extends TestCase { * Test with a wrong key after the content type in the request's headers. */ public function testWrongKeyInContentType(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); $this->expectExceptionMessage('Boundary is invalid'); $this->getMultipartParser( $bodyObject, @@ -308,7 +311,7 @@ class MultipartRequestParserTest extends TestCase { * Test with a null Content-Type in the request's headers. */ public function testNullContentType(): void { - $bodyObject = $this->getValidBodyObject(); + $bodyObject = self::getValidBodyObject(); $this->expectExceptionMessage('Content-Type can not be null'); $this->getMultipartParser( $bodyObject, diff --git a/apps/dav/tests/unit/Files/Sharing/FilesDropPluginTest.php b/apps/dav/tests/unit/Files/Sharing/FilesDropPluginTest.php index ce469a1c749..1a7ab7179e1 100644 --- a/apps/dav/tests/unit/Files/Sharing/FilesDropPluginTest.php +++ b/apps/dav/tests/unit/Files/Sharing/FilesDropPluginTest.php @@ -1,15 +1,19 @@ <?php + +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\DAV\Tests\Files\Sharing; +namespace OCA\DAV\Tests\unit\Files\Sharing; -use OC\Files\View; use OCA\DAV\Files\Sharing\FilesDropPlugin; +use OCP\Files\Folder; +use OCP\Files\NotFoundException; use OCP\Share\IAttributes; use OCP\Share\IShare; -use Sabre\DAV\Exception\MethodNotAllowed; +use PHPUnit\Framework\MockObject\MockObject; +use Sabre\DAV\Exception\BadRequest; use Sabre\DAV\Server; use Sabre\HTTP\RequestInterface; use Sabre\HTTP\ResponseInterface; @@ -17,38 +21,31 @@ use Test\TestCase; class FilesDropPluginTest extends TestCase { - /** @var View|\PHPUnit\Framework\MockObject\MockObject */ - private $view; - - /** @var IShare|\PHPUnit\Framework\MockObject\MockObject */ - private $share; - - /** @var Server|\PHPUnit\Framework\MockObject\MockObject */ - private $server; + private FilesDropPlugin $plugin; - /** @var FilesDropPlugin */ - private $plugin; - - /** @var RequestInterface|\PHPUnit\Framework\MockObject\MockObject */ - private $request; - - /** @var ResponseInterface|\PHPUnit\Framework\MockObject\MockObject */ - private $response; + private Folder&MockObject $node; + private IShare&MockObject $share; + private Server&MockObject $server; + private RequestInterface&MockObject $request; + private ResponseInterface&MockObject $response; protected function setUp(): void { parent::setUp(); - $this->view = $this->createMock(View::class); + $this->node = $this->createMock(Folder::class); + $this->node->method('getPath') + ->willReturn('/files/token'); + $this->share = $this->createMock(IShare::class); + $this->share->expects(self::any()) + ->method('getNode') + ->willReturn($this->node); $this->server = $this->createMock(Server::class); $this->plugin = new FilesDropPlugin(); $this->request = $this->createMock(RequestInterface::class); $this->response = $this->createMock(ResponseInterface::class); - $this->response->expects($this->never()) - ->method($this->anything()); - $attributes = $this->createMock(IAttributes::class); $this->share->expects($this->any()) ->method('getAttributes') @@ -59,22 +56,7 @@ class FilesDropPluginTest extends TestCase { ->willReturn('token'); } - public function testInitialize(): void { - $this->server->expects($this->once()) - ->method('on') - ->with( - $this->equalTo('beforeMethod:*'), - $this->equalTo([$this->plugin, 'beforeMethod']), - $this->equalTo(999) - ); - - $this->plugin->initialize($this->server); - } - public function testNotEnabled(): void { - $this->view->expects($this->never()) - ->method($this->anything()); - $this->request->expects($this->never()) ->method($this->anything()); @@ -83,7 +65,6 @@ class FilesDropPluginTest extends TestCase { public function testValid(): void { $this->plugin->enable(); - $this->plugin->setView($this->view); $this->plugin->setShare($this->share); $this->request->method('getMethod') @@ -95,9 +76,10 @@ class FilesDropPluginTest extends TestCase { $this->request->method('getBaseUrl') ->willReturn('https://example.com'); - $this->view->method('file_exists') - ->with('/file.txt') - ->willReturn(false); + $this->node->expects(self::once()) + ->method('getNonExistingName') + ->with('file.txt') + ->willReturn('file.txt'); $this->request->expects($this->once()) ->method('setUrl') @@ -108,7 +90,6 @@ class FilesDropPluginTest extends TestCase { public function testFileAlreadyExistsValid(): void { $this->plugin->enable(); - $this->plugin->setView($this->view); $this->plugin->setShare($this->share); $this->request->method('getMethod') @@ -120,14 +101,9 @@ class FilesDropPluginTest extends TestCase { $this->request->method('getBaseUrl') ->willReturn('https://example.com'); - $this->view->method('file_exists') - ->willReturnCallback(function ($path) { - if ($path === 'file.txt' || $path === '/file.txt') { - return true; - } else { - return false; - } - }); + $this->node->method('getNonExistingName') + ->with('file.txt') + ->willReturn('file (2).txt'); $this->request->expects($this->once()) ->method('setUrl') @@ -136,46 +112,147 @@ class FilesDropPluginTest extends TestCase { $this->plugin->beforeMethod($this->request, $this->response); } - public function testNoMKCOL(): void { + public function testNoMKCOLWithoutNickname(): void { + $this->plugin->enable(); + $this->plugin->setShare($this->share); + + $this->request->method('getMethod') + ->willReturn('MKCOL'); + + $this->expectException(BadRequest::class); + + $this->plugin->beforeMethod($this->request, $this->response); + } + + public function testMKCOLWithNickname(): void { $this->plugin->enable(); - $this->plugin->setView($this->view); $this->plugin->setShare($this->share); $this->request->method('getMethod') ->willReturn('MKCOL'); - $this->expectException(MethodNotAllowed::class); + $this->request->method('hasHeader') + ->with('X-NC-Nickname') + ->willReturn(true); + $this->request->method('getHeader') + ->with('X-NC-Nickname') + ->willReturn('nickname'); + + $this->expectNotToPerformAssertions(); $this->plugin->beforeMethod($this->request, $this->response); } - public function testNoSubdirPut(): void { + public function testSubdirPut(): void { $this->plugin->enable(); - $this->plugin->setView($this->view); $this->plugin->setShare($this->share); $this->request->method('getMethod') ->willReturn('PUT'); + $this->request->method('hasHeader') + ->with('X-NC-Nickname') + ->willReturn(true); + $this->request->method('getHeader') + ->with('X-NC-Nickname') + ->willReturn('nickname'); + $this->request->method('getPath') ->willReturn('/files/token/folder/file.txt'); $this->request->method('getBaseUrl') ->willReturn('https://example.com'); - $this->view->method('file_exists') - ->willReturnCallback(function ($path) { - if ($path === 'file.txt' || $path === '/file.txt') { - return true; - } else { - return false; - } - }); + $nodeName = $this->createMock(Folder::class); + $nodeFolder = $this->createMock(Folder::class); + $nodeFolder->expects(self::once()) + ->method('getPath') + ->willReturn('/files/token/nickname/folder'); + $nodeFolder->method('getNonExistingName') + ->with('file.txt') + ->willReturn('file.txt'); + $nodeName->expects(self::once()) + ->method('get') + ->with('folder') + ->willThrowException(new NotFoundException()); + $nodeName->expects(self::once()) + ->method('newFolder') + ->with('folder') + ->willReturn($nodeFolder); + + $this->node->expects(self::once()) + ->method('get') + ->willThrowException(new NotFoundException()); + $this->node->expects(self::once()) + ->method('newFolder') + ->with('nickname') + ->willReturn($nodeName); $this->request->expects($this->once()) ->method('setUrl') - ->with($this->equalTo('https://example.com/files/token/file (2).txt')); + ->with($this->equalTo('https://example.com/files/token/nickname/folder/file.txt')); $this->plugin->beforeMethod($this->request, $this->response); } + + public function testRecursiveFolderCreation(): void { + $this->plugin->enable(); + $this->plugin->setShare($this->share); + + $this->request->method('getMethod') + ->willReturn('PUT'); + $this->request->method('hasHeader') + ->with('X-NC-Nickname') + ->willReturn(true); + $this->request->method('getHeader') + ->with('X-NC-Nickname') + ->willReturn('nickname'); + + $this->request->method('getPath') + ->willReturn('/files/token/folder/subfolder/file.txt'); + $this->request->method('getBaseUrl') + ->willReturn('https://example.com'); + + $this->request->expects($this->once()) + ->method('setUrl') + ->with($this->equalTo('https://example.com/files/token/nickname/folder/subfolder/file.txt')); + + $subfolder = $this->createMock(Folder::class); + $subfolder->expects(self::once()) + ->method('getNonExistingName') + ->with('file.txt') + ->willReturn('file.txt'); + $subfolder->expects(self::once()) + ->method('getPath') + ->willReturn('/files/token/nickname/folder/subfolder'); + + $folder = $this->createMock(Folder::class); + $folder->expects(self::once()) + ->method('get') + ->with('subfolder') + ->willReturn($subfolder); + + $nickname = $this->createMock(Folder::class); + $nickname->expects(self::once()) + ->method('get') + ->with('folder') + ->willReturn($folder); + + $this->node->method('get') + ->with('nickname') + ->willReturn($nickname); + $this->plugin->beforeMethod($this->request, $this->response); + } + + public function testOnMkcol(): void { + $this->plugin->enable(); + $this->plugin->setShare($this->share); + + $this->response->expects($this->once()) + ->method('setStatus') + ->with(201); + + $response = $this->plugin->onMkcol($this->request, $this->response); + $this->assertFalse($response); + } } |