|
| 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\Provider\Footer\FooterPolicyValue; |
| 12 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +final class FooterPolicyValueTest extends TestCase { |
| 16 | + public function testDefaultsAreStable(): void { |
| 17 | + $this->assertSame([ |
| 18 | + 'enabled' => true, |
| 19 | + 'writeQrcodeOnFooter' => true, |
| 20 | + 'validationSite' => '', |
| 21 | + 'customizeFooterTemplate' => false, |
| 22 | + ], FooterPolicyValue::defaults()); |
| 23 | + } |
| 24 | + |
| 25 | + #[DataProvider('normalizeCases')] |
| 26 | + public function testNormalize(mixed $input, array $expected): void { |
| 27 | + $this->assertSame($expected, FooterPolicyValue::normalize($input)); |
| 28 | + } |
| 29 | + |
| 30 | + public function testEncodeReturnsCanonicalPayload(): void { |
| 31 | + $this->assertSame( |
| 32 | + '{"enabled":true,"writeQrcodeOnFooter":true,"validationSite":"","customizeFooterTemplate":false}', |
| 33 | + FooterPolicyValue::encode([ |
| 34 | + 'enabled' => true, |
| 35 | + 'writeQrcodeOnFooter' => true, |
| 36 | + 'validationSite' => '', |
| 37 | + 'customizeFooterTemplate' => false, |
| 38 | + ]), |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + #[DataProvider('isQrCodeEnabledCases')] |
| 43 | + public function testIsQrCodeEnabled(mixed $input, bool $expected): void { |
| 44 | + $this->assertSame($expected, FooterPolicyValue::isQrCodeEnabled($input)); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return array<string, array{0: mixed, 1: array{enabled: bool, writeQrcodeOnFooter: bool, validationSite: string, customizeFooterTemplate: bool}}> |
| 49 | + */ |
| 50 | + public static function normalizeCases(): array { |
| 51 | + return [ |
| 52 | + 'boolean false keeps defaults and disables footer' => [ |
| 53 | + false, |
| 54 | + [ |
| 55 | + 'enabled' => false, |
| 56 | + 'writeQrcodeOnFooter' => true, |
| 57 | + 'validationSite' => '', |
| 58 | + 'customizeFooterTemplate' => false, |
| 59 | + ], |
| 60 | + ], |
| 61 | + 'int one enables footer' => [ |
| 62 | + 1, |
| 63 | + [ |
| 64 | + 'enabled' => true, |
| 65 | + 'writeQrcodeOnFooter' => true, |
| 66 | + 'validationSite' => '', |
| 67 | + 'customizeFooterTemplate' => false, |
| 68 | + ], |
| 69 | + ], |
| 70 | + 'legacy scalar off' => [ |
| 71 | + '0', |
| 72 | + [ |
| 73 | + 'enabled' => false, |
| 74 | + 'writeQrcodeOnFooter' => true, |
| 75 | + 'validationSite' => '', |
| 76 | + 'customizeFooterTemplate' => false, |
| 77 | + ], |
| 78 | + ], |
| 79 | + 'legacy scalar yes treated as true' => [ |
| 80 | + 'yes', |
| 81 | + [ |
| 82 | + 'enabled' => true, |
| 83 | + 'writeQrcodeOnFooter' => true, |
| 84 | + 'validationSite' => '', |
| 85 | + 'customizeFooterTemplate' => false, |
| 86 | + ], |
| 87 | + ], |
| 88 | + 'structured json payload' => [ |
| 89 | + '{"enabled":true,"writeQrcodeOnFooter":false,"validationSite":"https://validation.example","customizeFooterTemplate":true}', |
| 90 | + [ |
| 91 | + 'enabled' => true, |
| 92 | + 'writeQrcodeOnFooter' => false, |
| 93 | + 'validationSite' => 'https://validation.example', |
| 94 | + 'customizeFooterTemplate' => true, |
| 95 | + ], |
| 96 | + ], |
| 97 | + 'legacy snake case array payload' => [ |
| 98 | + [ |
| 99 | + 'addFooter' => '1', |
| 100 | + 'write_qrcode_on_footer' => '0', |
| 101 | + 'validation_site' => ' https://legacy.example/base/ ', |
| 102 | + 'customize_footer_template' => '1', |
| 103 | + ], |
| 104 | + [ |
| 105 | + 'enabled' => true, |
| 106 | + 'writeQrcodeOnFooter' => false, |
| 107 | + 'validationSite' => 'https://legacy.example/base/', |
| 108 | + 'customizeFooterTemplate' => true, |
| 109 | + ], |
| 110 | + ], |
| 111 | + 'array payload with non scalar validation site is sanitized' => [ |
| 112 | + [ |
| 113 | + 'enabled' => true, |
| 114 | + 'writeQrcodeOnFooter' => true, |
| 115 | + 'validationSite' => ['invalid'], |
| 116 | + 'customizeFooterTemplate' => false, |
| 117 | + ], |
| 118 | + [ |
| 119 | + 'enabled' => true, |
| 120 | + 'writeQrcodeOnFooter' => true, |
| 121 | + 'validationSite' => '', |
| 122 | + 'customizeFooterTemplate' => false, |
| 123 | + ], |
| 124 | + ], |
| 125 | + 'empty string returns defaults' => [ |
| 126 | + '', |
| 127 | + [ |
| 128 | + 'enabled' => true, |
| 129 | + 'writeQrcodeOnFooter' => true, |
| 130 | + 'validationSite' => '', |
| 131 | + 'customizeFooterTemplate' => false, |
| 132 | + ], |
| 133 | + ], |
| 134 | + 'invalid json string treated as scalar false' => [ |
| 135 | + '{broken-json', |
| 136 | + [ |
| 137 | + 'enabled' => false, |
| 138 | + 'writeQrcodeOnFooter' => true, |
| 139 | + 'validationSite' => '', |
| 140 | + 'customizeFooterTemplate' => false, |
| 141 | + ], |
| 142 | + ], |
| 143 | + ]; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @return array<string, array{0: mixed, 1: bool}> |
| 148 | + */ |
| 149 | + public static function isQrCodeEnabledCases(): array { |
| 150 | + return [ |
| 151 | + 'legacy scalar one keeps qrcode enabled' => [ |
| 152 | + '1', |
| 153 | + true, |
| 154 | + ], |
| 155 | + 'legacy scalar off disables everything' => [ |
| 156 | + '0', |
| 157 | + false, |
| 158 | + ], |
| 159 | + 'enabled and qr on' => [ |
| 160 | + '{"enabled":true,"writeQrcodeOnFooter":true}', |
| 161 | + true, |
| 162 | + ], |
| 163 | + 'enabled and qr off' => [ |
| 164 | + '{"enabled":true,"writeQrcodeOnFooter":false}', |
| 165 | + false, |
| 166 | + ], |
| 167 | + 'footer disabled even with qr on' => [ |
| 168 | + '{"enabled":false,"writeQrcodeOnFooter":true}', |
| 169 | + false, |
| 170 | + ], |
| 171 | + ]; |
| 172 | + } |
| 173 | +} |
0 commit comments