Skip to content

Commit 13cc198

Browse files
salmart-devAndyScherzinger
authored andcommitted
test: add unit test for upload RootCollection
Assisted-by: Claude:claude-sonnet-5 Signed-off-by: Salvatore Martire <4652631+salmart-dev@users.noreply.github.com>
1 parent 966765b commit 13cc198

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 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\Upload\CleanupService;
13+
use OCA\DAV\Upload\RootCollection;
14+
use OCA\DAV\Upload\UploadHome;
15+
use OCP\Files\IRootFolder;
16+
use OCP\IUser;
17+
use OCP\IUserSession;
18+
use OCP\Share\IManager as IShareManager;
19+
use OCP\Share\IShare;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use Sabre\DAV\Exception\Forbidden;
22+
use Sabre\DAVACL\PrincipalBackend\BackendInterface;
23+
use Test\TestCase;
24+
25+
class RootCollectionTest extends TestCase {
26+
private BackendInterface&MockObject $principalBackend;
27+
private CleanupService&MockObject $cleanupService;
28+
private IRootFolder&MockObject $rootFolder;
29+
private IUserSession&MockObject $userSession;
30+
private IShareManager&MockObject $shareManager;
31+
private RootCollection $collection;
32+
33+
protected function setUp(): void {
34+
parent::setUp();
35+
36+
$this->principalBackend = $this->createMock(BackendInterface::class);
37+
$this->cleanupService = $this->createMock(CleanupService::class);
38+
$this->rootFolder = $this->createMock(IRootFolder::class);
39+
$this->userSession = $this->createMock(IUserSession::class);
40+
$this->shareManager = $this->createMock(IShareManager::class);
41+
42+
$this->collection = new RootCollection(
43+
$this->principalBackend,
44+
'principals/users',
45+
$this->cleanupService,
46+
$this->rootFolder,
47+
$this->userSession,
48+
$this->shareManager,
49+
);
50+
}
51+
52+
private function mockUser(string $uid): IUser&MockObject {
53+
$user = $this->createMock(IUser::class);
54+
$user->method('getUID')->willReturn($uid);
55+
return $user;
56+
}
57+
58+
public function testGetChildForPrincipalReturnsUploadHomeForOwnPrincipal(): void {
59+
$this->userSession->method('getUser')->willReturn($this->mockUser('alice'));
60+
61+
$node = $this->collection->getChildForPrincipal(['uri' => 'principals/users/alice']);
62+
63+
$this->assertInstanceOf(UploadHome::class, $node);
64+
}
65+
66+
public function testGetChildForPrincipalReturnsUploadHomeForShareToken(): void {
67+
$this->userSession->method('getUser')->willReturn($this->mockUser('alice'));
68+
69+
$share = $this->createMock(IShare::class);
70+
$share->method('getShareOwner')->willReturn('bob');
71+
$this->shareManager->method('getShareByToken')
72+
->with('sometoken')
73+
->willReturn($share);
74+
75+
$node = $this->collection->getChildForPrincipal(['uri' => 'principals/shares/sometoken']);
76+
77+
$this->assertInstanceOf(UploadHome::class, $node);
78+
}
79+
80+
public function testGetChildForPrincipalThrowsWhenPrincipalDoesNotMatchUser(): void {
81+
$this->userSession->method('getUser')->willReturn($this->mockUser('alice'));
82+
83+
$this->expectException(Forbidden::class);
84+
85+
$this->collection->getChildForPrincipal(['uri' => 'principals/users/bob']);
86+
}
87+
88+
public function testGetChildForPrincipalThrowsWhenNotLoggedIn(): void {
89+
$this->userSession->method('getUser')->willReturn(null);
90+
91+
$this->expectException(Forbidden::class);
92+
93+
$this->collection->getChildForPrincipal(['uri' => 'principals/users/alice']);
94+
}
95+
}

0 commit comments

Comments
 (0)