Skip to content

Commit 0acc76a

Browse files
committed
AC-461: Add reCAPTCHA support to coupon code
* Updated test case in Integration test * Fixed review comments
1 parent da139fc commit 0acc76a

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

ReCaptchaCheckoutSalesRule/Model/WebapiConfigProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ public function getConfigFor(EndpointInterface $endpoint): ?ValidationConfigInte
5151
$endpoint->getServiceMethod() === 'set')
5252
|| ($endpoint->getServiceClass() === 'Magento\QuoteGraphQl\Model\Resolver\ApplyCouponToCart'
5353
|| $endpoint->getServiceMethod() === 'ApplyCouponToCart')
54-
|| ($endpoint->getServiceClass() === 'Magento\Checkout\Controller\Cart\CouponPost'
55-
&& $endpoint->getServiceMethod() === 'execute')
5654
) {
5755
if ($this->isEnabled->isCaptchaEnabledFor(self::CAPTCHA_ID)) {
5856
return $this->configResolver->get(self::CAPTCHA_ID);

ReCaptchaCheckoutSalesRule/Test/Integration/CouponApplyPostTest.php

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
namespace Magento\ReCaptchaCheckoutSalesRule\Test\Integration;
99

1010
use Magento\Customer\Model\Session;
11+
use Magento\Checkout\Model\Session as CheckoutSession;
1112
use Magento\Framework\App\Request\Http;
1213
use Magento\Framework\Message\MessageInterface;
1314
use Magento\TestFramework\TestCase\AbstractController;
1415
use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface;
15-
use Magento\Framework\UrlInterface;
1616

1717
/**
18-
* Tests for create wish list
18+
* Tests for Coupon Post form
1919
*
2020
* @magentoDataFixture Magento/Customer/_files/customer.php
2121
* @magentoDbIsolation enabled
@@ -29,14 +29,16 @@ class CouponApplyPostTest extends AbstractController
2929
private const CUSTOMER_ID = 1;
3030

3131
/**
32+
* Customer session
3233
* @var Session
3334
*/
3435
private $customerSession;
3536

3637
/**
37-
* @var UrlInterface
38+
* Checkout Session
39+
* @var Session
3840
*/
39-
private $url;
41+
private $checkoutSession;
4042

4143
/**
4244
* @inheritdoc
@@ -46,7 +48,25 @@ protected function setUp(): void
4648
parent::setUp();
4749
$this->customerSession = $this->_objectManager->get(Session::class);
4850
$this->customerSession->setCustomerId(self::CUSTOMER_ID);
49-
$this->url = $this->_objectManager->get(UrlInterface::class);
51+
$this->checkoutSession = $this->_objectManager->create(CheckoutSession::class);
52+
}
53+
54+
/**
55+
* Verifying that recaptcha is present on the CouponPost form/page and keys are configured
56+
*
57+
* @magentoDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
58+
* @magentoDataFixture Magento/Usps/Fixtures/cart_rule_coupon_free_shipping.php
59+
*
60+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
61+
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
62+
* @magentoConfigFixture base_website recaptcha_frontend/type_for/coupon_code invisible
63+
* @magentoConfigFixture default_store recaptcha_frontend/type_for/coupon_code invisible
64+
*/
65+
public function testGetRequestIfReCaptchaIsEnabled(): void
66+
{
67+
$quote = $this->checkoutSession->getQuote();
68+
$quote->setData('trigger_recollect', 1)->setTotalsCollectedFlag(true);
69+
$this->checkSuccessfulGetResponse(true);
5070
}
5171

5272
/**
@@ -78,10 +98,28 @@ public function testPostRequestIfReCaptchaParameterIsMissed(): void
7898
public function testPostRequestWithFailedReCaptchaValidation(): void
7999
{
80100
$this->checkFailedPostRequest(true);
81-
$this->assertSessionMessages(
82-
$this->equalTo(['Something went wrong with reCAPTCHA. Please contact the store owner.']),
83-
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
84-
);
101+
}
102+
103+
/**
104+
* Checks GET response
105+
*
106+
* @param bool $shouldContainReCaptcha
107+
* @return void
108+
*/
109+
private function checkSuccessfulGetResponse(bool $shouldContainReCaptcha = false): void
110+
{
111+
$this->getRequest()->setMethod(Http::METHOD_GET);
112+
$this->dispatch('checkout/cart/');
113+
$response = $this->getResponse();
114+
$content = $response->getContent();
115+
116+
$this->assertNotEmpty($content);
117+
$shouldContainReCaptcha
118+
? $this->assertStringContainsString('field-recaptcha', $content)
119+
: $this->assertStringNotContainsString('field-recaptcha', $content);
120+
121+
$this->assertEmpty($this->getSessionMessages(
122+
\Magento\Framework\Message\MessageInterface::TYPE_ERROR));
85123
}
86124

87125
/**

0 commit comments

Comments
 (0)