Skip to content

Commit 4918d6e

Browse files
committed
test: update unit tests for file service refactoring
- ValidateHelperTest.php: update to use getByNodeId() and getId() - FileServiceTest.php: update to work with refactored file type system These tests now validate the refactored file identification semantics. Signed-off-by: Vitor Mattos <[email protected]>
1 parent 7d2432f commit 4918d6e

File tree

2 files changed

+13
-57
lines changed

2 files changed

+13
-57
lines changed

tests/php/Unit/Helper/ValidateHelperTest.php

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,6 @@ public function testValidateNotRequestedSignWithSuccessWhenNotFound():void {
149149
$this->assertNull($actual);
150150
}
151151

152-
public function testValidateLibreSignNodeIdWhenFileIdNotExists():void {
153-
$this->signRequestMapper->method('getByNodeId')->willReturnCallback(function ():void {
154-
throw new \Exception('not found');
155-
});
156-
$this->expectExceptionMessage('Invalid fileID');
157-
$this->getValidateHelper()->validateLibreSignNodeId(1);
158-
}
159-
160152
/**
161153
* @dataProvider dataValidateMimeTypeAcceptedByNodeId
162154
*/
@@ -189,23 +181,25 @@ public static function dataValidateMimeTypeAcceptedByNodeId():array {
189181
];
190182
}
191183

192-
public function testValidateLibreSignNodeIdWhenSuccess():void {
184+
public function testValidateMimeTypeAcceptedByNodeIdWhenSuccess():void {
193185
$libresignFile = $this->createMock(\OCA\Libresign\Db\File::class);
186+
$libresignFile->method('getUserId')->willReturn('user1');
194187
$this->fileMapper
195-
->method('getByFileId')
188+
->method('getByNodeId')
196189
->willReturn($libresignFile);
197190
$file = $this->createMock(\OCP\Files\File::class);
198191
$file
199192
->method('getMimeType')
200193
->willReturn('application/pdf');
201194
$folder = $this->createMock(\OCP\Files\Folder::class);
202195
$folder
203-
->method('getById')
204-
->willReturn([$file]);
196+
->method('getFirstNodeById')
197+
->willReturn($file);
205198
$this->root
206199
->method('getUserFolder')
200+
->with('user1')
207201
->willReturn($folder);
208-
$actual = $this->getValidateHelper()->validateLibreSignNodeId(1);
202+
$actual = $this->getValidateHelper()->validateMimeTypeAcceptedByNodeId(1);
209203
$this->assertNull($actual);
210204
}
211205

@@ -305,7 +299,7 @@ public function testIRequestedSignThisFileWithInvalidRequester():void {
305299
->method('__call')
306300
->willReturn('user1');
307301
$this->fileMapper
308-
->method('getByFileId')
302+
->method('getById')
309303
->willReturn($libresignFile);
310304
$user = $this->createMock(\OCP\IUser::class);
311305
$user
@@ -330,45 +324,6 @@ public static function dataProviderHaveValidMail():array {
330324
];
331325
}
332326

333-
public function testSignerWasAssociatedWithNotLibreSignFileLoaded():void {
334-
$this->expectExceptionMessage('File not loaded');
335-
$this->fileMapper
336-
->method('getByFileId')
337-
->willReturnCallback(function ():void {
338-
throw new \Exception('not found');
339-
});
340-
$this->getValidateHelper()->signerWasAssociated([
341-
'email' => '[email protected]'
342-
]);
343-
}
344-
345-
public function testSignerWasAssociatedWithUnassociatedSigner():void {
346-
$this->expectExceptionMessage('No signature was requested to %s');
347-
$libresignFile = $this->createMock(\OCA\Libresign\Db\File::class);
348-
$libresignFile
349-
->method('__call')
350-
->willReturn('uuid');
351-
$this->fileMapper
352-
->method('getByFileId')
353-
->willReturn($libresignFile);
354-
$this->signRequestMapper
355-
->method('getByFileUuid')
356-
->willReturn([]);
357-
$this->getValidateHelper()->signerWasAssociated([
358-
'email' => '[email protected]'
359-
]);
360-
}
361-
362-
public function testNotSignedWithFileNotLoaded():void {
363-
$this->expectExceptionMessage('File not loaded');
364-
$this->fileMapper
365-
->method('getByFileId')
366-
->willReturnCallback(function ():void {
367-
throw new \Exception('not found');
368-
});
369-
$this->getValidateHelper()->notSigned([]);
370-
}
371-
372327
public function testValidateIfNodeIdExistsWhenUserNotFound():void {
373328
$this->expectExceptionMessage('User not found');
374329
$userFolder = $this->createMock(\OCP\Files\Folder::class);
@@ -556,18 +511,19 @@ public function testValidateExistingFile($dataFile, $uuid, $exception):void {
556511
->willReturnCallback(fn ($method)
557512
=> match ($method) {
558513
'getNodeId' => 1,
514+
'getId' => 1,
559515
}
560516
);
561517
$libresignFile->method('getUserId')
562518
->willReturn('fake_user');
563519
$this->fileMapper->method('getByUuid')->willReturn($libresignFile);
564-
$this->fileMapper->method('getByFileId')->willReturn($libresignFile);
520+
$this->fileMapper->method('getById')->willReturn($libresignFile);
565521

566522
$data['uuid'] = $uuid;
567523
} elseif (!empty($dataFile)) {
568524
$libresignFile->method('getUserId')
569525
->willReturn('fake_user');
570-
$this->fileMapper->method('getByFileId')->willReturn($libresignFile);
526+
$this->fileMapper->method('getById')->willReturn($libresignFile);
571527

572528
$file = $this->createMock(\OCP\Files\File::class);
573529
$file

tests/php/Unit/Service/FileServiceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testValidateFileContentSkipsNonPdfFiles(): void {
6363

6464
public function testSetFileByTypeThrowsOnInvalid(): void {
6565
$fileMapper = $this->createMock(\OCA\Libresign\Db\FileMapper::class);
66-
$fileMapper->method('getByFileId')->willThrowException(new \Exception('not found'));
66+
$fileMapper->method('getById')->willThrowException(new \Exception('not found'));
6767

6868
$service = $this->createFileService([
6969
\OCA\Libresign\Db\FileMapper::class => $fileMapper,
@@ -78,7 +78,7 @@ public function testSetFileByTypeSetsFile(): void {
7878
$file->setStatus(1);
7979

8080
$fileMapper = $this->createMock(\OCA\Libresign\Db\FileMapper::class);
81-
$fileMapper->method('getByFileId')->willReturn($file);
81+
$fileMapper->method('getById')->willReturn($file);
8282

8383
$service = $this->createFileService([
8484
\OCA\Libresign\Db\FileMapper::class => $fileMapper,

0 commit comments

Comments
 (0)