Skip to content

Commit

Permalink
chore: tests improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <vitor@php.rio>
  • Loading branch information
vitormattos committed Nov 12, 2024
1 parent 239cccf commit 429f5f3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lib/Handler/FooterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function getTemplateVars(): array {
$this->templateVars['validateIn'] = $this->l10n->t('Validate in %s.', ['%s']);
}

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

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

0 comments on commit 429f5f3

Please sign in to comment.