-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathFooterServiceTest.php
More file actions
168 lines (137 loc) · 5.13 KB
/
FooterServiceTest.php
File metadata and controls
168 lines (137 loc) · 5.13 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
<?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\Handler\FooterHandler;
use OCA\Libresign\Service\FooterService;
use OCA\Libresign\Tests\Unit\TestCase;
use OCP\IAppConfig;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
class FooterServiceTest extends TestCase {
private IAppConfig $appConfig;
private FooterHandler|MockObject $footerHandler;
private FooterService $service;
public function setUp(): void {
parent::setUp();
$this->appConfig = $this->getMockAppConfigWithReset();
$this->footerHandler = $this->createMock(FooterHandler::class);
$this->service = new FooterService($this->appConfig, $this->footerHandler);
}
#[DataProvider('provideIsDefaultTemplateScenarios')]
public function testIsDefaultTemplate(string $configValue, bool $expected): void {
$this->appConfig->setValueString(Application::APP_ID, 'footer_template', $configValue);
$this->assertSame($expected, $this->service->isDefaultTemplate());
}
public static function provideIsDefaultTemplateScenarios(): array {
return [
'empty string is default' => ['', true],
'custom template is not default' => ['<div>Custom</div>', false],
'whitespace only is not default' => [' ', false],
];
}
public function testSaveTemplate(): void {
$template = '<div>My custom template</div>';
$this->service->saveTemplate($template);
$this->assertSame(
$template,
$this->appConfig->getValueString(Application::APP_ID, 'footer_template')
);
}
public function testSaveTemplateEqualToDefaultDeletesKey(): void {
$defaultTemplate = '<div>Default Footer</div>';
$this->footerHandler
->expects($this->once())
->method('getDefaultTemplate')
->willReturn($defaultTemplate);
$this->service->saveTemplate($defaultTemplate);
$this->assertSame(
'',
$this->appConfig->getValueString(Application::APP_ID, 'footer_template', '')
);
}
public function testGetTemplate(): void {
$template = '<div>Custom template</div>';
$this->appConfig->setValueString(Application::APP_ID, 'footer_template', $template);
$this->footerHandler
->expects($this->once())
->method('getTemplate')
->willReturn($template);
$this->assertSame($template, $this->service->getTemplate());
}
public function testGetTemplateReturnsDefaultWhenNotSet(): void {
$this->appConfig->deleteKey(Application::APP_ID, 'footer_template');
$defaultTemplate = '<div>Default footer template</div>';
$this->footerHandler
->expects($this->once())
->method('getTemplate')
->willReturn($defaultTemplate);
$this->assertSame($defaultTemplate, $this->service->getTemplate());
}
#[DataProvider('provideRenderPreviewPdfScenarios')]
public function testRenderPreviewPdf(?string $template, int $width, int $height, bool $shouldSaveTemplate): void {
$this->footerHandler
->expects($this->exactly(2))
->method('setTemplateVar')
->willReturnCallback(function ($key, $value) {
if ($key === 'uuid') {
$this->assertMatchesRegularExpression('/^preview-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/', $value);
} elseif ($key === 'signers') {
$this->assertIsArray($value);
$this->assertCount(1, $value);
$this->assertSame('Preview Signer', $value[0]['displayName']);
$this->assertArrayHasKey('signed', $value[0]);
}
return $this->footerHandler;
});
$this->footerHandler
->expects($this->once())
->method('getFooter')
->with([['w' => $width, 'h' => $height]])
->willReturn('PDF binary content');
$result = $this->service->renderPreviewPdf($template, $width, $height);
$this->assertSame('PDF binary content', $result);
if ($shouldSaveTemplate) {
$this->assertSame(
$template,
$this->appConfig->getValueString(Application::APP_ID, 'footer_template')
);
}
}
public static function provideRenderPreviewPdfScenarios(): array {
return [
'with custom template and default dimensions' => ['<div>Custom</div>', 595, 50, true],
'without template uses default' => ['', 595, 50, false],
'with custom dimensions' => ['<div>Test</div>', 800, 100, true],
'A4 width with custom height' => ['', 595, 75, false],
'empty string template' => ['', 595, 50, false],
'template with unicode characters' => ['<div>签名 подпись توقيع</div>', 595, 50, true],
'minimum dimensions' => ['<div>Min</div>', 1, 1, true],
'large dimensions' => ['', 2000, 500, false],
];
}
public function testGetTemplateVariablesMetadata(): void {
$expectedMetadata = [
'direction' => [
'type' => 'string',
'description' => 'Text direction for the footer',
'example' => 'ltr',
],
'uuid' => [
'type' => 'string',
'description' => 'Document unique identifier',
'example' => 'de0a18d4-fe65-4abc-bdd1-84e819700260',
],
];
$this->footerHandler
->expects($this->once())
->method('getTemplateVariablesMetadata')
->willReturn($expectedMetadata);
$result = $this->service->getTemplateVariablesMetadata();
$this->assertSame($expectedMetadata, $result);
}
}