aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dav/tests/unit/Upload/UploadAutoMkcolPluginTest.php
blob: baae839c8da4c0ee27ec5c0291503871f6992856 (plain)
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
131
132
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);
	}
}