-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathEnvelopeServiceTest.php
More file actions
189 lines (140 loc) · 6.16 KB
/
EnvelopeServiceTest.php
File metadata and controls
189 lines (140 loc) · 6.16 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Tests\Unit\Service;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Db\File as FileEntity;
use OCA\Libresign\Db\FileMapper;
use OCA\Libresign\Enum\NodeType;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Service\EnvelopeService;
use OCA\Libresign\Service\FolderService;
use OCA\Libresign\Tests\Unit\TestCase;
use OCP\Files\Folder;
use OCP\IAppConfig;
use OCP\IL10N;
use PHPUnit\Framework\MockObject\MockObject;
final class EnvelopeServiceTest extends TestCase {
private FileMapper&MockObject $fileMapper;
private IL10N $l10n;
private IAppConfig $appConfig;
private FolderService&MockObject $folderService;
private EnvelopeService $service;
public function setUp(): void {
parent::setUp();
$this->fileMapper = $this->createMock(FileMapper::class);
$this->l10n = \OCP\Server::get(\OCP\L10N\IFactory::class)->get(Application::APP_ID);
$this->appConfig = $this->getMockAppConfigWithReset();
$this->folderService = $this->createMock(FolderService::class);
$this->service = new EnvelopeService(
$this->fileMapper,
$this->l10n,
$this->appConfig,
$this->folderService,
);
}
public function testEnvelopeIsCreatedAsDraft(): void {
$this->fileMapper->method('insert')->willReturnArgument(0);
$mockFolder = $this->createMock(Folder::class);
$mockEnvelopeFolder = $this->createMock(Folder::class);
$mockEnvelopeFolder->method('getId')->willReturn(999);
$mockFolder->method('newFolder')->willReturn($mockEnvelopeFolder);
$this->folderService->method('getFolder')->willReturn($mockFolder);
$envelope = $this->service->createEnvelope('Contract Package', 'testuser');
$this->assertSame(FileEntity::STATUS_DRAFT, $envelope->getStatus());
}
public function testEnvelopeIsCreatedWithEnvelopeType(): void {
$this->fileMapper->method('insert')->willReturnArgument(0);
$mockFolder = $this->createMock(Folder::class);
$mockEnvelopeFolder = $this->createMock(Folder::class);
$mockEnvelopeFolder->method('getId')->willReturn(999);
$mockFolder->method('newFolder')->willReturn($mockEnvelopeFolder);
$this->folderService->method('getFolder')->willReturn($mockFolder);
$envelope = $this->service->createEnvelope('Contract Package', 'testuser');
$this->assertTrue($envelope->isEnvelope());
}
public function testCannotAddFileToRegularFile(): void {
$this->expectException(LibresignException::class);
$regularFile = new FileEntity();
$regularFile->setNodeTypeEnum(NodeType::FILE);
$this->fileMapper->method('getById')->willReturn($regularFile);
$this->service->addFileToEnvelope(1, new FileEntity());
}
public function testCannotAddFileToEnvelopeAfterSigningStarts(): void {
$this->expectException(LibresignException::class);
$envelope = new FileEntity();
$envelope->setNodeTypeEnum(NodeType::ENVELOPE);
$envelope->setStatus(FileEntity::STATUS_ABLE_TO_SIGN);
$this->fileMapper->method('getById')->willReturn($envelope);
$this->service->addFileToEnvelope(1, new FileEntity());
}
public function testCannotExceedMaximumFilesPerEnvelope(): void {
$this->expectException(LibresignException::class);
$envelope = new FileEntity();
$envelope->setNodeTypeEnum(NodeType::ENVELOPE);
$envelope->setStatus(FileEntity::STATUS_DRAFT);
$this->fileMapper->method('getById')->willReturn($envelope);
$this->fileMapper->method('countChildrenFiles')->willReturn(50);
$this->service->addFileToEnvelope(1, new FileEntity());
}
public function testFileIsLinkedToEnvelopeWhenAdded(): void {
$envelopeId = 100;
$envelope = new FileEntity();
$envelope->setId($envelopeId);
$envelope->setNodeTypeEnum(NodeType::ENVELOPE);
$envelope->setStatus(FileEntity::STATUS_DRAFT);
$file = new FileEntity();
$this->fileMapper->method('getById')->willReturn($envelope);
$this->fileMapper->method('countChildrenFiles')->willReturn(0);
$this->fileMapper->method('update')->willReturnArgument(0);
$result = $this->service->addFileToEnvelope($envelopeId, $file);
$this->assertSame($envelopeId, $result->getParentFileId());
}
public function testFileBecomesRegularFileTypeWhenAddedToEnvelope(): void {
$envelope = new FileEntity();
$envelope->setId(1);
$envelope->setNodeTypeEnum(NodeType::ENVELOPE);
$envelope->setStatus(FileEntity::STATUS_DRAFT);
$file = new FileEntity();
$this->fileMapper->method('getById')->willReturn($envelope);
$this->fileMapper->method('countChildrenFiles')->willReturn(0);
$this->fileMapper->method('update')->willReturnArgument(0);
$result = $this->service->addFileToEnvelope(1, $file);
$this->assertSame(NodeType::FILE, $result->getNodeTypeEnum());
}
public function testReturnsNullWhenFileHasNoEnvelope(): void {
$this->fileMapper->method('getParentEnvelope')
->willThrowException(new \OCP\AppFramework\Db\DoesNotExistException(''));
$result = $this->service->getEnvelopeByFileId(999);
$this->assertNull($result);
}
public function testReturnsEnvelopeWhenFileHasParent(): void {
$expectedEnvelope = new FileEntity();
$expectedEnvelope->setId(5);
$expectedEnvelope->setNodeTypeEnum(NodeType::ENVELOPE);
$this->fileMapper->method('getParentEnvelope')->willReturn($expectedEnvelope);
$result = $this->service->getEnvelopeByFileId(10);
$this->assertNotNull($result);
$this->assertSame(5, $result->getId());
}
public function testEnvelopeUuidMatchesFolderName(): void {
$this->fileMapper->method('insert')->willReturnArgument(0);
$mockFolder = $this->createMock(Folder::class);
$mockEnvelopeFolder = $this->createMock(Folder::class);
$mockEnvelopeFolder->method('getId')->willReturn(999);
$capturedFolderName = '';
$mockFolder->method('newFolder')->willReturnCallback(
function ($folderName) use ($mockEnvelopeFolder, &$capturedFolderName) {
$capturedFolderName = $folderName;
return $mockEnvelopeFolder;
}
);
$this->folderService->method('getFolder')->willReturn($mockFolder);
$envelope = $this->service->createEnvelope('Contract', 'user1');
$this->assertStringStartsWith('Contract_', $capturedFolderName);
$this->assertStringContainsString($envelope->getUuid(), $capturedFolderName);
}
}