Skip to content

Commit aa9db55

Browse files
Merge pull request #71 from magento-cia/2.4.5-develop-2.4-develop-sync-041922
Sync of 2.4.5-develop with 2.4-develop
2 parents f00e88c + 5a561d9 commit aa9db55

File tree

32 files changed

+299
-52
lines changed

32 files changed

+299
-52
lines changed

ReCaptchaAdminUi/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "magento/module-re-captcha-admin-ui",
33
"description": "Google reCAPTCHA integration for Magento2",
44
"require": {
5-
"php": "~7.4.0||~8.0.0||~8.1.0",
5+
"php": "~7.4.0||~8.1.0",
66
"magento/framework": "*",
77
"magento/module-config": "*",
88
"magento/module-re-captcha-ui": "*",

ReCaptchaCheckout/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "magento/module-re-captcha-checkout",
33
"description": "Google reCAPTCHA integration for Magento2",
44
"require": {
5-
"php": "~7.4.0||~8.0.0||~8.1.0",
5+
"php": "~7.4.0||~8.1.0",
66
"magento/framework": "*",
77
"magento/module-checkout": "*",
88
"magento/module-re-captcha-ui": "*",

ReCaptchaContact/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "magento/module-re-captcha-contact",
33
"description": "Google reCAPTCHA integration for Magento2",
44
"require": {
5-
"php": "~7.4.0||~8.0.0||~8.1.0",
5+
"php": "~7.4.0||~8.1.0",
66
"magento/framework": "*",
77
"magento/module-re-captcha-ui": "*"
88
},

ReCaptchaCustomer/Model/WebapiConfigProvider.php

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,55 @@ public function __construct(IsCaptchaEnabledInterface $isEnabled, ValidationConf
4646
$this->configResolver = $configResolver;
4747
}
4848

49+
/**
50+
* Validates ifChangedPasswordCaptchaEnabled using captcha_id
51+
*
52+
* @param string $serviceMethod
53+
* @param string $serviceClass
54+
* @return ValidationConfigInterface|null
55+
*/
56+
private function validateChangePasswordCaptcha($serviceMethod, $serviceClass): ?ValidationConfigInterface
57+
{
58+
//phpcs:disable Magento2.PHP.LiteralNamespaces
59+
if ($serviceMethod === 'resetPassword' || $serviceMethod === 'initiatePasswordReset'
60+
|| $serviceClass === 'Magento\CustomerGraphQl\Model\Resolver\ResetPassword'
61+
|| $serviceClass === 'Magento\CustomerGraphQl\Model\Resolver\RequestPasswordResetEmail'
62+
) {
63+
return $this->isEnabled->isCaptchaEnabledFor(self::RESET_PASSWORD_CAPTCHA_ID) ?
64+
$this->configResolver->get(self::RESET_PASSWORD_CAPTCHA_ID) : null;
65+
} elseif ($serviceMethod === 'changePasswordById'
66+
|| $serviceClass === 'Magento\CustomerGraphQl\Model\Resolver\ChangePassword'
67+
) {
68+
return $this->isEnabled->isCaptchaEnabledFor(self::CHANGE_PASSWORD_CAPTCHA_ID) ?
69+
$this->configResolver->get(self::CHANGE_PASSWORD_CAPTCHA_ID) : null;
70+
}
71+
//phpcs:enable Magento2.PHP.LiteralNamespaces
72+
73+
return null;
74+
}
75+
76+
/**
77+
* Validates ifLoginCaptchaEnabled using captcha_id
78+
*
79+
* @return ValidationConfigInterface|null
80+
*/
81+
private function validateLoginCaptcha(): ?ValidationConfigInterface
82+
{
83+
return $this->isEnabled->isCaptchaEnabledFor(self::LOGIN_CAPTCHA_ID) ?
84+
$this->configResolver->get(self::LOGIN_CAPTCHA_ID) : null;
85+
}
86+
87+
/**
88+
* Validates ifCreateCustomerCaptchaEnabled using captcha_id
89+
*
90+
* @return ValidationConfigInterface|null
91+
*/
92+
private function validateCreateCustomerCaptcha(): ?ValidationConfigInterface
93+
{
94+
return $this->isEnabled->isCaptchaEnabledFor(self::CREATE_CAPTCHA_ID) ?
95+
$this->configResolver->get(self::CREATE_CAPTCHA_ID) : null;
96+
}
97+
4998
/**
5099
* @inheritDoc
51100
*/
@@ -55,38 +104,19 @@ public function getConfigFor(EndpointInterface $endpoint): ?ValidationConfigInte
55104
$serviceMethod = $endpoint->getServiceMethod();
56105

57106
//phpcs:disable Magento2.PHP.LiteralNamespaces
58-
if ($serviceMethod === 'resetPassword'
59-
|| $serviceMethod === 'initiatePasswordReset'
60-
|| $serviceClass === 'Magento\CustomerGraphQl\Model\Resolver\ResetPassword') {
61-
if ($this->isEnabled->isCaptchaEnabledFor(self::RESET_PASSWORD_CAPTCHA_ID)) {
62-
return $this->configResolver->get(self::RESET_PASSWORD_CAPTCHA_ID);
63-
}
64-
} elseif ($serviceMethod === 'changePasswordById'
65-
|| $serviceClass === 'Magento\CustomerGraphQl\Model\Resolver\ChangePassword') {
66-
if ($this->isEnabled->isCaptchaEnabledFor(self::CHANGE_PASSWORD_CAPTCHA_ID)) {
67-
return $this->configResolver->get(self::CHANGE_PASSWORD_CAPTCHA_ID);
68-
}
69-
} elseif (
70-
($serviceClass === 'Magento\Integration\Api\CustomerTokenServiceInterface'
71-
&& $serviceMethod === 'createCustomerAccessToken'
72-
)
107+
if (($serviceClass === 'Magento\Integration\Api\CustomerTokenServiceInterface'
108+
&& $serviceMethod === 'createCustomerAccessToken')
73109
|| $serviceClass === 'Magento\CustomerGraphQl\Model\Resolver\GenerateCustomerToken'
74110
) {
75-
if ($this->isEnabled->isCaptchaEnabledFor(self::LOGIN_CAPTCHA_ID)) {
76-
return $this->configResolver->get(self::LOGIN_CAPTCHA_ID);
77-
}
78-
} elseif (
79-
($serviceClass === 'Magento\Customer\Api\AccountManagementInterface'
80-
&& $serviceMethod === 'createAccount'
81-
)
111+
return $this->validateLoginCaptcha();
112+
} elseif (($serviceClass === 'Magento\Customer\Api\AccountManagementInterface'
113+
&& $serviceMethod === 'createAccount')
82114
|| $serviceClass === 'Magento\CustomerGraphQl\Model\Resolver\CreateCustomer'
83115
) {
84-
if ($this->isEnabled->isCaptchaEnabledFor(self::CREATE_CAPTCHA_ID)) {
85-
return $this->configResolver->get(self::CREATE_CAPTCHA_ID);
86-
}
116+
return $this->validateCreateCustomerCaptcha();
87117
}
88118
//phpcs:enable Magento2.PHP.LiteralNamespaces
89119

90-
return null;
120+
return $this->validateChangePasswordCaptcha($serviceMethod, $serviceClass);
91121
}
92122
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ReCaptchaCustomer\Plugin\Customer;
9+
10+
use Magento\Framework\Exception\InputException;
11+
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
12+
use Magento\Customer\ViewModel\CreateAccountButton;
13+
14+
/**
15+
* Disable button Create Account while captcha is loading
16+
*/
17+
class DisableCreateAccountButton
18+
{
19+
/**
20+
* @var IsCaptchaEnabledInterface
21+
*/
22+
private $isCaptchaEnabled;
23+
24+
/**
25+
* @param IsCaptchaEnabledInterface $isCaptchaEnabled
26+
*/
27+
public function __construct(
28+
IsCaptchaEnabledInterface $isCaptchaEnabled
29+
) {
30+
$this->isCaptchaEnabled = $isCaptchaEnabled;
31+
}
32+
33+
/**
34+
* Temporally disable button Create Account while captcha is loading
35+
*
36+
* @param CreateAccountButton $subject
37+
* @return bool
38+
* @throws InputException
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function afterDisabled(CreateAccountButton $subject): bool
42+
{
43+
$key = 'customer_create';
44+
return $this->isCaptchaEnabled->isCaptchaEnabledFor($key);
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ReCaptchaCustomer\Plugin\Customer;
9+
10+
use Magento\Framework\Exception\InputException;
11+
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
12+
use Magento\ReCaptchaUi\Model\UiConfigResolverInterface;
13+
use Magento\Customer\ViewModel\LoginButton;
14+
15+
/**
16+
* Disable Login button while captcha is loading
17+
*/
18+
class DisableLoginButton
19+
{
20+
/**
21+
* @var IsCaptchaEnabledInterface
22+
*/
23+
private $isCaptchaEnabled;
24+
25+
/**
26+
* @param IsCaptchaEnabledInterface $isCaptchaEnabled
27+
*/
28+
public function __construct(
29+
IsCaptchaEnabledInterface $isCaptchaEnabled
30+
) {
31+
$this->isCaptchaEnabled = $isCaptchaEnabled;
32+
}
33+
34+
/**
35+
* Temporally disable Login button while captcha is loading
36+
*
37+
* @param LoginButton $subject
38+
* @return bool
39+
* @throws InputException
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
*/
42+
public function afterDisabled(LoginButton $subject): bool
43+
{
44+
$key = 'customer_login';
45+
return $this->isCaptchaEnabled->isCaptchaEnabledFor($key);
46+
}
47+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ReCaptchaCustomer\Test\Unit\Plugin\Customer;
9+
10+
use Magento\Customer\ViewModel\CreateAccountButton;
11+
use Magento\ReCaptchaCustomer\Plugin\Customer\DisableCreateAccountButton;
12+
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test disable Login button while captcha is loading
18+
*/
19+
class DisableCreateAccountButtonTest extends TestCase
20+
{
21+
/**
22+
* @var IsCaptchaEnabledInterface|MockObject
23+
*/
24+
protected $isCaptchaEnabled;
25+
26+
/**
27+
* @var CreateAccountButton|MockObject
28+
*/
29+
protected $subject;
30+
31+
/**
32+
* @var DisableCreateAccountButton
33+
*/
34+
protected $plugin;
35+
36+
protected function setUp(): void
37+
{
38+
$this->isCaptchaEnabled = $this->getMockForAbstractClass(
39+
IsCaptchaEnabledInterface::class
40+
);
41+
$this->subject = $this->createMock(CreateAccountButton::class);
42+
43+
$this->plugin = new DisableCreateAccountButton(
44+
$this->isCaptchaEnabled
45+
);
46+
}
47+
48+
public function testAfterEnabled()
49+
{
50+
$key = 'customer_create';
51+
$this->isCaptchaEnabled->expects($this->once())
52+
->method('isCaptchaEnabledFor')->with($key)->willReturn(true);
53+
$this->assertEquals(true, $this->plugin->afterDisabled($this->subject));
54+
}
55+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\ReCaptchaCustomer\Test\Unit\Plugin\Customer;
9+
10+
use Magento\Customer\ViewModel\LoginButton;
11+
use Magento\ReCaptchaCustomer\Plugin\Customer\DisableLoginButton;
12+
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test disable Login button while captcha is loading
18+
*/
19+
class DisableLoginButtonTest extends TestCase
20+
{
21+
/**
22+
* IsCaptcha Enabled mock
23+
*
24+
* @var IsCaptchaEnabledInterface|MockObject
25+
*/
26+
protected $isCaptchaEnabled;
27+
28+
/**
29+
* Subject LoginButton
30+
*
31+
* @var LoginButton|MockObject
32+
*/
33+
protected $subject;
34+
35+
/**
36+
* Tested plugin
37+
*
38+
* @var DisableLoginButtonTest
39+
*/
40+
protected $plugin;
41+
42+
protected function setUp(): void
43+
{
44+
$this->isCaptchaEnabled = $this->getMockForAbstractClass(
45+
IsCaptchaEnabledInterface::class
46+
);
47+
$this->subject = $this->createMock(LoginButton::class);
48+
49+
$this->plugin = new DisableLoginButton(
50+
$this->isCaptchaEnabled
51+
);
52+
}
53+
54+
public function testAfterEnabled()
55+
{
56+
$key = 'customer_login';
57+
$this->isCaptchaEnabled->expects($this->once())
58+
->method('isCaptchaEnabledFor')->with($key)->willReturn(true);
59+
$this->assertEquals(true, $this->plugin->afterDisabled($this->subject));
60+
}
61+
}

ReCaptchaCustomer/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "magento/module-re-captcha-customer",
33
"description": "Google reCAPTCHA integration for Magento2",
44
"require": {
5-
"php": "~7.4.0||~8.0.0||~8.1.0",
5+
"php": "~7.4.0||~8.1.0",
66
"magento/framework": "*",
77
"magento/module-customer": "*",
88
"magento/module-re-captcha-ui": "*",

ReCaptchaCustomer/etc/frontend/di.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,12 @@
2020
<plugin sortOrder="1" name="inject_recaptcha_in_authentication_popup"
2121
type="Magento\ReCaptchaCustomer\Plugin\Block\Account\InjectRecaptchaInAuthenticationPopup"/>
2222
</type>
23-
23+
<type name="Magento\Customer\ViewModel\LoginButton">
24+
<plugin sortOrder="1" name="recaptcha_disable_login_button"
25+
type="Magento\ReCaptchaCustomer\Plugin\Customer\DisableLoginButton"/>
26+
</type>
27+
<type name="Magento\Customer\ViewModel\CreateAccountButton">
28+
<plugin sortOrder="1" name="recaptcha_disable_create_account_button"
29+
type="Magento\ReCaptchaCustomer\Plugin\Customer\DisableCreateAccountButton"/>
30+
</type>
2431
</config>

0 commit comments

Comments
 (0)