Skip to content

Commit c0a8275

Browse files
committed
Merge branch 'ACP2E-2428' of https://github.com/magento-l3/magento2ce into PR-VK-2023-09-29
2 parents 42d0637 + 2bfb486 commit c0a8275

File tree

5 files changed

+217
-12
lines changed

5 files changed

+217
-12
lines changed

app/code/Magento/Quote/Model/CouponManagement.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@
1313
use Magento\Framework\Exception\CouldNotSaveException;
1414
use Magento\Framework\Exception\NoSuchEntityException;
1515

16-
/**
17-
* Coupon management object.
18-
*/
1916
class CouponManagement implements CouponManagementInterface
2017
{
2118
/**
22-
* Quote repository.
23-
*
2419
* @var \Magento\Quote\Api\CartRepositoryInterface
2520
*/
2621
protected $quoteRepository;
@@ -51,6 +46,7 @@ public function get($cartId)
5146
*/
5247
public function set($cartId, $couponCode)
5348
{
49+
$couponCode = trim($couponCode);
5450
/** @var \Magento\Quote\Model\Quote $quote */
5551
$quote = $this->quoteRepository->getActive($cartId);
5652
if (!$quote->getItemsCount()) {

app/code/Magento/Quote/Model/GuestCart/GuestCouponManagement.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(
4141
}
4242

4343
/**
44-
* {@inheritdoc}
44+
* @inheritdoc
4545
*/
4646
public function get($cartId)
4747
{
@@ -51,17 +51,17 @@ public function get($cartId)
5151
}
5252

5353
/**
54-
* {@inheritdoc}
54+
* @inheritdoc
5555
*/
5656
public function set($cartId, $couponCode)
5757
{
5858
/** @var $quoteIdMask QuoteIdMask */
5959
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
60-
return $this->couponManagement->set($quoteIdMask->getQuoteId(), $couponCode);
60+
return $this->couponManagement->set($quoteIdMask->getQuoteId(), trim($couponCode));
6161
}
6262

6363
/**
64-
* {@inheritdoc}
64+
* @inheritdoc
6565
*/
6666
public function remove($cartId)
6767
{

app/code/Magento/Quote/Test/Unit/Model/GuestCart/GuestCouponManagementTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function setUp(): void
5656
$objectManager = new ObjectManager($this);
5757
$this->couponManagementMock = $this->getMockForAbstractClass(CouponManagementInterface::class);
5858

59-
$this->couponCode = 'test_coupon_code';
59+
$this->couponCode = ' test_coupon_code';
6060
$this->maskedCartId = 'f216207248d65c789b17be8545e0aa73';
6161
$this->cartId = 123;
6262

@@ -83,7 +83,10 @@ public function testGet()
8383

8484
public function testSet()
8585
{
86-
$this->couponManagementMock->expects($this->once())->method('set')->willReturn(true);
86+
$this->couponManagementMock->expects($this->once())
87+
->method('set')
88+
->with($this->cartId, trim($this->couponCode))
89+
->willReturn(true);
8790
$this->assertTrue($this->model->set($this->maskedCartId, $this->couponCode));
8891
}
8992

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
5+
*
6+
* ADOBE CONFIDENTIAL
7+
*
8+
* NOTICE: All information contained herein is, and remains
9+
* the property of Adobe and its suppliers, if any. The intellectual
10+
* and technical concepts contained herein are proprietary to Adobe
11+
* and its suppliers and are protected by all applicable intellectual
12+
* property laws, including trade secret and copyright laws.
13+
* Dissemination of this information or reproduction of this material
14+
* is strictly forbidden unless prior written permission is obtained
15+
* from Adobe.
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Quote\Test\Unit\Model\Quote;
20+
21+
use Magento\Framework\Exception\CouldNotSaveException;
22+
use Magento\Framework\Exception\NoSuchEntityException;
23+
use Magento\Quote\Api\CartRepositoryInterface;
24+
use Magento\Quote\Model\CouponManagement;
25+
use Magento\Quote\Model\Quote;
26+
use Magento\Quote\Model\Quote\Address;
27+
use PHPUnit\Framework\MockObject\MockObject;
28+
use PHPUnit\Framework\TestCase;
29+
30+
class CouponManagementTest extends TestCase
31+
{
32+
/**
33+
* @var CartRepositoryInterface|MockObject
34+
*/
35+
private CartRepositoryInterface|MockObject $quoteRepository;
36+
37+
/**
38+
* @var CouponManagement
39+
*/
40+
private CouponManagement $couponManagement;
41+
42+
protected function setUp(): void
43+
{
44+
$this->quoteRepository = $this->getMockForAbstractClass(CartRepositoryInterface::class);
45+
$this->couponManagement = new CouponManagement($this->quoteRepository);
46+
47+
parent::setUp();
48+
}
49+
50+
/**
51+
* @return void
52+
* @throws CouldNotSaveException
53+
* @throws NoSuchEntityException
54+
*/
55+
public function testSetCouponSuccess(): void
56+
{
57+
$cartId = 1;
58+
$couponCode = ' code ';
59+
60+
$shippingAddress = $this->getShippingAddressMock();
61+
$shippingAddress->expects($this->once())->method('setCollectShippingRates')->with(true);
62+
$quote = $this->getMockBuilder(Quote::class)
63+
->disableOriginalConstructor()
64+
->addMethods(['setCouponCode', 'getCouponCode'])
65+
->onlyMethods(['getItemsCount', 'getStoreId', 'getShippingAddress', 'collectTotals'])
66+
->getMock();
67+
$quote->expects($this->once())->method('getItemsCount')->willReturn(2);
68+
$quote->expects($this->once())->method('getStoreId')->willReturn(1);
69+
$quote->expects($this->once())->method('getShippingAddress')->willReturn($shippingAddress);
70+
$quote->expects($this->once())->method('setCouponCode')->with(trim($couponCode));
71+
$quote->expects($this->once())->method('collectTotals')->willReturnSelf();
72+
$quote->expects($this->once())->method('getCouponCode')->willReturn(trim($couponCode));
73+
$this->quoteRepository->expects($this->once())->method('getActive')->with($cartId)->willReturn($quote);
74+
$this->quoteRepository->expects($this->once())->method('save');
75+
76+
$this->assertTrue($this->couponManagement->set($cartId, $couponCode));
77+
}
78+
79+
/**
80+
* @return void
81+
* @throws CouldNotSaveException
82+
* @throws NoSuchEntityException
83+
*/
84+
public function testSetCouponNoEntityExceptionProducts(): void
85+
{
86+
$cartId = 1;
87+
$couponCode = ' code ';
88+
89+
$this->expectException(NoSuchEntityException::class);
90+
$this->expectExceptionMessage('The "' . $cartId . '" Cart doesn\'t contain products.');
91+
92+
$quote = $this->getMockBuilder(Quote::class)
93+
->disableOriginalConstructor()
94+
->addMethods(['setCouponCode', 'getCouponCode'])
95+
->onlyMethods(['getItemsCount', 'getStoreId', 'getShippingAddress', 'collectTotals'])
96+
->getMock();
97+
$quote->expects($this->once())->method('getItemsCount')->willReturn(0);
98+
$this->quoteRepository->expects($this->once())->method('getActive')->with($cartId)->willReturn($quote);
99+
100+
$this->couponManagement->set($cartId, $couponCode);
101+
}
102+
103+
/**
104+
* @return void
105+
* @throws CouldNotSaveException
106+
* @throws NoSuchEntityException
107+
*/
108+
public function testSetCouponNoEntityExceptionStore(): void
109+
{
110+
$cartId = 1;
111+
$couponCode = ' code ';
112+
113+
$this->expectException(NoSuchEntityException::class);
114+
$this->expectExceptionMessage('Cart isn\'t assigned to correct store');
115+
116+
$quote = $this->getMockBuilder(Quote::class)
117+
->disableOriginalConstructor()
118+
->addMethods(['setCouponCode', 'getCouponCode'])
119+
->onlyMethods(['getItemsCount', 'getStoreId', 'getShippingAddress', 'collectTotals'])
120+
->getMock();
121+
$quote->expects($this->once())->method('getItemsCount')->willReturn(1);
122+
$quote->expects($this->once())->method('getStoreId')->willReturn(0);
123+
$this->quoteRepository->expects($this->once())->method('getActive')->with($cartId)->willReturn($quote);
124+
125+
$this->couponManagement->set($cartId, $couponCode);
126+
}
127+
128+
/**
129+
* @return void
130+
* @throws CouldNotSaveException
131+
* @throws NoSuchEntityException
132+
*/
133+
public function testSetCouponExceptionSave(): void
134+
{
135+
$cartId = 1;
136+
$couponCode = ' code ';
137+
138+
$this->expectException(CouldNotSaveException::class);
139+
$this->expectExceptionMessage("The coupon code couldn't be applied. Verify the coupon code and try again.");
140+
141+
$shippingAddress = $this->getShippingAddressMock();
142+
$shippingAddress->expects($this->once())->method('setCollectShippingRates')->with(true);
143+
$quote = $this->getMockBuilder(Quote::class)
144+
->disableOriginalConstructor()
145+
->addMethods(['setCouponCode', 'getCouponCode'])
146+
->onlyMethods(['getItemsCount', 'getStoreId', 'getShippingAddress', 'collectTotals'])
147+
->getMock();
148+
$quote->expects($this->once())->method('getItemsCount')->willReturn(2);
149+
$quote->expects($this->once())->method('getStoreId')->willReturn(1);
150+
$quote->expects($this->once())->method('getShippingAddress')->willReturn($shippingAddress);
151+
$quote->expects($this->once())->method('setCouponCode')->with(trim($couponCode));
152+
$quote->expects($this->once())->method('collectTotals')->willReturnSelf();
153+
$this->quoteRepository->expects($this->once())->method('getActive')->with($cartId)->willReturn($quote);
154+
$this->quoteRepository->expects($this->once())->method('save')->willThrowException(new \Exception());
155+
156+
$this->couponManagement->set($cartId, $couponCode);
157+
}
158+
159+
/**
160+
* @return void
161+
* @throws CouldNotSaveException
162+
* @throws NoSuchEntityException
163+
*/
164+
public function testSetCouponNoEntityExceptionCoupon(): void
165+
{
166+
$cartId = 1;
167+
$couponCode = ' code ';
168+
169+
$this->expectException(NoSuchEntityException::class);
170+
$this->expectExceptionMessage("The coupon code isn't valid. Verify the code and try again.");
171+
172+
$shippingAddress = $this->getShippingAddressMock();
173+
$shippingAddress->expects($this->once())->method('setCollectShippingRates')->with(true);
174+
$quote = $this->getMockBuilder(Quote::class)
175+
->disableOriginalConstructor()
176+
->addMethods(['setCouponCode', 'getCouponCode'])
177+
->onlyMethods(['getItemsCount', 'getStoreId', 'getShippingAddress', 'collectTotals'])
178+
->getMock();
179+
$quote->expects($this->once())->method('getItemsCount')->willReturn(2);
180+
$quote->expects($this->once())->method('getStoreId')->willReturn(1);
181+
$quote->expects($this->once())->method('getShippingAddress')->willReturn($shippingAddress);
182+
$quote->expects($this->once())->method('setCouponCode')->with(trim($couponCode));
183+
$quote->expects($this->once())->method('collectTotals')->willReturnSelf();
184+
$quote->expects($this->once())->method('getCouponCode')->willReturn(null);
185+
$this->quoteRepository->expects($this->once())->method('getActive')->with($cartId)->willReturn($quote);
186+
$this->quoteRepository->expects($this->once())->method('save');
187+
188+
$this->couponManagement->set($cartId, $couponCode);
189+
}
190+
191+
/**
192+
* @return MockObject
193+
*/
194+
private function getShippingAddressMock(): MockObject
195+
{
196+
return $this->getMockBuilder(Address::class)
197+
->disableOriginalConstructor()
198+
->addMethods(['setCollectShippingRates'])
199+
->getMock();
200+
}
201+
}

app/code/Magento/SalesRule/view/frontend/web/js/view/payment/discount.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ define([
5959
* @returns {Boolean}
6060
*/
6161
validate: function () {
62-
var form = '#discount-form';
62+
let form = '#discount-form';
6363

64+
$(form + ' input[type="text"]').each(function () {
65+
let currentValue = $(this).val();
66+
67+
$(this).val(currentValue.trim());
68+
});
6469
return $(form).validation() && $(form).validation('isValid');
6570
}
6671
});

0 commit comments

Comments
 (0)