|
| 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 unit\Connector\Sabre; |
| 10 | + |
| 11 | +use LogicException; |
| 12 | +use OCA\DAV\Connector\Sabre\AddExtraHeadersPlugin; |
| 13 | +use OCA\DAV\Connector\Sabre\Node; |
| 14 | +use OCA\DAV\Connector\Sabre\Server; |
| 15 | +use OCP\IUser; |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use Psr\Log\LoggerInterface; |
| 19 | +use Sabre\DAV\Exception\NotFound; |
| 20 | +use Sabre\DAV\Tree; |
| 21 | +use Sabre\HTTP\RequestInterface; |
| 22 | +use Sabre\HTTP\ResponseInterface; |
| 23 | +use Test\TestCase; |
| 24 | + |
| 25 | +class AddExtraHeadersPluginTest extends TestCase { |
| 26 | + |
| 27 | + private AddExtraHeadersPlugin $plugin; |
| 28 | + private Server&MockObject $server; |
| 29 | + private LoggerInterface&MockObject $logger; |
| 30 | + private RequestInterface&MockObject $request; |
| 31 | + private ResponseInterface&MockObject $response; |
| 32 | + private Tree&MockObject $tree; |
| 33 | + |
| 34 | + public static function afterPutData(): array { |
| 35 | + return [ |
| 36 | + 'owner and permissions present' => [ |
| 37 | + 'user', true, 'PERMISSIONS', true, 2 |
| 38 | + ], |
| 39 | + 'permissions only' => [ |
| 40 | + null, false, 'PERMISSIONS', true, 1 |
| 41 | + ], |
| 42 | + ]; |
| 43 | + } |
| 44 | + |
| 45 | + public function testAfterPutNotFoundException(): void { |
| 46 | + $afterPut = null; |
| 47 | + $this->server->expects($this->once()) |
| 48 | + ->method('on') |
| 49 | + ->willReturnCallback( |
| 50 | + function ($method, $callback) use (&$afterPut) { |
| 51 | + $this->assertSame('afterMethod:PUT', $method); |
| 52 | + $afterPut = $callback; |
| 53 | + }); |
| 54 | + |
| 55 | + $this->plugin->initialize($this->server); |
| 56 | + $node = $this->createMock(Node::class); |
| 57 | + $this->tree->expects($this->once())->method('getNodeForPath') |
| 58 | + ->willThrowException(new NotFound()); |
| 59 | + |
| 60 | + $this->logger->expects($this->once())->method('error'); |
| 61 | + |
| 62 | + $afterPut($this->request, $this->response); |
| 63 | + } |
| 64 | + |
| 65 | + #[DataProvider('afterPutData')] |
| 66 | + public function testAfterPut(?string $ownerId, bool $expectOwnerIdHeader, |
| 67 | + ?string $permissions, bool $expectPermissionsHeader, |
| 68 | + int $expectedInvocations): void { |
| 69 | + $afterPut = null; |
| 70 | + $this->server->expects($this->once()) |
| 71 | + ->method('on') |
| 72 | + ->willReturnCallback( |
| 73 | + function ($method, $callback) use (&$afterPut) { |
| 74 | + $this->assertSame('afterMethod:PUT', $method); |
| 75 | + $afterPut = $callback; |
| 76 | + }); |
| 77 | + |
| 78 | + $this->plugin->initialize($this->server); |
| 79 | + $node = $this->createMock(Node::class); |
| 80 | + $this->tree->expects($this->once())->method('getNodeForPath') |
| 81 | + ->willReturn($node); |
| 82 | + |
| 83 | + $user = $this->createMock(IUser::class); |
| 84 | + $node->expects($this->once())->method('getOwner')->willReturn($user); |
| 85 | + $user->expects($this->once())->method('getUID')->willReturn($ownerId); |
| 86 | + $node->expects($this->once())->method('getDavPermissions')->willReturn($permissions); |
| 87 | + |
| 88 | + $matcher = $this->exactly($expectedInvocations); |
| 89 | + $this->response->expects($matcher)->method('setHeader') |
| 90 | + ->willReturnCallback(function ($name, $value) use ( |
| 91 | + $expectedInvocations, |
| 92 | + $expectPermissionsHeader, |
| 93 | + $expectOwnerIdHeader, |
| 94 | + $matcher, |
| 95 | + $ownerId, $permissions) { |
| 96 | + $invocationNumber = $matcher->numberOfInvocations(); |
| 97 | + if ($invocationNumber === 0) { |
| 98 | + throw new LogicException('No invocations were expected'); |
| 99 | + } |
| 100 | + |
| 101 | + if (($expectOwnerIdHeader && $expectedInvocations === 1) |
| 102 | + || ($expectedInvocations |
| 103 | + === 2 && $invocationNumber === 1)) { |
| 104 | + $this->assertEquals('X-NC-OwnerId', $name); |
| 105 | + $this->assertEquals($ownerId, $value); |
| 106 | + } |
| 107 | + |
| 108 | + if (($expectPermissionsHeader && $expectedInvocations === 1) |
| 109 | + || ($expectedInvocations |
| 110 | + === 2 && $invocationNumber === 2)) { |
| 111 | + $this->assertEquals('X-NC-Permissions', $name); |
| 112 | + $this->assertEquals($permissions, $value); |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + $afterPut($this->request, $this->response); |
| 117 | + } |
| 118 | + |
| 119 | + protected function setUp(): void { |
| 120 | + parent::setUp(); |
| 121 | + |
| 122 | + $this->server = $this->createMock(Server::class); |
| 123 | + $this->tree = $this->createMock(Tree::class); |
| 124 | + $this->server->tree = $this->tree; |
| 125 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 126 | + $this->plugin = new AddExtraHeadersPlugin($this->logger, false); |
| 127 | + $this->request = $this->createMock(RequestInterface::class); |
| 128 | + $this->response = $this->createMock(ResponseInterface::class); |
| 129 | + } |
| 130 | +} |
0 commit comments