Skip to content

Commit b00e230

Browse files
committed
test: add tests for EnvelopeFileRelocator
Add tests for file relocation logic validating files are copied into envelope folders when outside, returned as-is when already inside, and proper error handling for invalid nodes. Update namespace references in related test files. Signed-off-by: Vitor Mattos <[email protected]>
1 parent d8643e0 commit b00e230

3 files changed

Lines changed: 121 additions & 2 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Libresign\Tests\Unit\Service;
10+
11+
use OCA\Libresign\Exception\LibresignException;
12+
use OCA\Libresign\Service\Envelope\EnvelopeFileRelocator;
13+
use OCA\Libresign\Service\FolderService;
14+
use OCP\Files\Folder;
15+
use OCP\Files\Node;
16+
use OCP\IUser;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
19+
class EnvelopeFileRelocatorTest extends \OCA\Libresign\Tests\Unit\TestCase {
20+
private FolderService&MockObject $folderService;
21+
private EnvelopeFileRelocator $relocator;
22+
23+
public function setUp(): void {
24+
parent::setUp();
25+
$this->folderService = $this->createMock(FolderService::class);
26+
$this->relocator = new EnvelopeFileRelocator($this->folderService);
27+
}
28+
29+
public function testReturnsOriginalWhenAlreadyInside(): void {
30+
$sourceFile = $this->createMock(\OCP\Files\File::class);
31+
$sourceFile->method('getPath')->willReturn('/user/files/Envelope/doc.pdf');
32+
33+
$envelopeFolder = $this->createMock(Folder::class);
34+
$envelopeFolder->method('getPath')->willReturn('/user/files/Envelope');
35+
36+
$rootFolder = $this->createMock(Folder::class);
37+
$rootFolder->method('getFirstNodeById')->with(10)->willReturn($envelopeFolder);
38+
39+
$this->folderService->expects($this->once())->method('setUserId')->with('u1');
40+
$this->folderService->method('getUserRootFolder')->willReturn($rootFolder);
41+
42+
$user = $this->createMock(IUser::class);
43+
$user->method('getUID')->willReturn('u1');
44+
45+
$result = $this->relocator->ensureFileInEnvelopeFolder($sourceFile, 10, $user);
46+
self::assertSame($sourceFile, $result);
47+
}
48+
49+
public function testCopiesFileWhenOutside(): void {
50+
$sourceFile = $this->createMock(\OCP\Files\File::class);
51+
$sourceFile->method('getPath')->willReturn('/user/files/Other/doc.pdf');
52+
$sourceFile->method('getName')->willReturn('doc.pdf');
53+
$sourceFile->method('getContent')->willReturn('content');
54+
55+
$copiedFile = $this->createMock(\OCP\Files\File::class);
56+
57+
$envelopeFolder = $this->createMock(Folder::class);
58+
$envelopeFolder->method('getPath')->willReturn('/user/files/Envelope');
59+
$envelopeFolder->expects($this->once())
60+
->method('newFile')
61+
->with('doc.pdf', 'content')
62+
->willReturn($copiedFile);
63+
64+
$rootFolder = $this->createMock(Folder::class);
65+
$rootFolder->method('getFirstNodeById')->with(10)->willReturn($envelopeFolder);
66+
67+
$this->folderService->expects($this->once())->method('setUserId')->with('u1');
68+
$this->folderService->method('getUserRootFolder')->willReturn($rootFolder);
69+
70+
$user = $this->createMock(IUser::class);
71+
$user->method('getUID')->willReturn('u1');
72+
73+
$result = $this->relocator->ensureFileInEnvelopeFolder($sourceFile, 10, $user);
74+
self::assertSame($copiedFile, $result);
75+
}
76+
77+
public function testThrowsWhenEnvelopeFolderNotFound(): void {
78+
$sourceFile = $this->createMock(\OCP\Files\File::class);
79+
$sourceFile->method('getPath')->willReturn('/user/files/doc.pdf');
80+
81+
$rootFolder = $this->createMock(Folder::class);
82+
$rootFolder->method('getFirstNodeById')->with(10)->willReturn($this->createMock(Node::class));
83+
84+
$this->folderService->method('setUserId');
85+
$this->folderService->method('getUserRootFolder')->willReturn($rootFolder);
86+
87+
$user = $this->createMock(IUser::class);
88+
$user->method('getUID')->willReturn('u1');
89+
90+
$this->expectException(LibresignException::class);
91+
$this->expectExceptionMessage('Envelope folder not found');
92+
$this->relocator->ensureFileInEnvelopeFolder($sourceFile, 10, $user);
93+
}
94+
95+
public function testThrowsWhenSourceIsNotFile(): void {
96+
$sourceNode = $this->createMock(Node::class);
97+
$sourceNode->method('getPath')->willReturn('/user/files/Other');
98+
99+
$envelopeFolder = $this->createMock(Folder::class);
100+
$envelopeFolder->method('getPath')->willReturn('/user/files/Envelope');
101+
102+
$rootFolder = $this->createMock(Folder::class);
103+
$rootFolder->method('getFirstNodeById')->with(10)->willReturn($envelopeFolder);
104+
105+
$this->folderService->method('setUserId');
106+
$this->folderService->method('getUserRootFolder')->willReturn($rootFolder);
107+
108+
$user = $this->createMock(IUser::class);
109+
$user->method('getUID')->willReturn('u1');
110+
111+
$this->expectException(LibresignException::class);
112+
$this->expectExceptionMessage('Invalid file type for envelope');
113+
$this->relocator->ensureFileInEnvelopeFolder($sourceNode, 10, $user);
114+
}
115+
}

tests/php/Unit/Service/FileServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private function createFileService(array $overrides = []): FileService {
2929
\OCP\Files\IRootFolder::class,
3030
\Psr\Log\LoggerInterface::class,
3131
\OCP\IL10N::class,
32-
\OCA\Libresign\Service\EnvelopeService::class,
32+
\OCA\Libresign\Service\Envelope\EnvelopeService::class,
3333
\OCA\Libresign\Service\File\SignersLoader::class,
3434
\OCA\Libresign\Helper\FileUploadHelper::class,
3535
\OCA\Libresign\Service\File\EnvelopeAssembler::class,

tests/php/Unit/Service/RequestSignatureServiceTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
use OCA\Libresign\Helper\FileUploadHelper;
1616
use OCA\Libresign\Helper\ValidateHelper;
1717
use OCA\Libresign\Service\DocMdpConfigService;
18-
use OCA\Libresign\Service\EnvelopeService;
18+
use OCA\Libresign\Service\Envelope\EnvelopeFileRelocator;
19+
use OCA\Libresign\Service\Envelope\EnvelopeService;
1920
use OCA\Libresign\Service\FileElementService;
2021
use OCA\Libresign\Service\FileService;
2122
use OCA\Libresign\Service\FileStatusService;
@@ -63,6 +64,7 @@ final class RequestSignatureServiceTest extends \OCA\Libresign\Tests\Unit\TestCa
6364
private FileStatusService&MockObject $fileStatusService;
6465
private DocMdpConfigService&MockObject $docMdpConfigService;
6566
private EnvelopeService&MockObject $envelopeService;
67+
private EnvelopeFileRelocator&MockObject $envelopeFileRelocator;
6668
private FileUploadHelper&MockObject $uploadHelper;
6769
private SignRequestService&MockObject $signRequestService;
6870

@@ -96,6 +98,7 @@ public function setUp(): void {
9698
$this->fileStatusService = $this->createMock(FileStatusService::class);
9799
$this->docMdpConfigService = $this->createMock(DocMdpConfigService::class);
98100
$this->envelopeService = $this->createMock(EnvelopeService::class);
101+
$this->envelopeFileRelocator = $this->createMock(EnvelopeFileRelocator::class);
99102
$this->uploadHelper = $this->createMock(FileUploadHelper::class);
100103
$this->signRequestService = $this->createMock(SignRequestService::class);
101104
}
@@ -124,6 +127,7 @@ private function getService(): RequestSignatureService {
124127
$this->fileStatusService,
125128
$this->docMdpConfigService,
126129
$this->envelopeService,
130+
$this->envelopeFileRelocator,
127131
$this->uploadHelper,
128132
$this->signRequestService,
129133
);

0 commit comments

Comments
 (0)