|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © OXID eSales AG. All rights reserved. |
| 5 | + * See LICENSE file for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OxidEsales\SecurityModule\Tests\Integration\Authentication\TwoFactorAuth\Controller; |
| 11 | + |
| 12 | +use Generator; |
| 13 | +use OxidEsales\Eshop\Application\Controller\AccountController; |
| 14 | +use OxidEsales\Eshop\Application\Model\User; |
| 15 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Controller\AccountSecurityController; |
| 16 | +use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Settings\TwoFAUserSettingsInterface; |
| 17 | +use OxidEsales\SecurityModule\Tests\Integration\IntegrationTestCase; |
| 18 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 19 | +use PHPUnit\Framework\Attributes\Test; |
| 20 | + |
| 21 | +class AccountSecurityControllerTest extends IntegrationTestCase |
| 22 | +{ |
| 23 | + #[Test] |
| 24 | + public function extendsAccountController(): void |
| 25 | + { |
| 26 | + $this->assertInstanceOf(AccountController::class, $this->getSut()); |
| 27 | + } |
| 28 | + |
| 29 | + #[Test] |
| 30 | + #[DataProvider('renderSetsTwoFAEnabledDataProvider')] |
| 31 | + public function renderSetsTwoFAEnabled(bool $userSettingEnabled): void |
| 32 | + { |
| 33 | + $userId = uniqid(); |
| 34 | + |
| 35 | + $userStub = $this->createStub(User::class); |
| 36 | + $userStub->method('getId')->willReturn($userId); |
| 37 | + |
| 38 | + $userSettingsStub = $this->createStub(TwoFAUserSettingsInterface::class); |
| 39 | + $userSettingsStub->method('isEnabledForUser')->with($userId)->willReturn($userSettingEnabled); |
| 40 | + |
| 41 | + $sut = $this->getSut(userSettingsService: $userSettingsStub); |
| 42 | + $sut->method('getUser')->willReturn($userStub); |
| 43 | + $sut->render(); |
| 44 | + |
| 45 | + $this->assertSame($userSettingEnabled, $sut->getViewDataElement('twoFAEnabledForUser')); |
| 46 | + } |
| 47 | + |
| 48 | + public static function renderSetsTwoFAEnabledDataProvider(): Generator |
| 49 | + { |
| 50 | + yield 'user has 2FA enabled' => ['userSettingEnabled' => true]; |
| 51 | + yield 'user has 2FA disabled' => ['userSettingEnabled' => false]; |
| 52 | + } |
| 53 | + |
| 54 | + private function getSut( |
| 55 | + TwoFAUserSettingsInterface $userSettingsService = null, |
| 56 | + ): AccountSecurityController { |
| 57 | + return $this->getMockBuilder(AccountSecurityController::class) |
| 58 | + ->setConstructorArgs([ |
| 59 | + 'userSettingsService' => $userSettingsService ?? $this->createStub(TwoFAUserSettingsInterface::class), |
| 60 | + ]) |
| 61 | + ->onlyMethods(['getUser']) |
| 62 | + ->getMock(); |
| 63 | + } |
| 64 | +} |
0 commit comments