Skip to content

Commit d8bcdc0

Browse files
authored
Merge pull request #211 from magento-cia/cia-pre-release-develop-sync-09112025
Cia pre release develop sync 09112025
2 parents 3ae284c + 14c6cf5 commit d8bcdc0

File tree

9 files changed

+521
-119
lines changed

9 files changed

+521
-119
lines changed

ReCaptchaCheckoutSalesRule/Test/Api/CouponApplyFormRecaptchaTest.php

Lines changed: 122 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,50 @@
88

99
namespace Magento\ReCaptchaCheckoutSalesRule\Test\Api;
1010

11+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
12+
use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture;
13+
use Magento\Checkout\Test\Fixture\SetShippingAddress;
14+
use Magento\Customer\Test\Fixture\Customer;
15+
use Magento\Framework\Exception\AuthenticationException;
16+
use Magento\Framework\Exception\EmailNotConfirmedException;
1117
use Magento\Framework\Webapi\Rest\Request;
12-
use Magento\Quote\Model\Quote;
18+
use Magento\Integration\Api\CustomerTokenServiceInterface;
1319
use Magento\Quote\Model\QuoteFactory;
20+
use Magento\Quote\Test\Fixture\AddProductToCart;
21+
use Magento\Quote\Test\Fixture\CustomerCart;
22+
use Magento\Quote\Test\Fixture\GuestCart;
23+
use Magento\Quote\Test\Fixture\QuoteIdMask;
24+
use Magento\SalesRule\Test\Fixture\Rule as SalesRuleFixture;
25+
use Magento\TestFramework\Fixture\Config as ConfigFixture;
26+
use Magento\TestFramework\Fixture\DataFixture;
27+
use Magento\TestFramework\Fixture\DataFixtureStorage;
28+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
29+
use Magento\TestFramework\Helper\Bootstrap;
1430
use Magento\TestFramework\TestCase\WebapiAbstract;
31+
use Throwable;
1532

1633
/**
1734
* Test that Coupon APIs are covered with ReCaptcha
1835
*/
1936
class CouponApplyFormRecaptchaTest extends WebapiAbstract
2037
{
2138
private const API_ROUTE = '/V1/carts/mine/coupons/%s';
22-
private const COUPON_CODE = 'testCoupon';
2339

2440
/**
25-
* @var \Magento\TestFramework\ObjectManager
41+
* @var CustomerTokenServiceInterface
2642
*/
27-
protected $objectManager;
43+
private $customerTokenService;
2844

2945
/**
3046
* @var QuoteFactory
3147
*/
3248
private $quoteFactory;
3349

50+
/**
51+
* @var DataFixtureStorage
52+
*/
53+
private $fixtures;
54+
3455
/**
3556
* @inheritDoc
3657
*/
@@ -39,78 +60,115 @@ protected function setUp(): void
3960
parent::setUp();
4061

4162
$this->_markTestAsRestOnly();
42-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
43-
$this->quoteFactory = $this->objectManager->get(QuoteFactory::class);
63+
$this->quoteFactory = Bootstrap::getObjectManager()->get(QuoteFactory::class);
64+
$this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage();
65+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
4466
}
4567

46-
/**
47-
* @magentoApiDataFixture Magento/Checkout/_files/quote.php
48-
* @magentoApiDataFixture Magento/Customer/_files/customer.php
49-
* @magentoConfigFixture default_store customer/captcha/enable 0
50-
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
51-
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
52-
* @magentoConfigFixture base_website recaptcha_frontend/type_for/coupon_code invisible
53-
*/
68+
#[
69+
ConfigFixture('recaptcha_frontend/type_invisible/public_key', 'test_public_key'),
70+
ConfigFixture('recaptcha_frontend/type_invisible/private_key', 'test_private_key'),
71+
ConfigFixture('recaptcha_frontend/type_for/coupon_code', 'invisible'),
72+
DataFixture(ProductFixture::class, as: 'product'),
73+
DataFixture(Customer::class, as: 'customer'),
74+
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'cart'),
75+
DataFixture(
76+
AddProductToCart::class,
77+
['cart_id' => '$cart.id$', 'product_id' => '$product.id$', 'qty' => 5],
78+
'cart_item'
79+
),
80+
DataFixture(SetShippingAddress::class, ['cart_id' => '$cart.id$']),
81+
DataFixture(
82+
SalesRuleFixture::class,
83+
[
84+
'coupon_code' => 'coupon%uniqid%',
85+
'discount_amount' => 5.00,
86+
'coupon_type' => 2,
87+
'simple_action' => 'by_fixed'
88+
],
89+
'sales_rule'
90+
)
91+
]
5492
public function testRequired(): void
5593
{
56-
$this->expectException(\Throwable::class);
94+
$this->expectException(Throwable::class);
5795
$this->expectExceptionCode(400);
58-
$this->expectExceptionMessage('{"message":"ReCaptcha validation failed, please try again"}');
59-
60-
// get customer ID token
61-
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
62-
$customerTokenService = $this->objectManager->create(
63-
\Magento\Integration\Api\CustomerTokenServiceInterface::class
96+
$this->expectExceptionMessageMatches('/.*ReCaptcha validation failed, please try again.*/');
97+
98+
$this->_webApiCall(
99+
[
100+
'rest' => [
101+
'resourcePath' => sprintf(
102+
self::API_ROUTE,
103+
$this->fixtures->get('sales_rule')->getCouponCode()
104+
),
105+
'httpMethod' => Request::HTTP_METHOD_PUT,
106+
'token' => $this->getCustomerAuthToken(
107+
$this->fixtures->get('customer')->getEmail()
108+
),
109+
],
110+
],
111+
[
112+
'cart_id' => $this->fixtures->get('cart')->getId()
113+
]
64114
);
65-
$token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
66-
67-
/** @var Quote $quote */
68-
$quote = $this->quoteFactory->create();
69-
$quote->load('test_order_1', 'reserved_order_id');
70-
$cartId = $quote->getId();
115+
}
71116

72-
$api_url = sprintf(self::API_ROUTE, self::COUPON_CODE);
73-
$serviceInfo = [
74-
'rest' => [
75-
'resourcePath' => $api_url,
76-
'httpMethod' => Request::HTTP_METHOD_PUT,
77-
'token' => $token,
117+
#[
118+
ConfigFixture('recaptcha_frontend/type_invisible/public_key', 'test_public_key'),
119+
ConfigFixture('recaptcha_frontend/type_invisible/private_key', 'test_private_key'),
120+
ConfigFixture('recaptcha_frontend/type_for/coupon_code', 'invisible'),
121+
DataFixture(ProductFixture::class, as: 'product'),
122+
DataFixture(GuestCart::class, as: 'quote'),
123+
DataFixture(QuoteIdMask::class, ['cart_id' => '$quote.id$'], 'cart_mask'),
124+
DataFixture(SetGuestEmailFixture::class, ['cart_id' => '$quote.id$']),
125+
DataFixture(
126+
AddProductToCart::class,
127+
['cart_id' => '$quote.id$', 'product_id' => '$product.id$', 'qty' => 5],
128+
'cart_item'
129+
),
130+
DataFixture(SetShippingAddress::class, ['cart_id' => '$quote.id$']),
131+
DataFixture(
132+
SalesRuleFixture::class,
133+
[
134+
'coupon_code' => 'coupon%uniqid%',
135+
'discount_amount' => 5.00,
136+
'coupon_type' => 2,
137+
'simple_action' => 'by_fixed'
78138
],
79-
];
80-
$requestData = [
81-
'cart_id' => $cartId
82-
];
83-
84-
$this->_webApiCall($serviceInfo, $requestData);
139+
'sales_rule'
140+
)
141+
]
142+
public function testGuestCartTest(): void
143+
{
144+
$this->expectException(Throwable::class);
145+
$this->expectExceptionCode(400);
146+
$this->expectExceptionMessageMatches('/.*ReCaptcha validation failed, please try again.*/');
147+
148+
$cartId = $this->fixtures->get('cart_mask')->getMaskedId();
149+
$couponCode = $this->fixtures->get('sales_rule')->getCouponCode();
150+
$this->_webApiCall(
151+
[
152+
'rest' => [
153+
'resourcePath' => "/V1/guest-carts/$cartId/coupons/" . $couponCode,
154+
'httpMethod' => Request::HTTP_METHOD_PUT,
155+
'token' => null
156+
],
157+
],
158+
[]
159+
);
85160
}
86161

87162
/**
88-
* @magentoApiDataFixture Magento/Checkout/_files/quote.php
89-
* @magentoConfigFixture default_store customer/captcha/enable 0
90-
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/public_key test_public_key
91-
* @magentoConfigFixture base_website recaptcha_frontend/type_invisible/private_key test_private_key
92-
* @magentoConfigFixture base_website recaptcha_frontend/type_for/coupon_code invisible
163+
* Get customer authentication token
164+
*
165+
* @param string $email
166+
* @return string
167+
* @throws AuthenticationException
168+
* @throws EmailNotConfirmedException
93169
*/
94-
public function testGuestCartTest(): void
170+
private function getCustomerAuthToken(string $email): string
95171
{
96-
$this->expectException(\Throwable::class);
97-
$this->expectExceptionCode(400);
98-
$this->expectExceptionMessage('{"message":"ReCaptcha validation failed, please try again"}');
99-
100-
/** @var Quote $quote */
101-
$quote = $this->quoteFactory->create();
102-
$quote->load('test_order_1', 'reserved_order_id');
103-
$cartId = $quote->getId();
104-
$api_url = "/V1/guest-carts/$cartId/coupons/".self::COUPON_CODE;
105-
106-
$serviceInfo = [
107-
'rest' => [
108-
'resourcePath' => $api_url,
109-
'httpMethod' => Request::HTTP_METHOD_PUT,
110-
'token' => null
111-
],
112-
];
113-
$requestData = [];
114-
$this->_webApiCall($serviceInfo, $requestData);
172+
return $this->customerTokenService->createCustomerAccessToken($email, 'password');
115173
}
116174
}

0 commit comments

Comments
 (0)