Skip to content

Commit db890be

Browse files
Mikhaël Boisericeoeur
andauthored
[PWA-2063] Add reCaptcha to the Forgot Password Forms (#12)
* PWA-2063 Plugin to add missing config * PWA-2063 Add unit tests * PWA-2063 Update namespace * PWA-2063 Clean code * PWA-2063 Fix deps * PWA-2063 Fix deps + Add phpmd ignore Co-authored-by: Eric Oeur <31492142+ericeoeur@users.noreply.github.com>
1 parent 9fff323 commit db890be

File tree

6 files changed

+231
-7
lines changed

6 files changed

+231
-7
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\ReCaptchaPwa\Plugin\Model;
9+
10+
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
11+
use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface;
12+
use Magento\ReCaptchaValidationApi\Api\Data\ValidationConfigInterface;
13+
use Magento\ReCaptchaWebapiApi\Api\Data\EndpointInterface;
14+
15+
class WebapiConfigProvider
16+
{
17+
private const RESET_PASSWORD_CAPTCHA_ID = 'customer_forgot_password';
18+
19+
/**
20+
* @var IsCaptchaEnabledInterface
21+
*/
22+
private $isEnabled;
23+
24+
/**
25+
* @var ValidationConfigResolverInterface
26+
*/
27+
private $configResolver;
28+
29+
/**
30+
* @param IsCaptchaEnabledInterface $isEnabled
31+
* @param ValidationConfigResolverInterface $configResolver
32+
*/
33+
public function __construct(IsCaptchaEnabledInterface $isEnabled, ValidationConfigResolverInterface $configResolver)
34+
{
35+
$this->isEnabled = $isEnabled;
36+
$this->configResolver = $configResolver;
37+
}
38+
39+
/**
40+
* Adds missing config
41+
*
42+
* @param \Magento\ReCaptchaCustomer\Model\WebapiConfigProvider $subject
43+
* @param ValidationConfigInterface|null $result
44+
* @param EndpointInterface $endpoint
45+
* @return ValidationConfigInterface|null
46+
* @throws \Magento\Framework\Exception\InputException
47+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
48+
*/
49+
public function afterGetConfigFor(
50+
\Magento\ReCaptchaCustomer\Model\WebapiConfigProvider $subject,
51+
$result,
52+
EndpointInterface $endpoint
53+
): ?ValidationConfigInterface {
54+
$serviceClass = $endpoint->getServiceClass();
55+
$serviceMethod = $endpoint->getServiceMethod();
56+
57+
//phpcs:disable Magento2.PHP.LiteralNamespaces
58+
if ($serviceMethod === 'resetPassword'
59+
|| $serviceMethod === 'initiatePasswordReset'
60+
|| $serviceClass === 'Magento\CustomerGraphQl\Model\Resolver\ResetPassword'
61+
|| $serviceClass === 'Magento\CustomerGraphQl\Model\Resolver\RequestPasswordResetEmail') {
62+
if ($this->isEnabled->isCaptchaEnabledFor(self::RESET_PASSWORD_CAPTCHA_ID)) {
63+
return $this->configResolver->get(self::RESET_PASSWORD_CAPTCHA_ID);
64+
}
65+
}
66+
//phpcs:enable Magento2.PHP.LiteralNamespaces
67+
68+
return $result;
69+
}
70+
}

ReCaptchaPwa/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Magento_ReCaptchaPwa
22

3-
**Magento_ReCaptchaPwa** adds warnings to Magento ReCaptcha modules.
3+
**Magento_ReCaptchaPwa** adds warnings to Magento ReCaptcha modules and adds config validation for the Request Password Reset Form.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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\ReCaptchaPwa\Test\Unit\Plugin\Model;
9+
10+
use Magento\ReCaptchaWebapiApi\Api\Data\EndpointInterface;
11+
use PHPUnit\Framework\TestCase;
12+
use PHPUnit\Framework\MockObject\MockObject;
13+
use Magento\ReCaptchaCustomer\Model\WebapiConfigProvider;
14+
use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface;
15+
use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface;
16+
use Magento\ReCaptchaValidationApi\Api\Data\ValidationConfigInterface;
17+
use Magento\ReCaptchaPwa\Plugin\Model\WebapiConfigProvider as WebapiConfigProviderPlugin;
18+
19+
class WebapiConfigProviderTest extends TestCase
20+
{
21+
/**
22+
* @var WebapiConfigProviderPlugin
23+
*/
24+
protected $model;
25+
26+
/**
27+
* @var MockObject
28+
*/
29+
protected $isCaptchaEnabledInterfaceMock;
30+
31+
/**
32+
* @var MockObject
33+
*/
34+
protected $validationConfigResolverInterfaceMock;
35+
36+
/**
37+
* @var MockObject
38+
*/
39+
protected $validationConfigInterfaceMock;
40+
41+
protected function setUp(): void
42+
{
43+
$this->isCaptchaEnabledInterfaceMock = $this->createMock(IsCaptchaEnabledInterface::class);
44+
$this->validationConfigResolverInterfaceMock = $this->createMock(ValidationConfigResolverInterface::class);
45+
46+
$this->validationConfigInterfaceMock = $this->createMock(ValidationConfigInterface::class);
47+
48+
$this->model = new WebapiConfigProviderPlugin(
49+
$this->isCaptchaEnabledInterfaceMock,
50+
$this->validationConfigResolverInterfaceMock
51+
);
52+
}
53+
54+
public function testAfterGetConfigForResetPasswordForm()
55+
{
56+
$this->isCaptchaEnabledInterfaceMock
57+
->expects($this->once())
58+
->method('isCaptchaEnabledFor')
59+
->willReturn(true);
60+
$this->validationConfigResolverInterfaceMock
61+
->expects($this->once())
62+
->method('get')
63+
->willReturn($this->validationConfigInterfaceMock);
64+
65+
$webapiConfigProviderMock = $this->createMock(WebapiConfigProvider::class);
66+
$resultMock = null;
67+
$endpointInterfaceMock = $this->createMock(EndpointInterface::class);
68+
$endpointInterfaceMock
69+
->expects($this->once())
70+
->method('getServiceMethod')
71+
->willReturn('resetPassword');
72+
$endpointInterfaceMock
73+
->expects($this->once())
74+
->method('getServiceClass')
75+
->willReturn('Magento\CustomerGraphQl\Model\Resolver\ResetPassword');
76+
77+
$this->assertEquals(
78+
$this->validationConfigInterfaceMock,
79+
$this->model->afterGetConfigFor($webapiConfigProviderMock, $resultMock, $endpointInterfaceMock)
80+
);
81+
}
82+
83+
public function testAfterGetConfigForPasswordResetForm()
84+
{
85+
$this->isCaptchaEnabledInterfaceMock
86+
->expects($this->once())
87+
->method('isCaptchaEnabledFor')
88+
->willReturn(true);
89+
$this->validationConfigResolverInterfaceMock
90+
->expects($this->once())
91+
->method('get')
92+
->willReturn($this->validationConfigInterfaceMock);
93+
94+
$webapiConfigProviderMock = $this->createMock(WebapiConfigProvider::class);
95+
$resultMock = null;
96+
$endpointInterfaceMock = $this->createMock(EndpointInterface::class);
97+
$endpointInterfaceMock
98+
->expects($this->once())
99+
->method('getServiceMethod')
100+
->willReturn('initiatePasswordReset');
101+
$endpointInterfaceMock
102+
->expects($this->once())
103+
->method('getServiceClass')
104+
->willReturn('Magento\CustomerGraphQl\Model\Resolver\RequestPasswordResetEmail');
105+
106+
$this->assertEquals(
107+
$this->validationConfigInterfaceMock,
108+
$this->model->afterGetConfigFor($webapiConfigProviderMock, $resultMock, $endpointInterfaceMock)
109+
);
110+
}
111+
112+
public function testAfterGetConfigForFakeForm()
113+
{
114+
$webapiConfigProviderMock = $this->createMock(WebapiConfigProvider::class);
115+
$resultMock = null;
116+
$endpointInterfaceMock = $this->createMock(EndpointInterface::class);
117+
$endpointInterfaceMock
118+
->expects($this->once())
119+
->method('getServiceMethod')
120+
->willReturn('fakeForm');
121+
$endpointInterfaceMock
122+
->expects($this->once())
123+
->method('getServiceClass')
124+
->willReturn('Magento\CustomerGraphQl\Model\Resolver\FakeForm');
125+
126+
$this->assertEquals(
127+
$resultMock,
128+
$this->model->afterGetConfigFor($webapiConfigProviderMock, $resultMock, $endpointInterfaceMock)
129+
);
130+
}
131+
}

ReCaptchaPwa/composer.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "magento/module-re-captcha-pwa",
3-
"description": "Adds warnings to Magento ReCaptcha modules.",
3+
"description": "Adds warnings and config validation to Magento ReCaptcha modules.",
44
"type": "magento2-module",
55
"license": [
66
"OSL-3.0",
@@ -11,10 +11,13 @@
1111
},
1212
"require": {
1313
"php": "~7.3.0||~7.4.0",
14-
"magento/framework": "*"
15-
},
16-
"suggest": {
17-
"magento/security-package": "*"
14+
"magento/framework": "*",
15+
"magento/module-config": "*",
16+
"magento/module-customer-graph-ql": "*",
17+
"magento/module-re-captcha-customer": "*",
18+
"magento/module-re-captcha-ui": "*",
19+
"magento/module-re-captcha-validation-api": "*",
20+
"magento/module-re-captcha-webapi-api": "*"
1821
},
1922
"autoload": {
2023
"files": [
@@ -25,4 +28,3 @@
2528
}
2629
}
2730
}
28-

ReCaptchaPwa/etc/di.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10+
<type
11+
name="Magento\ReCaptchaCustomer\Model\WebapiConfigProvider">
12+
<plugin name="update-recaptcha-config-provider"
13+
type="Magento\ReCaptchaPwa\Plugin\Model\WebapiConfigProvider"/>
14+
</type>
15+
</config>

ReCaptchaPwa/etc/module.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
1010
<module name="Magento_ReCaptchaPwa">
1111
<sequence>
12+
<module name="Magento_Config"/>
13+
<module name="Magento_CustomerGraphQl"/>
14+
<module name="Magento_ReCaptchaUi"/>
1215
<module name="Magento_ReCaptchaAdminUi"/>
16+
<module name="Magento_ReCaptchaCustomer"/>
17+
<module name="Magento_ReCaptchaValidationApi"/>
1318
<module name="Magento_ReCaptchaVersion2Checkbox"/>
1419
<module name="Magento_ReCaptchaVersion2Invisible"/>
20+
<module name="Magento_ReCaptchaWebapiApi"/>
1521
</sequence>
1622
</module>
1723
</config>

0 commit comments

Comments
 (0)