|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\DAV\Tests\unit\Upload; |
| 11 | + |
| 12 | +use OCA\DAV\Connector\Sabre\Directory; |
| 13 | +use OCA\DAV\Upload\ChunkingV2Plugin; |
| 14 | +use OCA\DAV\Upload\FutureFile; |
| 15 | +use OCA\DAV\Upload\UploadFile; |
| 16 | +use OCP\ICache; |
| 17 | +use OCP\ICacheFactory; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use Sabre\DAV\Exception\MethodNotAllowed; |
| 20 | +use Sabre\DAV\Exception\NotFound; |
| 21 | +use Sabre\DAV\Server; |
| 22 | +use Sabre\DAV\Tree; |
| 23 | +use Sabre\HTTP\RequestInterface; |
| 24 | +use Sabre\HTTP\ResponseInterface; |
| 25 | +use Test\TestCase; |
| 26 | + |
| 27 | +class ChunkingV2PluginTest extends TestCase { |
| 28 | + /** @var Server | MockObject */ |
| 29 | + private $server; |
| 30 | + /** @var Tree | MockObject */ |
| 31 | + private $tree; |
| 32 | + /** @var ChunkingV2Plugin */ |
| 33 | + private $plugin; |
| 34 | + /** @var RequestInterface | MockObject */ |
| 35 | + private $request; |
| 36 | + /** @var ResponseInterface | MockObject */ |
| 37 | + private $response; |
| 38 | + |
| 39 | + protected function setUp(): void { |
| 40 | + parent::setUp(); |
| 41 | + |
| 42 | + $this->server = $this->getMockBuilder('\Sabre\DAV\Server') |
| 43 | + ->disableOriginalConstructor() |
| 44 | + ->getMock(); |
| 45 | + $this->tree = $this->getMockBuilder('\Sabre\DAV\Tree') |
| 46 | + ->disableOriginalConstructor() |
| 47 | + ->getMock(); |
| 48 | + $this->server->tree = $this->tree; |
| 49 | + |
| 50 | + $cacheFactory = $this->createMock(ICacheFactory::class); |
| 51 | + $cacheFactory->method('createDistributed')->willReturn($this->createMock(ICache::class)); |
| 52 | + |
| 53 | + $this->plugin = new ChunkingV2Plugin($cacheFactory); |
| 54 | + |
| 55 | + $this->request = $this->createMock(RequestInterface::class); |
| 56 | + $this->response = $this->createMock(ResponseInterface::class); |
| 57 | + $this->server->httpRequest = $this->request; |
| 58 | + $this->server->httpResponse = $this->response; |
| 59 | + |
| 60 | + $this->plugin->initialize($this->server); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * The handler only blocks reading intermediate uploads. A path it cannot |
| 65 | + * resolve (e.g. an app-provided collection such as `versions`/`trashbin` |
| 66 | + * that is registered later in the request lifecycle) must not abort the |
| 67 | + * request: beforeGet has to swallow the NotFound and let normal handling |
| 68 | + * (and any real 404) take over. This is the regression guard for the |
| 69 | + * "version/trashbin downloads return 404" bug. |
| 70 | + */ |
| 71 | + public function testBeforeGetIgnoresUnresolvablePath(): void { |
| 72 | + $this->request->method('getPath')->willReturn('versions/admin/versions/74/1782831952'); |
| 73 | + $this->tree->expects($this->once()) |
| 74 | + ->method('getNodeForPath') |
| 75 | + ->with('versions/admin/versions/74/1782831952') |
| 76 | + ->willThrowException(new NotFound("File not found: versions in 'root'")); |
| 77 | + |
| 78 | + $this->assertTrue($this->plugin->beforeGet($this->request)); |
| 79 | + } |
| 80 | + |
| 81 | + public function testBeforeGetBlocksFutureFile(): void { |
| 82 | + $this->expectException(MethodNotAllowed::class); |
| 83 | + |
| 84 | + $this->request->method('getPath')->willReturn('uploads/admin/web-file-upload-id/1'); |
| 85 | + $this->tree->method('getNodeForPath')->willReturn($this->createMock(FutureFile::class)); |
| 86 | + |
| 87 | + $this->plugin->beforeGet($this->request); |
| 88 | + } |
| 89 | + |
| 90 | + public function testBeforeGetBlocksUploadFile(): void { |
| 91 | + $this->expectException(MethodNotAllowed::class); |
| 92 | + |
| 93 | + $this->request->method('getPath')->willReturn('uploads/admin/web-file-upload-id/.target'); |
| 94 | + $this->tree->method('getNodeForPath')->willReturn($this->createMock(UploadFile::class)); |
| 95 | + |
| 96 | + $this->plugin->beforeGet($this->request); |
| 97 | + } |
| 98 | + |
| 99 | + public function testBeforeGetAllowsRegularNode(): void { |
| 100 | + $this->request->method('getPath')->willReturn('files/admin/foo.txt'); |
| 101 | + $this->tree->method('getNodeForPath')->willReturn($this->createMock(Directory::class)); |
| 102 | + |
| 103 | + $this->assertTrue($this->plugin->beforeGet($this->request)); |
| 104 | + } |
| 105 | +} |
0 commit comments