|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OCA\DAV\Tests\unit\Upload; |
| 10 | + |
| 11 | +use Generator; |
| 12 | +use OCA\DAV\Upload\UploadAutoMkcolPlugin; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use Sabre\DAV\ICollection; |
| 15 | +use Sabre\DAV\INode; |
| 16 | +use Sabre\DAV\Server; |
| 17 | +use Sabre\DAV\Tree; |
| 18 | +use Sabre\HTTP\RequestInterface; |
| 19 | +use Sabre\HTTP\ResponseInterface; |
| 20 | +use Test\TestCase; |
| 21 | + |
| 22 | +class UploadAutoMkcolPluginTest extends TestCase { |
| 23 | + |
| 24 | + private Tree&MockObject $tree; |
| 25 | + private RequestInterface&MockObject $request; |
| 26 | + private ResponseInterface&MockObject $response; |
| 27 | + |
| 28 | + public static function dataMissingHeaderShouldReturnTrue(): Generator { |
| 29 | + yield 'missing X-NC-WebDAV-Auto-Mkcol header' => [null]; |
| 30 | + yield 'empty X-NC-WebDAV-Auto-Mkcol header' => ['']; |
| 31 | + yield 'invalid X-NC-WebDAV-Auto-Mkcol header' => ['enable']; |
| 32 | + } |
| 33 | + |
| 34 | + public function testBeforeMethodWithRootNodeNotAnICollectionShouldReturnTrue(): void { |
| 35 | + $this->request->method('getHeader')->willReturn('1'); |
| 36 | + $this->request->expects(self::once()) |
| 37 | + ->method('getPath') |
| 38 | + ->willReturn('/non-relevant/path.txt'); |
| 39 | + $this->tree->expects(self::once()) |
| 40 | + ->method('nodeExists') |
| 41 | + ->with('/non-relevant') |
| 42 | + ->willReturn(false); |
| 43 | + |
| 44 | + $mockNode = $this->getMockBuilder(INode::class); |
| 45 | + $this->tree->expects(self::once()) |
| 46 | + ->method('getNodeForPath') |
| 47 | + ->willReturn($mockNode); |
| 48 | + |
| 49 | + $return = $this->plugin->beforeMethod($this->request, $this->response); |
| 50 | + $this->assertTrue($return); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @dataProvider dataMissingHeaderShouldReturnTrue |
| 55 | + */ |
| 56 | + public function testBeforeMethodWithMissingHeaderShouldReturnTrue(?string $header): void { |
| 57 | + $this->request->expects(self::once()) |
| 58 | + ->method('getHeader') |
| 59 | + ->with('X-NC-WebDAV-Auto-Mkcol') |
| 60 | + ->willReturn($header); |
| 61 | + |
| 62 | + $this->request->expects(self::never()) |
| 63 | + ->method('getPath'); |
| 64 | + |
| 65 | + $return = $this->plugin->beforeMethod($this->request, $this->response); |
| 66 | + self::assertTrue($return); |
| 67 | + } |
| 68 | + |
| 69 | + public function testBeforeMethodWithExistingPathShouldReturnTrue(): void { |
| 70 | + $this->request->method('getHeader')->willReturn('1'); |
| 71 | + $this->request->expects(self::once()) |
| 72 | + ->method('getPath') |
| 73 | + ->willReturn('/files/user/deep/image.jpg'); |
| 74 | + $this->tree->expects(self::once()) |
| 75 | + ->method('nodeExists') |
| 76 | + ->with('/files/user/deep') |
| 77 | + ->willReturn(true); |
| 78 | + |
| 79 | + $this->tree->expects(self::never()) |
| 80 | + ->method('getNodeForPath'); |
| 81 | + |
| 82 | + $return = $this->plugin->beforeMethod($this->request, $this->response); |
| 83 | + self::assertTrue($return); |
| 84 | + } |
| 85 | + |
| 86 | + public function testBeforeMethodShouldSucceed(): void { |
| 87 | + $this->request->method('getHeader')->willReturn('1'); |
| 88 | + $this->request->expects(self::once()) |
| 89 | + ->method('getPath') |
| 90 | + ->willReturn('/files/user/my/deep/path/image.jpg'); |
| 91 | + $this->tree->expects(self::once()) |
| 92 | + ->method('nodeExists') |
| 93 | + ->with('/files/user/my/deep/path') |
| 94 | + ->willReturn(false); |
| 95 | + |
| 96 | + $mockNode = $this->createMock(ICollection::class); |
| 97 | + $this->tree->expects(self::once()) |
| 98 | + ->method('getNodeForPath') |
| 99 | + ->with('/files') |
| 100 | + ->willReturn($mockNode); |
| 101 | + $mockNode->expects(self::exactly(4)) |
| 102 | + ->method('childExists') |
| 103 | + ->willReturnMap([ |
| 104 | + ['user', true], |
| 105 | + ['my', true], |
| 106 | + ['deep', false], |
| 107 | + ['path', false], |
| 108 | + ]); |
| 109 | + $mockNode->expects(self::exactly(2)) |
| 110 | + ->method('createDirectory'); |
| 111 | + $mockNode->expects(self::exactly(4)) |
| 112 | + ->method('getChild') |
| 113 | + ->willReturn($mockNode); |
| 114 | + |
| 115 | + $return = $this->plugin->beforeMethod($this->request, $this->response); |
| 116 | + self::assertTrue($return); |
| 117 | + } |
| 118 | + |
| 119 | + protected function setUp(): void { |
| 120 | + parent::setUp(); |
| 121 | + |
| 122 | + $server = $this->createMock(Server::class); |
| 123 | + $this->tree = $this->createMock(Tree::class); |
| 124 | + |
| 125 | + $server->tree = $this->tree; |
| 126 | + $this->plugin = new UploadAutoMkcolPlugin(); |
| 127 | + |
| 128 | + $this->request = $this->createMock(RequestInterface::class); |
| 129 | + $this->response = $this->createMock(ResponseInterface::class); |
| 130 | + $server->httpRequest = $this->request; |
| 131 | + $server->httpResponse = $this->response; |
| 132 | + |
| 133 | + $this->plugin->initialize($server); |
| 134 | + } |
| 135 | +} |
0 commit comments