|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Libresign\Tests\Unit\Controller; |
| 11 | + |
| 12 | +use OCA\Libresign\Controller\RequestSignatureController; |
| 13 | +use OCA\Libresign\Db\File as FileEntity; |
| 14 | +use OCA\Libresign\Db\FileMapper; |
| 15 | +use OCA\Libresign\Helper\ValidateHelper; |
| 16 | +use OCA\Libresign\Service\File\FileListService; |
| 17 | +use OCA\Libresign\Service\FileService; |
| 18 | +use OCA\Libresign\Service\RequestSignatureService; |
| 19 | +use OCP\AppFramework\Http; |
| 20 | +use OCP\IL10N; |
| 21 | +use OCP\IRequest; |
| 22 | +use OCP\IUser; |
| 23 | +use OCP\IUserSession; |
| 24 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 25 | +use PHPUnit\Framework\MockObject\MockObject; |
| 26 | +use PHPUnit\Framework\TestCase; |
| 27 | + |
| 28 | +final class RequestSignatureControllerTest extends TestCase { |
| 29 | + private RequestSignatureController $controller; |
| 30 | + private IRequest&MockObject $request; |
| 31 | + private IL10N&MockObject $l10n; |
| 32 | + private IUserSession&MockObject $userSession; |
| 33 | + private FileService&MockObject $fileService; |
| 34 | + private FileListService&MockObject $fileListService; |
| 35 | + private ValidateHelper&MockObject $validateHelper; |
| 36 | + private RequestSignatureService&MockObject $requestSignatureService; |
| 37 | + private FileMapper&MockObject $fileMapper; |
| 38 | + private IUser&MockObject $user; |
| 39 | + |
| 40 | + protected function setUp(): void { |
| 41 | + $this->request = $this->createMock(IRequest::class); |
| 42 | + $this->l10n = $this->createMock(IL10N::class); |
| 43 | + $this->userSession = $this->createMock(IUserSession::class); |
| 44 | + $this->fileService = $this->createMock(FileService::class); |
| 45 | + $this->fileListService = $this->createMock(FileListService::class); |
| 46 | + $this->validateHelper = $this->createMock(ValidateHelper::class); |
| 47 | + $this->requestSignatureService = $this->createMock(RequestSignatureService::class); |
| 48 | + $this->fileMapper = $this->createMock(FileMapper::class); |
| 49 | + $this->user = $this->createMock(IUser::class); |
| 50 | + |
| 51 | + $this->userSession->method('getUser')->willReturn($this->user); |
| 52 | + |
| 53 | + $this->controller = new RequestSignatureController( |
| 54 | + $this->request, |
| 55 | + $this->l10n, |
| 56 | + $this->userSession, |
| 57 | + $this->fileService, |
| 58 | + $this->fileListService, |
| 59 | + $this->validateHelper, |
| 60 | + $this->requestSignatureService, |
| 61 | + $this->fileMapper, |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function testRequestOmitsStatusWhenNull(): void { |
| 66 | + $file = new FileEntity(); |
| 67 | + $file->setId(10); |
| 68 | + $file->setParentFileId(99); |
| 69 | + |
| 70 | + $this->requestSignatureService |
| 71 | + ->expects($this->once()) |
| 72 | + ->method('validateNewRequestToFile') |
| 73 | + ->with($this->callback(static function (array $payload): bool { |
| 74 | + return !array_key_exists('status', $payload); |
| 75 | + })); |
| 76 | + |
| 77 | + $this->requestSignatureService |
| 78 | + ->expects($this->once()) |
| 79 | + ->method('save') |
| 80 | + ->willReturn($file); |
| 81 | + |
| 82 | + $this->fileListService |
| 83 | + ->expects($this->once()) |
| 84 | + ->method('formatFileWithChildren') |
| 85 | + ->with($file, [], $this->user) |
| 86 | + ->willReturn(['ok' => true]); |
| 87 | + |
| 88 | + $response = $this->controller->request( |
| 89 | + signers: [[ |
| 90 | + 'identifyMethods' => [[ |
| 91 | + 'method' => 'email', |
| 92 | + 'value' => 'user@test.coop', |
| 93 | + 'mandatory' => 0, |
| 94 | + ]], |
| 95 | + ]], |
| 96 | + name: 'contract.pdf', |
| 97 | + settings: [], |
| 98 | + file: ['nodeId' => 12], |
| 99 | + files: [], |
| 100 | + callback: null, |
| 101 | + status: null, |
| 102 | + signatureFlow: null, |
| 103 | + ); |
| 104 | + |
| 105 | + $this->assertSame(Http::STATUS_OK, $response->getStatus()); |
| 106 | + } |
| 107 | + |
| 108 | + #[DataProvider('statusPayloadScenarios')] |
| 109 | + public function testUpdateSignStatusPropagation(?int $status, bool $expectStatusKey): void { |
| 110 | + $file = new FileEntity(); |
| 111 | + $file->setId(20); |
| 112 | + $file->setParentFileId(88); |
| 113 | + |
| 114 | + $this->validateHelper |
| 115 | + ->expects($this->once()) |
| 116 | + ->method('validateExistingFile'); |
| 117 | + |
| 118 | + $this->validateHelper |
| 119 | + ->expects($this->once()) |
| 120 | + ->method('validateFileStatus') |
| 121 | + ->with($this->callback(static function (array $payload) use ($expectStatusKey, $status): bool { |
| 122 | + $hasStatus = array_key_exists('status', $payload); |
| 123 | + if ($expectStatusKey !== $hasStatus) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + if ($expectStatusKey) { |
| 127 | + return $payload['status'] === $status; |
| 128 | + } |
| 129 | + return true; |
| 130 | + })); |
| 131 | + |
| 132 | + $this->validateHelper |
| 133 | + ->expects($this->once()) |
| 134 | + ->method('validateIdentifySigners'); |
| 135 | + |
| 136 | + $this->requestSignatureService |
| 137 | + ->expects($this->once()) |
| 138 | + ->method('save') |
| 139 | + ->willReturn($file); |
| 140 | + |
| 141 | + $this->fileListService |
| 142 | + ->expects($this->once()) |
| 143 | + ->method('formatFileWithChildren') |
| 144 | + ->with($file, [], $this->user) |
| 145 | + ->willReturn(['ok' => true]); |
| 146 | + |
| 147 | + $response = $this->controller->updateSign( |
| 148 | + signers: [[ |
| 149 | + 'identifyMethods' => [[ |
| 150 | + 'method' => 'email', |
| 151 | + 'value' => 'user@test.coop', |
| 152 | + 'mandatory' => 0, |
| 153 | + ]], |
| 154 | + ]], |
| 155 | + uuid: '550e8400-e29b-41d4-a716-446655440000', |
| 156 | + visibleElements: null, |
| 157 | + file: [], |
| 158 | + status: $status, |
| 159 | + signatureFlow: null, |
| 160 | + name: null, |
| 161 | + settings: [], |
| 162 | + files: [], |
| 163 | + ); |
| 164 | + |
| 165 | + $this->assertSame(Http::STATUS_OK, $response->getStatus()); |
| 166 | + } |
| 167 | + |
| 168 | + public static function statusPayloadScenarios(): array { |
| 169 | + return [ |
| 170 | + 'null status is omitted' => [ |
| 171 | + 'status' => null, |
| 172 | + 'expectStatusKey' => false, |
| 173 | + ], |
| 174 | + 'explicit status is preserved' => [ |
| 175 | + 'status' => 4, |
| 176 | + 'expectStatusKey' => true, |
| 177 | + ], |
| 178 | + ]; |
| 179 | + } |
| 180 | +} |
0 commit comments