Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add back the contition to write_qrcode_on_footer #3927

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/Handler/FooterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ private function getTemplateVars(): array {
$this->templateVars['validateIn'] = $this->l10n->t('Validate in %s.', ['%s']);
}

$this->templateVars['qrcode'] = $this->getQrCodeImageBase64($this->templateVars['validationSite']);
if ($this->appConfig->getAppValue('write_qrcode_on_footer', '1')) {
$this->templateVars['qrcode'] = $this->getQrCodeImageBase64($this->templateVars['validationSite']);
}

return $this->templateVars;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Handler/Templates/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/

?>
<table style="width:100%;border:0;font-size:8px;">
<table style="width:100%;border:0;<?php if (empty($qrcode)) { ?>padding-left:15px;<?php } ?>font-size:8px;">
<tr>
<?php if ($qrcode) { ?>
<?php if (!empty($qrcode)) { ?>
<td width="<?= $qrcodeSize; ?>px">
<img src="data:image/png;base64,<?= $qrcode; ?>" style="width:<?= $qrcodeSize; ?>px"/>
</td>
Expand Down
108 changes: 73 additions & 35 deletions tests/Unit/Handler/FooterHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCP\IL10N;
use OCP\ITempManager;
use OCP\IURLGenerator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;

final class FooterHandlerTest extends \OCA\Libresign\Tests\Unit\TestCase {
Expand Down Expand Up @@ -55,29 +56,16 @@ public function testGetFooterWithoutValidationSite(): void {
$this->assertEmpty($actual);
}

public function testGetFooterWithSuccess(): void {
#[DataProvider('dataGetFooterWithSuccess')]
public function testGetFooterWithSuccess(array $settings, array $expected): void {
$this->appConfig = $this->createMock(IAppConfig::class);
$this->appConfig
->method('getAppValue')
->willReturnCallback(function ($key, $default):string {
return match ($key) {
'add_footer' => '1',
'validation_site' => 'http://test.coop',
'write_qrcode_on_footer' => '1',
'footer_link_to_site' => 'https://libresign.coop',
'footer_signed_by' => 'Digital signed by LibreSign.',
'footer_validate_in' => 'Validate in %s.',
'footer_template' => <<<'HTML'
<div style="font-size:8px;">
qrcodeSize:<?= $qrcodeSize ?><br />
signedBy:<?= $signedBy ?><br />
validateIn:<?= $validateIn ?><br />
test:<?= $test ?><br />
qrcode:<?= $qrcode ?>
</div>
HTML,
default => '',
};
->willReturnCallback(function ($key, $default) use ($settings):string {
if (array_key_exists($key, $settings)) {
return $settings[$key];
}
return '';
});
$this->tempManager->method('getTempBaseDir')->willReturn(sys_get_temp_dir());
$tempName = sys_get_temp_dir() . '/' . mt_rand() . '.php';
Expand Down Expand Up @@ -105,22 +93,72 @@ public function testGetFooterWithSuccess(): void {
$pdf = $this->getClass()
->setTemplateVar('test', 'fake value')
->getFooter($file, $libresignFile);
$actual = $this->extractPdfContent($pdf, [
'qrcodeSize',
'signedBy',
'validateIn',
'test',
'qrcode',
]);
$expected = [
'qrcodeSize' => '108',
'signedBy' => 'Digital signed by LibreSign.',
'validateIn' => 'Validate in %s.',
'test' => 'fake value',
if ($settings['add_footer']) {
$actual = $this->extractPdfContent($pdf, array_keys($expected));
if ($settings['write_qrcode_on_footer']) {
$this->assertNotEmpty($actual['qrcode'], 'Invalid qrcode content');
unset($actual['qrcode'], $expected['qrcode']);
}
$this->assertEquals($expected, $actual);
} else {
$this->assertEmpty($pdf);
}
}

public static function dataGetFooterWithSuccess(): array {
return [
[
['add_footer' => '0',], []
],
[
[
'add_footer' => '1',
'validation_site' => 'http://test.coop',
'write_qrcode_on_footer' => '1',
'footer_link_to_site' => 'https://libresign.coop',
'footer_signed_by' => 'Digital signed by LibreSign.',
'footer_validate_in' => 'Validate in %s.',
'footer_template' => <<<'HTML'
<div style="font-size:8px;">
qrcodeSize:<?= $qrcodeSize ?><br />
signedBy:<?= $signedBy ?><br />
validateIn:<?= $validateIn ?><br />
test:<?= $test ?><br />
qrcode:<?= $qrcode ?>
</div>
HTML,
],
[
'qrcode' => 'dummy value',
'qrcodeSize' => '108',
'signedBy' => 'Digital signed by LibreSign.',
'validateIn' => 'Validate in %s.',
'test' => 'fake value',
]
],
[
[
'add_footer' => '1',
'validation_site' => 'http://test.coop',
'write_qrcode_on_footer' => '0',
'footer_link_to_site' => 'https://libresign.coop',
'footer_signed_by' => 'Digital signed by LibreSign.',
'footer_validate_in' => 'Validate in %s.',
'footer_template' => <<<'HTML'
<div style="font-size:8px;">
signedBy:<?= $signedBy ?><br />
validateIn:<?= $validateIn ?><br />
test:<?= $test ?>
</div>
HTML,
],
[
'signedBy' => 'Digital signed by LibreSign.',
'validateIn' => 'Validate in %s.',
'test' => 'fake value',
]
]
];
$this->assertArrayHasKey('qrcode', $actual);
unset($actual['qrcode']);
$this->assertEquals($expected, $actual);
}

private function extractPdfContent(string $content, array $keys): array {
Expand Down
Loading