|
| 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\IdentifyMethod\SignatureMethod; |
| 10 | + |
| 11 | +use OCA\Libresign\Service\IdentifyMethod\IdentifyService; |
| 12 | +use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\ISignatureMethod; |
| 13 | +use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\SignalToken; |
| 14 | +use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\SmsToken; |
| 15 | +use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\TelegramToken; |
| 16 | +use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\TokenService; |
| 17 | +use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\WhatsappToken; |
| 18 | +use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\XmppToken; |
| 19 | +use OCP\L10N\IFactory as IL10NFactory; |
| 20 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +final class TokenClassesTest extends TestCase { |
| 25 | + private IdentifyService&MockObject $identifyService; |
| 26 | + private TokenService&MockObject $tokenService; |
| 27 | + |
| 28 | + public function setUp(): void { |
| 29 | + $identifyService = $this->getMockBuilder(IdentifyService::class) |
| 30 | + ->disableOriginalConstructor() |
| 31 | + ->onlyMethods(['getL10n']) |
| 32 | + ->getMock(); |
| 33 | + $identifyService->method('getL10n')->willReturn( |
| 34 | + \OCP\Server::get(IL10NFactory::class)->get(\OCA\Libresign\AppInfo\Application::APP_ID) |
| 35 | + ); |
| 36 | + $this->identifyService = $identifyService; |
| 37 | + $this->tokenService = $this->createMock(TokenService::class); |
| 38 | + } |
| 39 | + |
| 40 | + public function testSmsTokenGetFriendlyName(): void { |
| 41 | + $smsToken = new SmsToken( |
| 42 | + $this->identifyService, |
| 43 | + $this->tokenService, |
| 44 | + ); |
| 45 | + |
| 46 | + $this->assertEquals('SMS token', $smsToken->getFriendlyName()); |
| 47 | + } |
| 48 | + |
| 49 | + public function testSmsTokenConstant(): void { |
| 50 | + $this->assertEquals('smsToken', ISignatureMethod::SIGNATURE_METHOD_SMS); |
| 51 | + } |
| 52 | + |
| 53 | + public function testSignalTokenGetFriendlyName(): void { |
| 54 | + $signalToken = new SignalToken( |
| 55 | + $this->identifyService, |
| 56 | + $this->tokenService, |
| 57 | + ); |
| 58 | + |
| 59 | + $this->assertEquals('Signal token', $signalToken->getFriendlyName()); |
| 60 | + } |
| 61 | + |
| 62 | + public function testSignalTokenConstant(): void { |
| 63 | + $this->assertEquals('signalToken', ISignatureMethod::SIGNATURE_METHOD_SIGNAL); |
| 64 | + } |
| 65 | + |
| 66 | + public function testTelegramTokenGetFriendlyName(): void { |
| 67 | + $telegramToken = new TelegramToken( |
| 68 | + $this->identifyService, |
| 69 | + $this->tokenService, |
| 70 | + ); |
| 71 | + |
| 72 | + $this->assertEquals('Telegram token', $telegramToken->getFriendlyName()); |
| 73 | + } |
| 74 | + |
| 75 | + public function testTelegramTokenConstant(): void { |
| 76 | + $this->assertEquals('telegramToken', ISignatureMethod::SIGNATURE_METHOD_TELEGRAM); |
| 77 | + } |
| 78 | + |
| 79 | + public function testWhatsappTokenGetFriendlyName(): void { |
| 80 | + $whatsappToken = new WhatsappToken( |
| 81 | + $this->identifyService, |
| 82 | + $this->tokenService, |
| 83 | + ); |
| 84 | + |
| 85 | + $this->assertEquals('WhatsApp token', $whatsappToken->getFriendlyName()); |
| 86 | + } |
| 87 | + |
| 88 | + public function testWhatsappTokenConstant(): void { |
| 89 | + $this->assertEquals('whatsappToken', ISignatureMethod::SIGNATURE_METHOD_WHATSAPP); |
| 90 | + } |
| 91 | + |
| 92 | + public function testXmppTokenGetFriendlyName(): void { |
| 93 | + $xmppToken = new XmppToken( |
| 94 | + $this->identifyService, |
| 95 | + $this->tokenService, |
| 96 | + ); |
| 97 | + |
| 98 | + $this->assertEquals('XMPP token', $xmppToken->getFriendlyName()); |
| 99 | + } |
| 100 | + |
| 101 | + public function testXmppTokenConstant(): void { |
| 102 | + $this->assertEquals('xmppToken', ISignatureMethod::SIGNATURE_METHOD_XMPP); |
| 103 | + } |
| 104 | + |
| 105 | + #[DataProvider('providerTokenClasses')] |
| 106 | + public function testAllTokenClassesExtendTwofactorGatewayToken(string $className): void { |
| 107 | + $reflection = new \ReflectionClass($className); |
| 108 | + $parent = $reflection->getParentClass(); |
| 109 | + |
| 110 | + $this->assertNotFalse($parent); |
| 111 | + $this->assertEquals('OCA\Libresign\Service\IdentifyMethod\SignatureMethod\TwofactorGatewayToken', $parent->getName()); |
| 112 | + } |
| 113 | + |
| 114 | + public static function providerTokenClasses(): array { |
| 115 | + return [ |
| 116 | + [SignalToken::class], |
| 117 | + [SmsToken::class], |
| 118 | + [TelegramToken::class], |
| 119 | + [WhatsappToken::class], |
| 120 | + [XmppToken::class], |
| 121 | + ]; |
| 122 | + } |
| 123 | + |
| 124 | + #[DataProvider('providerFriendlyNames')] |
| 125 | + public function testFriendlyNamesIncludeTokenSuffix(string $className, string $expectedName): void { |
| 126 | + $instance = new $className( |
| 127 | + $this->identifyService, |
| 128 | + $this->tokenService, |
| 129 | + ); |
| 130 | + |
| 131 | + $friendlyName = $instance->getFriendlyName(); |
| 132 | + |
| 133 | + $this->assertStringContainsString('token', strtolower($friendlyName)); |
| 134 | + $this->assertEquals($expectedName, $friendlyName); |
| 135 | + } |
| 136 | + |
| 137 | + public static function providerFriendlyNames(): array { |
| 138 | + return [ |
| 139 | + [SignalToken::class, 'Signal token'], |
| 140 | + [SmsToken::class, 'SMS token'], |
| 141 | + [TelegramToken::class, 'Telegram token'], |
| 142 | + [WhatsappToken::class, 'WhatsApp token'], |
| 143 | + [XmppToken::class, 'XMPP token'], |
| 144 | + ]; |
| 145 | + } |
| 146 | + |
| 147 | + #[DataProvider('providerConstantValues')] |
| 148 | + public function testConstantValuesMatchClassName(string $constant, string $expectedValue): void { |
| 149 | + $this->assertEquals($expectedValue, $constant); |
| 150 | + $this->assertStringEndsWith('Token', $expectedValue); |
| 151 | + } |
| 152 | + |
| 153 | + public static function providerConstantValues(): array { |
| 154 | + return [ |
| 155 | + [ISignatureMethod::SIGNATURE_METHOD_SIGNAL, 'signalToken'], |
| 156 | + [ISignatureMethod::SIGNATURE_METHOD_SMS, 'smsToken'], |
| 157 | + [ISignatureMethod::SIGNATURE_METHOD_TELEGRAM, 'telegramToken'], |
| 158 | + [ISignatureMethod::SIGNATURE_METHOD_WHATSAPP, 'whatsappToken'], |
| 159 | + [ISignatureMethod::SIGNATURE_METHOD_XMPP, 'xmppToken'], |
| 160 | + ]; |
| 161 | + } |
| 162 | +} |
0 commit comments