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