From 429f5f34a599248bb2e3fc654eaf98c0ef5c5697 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Tue, 12 Nov 2024 15:29:07 -0300 Subject: [PATCH] chore: tests improvements Signed-off-by: Vitor Mattos --- lib/Handler/FooterHandler.php | 2 +- lib/Handler/Templates/footer.php | 4 +- tests/Unit/Handler/FooterHandlerTest.php | 108 +++++++++++++++-------- 3 files changed, 76 insertions(+), 38 deletions(-) diff --git a/lib/Handler/FooterHandler.php b/lib/Handler/FooterHandler.php index c934a60672..5575b99d5e 100644 --- a/lib/Handler/FooterHandler.php +++ b/lib/Handler/FooterHandler.php @@ -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']); } diff --git a/lib/Handler/Templates/footer.php b/lib/Handler/Templates/footer.php index 30024c20d7..3986c75298 100644 --- a/lib/Handler/Templates/footer.php +++ b/lib/Handler/Templates/footer.php @@ -5,9 +5,9 @@ */ ?> - +
- + diff --git a/tests/Unit/Handler/FooterHandlerTest.php b/tests/Unit/Handler/FooterHandlerTest.php index 2b840bf4cd..d9297eb1f1 100644 --- a/tests/Unit/Handler/FooterHandlerTest.php +++ b/tests/Unit/Handler/FooterHandlerTest.php @@ -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 { @@ -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' -
- qrcodeSize:
- signedBy:
- validateIn:
- test:
- qrcode: -
- 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'; @@ -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' +
+ qrcodeSize:
+ signedBy:
+ validateIn:
+ test:
+ qrcode: +
+ 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' +
+ signedBy:
+ validateIn:
+ test: +
+ 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 {