Skip to content

Commit

Permalink
Merge pull request #3931 from LibreSign/backport/3927/stable29
Browse files Browse the repository at this point in the history
[stable29] fix: add back the contition to write_qrcode_on_footer
  • Loading branch information
vitormattos authored Nov 12, 2024
2 parents 452d293 + c014ed7 commit e38e090
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 38 deletions.
4 changes: 3 additions & 1 deletion lib/Handler/FooterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,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
@@ -1,6 +1,6 @@
<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
109 changes: 74 additions & 35 deletions tests/Unit/Handler/FooterHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,18 @@ 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 @@ -101,22 +90,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

0 comments on commit e38e090

Please sign in to comment.