Skip to content

Commit 7653979

Browse files
committed
OXDEV-9078 Move test-related methods out of production code
1 parent 5ae0ce0 commit 7653979

7 files changed

Lines changed: 109 additions & 144 deletions

File tree

src/Captcha/Service/ModuleSettingsService.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,11 @@ public function isCaptchaEnabled(): bool
3737
return $this->moduleSettingService->getBoolean(self::CAPTCHA_ENABLE, Module::MODULE_ID);
3838
}
3939

40-
public function saveIsCaptchaEnabled(bool $value): void
41-
{
42-
$this->moduleSettingService->saveBoolean(self::CAPTCHA_ENABLE, $value, Module::MODULE_ID);
43-
}
44-
4540
public function isHoneyPotCaptchaEnabled(): bool
4641
{
4742
return $this->moduleSettingService->getBoolean(self::HONEYPOT_CAPTCHA_ENABLE, Module::MODULE_ID);
4843
}
4944

50-
public function saveIsHoneyPotCaptchaEnabled(bool $value): void
51-
{
52-
$this->moduleSettingService->saveBoolean(self::HONEYPOT_CAPTCHA_ENABLE, $value, Module::MODULE_ID);
53-
}
54-
5545
public function getCaptchaLifeTime(): string
5646
{
5747
$key = $this->moduleSettingService

src/Captcha/Service/ModuleSettingsServiceInterface.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,5 @@ public function isCaptchaEnabled(): bool;
1313

1414
public function isHoneyPotCaptchaEnabled(): bool;
1515

16-
public function saveIsCaptchaEnabled(bool $value): void;
17-
18-
public function saveIsHoneyPotCaptchaEnabled(bool $value): void;
19-
2016
public function getCaptchaLifeTime(): string;
2117
}

tests/Codeception/Acceptance/BaseCest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use OxidEsales\EshopCommunity\Core\Di\ContainerFacade;
1414
use OxidEsales\EshopCommunity\Internal\Framework\Module\Facade\ModuleSettingServiceInterface;
1515
use OxidEsales\SecurityModule\Authentication\TwoFactorAuth\Settings\TwoFAShopSettings;
16-
use OxidEsales\SecurityModule\Captcha\Service\ModuleSettingsServiceInterface as CaptchaSettingsServiceInterface;
16+
use OxidEsales\SecurityModule\Captcha\Service\ModuleSettingsService as CaptchaModuleSettingsService;
1717
use OxidEsales\SecurityModule\Core\Module;
1818
use OxidEsales\SecurityModule\Tests\Codeception\Support\AcceptanceTester;
1919
use OxidEsales\SecurityModule\PasswordPolicy\Service\ModuleSettingsServiceInterface as PasswordSettingsServiceInterface;
@@ -35,14 +35,16 @@ protected function setPasswordState(bool $state)
3535
ContainerFacade::get(PasswordSettingsServiceInterface::class)->saveIsPasswordPolicyEnabled($state);
3636
}
3737

38-
protected function setCaptchaState(bool $state)
38+
protected function setCaptchaState(bool $state): void
3939
{
40-
ContainerFacade::get(CaptchaSettingsServiceInterface::class)->saveIsCaptchaEnabled($state);
40+
ContainerFacade::get(ModuleSettingServiceInterface::class)
41+
->saveBoolean(CaptchaModuleSettingsService::CAPTCHA_ENABLE, $state, Module::MODULE_ID);
4142
}
4243

43-
protected function setHoneyPotCaptchaState(bool $state)
44+
protected function setHoneyPotCaptchaState(bool $state): void
4445
{
45-
ContainerFacade::get(CaptchaSettingsServiceInterface::class)->saveIsHoneyPotCaptchaEnabled($state);
46+
ContainerFacade::get(ModuleSettingServiceInterface::class)
47+
->saveBoolean(CaptchaModuleSettingsService::HONEYPOT_CAPTCHA_ENABLE, $state, Module::MODULE_ID);
4648
}
4749

4850
protected function setTwoFactorAuthState(bool $state)

tests/Codeception/Acceptance/CheckoutCest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public function anonymousUserCanCheckout(AcceptanceTester $I): void
5050

5151
private function ensureCorrectCaptchaSettings(AcceptanceTester $I): void
5252
{
53-
/** @var CaptchaSettingsServiceInterface $captchaSettings */
53+
$this->setCaptchaState(false);
54+
5455
$captchaSettings = ContainerFacade::get(CaptchaSettingsServiceInterface::class);
55-
$captchaSettings->saveIsCaptchaEnabled(false);
5656
$I->assertTrue($captchaSettings->isHoneyPotCaptchaEnabled());
5757
}
5858
}

tests/Integration/Captcha/Shop/NewsletterControllerTest.php

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010
namespace OxidEsales\SecurityModule\Tests\Integration\Captcha\Shop;
1111

12-
use OxidEsales\Eshop\Application\Controller\NewsletterController;
12+
use OxidEsales\Eshop\Core\Exception\StandardException;
1313
use OxidEsales\Eshop\Core\Registry;
1414
use OxidEsales\Eshop\Core\Request;
1515
use OxidEsales\Eshop\Core\UtilsView;
1616
use OxidEsales\EshopCommunity\Core\Di\ContainerFacade;
1717
use OxidEsales\EshopCommunity\Tests\Integration\IntegrationTestCase;
18+
use OxidEsales\SecurityModule\Captcha\Service\CaptchaServiceInterface;
1819
use OxidEsales\SecurityModule\Captcha\Service\ModuleSettingsServiceInterface;
20+
use OxidEsales\SecurityModule\Captcha\Shop\NewsletterController as ModuleNewsletterController;
1921

2022
class NewsletterControllerTest extends IntegrationTestCase
2123
{
@@ -26,9 +28,6 @@ public function setUp(): void
2628
{
2729
parent::setUp();
2830

29-
$moduleSettings = ContainerFacade::get(ModuleSettingsServiceInterface::class);
30-
$moduleSettings->saveIsCaptchaEnabled(true);
31-
3231
$this->utilsViewMock = $this->getMockBuilder(UtilsView::class)
3332
->disableOriginalConstructor()
3433
->onlyMethods(['addErrorToDisplay'])
@@ -41,18 +40,13 @@ public function setUp(): void
4140

4241
Registry::set(UtilsView::class, $this->utilsViewMock);
4342
Registry::set(Request::class, $this->requestMock);
44-
Registry::getSession()->setVariable('captcha', 'valid_captcha');
45-
Registry::getSession()->setVariable('captcha_expiration', time() + 60);
4643
}
4744

4845
public function testSendWithValidCaptcha()
4946
{
5047
$this->requestMock
5148
->method('getRequestParameter')
5249
->willReturnCallback(function ($param) {
53-
if ($param === 'captcha') {
54-
return 'valid_captcha';
55-
}
5650
if ($param === 'lastname_confirm') {
5751
return '';
5852
}
@@ -66,62 +60,73 @@ public function testSendWithValidCaptcha()
6660
$this->assertNotEquals('ERROR_INVALID_CAPTCHA', $message);
6761
});
6862

69-
$subject = oxNew(NewsletterController::class);
63+
$subject = $this->getSut();
7064
$subject->send();
7165
}
7266

7367
public function testSendWithInvalidCaptcha()
7468
{
75-
$this->requestMock
76-
->method('getRequestParameter')
77-
->with('captcha')
78-
->willReturn('invalid_captcha');
79-
8069
$this->utilsViewMock
8170
->expects($this->once())
8271
->method('addErrorToDisplay')
8372
->with('ERROR_INVALID_CAPTCHA');
8473

85-
$subject = oxNew(NewsletterController::class);
74+
$captchaService = $this->createMock(CaptchaServiceInterface::class);
75+
$captchaService->method('validate')->willThrowException(new StandardException('ERROR_INVALID_CAPTCHA'));
76+
77+
$subject = $this->getSut([CaptchaServiceInterface::class => $captchaService]);
8678
$subject->send();
8779
}
8880

8981
public function testSendWithEmptyCaptcha()
9082
{
91-
$this->requestMock
92-
->method('getRequestParameter')
93-
->with('captcha')
94-
->willReturn('');
95-
9683
$this->utilsViewMock
9784
->expects($this->once())
9885
->method('addErrorToDisplay')
9986
->with('ERROR_EMPTY_CAPTCHA');
10087

101-
$subject = oxNew(NewsletterController::class);
88+
$captchaService = $this->createMock(CaptchaServiceInterface::class);
89+
$captchaService->method('validate')->willThrowException(new StandardException('ERROR_EMPTY_CAPTCHA'));
90+
91+
$subject = $this->getSut([CaptchaServiceInterface::class => $captchaService]);
10292
$subject->send();
10393
}
10494

10595
public function testSendWithInvalidHoneyPotCaptcha()
10696
{
107-
$this->requestMock
108-
->method('getRequestParameter')
109-
->willReturnCallback(function ($param) {
110-
if ($param === 'captcha') {
111-
return 'valid_captcha';
112-
}
113-
if ($param === 'lastname_confirm') {
114-
return 'some-text';
115-
}
116-
return '';
117-
});
118-
11997
$this->utilsViewMock
12098
->expects($this->once())
12199
->method('addErrorToDisplay')
122100
->with('FORM_VALIDATION_FAILED');
123101

124-
$subject = oxNew(NewsletterController::class);
102+
$captchaService = $this->createMock(CaptchaServiceInterface::class);
103+
$captchaService->method('validate')->willThrowException(new StandardException('FORM_VALIDATION_FAILED'));
104+
105+
$subject = $this->getSut([CaptchaServiceInterface::class => $captchaService]);
125106
$subject->send();
126107
}
108+
109+
private function getSut(array $serviceOverrides = []): ModuleNewsletterController
110+
{
111+
$services = array_merge(
112+
[
113+
ModuleSettingsServiceInterface::class => $this->createConfiguredStub(
114+
ModuleSettingsServiceInterface::class,
115+
['isCaptchaEnabled' => true]
116+
),
117+
CaptchaServiceInterface::class => $this->createStub(CaptchaServiceInterface::class),
118+
],
119+
$serviceOverrides
120+
);
121+
122+
/** @var ModuleNewsletterController $sut */
123+
$sut = $this->getMockBuilder(ModuleNewsletterController::class)
124+
->onlyMethods(['getService'])
125+
->getMock();
126+
$sut->method('getService')->willReturnCallback(
127+
fn(string $id) => $services[$id] ?? ContainerFacade::get($id)
128+
);
129+
130+
return $sut;
131+
}
127132
}

tests/Integration/Shared/Controller/ForgotPasswordControllerTest.php

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
namespace OxidEsales\SecurityModule\Tests\Integration\Shared\Controller;
1111

12-
use OxidEsales\Eshop\Application\Controller\ForgotPasswordController;
12+
use OxidEsales\Eshop\Core\Exception\StandardException;
1313
use OxidEsales\Eshop\Core\Registry;
1414
use OxidEsales\Eshop\Core\Request;
1515
use OxidEsales\Eshop\Core\UtilsView;
1616
use OxidEsales\EshopCommunity\Core\Di\ContainerFacade;
17-
use OxidEsales\SecurityModule\Tests\Integration\IntegrationTestCase;
17+
use OxidEsales\SecurityModule\Captcha\Service\CaptchaServiceInterface;
1818
use OxidEsales\SecurityModule\Captcha\Service\ModuleSettingsServiceInterface;
19-
use OxidEsales\SecurityModule\PasswordPolicy\Service\ModuleSettingsServiceInterface as PasswordSettingsServiceInterface;
19+
use OxidEsales\SecurityModule\Shared\Controller\ForgotPasswordController as ModuleForgotPasswordController;
20+
use OxidEsales\SecurityModule\Tests\Integration\IntegrationTestCase;
2021

2122
class ForgotPasswordControllerTest extends IntegrationTestCase
2223
{
@@ -27,12 +28,6 @@ public function setUp(): void
2728
{
2829
parent::setUp();
2930

30-
$moduleSettings = ContainerFacade::get(ModuleSettingsServiceInterface::class);
31-
$moduleSettings->saveIsCaptchaEnabled(true);
32-
33-
$passwordSettings = ContainerFacade::get(PasswordSettingsServiceInterface::class);
34-
$passwordSettings->saveIsPasswordPolicyEnabled(false);
35-
3631
$this->utilsViewMock = $this->getMockBuilder(UtilsView::class)
3732
->disableOriginalConstructor()
3833
->onlyMethods(['addErrorToDisplay'])
@@ -45,18 +40,13 @@ public function setUp(): void
4540

4641
Registry::set(UtilsView::class, $this->utilsViewMock);
4742
Registry::set(Request::class, $this->requestMock);
48-
Registry::getSession()->setVariable('captcha', 'valid_captcha');
49-
Registry::getSession()->setVariable('captcha_expiration', time() + 60);
5043
}
5144

5245
public function testForgotPasswordWithValidCaptcha()
5346
{
5447
$this->requestMock
5548
->method('getRequestParameter')
5649
->willReturnCallback(function ($param) {
57-
if ($param === 'captcha') {
58-
return 'valid_captcha';
59-
}
6050
return '';
6151
});
6252

@@ -67,62 +57,73 @@ public function testForgotPasswordWithValidCaptcha()
6757
$this->assertNotEquals('ERROR_INVALID_CAPTCHA', $message);
6858
});
6959

70-
$subject = oxNew(ForgotPasswordController::class);
60+
$subject = $this->getSut();
7161
$subject->forgotPassword();
7262
}
7363

7464
public function testForgotPasswordWithInvalidCaptcha()
7565
{
76-
$this->requestMock
77-
->method('getRequestParameter')
78-
->with('captcha')
79-
->willReturn('invalid_captcha');
80-
8166
$this->utilsViewMock
8267
->expects($this->once())
8368
->method('addErrorToDisplay')
8469
->with('ERROR_INVALID_CAPTCHA');
8570

86-
$subject = oxNew(ForgotPasswordController::class);
71+
$captchaService = $this->createMock(CaptchaServiceInterface::class);
72+
$captchaService->method('validate')->willThrowException(new StandardException('ERROR_INVALID_CAPTCHA'));
73+
74+
$subject = $this->getSut([CaptchaServiceInterface::class => $captchaService]);
8775
$subject->forgotPassword();
8876
}
8977

9078
public function testForgotPasswordWithInvalidHoneyPotCaptcha()
9179
{
92-
$this->requestMock
93-
->method('getRequestParameter')
94-
->willReturnCallback(function ($param) {
95-
if ($param === 'captcha') {
96-
return 'valid_captcha';
97-
}
98-
if ($param === 'lastname_confirm') {
99-
return 'some-text';
100-
}
101-
return '';
102-
});
103-
10480
$this->utilsViewMock
10581
->expects($this->once())
10682
->method('addErrorToDisplay')
10783
->with('FORM_VALIDATION_FAILED');
10884

109-
$subject = oxNew(ForgotPasswordController::class);
85+
$captchaService = $this->createMock(CaptchaServiceInterface::class);
86+
$captchaService->method('validate')->willThrowException(new StandardException('FORM_VALIDATION_FAILED'));
87+
88+
$subject = $this->getSut([CaptchaServiceInterface::class => $captchaService]);
11089
$subject->forgotPassword();
11190
}
11291

11392
public function testForgotPasswordWithEmptyCaptcha()
11493
{
115-
$this->requestMock
116-
->method('getRequestParameter')
117-
->with('captcha')
118-
->willReturn('');
119-
12094
$this->utilsViewMock
12195
->expects($this->once())
12296
->method('addErrorToDisplay')
12397
->with('ERROR_EMPTY_CAPTCHA');
12498

125-
$subject = oxNew(ForgotPasswordController::class);
99+
$captchaService = $this->createMock(CaptchaServiceInterface::class);
100+
$captchaService->method('validate')->willThrowException(new StandardException('ERROR_EMPTY_CAPTCHA'));
101+
102+
$subject = $this->getSut([CaptchaServiceInterface::class => $captchaService]);
126103
$subject->forgotPassword();
127104
}
105+
106+
private function getSut(array $serviceOverrides = []): ModuleForgotPasswordController
107+
{
108+
$services = array_merge(
109+
[
110+
ModuleSettingsServiceInterface::class => $this->createConfiguredStub(
111+
ModuleSettingsServiceInterface::class,
112+
['isCaptchaEnabled' => true]
113+
),
114+
CaptchaServiceInterface::class => $this->createStub(CaptchaServiceInterface::class),
115+
],
116+
$serviceOverrides
117+
);
118+
119+
/** @var ModuleForgotPasswordController $sut */
120+
$sut = $this->getMockBuilder(ModuleForgotPasswordController::class)
121+
->onlyMethods(['getService'])
122+
->getMock();
123+
$sut->method('getService')->willReturnCallback(
124+
fn(string $id) => $services[$id] ?? ContainerFacade::get($id)
125+
);
126+
127+
return $sut;
128+
}
128129
}

0 commit comments

Comments
 (0)