diff options
Diffstat (limited to 'apps/dav/tests/unit/Upload')
-rw-r--r-- | apps/dav/tests/unit/Upload/AssemblyStreamTest.php | 12 | ||||
-rw-r--r-- | apps/dav/tests/unit/Upload/ChunkingPluginTest.php | 109 | ||||
-rw-r--r-- | apps/dav/tests/unit/Upload/FutureFileTest.php | 14 | ||||
-rw-r--r-- | apps/dav/tests/unit/Upload/UploadAutoMkcolPluginTest.php | 133 |
4 files changed, 193 insertions, 75 deletions
diff --git a/apps/dav/tests/unit/Upload/AssemblyStreamTest.php b/apps/dav/tests/unit/Upload/AssemblyStreamTest.php index e0bc3c589bc..ec5d0a9ab5b 100644 --- a/apps/dav/tests/unit/Upload/AssemblyStreamTest.php +++ b/apps/dav/tests/unit/Upload/AssemblyStreamTest.php @@ -12,9 +12,7 @@ use Sabre\DAV\File; class AssemblyStreamTest extends \Test\TestCase { - /** - * @dataProvider providesNodes() - */ + #[\PHPUnit\Framework\Attributes\DataProvider('providesNodes')] public function testGetContents(string $expected, array $nodeData): void { $nodes = []; foreach ($nodeData as $data) { @@ -26,9 +24,7 @@ class AssemblyStreamTest extends \Test\TestCase { $this->assertEquals($expected, $content); } - /** - * @dataProvider providesNodes() - */ + #[\PHPUnit\Framework\Attributes\DataProvider('providesNodes')] public function testGetContentsFread(string $expected, array $nodeData, int $chunkLength = 3): void { $nodes = []; foreach ($nodeData as $data) { @@ -48,9 +44,7 @@ class AssemblyStreamTest extends \Test\TestCase { $this->assertEquals($expected, $content); } - /** - * @dataProvider providesNodes() - */ + #[\PHPUnit\Framework\Attributes\DataProvider('providesNodes')] public function testSeek(string $expected, array $nodeData): void { $nodes = []; foreach ($nodeData as $data) { diff --git a/apps/dav/tests/unit/Upload/ChunkingPluginTest.php b/apps/dav/tests/unit/Upload/ChunkingPluginTest.php index 87feebf5d09..00ed7657dd3 100644 --- a/apps/dav/tests/unit/Upload/ChunkingPluginTest.php +++ b/apps/dav/tests/unit/Upload/ChunkingPluginTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2017 ownCloud GmbH @@ -10,40 +11,24 @@ namespace OCA\DAV\Tests\unit\Upload; use OCA\DAV\Connector\Sabre\Directory; use OCA\DAV\Upload\ChunkingPlugin; use OCA\DAV\Upload\FutureFile; +use PHPUnit\Framework\MockObject\MockObject; use Sabre\DAV\Exception\NotFound; use Sabre\HTTP\RequestInterface; use Sabre\HTTP\ResponseInterface; use Test\TestCase; class ChunkingPluginTest extends TestCase { - /** - * @var \Sabre\DAV\Server | \PHPUnit\Framework\MockObject\MockObject - */ - private $server; - - /** - * @var \Sabre\DAV\Tree | \PHPUnit\Framework\MockObject\MockObject - */ - private $tree; - - /** - * @var ChunkingPlugin - */ - private $plugin; - /** @var RequestInterface | \PHPUnit\Framework\MockObject\MockObject */ - private $request; - /** @var ResponseInterface | \PHPUnit\Framework\MockObject\MockObject */ - private $response; + private \Sabre\DAV\Server&MockObject $server; + private \Sabre\DAV\Tree&MockObject $tree; + private ChunkingPlugin $plugin; + private RequestInterface&MockObject $request; + private ResponseInterface&MockObject $response; protected function setUp(): void { parent::setUp(); - $this->server = $this->getMockBuilder('\Sabre\DAV\Server') - ->disableOriginalConstructor() - ->getMock(); - $this->tree = $this->getMockBuilder('\Sabre\DAV\Tree') - ->disableOriginalConstructor() - ->getMock(); + $this->server = $this->createMock('\Sabre\DAV\Server'); + $this->tree = $this->createMock('\Sabre\DAV\Tree'); $this->server->tree = $this->tree; $this->plugin = new ChunkingPlugin(); @@ -78,14 +63,10 @@ class ChunkingPluginTest extends TestCase { $this->tree->expects($this->exactly(2)) ->method('getNodeForPath') - ->withConsecutive( - ['source'], - ['target'], - ) - ->willReturnOnConsecutiveCalls( - $sourceNode, - $targetNode, - ); + ->willReturnMap([ + ['source', $sourceNode], + ['target', $targetNode], + ]); $this->response->expects($this->never()) ->method('setStatus'); @@ -98,16 +79,20 @@ class ChunkingPluginTest extends TestCase { ->method('getSize') ->willReturn(4); + $calls = [ + ['source', $sourceNode], + ['target', new NotFound()], + ]; $this->tree->expects($this->exactly(2)) ->method('getNodeForPath') - ->withConsecutive( - ['source'], - ['target'], - ) - ->willReturnOnConsecutiveCalls( - $sourceNode, - $this->throwException(new NotFound()), - ); + ->willReturnCallback(function (string $path) use (&$calls) { + $expected = array_shift($calls); + $this->assertSame($expected[0], $path); + if ($expected[1] instanceof \Throwable) { + throw $expected[1]; + } + return $expected[1]; + }); $this->tree->expects($this->any()) ->method('nodeExists') ->with('target') @@ -132,17 +117,21 @@ class ChunkingPluginTest extends TestCase { ->method('getSize') ->willReturn(4); - + $calls = [ + ['source', $sourceNode], + ['target', new NotFound()], + ]; $this->tree->expects($this->exactly(2)) ->method('getNodeForPath') - ->withConsecutive( - ['source'], - ['target'], - ) - ->willReturnOnConsecutiveCalls( - $sourceNode, - $this->throwException(new NotFound()), - ); + ->willReturnCallback(function (string $path) use (&$calls) { + $expected = array_shift($calls); + $this->assertSame($expected[0], $path); + if ($expected[1] instanceof \Throwable) { + throw $expected[1]; + } + return $expected[1]; + }); + $this->tree->expects($this->any()) ->method('nodeExists') ->with('target') @@ -175,17 +164,21 @@ class ChunkingPluginTest extends TestCase { ->method('getSize') ->willReturn(3); - + $calls = [ + ['source', $sourceNode], + ['target', new NotFound()], + ]; $this->tree->expects($this->exactly(2)) ->method('getNodeForPath') - ->withConsecutive( - ['source'], - ['target'], - ) - ->willReturnOnConsecutiveCalls( - $sourceNode, - $this->throwException(new NotFound()), - ); + ->willReturnCallback(function (string $path) use (&$calls) { + $expected = array_shift($calls); + $this->assertSame($expected[0], $path); + if ($expected[1] instanceof \Throwable) { + throw $expected[1]; + } + return $expected[1]; + }); + $this->request->expects($this->once()) ->method('getHeader') ->with('OC-Total-Length') diff --git a/apps/dav/tests/unit/Upload/FutureFileTest.php b/apps/dav/tests/unit/Upload/FutureFileTest.php index 750670992eb..1409df937c0 100644 --- a/apps/dav/tests/unit/Upload/FutureFileTest.php +++ b/apps/dav/tests/unit/Upload/FutureFileTest.php @@ -1,5 +1,6 @@ <?php +declare(strict_types=1); /** * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors * SPDX-FileCopyrightText: 2016 ownCloud, Inc. @@ -45,7 +46,7 @@ class FutureFileTest extends \Test\TestCase { public function testDelete(): void { $d = $this->getMockBuilder(Directory::class) ->disableOriginalConstructor() - ->setMethods(['delete']) + ->onlyMethods(['delete']) ->getMock(); $d->expects($this->once()) @@ -55,7 +56,7 @@ class FutureFileTest extends \Test\TestCase { $f->delete(); } - + public function testPut(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); @@ -63,7 +64,7 @@ class FutureFileTest extends \Test\TestCase { $f->put(''); } - + public function testSetName(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); @@ -71,13 +72,10 @@ class FutureFileTest extends \Test\TestCase { $f->setName(''); } - /** - * @return FutureFile - */ - private function mockFutureFile() { + private function mockFutureFile(): FutureFile { $d = $this->getMockBuilder(Directory::class) ->disableOriginalConstructor() - ->setMethods(['getETag', 'getLastModified', 'getChildren']) + ->onlyMethods(['getETag', 'getLastModified', 'getChildren']) ->getMock(); $d->expects($this->any()) diff --git a/apps/dav/tests/unit/Upload/UploadAutoMkcolPluginTest.php b/apps/dav/tests/unit/Upload/UploadAutoMkcolPluginTest.php new file mode 100644 index 00000000000..baae839c8da --- /dev/null +++ b/apps/dav/tests/unit/Upload/UploadAutoMkcolPluginTest.php @@ -0,0 +1,133 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\DAV\Tests\unit\Upload; + +use Generator; +use OCA\DAV\Upload\UploadAutoMkcolPlugin; +use PHPUnit\Framework\MockObject\MockObject; +use Sabre\DAV\ICollection; +use Sabre\DAV\INode; +use Sabre\DAV\Server; +use Sabre\DAV\Tree; +use Sabre\HTTP\RequestInterface; +use Sabre\HTTP\ResponseInterface; +use Test\TestCase; + +class UploadAutoMkcolPluginTest extends TestCase { + + private Tree&MockObject $tree; + private RequestInterface&MockObject $request; + private ResponseInterface&MockObject $response; + + public static function dataMissingHeaderShouldReturnTrue(): Generator { + yield 'missing X-NC-WebDAV-Auto-Mkcol header' => [null]; + yield 'empty X-NC-WebDAV-Auto-Mkcol header' => ['']; + yield 'invalid X-NC-WebDAV-Auto-Mkcol header' => ['enable']; + } + + public function testBeforeMethodWithRootNodeNotAnICollectionShouldReturnTrue(): void { + $this->request->method('getHeader')->willReturn('1'); + $this->request->expects(self::once()) + ->method('getPath') + ->willReturn('/non-relevant/path.txt'); + $this->tree->expects(self::once()) + ->method('nodeExists') + ->with('/non-relevant') + ->willReturn(false); + + $mockNode = $this->getMockBuilder(INode::class); + $this->tree->expects(self::once()) + ->method('getNodeForPath') + ->willReturn($mockNode); + + $return = $this->plugin->beforeMethod($this->request, $this->response); + $this->assertTrue($return); + } + + #[\PHPUnit\Framework\Attributes\DataProvider('dataMissingHeaderShouldReturnTrue')] + public function testBeforeMethodWithMissingHeaderShouldReturnTrue(?string $header): void { + $this->request->expects(self::once()) + ->method('getHeader') + ->with('X-NC-WebDAV-Auto-Mkcol') + ->willReturn($header); + + $this->request->expects(self::never()) + ->method('getPath'); + + $return = $this->plugin->beforeMethod($this->request, $this->response); + self::assertTrue($return); + } + + public function testBeforeMethodWithExistingPathShouldReturnTrue(): void { + $this->request->method('getHeader')->willReturn('1'); + $this->request->expects(self::once()) + ->method('getPath') + ->willReturn('/files/user/deep/image.jpg'); + $this->tree->expects(self::once()) + ->method('nodeExists') + ->with('/files/user/deep') + ->willReturn(true); + + $this->tree->expects(self::never()) + ->method('getNodeForPath'); + + $return = $this->plugin->beforeMethod($this->request, $this->response); + self::assertTrue($return); + } + + public function testBeforeMethodShouldSucceed(): void { + $this->request->method('getHeader')->willReturn('1'); + $this->request->expects(self::once()) + ->method('getPath') + ->willReturn('/files/user/my/deep/path/image.jpg'); + $this->tree->expects(self::once()) + ->method('nodeExists') + ->with('/files/user/my/deep/path') + ->willReturn(false); + + $mockNode = $this->createMock(ICollection::class); + $this->tree->expects(self::once()) + ->method('getNodeForPath') + ->with('/files') + ->willReturn($mockNode); + $mockNode->expects(self::exactly(4)) + ->method('childExists') + ->willReturnMap([ + ['user', true], + ['my', true], + ['deep', false], + ['path', false], + ]); + $mockNode->expects(self::exactly(2)) + ->method('createDirectory'); + $mockNode->expects(self::exactly(4)) + ->method('getChild') + ->willReturn($mockNode); + + $return = $this->plugin->beforeMethod($this->request, $this->response); + self::assertTrue($return); + } + + protected function setUp(): void { + parent::setUp(); + + $server = $this->createMock(Server::class); + $this->tree = $this->createMock(Tree::class); + + $server->tree = $this->tree; + $this->plugin = new UploadAutoMkcolPlugin(); + + $this->request = $this->createMock(RequestInterface::class); + $this->response = $this->createMock(ResponseInterface::class); + $server->httpRequest = $this->request; + $server->httpResponse = $this->response; + + $this->plugin->initialize($server); + } +} |