|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Tests\Unit\Service\Policy\Provider\Footer; |
| 10 | + |
| 11 | +use OCA\Libresign\Service\Policy\Model\PolicyContext; |
| 12 | +use OCA\Libresign\Service\Policy\Provider\Footer\FooterPolicy; |
| 13 | +use OCA\Libresign\Service\Policy\Provider\Footer\FooterPolicyValue; |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +final class FooterPolicyTest extends TestCase { |
| 18 | + public function testProviderBuildsAddFooterDefinition(): void { |
| 19 | + $provider = new FooterPolicy(); |
| 20 | + $this->assertSame([FooterPolicy::KEY], $provider->keys()); |
| 21 | + $definition = $provider->get(FooterPolicy::KEY); |
| 22 | + |
| 23 | + $this->assertSame(FooterPolicy::KEY, $definition->key()); |
| 24 | + $this->assertSame( |
| 25 | + FooterPolicyValue::encode(FooterPolicyValue::defaults()), |
| 26 | + $definition->defaultSystemValue(), |
| 27 | + ); |
| 28 | + $this->assertSame([], $definition->allowedValues(new PolicyContext())); |
| 29 | + } |
| 30 | + |
| 31 | + #[DataProvider('normalizationCases')] |
| 32 | + public function testProviderNormalizesValues(mixed $input, array $expected): void { |
| 33 | + $provider = new FooterPolicy(); |
| 34 | + $definition = $provider->get(FooterPolicy::KEY); |
| 35 | + |
| 36 | + $this->assertSame( |
| 37 | + FooterPolicyValue::encode($expected), |
| 38 | + $definition->normalizeValue($input), |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @return array<string, array{0: mixed, 1: array{enabled: bool, writeQrcodeOnFooter: bool, validationSite: string, customizeFooterTemplate: bool}}> |
| 44 | + */ |
| 45 | + public static function normalizationCases(): array { |
| 46 | + return [ |
| 47 | + 'boolean true enables footer with defaults' => [ |
| 48 | + true, |
| 49 | + [ |
| 50 | + 'enabled' => true, |
| 51 | + 'writeQrcodeOnFooter' => true, |
| 52 | + 'validationSite' => '', |
| 53 | + 'customizeFooterTemplate' => false, |
| 54 | + ], |
| 55 | + ], |
| 56 | + 'string zero disables footer with defaults' => [ |
| 57 | + '0', |
| 58 | + [ |
| 59 | + 'enabled' => false, |
| 60 | + 'writeQrcodeOnFooter' => true, |
| 61 | + 'validationSite' => '', |
| 62 | + 'customizeFooterTemplate' => false, |
| 63 | + ], |
| 64 | + ], |
| 65 | + 'structured json keeps full explicit payload' => [ |
| 66 | + '{"enabled":true,"writeQrcodeOnFooter":false,"validationSite":"https://validation.example","customizeFooterTemplate":true}', |
| 67 | + [ |
| 68 | + 'enabled' => true, |
| 69 | + 'writeQrcodeOnFooter' => false, |
| 70 | + 'validationSite' => 'https://validation.example', |
| 71 | + 'customizeFooterTemplate' => true, |
| 72 | + ], |
| 73 | + ], |
| 74 | + 'legacy snake case json is normalized' => [ |
| 75 | + '{"addFooter":"1","write_qrcode_on_footer":"0","validation_site":" https://legacy.example/base/ ","customize_footer_template":"1"}', |
| 76 | + [ |
| 77 | + 'enabled' => true, |
| 78 | + 'writeQrcodeOnFooter' => false, |
| 79 | + 'validationSite' => 'https://legacy.example/base/', |
| 80 | + 'customizeFooterTemplate' => true, |
| 81 | + ], |
| 82 | + ], |
| 83 | + 'invalid json falls back to enabled false from scalar parser' => [ |
| 84 | + '{invalid-json', |
| 85 | + [ |
| 86 | + 'enabled' => false, |
| 87 | + 'writeQrcodeOnFooter' => true, |
| 88 | + 'validationSite' => '', |
| 89 | + 'customizeFooterTemplate' => false, |
| 90 | + ], |
| 91 | + ], |
| 92 | + ]; |
| 93 | + } |
| 94 | +} |
0 commit comments