Skip to content

Commit 699b132

Browse files
committed
refactor: translate signature validation messages via l10n
Signed-off-by: Vitor Mattos <[email protected]>
1 parent dce1922 commit 699b132

2 files changed

Lines changed: 49 additions & 22 deletions

File tree

lib/Service/Signature/PdfSignatureValidationService.php

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use LibreSign\PdfSignatureValidator\Model\ValidationState;
1515
use OCA\Libresign\AppInfo\Application;
1616
use OCP\IAppConfig;
17+
use OCP\IL10N;
1718
use Psr\Log\LoggerInterface;
1819

1920
/**
@@ -29,6 +30,7 @@ class PdfSignatureValidationService {
2930

3031
public function __construct(
3132
private IAppConfig $appConfig,
33+
private IL10N $l10n,
3234
private LoggerInterface $logger,
3335
) {
3436
$this->validator = new PdfSignatureValidator();
@@ -158,31 +160,31 @@ private function mapSignatureValidation(ValidationResult $result): array {
158160
return match ($result->state) {
159161
ValidationState::SIGNATURE_VALID => [
160162
'id' => 1,
161-
'label' => 'Signature is valid.',
163+
'label' => $this->l10n->t('Signature is valid.'),
162164
'isValid' => true,
163165
],
164166
ValidationState::SIGNATURE_INVALID => [
165167
'id' => 2,
166-
'label' => 'Signature is invalid.',
167-
'reason' => $result->reason,
168+
'label' => $this->l10n->t('Signature is invalid.'),
169+
'reason' => $this->translateReason($result->reason),
168170
'isValid' => false,
169171
],
170172
ValidationState::DIGEST_MISMATCH => [
171173
'id' => 3,
172-
'label' => 'Digest mismatch.',
173-
'reason' => $result->reason,
174+
'label' => $this->l10n->t('Digest mismatch.'),
175+
'reason' => $this->translateReason($result->reason),
174176
'isValid' => false,
175177
],
176178
ValidationState::NOT_VERIFIED => [
177179
'id' => 5,
178-
'label' => 'Signature has not yet been verified.',
179-
'reason' => $result->reason,
180+
'label' => $this->l10n->t('Signature has not yet been verified.'),
181+
'reason' => $this->translateReason($result->reason),
180182
'isValid' => false,
181183
],
182184
default => [
183185
'id' => 6,
184-
'label' => 'Unknown validation failure.',
185-
'reason' => $result->reason,
186+
'label' => $this->l10n->t('Unknown validation failure.'),
187+
'reason' => $this->translateReason($result->reason),
186188
'isValid' => false,
187189
],
188190
};
@@ -195,48 +197,56 @@ private function mapCertificateValidation(ValidationResult $result): array {
195197
return match ($result->state) {
196198
ValidationState::CERT_TRUSTED => [
197199
'id' => 1,
198-
'label' => 'Certificate is trusted.',
200+
'label' => $this->l10n->t('Certificate is trusted.'),
199201
'isValid' => true,
200202
],
201203
ValidationState::CERT_ISSUER_NOT_TRUSTED => [
202204
'id' => 2,
203-
'label' => "Certificate issuer isn't trusted.",
204-
'reason' => $result->reason,
205+
'label' => $this->l10n->t("Certificate issuer isn't trusted."),
206+
'reason' => $this->translateReason($result->reason),
205207
'isValid' => false,
206208
],
207209
ValidationState::CERT_ISSUER_UNKNOWN => [
208210
'id' => 3,
209-
'label' => 'Certificate issuer is unknown.',
210-
'reason' => $result->reason,
211+
'label' => $this->l10n->t('Certificate issuer is unknown.'),
212+
'reason' => $this->translateReason($result->reason),
211213
'isValid' => false,
212214
],
213215
ValidationState::CERT_REVOKED => [
214216
'id' => 4,
215-
'label' => 'Certificate has been revoked.',
216-
'reason' => $result->reason,
217+
'label' => $this->l10n->t('Certificate has been revoked.'),
218+
'reason' => $this->translateReason($result->reason),
217219
'isValid' => false,
218220
],
219221
ValidationState::CERT_EXPIRED => [
220222
'id' => 5,
221-
'label' => 'Certificate has expired.',
222-
'reason' => $result->reason,
223+
'label' => $this->l10n->t('Certificate has expired.'),
224+
'reason' => $this->translateReason($result->reason),
223225
'isValid' => false,
224226
],
225227
ValidationState::CERT_NOT_VERIFIED => [
226228
'id' => 6,
227-
'label' => 'Certificate has not yet been verified.',
228-
'reason' => $result->reason,
229+
'label' => $this->l10n->t('Certificate has not yet been verified.'),
230+
'reason' => $this->translateReason($result->reason),
229231
'isValid' => false,
230232
],
231233
default => [
232234
'id' => 7,
233-
'label' => 'Unknown issue with certificate or corrupted data.',
234-
'reason' => $result->reason,
235+
'label' => $this->l10n->t('Unknown issue with certificate or corrupted data.'),
236+
'reason' => $this->translateReason($result->reason),
235237
'isValid' => false,
236238
],
237239
};
238240
}
239241

242+
private function translateReason(?string $reason): ?string {
243+
if ($reason === null || $reason === '') {
244+
return $reason;
245+
}
246+
247+
return $this->l10n->t($reason);
248+
}
249+
240250
/**
241251
* Check if LibreSign CA is loaded.
242252
*/

tests/php/Unit/Service/Signature/PdfSignatureValidationServiceTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@
1212
use LibreSign\PdfSignatureValidator\Model\ValidationState;
1313
use OCA\Libresign\Service\Signature\PdfSignatureValidationService;
1414
use OCA\Libresign\Tests\Unit\TestCase;
15+
use OCP\IL10N;
16+
use PHPUnit\Framework\MockObject\MockObject;
1517

1618
final class PdfSignatureValidationServiceTest extends TestCase {
19+
private IL10N&MockObject $l10n;
20+
21+
public function setUp(): void {
22+
parent::setUp();
23+
$this->l10n = $this->createMock(IL10N::class);
24+
$this->l10n
25+
->method('t')
26+
->willReturnCallback(static fn (string $text): string => $text);
27+
}
28+
1729
public function testMapSignatureValidationWithEnumState(): void {
1830
$service = $this->newServiceWithoutConstructor();
1931
$result = $this->invokePrivateMethod(
@@ -45,6 +57,11 @@ private function newServiceWithoutConstructor(): PdfSignatureValidationService {
4557
$reflection = new \ReflectionClass(PdfSignatureValidationService::class);
4658
/** @var PdfSignatureValidationService $service */
4759
$service = $reflection->newInstanceWithoutConstructor();
60+
61+
$l10nProperty = $reflection->getProperty('l10n');
62+
$l10nProperty->setAccessible(true);
63+
$l10nProperty->setValue($service, $this->l10n);
64+
4865
return $service;
4966
}
5067

0 commit comments

Comments
 (0)