Skip to content

Commit a47b41b

Browse files
committed
ACP2E-1053: Generate coupon codes ignores Code Length
- Fixed the CR comments.
1 parent 82d149d commit a47b41b

File tree

6 files changed

+63
-69
lines changed

6 files changed

+63
-69
lines changed

app/code/Magento/SalesRule/Controller/Adminhtml/Promo/Quote/Generate.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
use Magento\Framework\App\Action\HttpPostActionInterface;
99
use Magento\Framework\Filter\FilterInput;
10-
use Magento\SalesRule\Model\CouponGenerator;
1110
use Magento\Framework\MessageQueue\PublisherInterface;
1211
use Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory;
13-
use Magento\SalesRule\Model\Quote\ValidateCouponLengthWithQuantityInterface;
12+
use Magento\SalesRule\Model\CouponGenerator;
13+
use Magento\SalesRule\Model\Quote\GetCouponCodeLengthInterface;
1414

1515
/**
1616
* Generate promo quote
@@ -35,9 +35,9 @@ class Generate extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote imple
3535
private $generationSpecFactory;
3636

3737
/**
38-
* @var ValidateCouponLengthWithQuantityInterface
38+
* @var GetCouponCodeLengthInterface
3939
*/
40-
private $validateCouponLengthWithQuantity;
40+
private $getCouponCodeLength;
4141

4242
/**
4343
* Generate constructor.
@@ -48,7 +48,7 @@ class Generate extends \Magento\SalesRule\Controller\Adminhtml\Promo\Quote imple
4848
* @param CouponGenerator|null $couponGenerator
4949
* @param PublisherInterface|null $publisher
5050
* @param CouponGenerationSpecInterfaceFactory|null $generationSpecFactory
51-
* @param ValidateCouponLengthWithQuantityInterface|null $validateCouponLengthWithQuantity
51+
* @param GetCouponCodeLengthInterface|null $getCouponCodeLength
5252
*/
5353
public function __construct(
5454
\Magento\Backend\App\Action\Context $context,
@@ -58,7 +58,7 @@ public function __construct(
5858
CouponGenerator $couponGenerator = null,
5959
PublisherInterface $publisher = null,
6060
CouponGenerationSpecInterfaceFactory $generationSpecFactory = null,
61-
ValidateCouponLengthWithQuantityInterface $validateCouponLengthWithQuantity = null
61+
GetCouponCodeLengthInterface $getCouponCodeLength = null
6262
) {
6363
parent::__construct($context, $coreRegistry, $fileFactory, $dateFilter);
6464
$this->couponGenerator = $couponGenerator ?:
@@ -69,9 +69,9 @@ public function __construct(
6969
\Magento\Framework\App\ObjectManager::getInstance()->get(
7070
CouponGenerationSpecInterfaceFactory::class
7171
);
72-
$this->validateCouponLengthWithQuantity = $validateCouponLengthWithQuantity ?:
72+
$this->getCouponCodeLength = $getCouponCodeLength ?:
7373
\Magento\Framework\App\ObjectManager::getInstance()->get(
74-
ValidateCouponLengthWithQuantityInterface::class
74+
GetCouponCodeLengthInterface::class
7575
);
7676
}
7777

@@ -92,33 +92,41 @@ public function execute()
9292

9393
$rule = $this->_coreRegistry->registry(\Magento\SalesRule\Model\RegistryConstants::CURRENT_SALES_RULE);
9494

95+
$data = $this->getRequest()->getParams();
96+
9597
if (!$rule->getId()) {
9698
$result['error'] = __('Rule is not defined');
9799
} elseif ((int) $rule->getCouponType() !== \Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO
98100
&& !$rule->getUseAutoGeneration()) {
99101
$result['error'] =
100102
__('The rule coupon settings changed. Please save the rule before using auto-generation.');
101-
} elseif ((int)$this->getRequest()->getParams()['length'] !==
102-
$this->validateCouponLengthWithQuantity->validateCouponCodeLengthWithQuantity(
103-
$this->getRequest()->getParams()
103+
} elseif ((int)$data['length'] !==
104+
$this->getCouponCodeLength->fetchCouponCodeLength(
105+
$data
104106
)
105107
) {
106-
$minimumLength = $this->validateCouponLengthWithQuantity->validateCouponCodeLengthWithQuantity(
107-
$this->getRequest()->getParams()
108-
);
109-
$quantity = $this->getRequest()->getParams()['qty'];
110-
$this->messageManager->addErrorMessage(
111-
__(
112-
'When coupon quantity exceeds %1, the coupon code length must be minimum %2',
113-
$quantity,
114-
$minimumLength
115-
)
116-
);
117-
$this->_view->getLayout()->initMessages();
118-
$result['messages'] = $this->_view->getLayout()->getMessagesBlock()->getGroupedHtml();
108+
try {
109+
$minimumLength = $this->getCouponCodeLength->fetchCouponCodeLength(
110+
$data
111+
);
112+
$quantity = $data['qty'];
113+
$this->messageManager->addErrorMessage(
114+
__(
115+
'When coupon quantity exceeds %1, the coupon code length must be minimum %2',
116+
$quantity,
117+
$minimumLength
118+
)
119+
);
120+
$this->_view->getLayout()->initMessages();
121+
$result['messages'] = $this->_view->getLayout()->getMessagesBlock()->getGroupedHtml();
122+
} catch (\Exception $e) {
123+
$result['error'] = __(
124+
'Something went wrong while validating coupon code length. Please review the log and try again.'
125+
);
126+
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
127+
}
119128
} else {
120129
try {
121-
$data = $this->getRequest()->getParams();
122130
if (!empty($data['to_date'])) {
123131
$inputFilter = new FilterInput(['to_date' => $this->_dateFilter], [], $data);
124132
$data = $inputFilter->getUnescaped();

app/code/Magento/SalesRule/Model/Quote/ValidateCouponLengthWithQuantity.php renamed to app/code/Magento/SalesRule/Model/Quote/GetCouponCodeLength.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
use Magento\SalesRule\Model\Coupon\Massgenerator;
1212

1313
/**
14-
* Validate the coupon code length and quantity.
14+
* The class to get the coupon code length.
1515
*/
16-
class ValidateCouponLengthWithQuantity extends Massgenerator implements ValidateCouponLengthWithQuantityInterface
16+
class GetCouponCodeLength extends Massgenerator implements GetCouponCodeLengthInterface
1717
{
1818
/**
19-
* Validate coupon code length with quantity
19+
* Fetch the coupon code length.
2020
*
2121
* @param array $couponCodeDataArray
2222
* @return int
2323
*/
24-
public function validateCouponCodeLengthWithQuantity(array $couponCodeDataArray): int
24+
public function fetchCouponCodeLength(array $couponCodeDataArray): int
2525
{
2626
$this->setData($couponCodeDataArray);
2727
$this->increaseLength();

app/code/Magento/SalesRule/Model/Quote/ValidateCouponLengthWithQuantityInterface.php renamed to app/code/Magento/SalesRule/Model/Quote/GetCouponCodeLengthInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
namespace Magento\SalesRule\Model\Quote;
1010

1111
/**
12-
* Validate the coupon code length and quantity.
12+
* The class to get the coupon code length.
1313
*/
14-
interface ValidateCouponLengthWithQuantityInterface
14+
interface GetCouponCodeLengthInterface
1515
{
1616
/**
17-
* Validate coupon code length with quantity
17+
* Fetch the coupon code length.
1818
*
1919
* @param array $couponCodeDataArray
2020
* @return int
2121
*/
22-
public function validateCouponCodeLengthWithQuantity(array $couponCodeDataArray): int;
22+
public function fetchCouponCodeLength(array $couponCodeDataArray): int;
2323
}

app/code/Magento/SalesRule/Test/Mftf/Test/AdminValidateCouponCodeLengthWithQuantityTest.xml

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,19 @@
1818
<group value="SalesRule"/>
1919
</annotations>
2020
<before>
21+
<createData entity="SalesRuleSpecificCoupon" stepKey="createSalesRule"/>
2122
<!--Login to backend-->
2223
<actionGroup ref="AdminLoginActionGroup" stepKey="loginAsAdmin"/>
2324
</before>
2425
<after>
25-
<actionGroup ref="AdminDeleteCartPriceRuleActionGroup" stepKey="deleteCartPriceRule">
26-
<argument name="ruleName" value="ApiSalesRule"/>
27-
</actionGroup>
26+
<deleteData createDataKey="createSalesRule" stepKey="deleteSalesRule"/>
2827
<actionGroup ref="AdminLogoutActionGroup" stepKey="logout"/>
2928
</after>
30-
<actionGroup ref="AdminOpenCartPriceRulesPageActionGroup" stepKey="amOnCartPriceList"/>
31-
<click selector="{{AdminCartPriceRulesSection.addNewRuleButton}}" stepKey="clickAddNewRule"/>
32-
<fillField selector="{{AdminCartPriceRulesFormSection.ruleName}}" userInput="{{ApiSalesRule.name}}" stepKey="fillRuleName"/>
33-
<selectOption selector="{{AdminCartPriceRulesFormSection.websites}}" userInput="Main Website" stepKey="selectWebsite"/>
34-
<selectOption selector="{{AdminCartPriceRulesFormSection.customerGroups}}" parameterArray="[{{'NOT LOGGED IN','General','Wholesale','Retailer'}}]" stepKey="selectSpecifiedCustomerGroups"/>
35-
<!-- Choose coupon type specific coupon and tick auto generation checkbox -->
36-
<selectOption selector="{{AdminCartPriceRulesFormSection.coupon}}" userInput="Specific Coupon" stepKey="selectCouponType"/>
37-
<checkOption selector="{{AdminCartPriceRulesFormSection.useAutoGeneration}}" stepKey="tickAutoGeneration"/>
38-
<!-- Navigate to action tab section to fill the apply and discount amount -->
39-
<conditionalClick selector="{{AdminCartPriceRulesFormSection.actionsTab}}"
40-
dependentSelector="{{AdminCartPriceRulesFormSection.actionsTab}}" visible="true"
41-
stepKey="clickActionsTab"/>
42-
<selectOption selector="{{AdminCartPriceRulesFormSection.apply}}" userInput="Percent of product price discount" stepKey="selectActionType"/>
43-
<fillField selector="{{AdminCartPriceRulesFormSection.discountAmount}}" userInput="10" stepKey="fillDiscountAmount"/>
44-
<click selector="{{AdminCartPriceRulesFormSection.saveAndContinue}}" stepKey="ClickSaveAndContinue"/>
45-
<waitForPageLoad stepKey="WaitForRuleSaved"/>
46-
<see userInput="You saved the rule." stepKey="verifyRuleSaved"/>
29+
<!-- Search and go to cart price rule page -->
30+
<actionGroup ref="AdminOpenCartPriceRulesPageActionGroup" stepKey="goToCartPriceRulePage"/>
31+
<actionGroup ref="AdminFilterCartPriceRuleActionGroup" stepKey="filterCartPriceRule">
32+
<argument name="ruleName" value="$$createSalesRule.name$$"/>
33+
</actionGroup>
4734
<!-- Navigate to Manage Coupon Codes section to validate coupon code length and quantity -->
4835
<conditionalClick selector="{{AdminCartPriceRulesFormSection.manageCouponCodesHeader}}"
4936
dependentSelector="{{AdminCartPriceRulesFormSection.manageCouponCodesHeader}}" visible="true"

app/code/Magento/SalesRule/Test/Unit/Controller/Adminhtml/Promo/Quote/GenerateTest.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
use Magento\SalesRule\Api\Data\CouponGenerationSpecInterfaceFactory;
2323
use Magento\SalesRule\Controller\Adminhtml\Promo\Quote\Generate;
2424
use Magento\SalesRule\Model\CouponGenerator;
25+
use Magento\SalesRule\Model\Quote\GetCouponCodeLengthInterface;
2526
use Magento\SalesRule\Model\Rule;
2627
use PHPUnit\Framework\MockObject\MockObject;
2728
use PHPUnit\Framework\TestCase;
28-
use Magento\SalesRule\Model\Quote\ValidateCouponLengthWithQuantityInterface;
2929

3030
/**
3131
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -72,9 +72,9 @@ class GenerateTest extends TestCase
7272
private $couponGenerationSpec;
7373

7474
/**
75-
* @var ValidateCouponLengthWithQuantityInterface|MockObject
75+
* @var GetCouponCodeLengthInterface|MockObject
7676
*/
77-
private $validateCouponLengthWithQuantity;
77+
private $getCouponCodeLength;
7878

7979
/**
8080
* Test setup
@@ -127,8 +127,8 @@ protected function setUp(): void
127127
$this->couponGenerationSpec = $this->getMockBuilder(CouponGenerationSpecInterfaceFactory::class)
128128
->disableOriginalConstructor()
129129
->getMock();
130-
$this->validateCouponLengthWithQuantity = $this->getMockBuilder(
131-
ValidateCouponLengthWithQuantityInterface::class
130+
$this->getCouponCodeLength = $this->getMockBuilder(
131+
GetCouponCodeLengthInterface::class
132132
)
133133
->disableOriginalConstructor()
134134
->getMock();
@@ -143,7 +143,7 @@ protected function setUp(): void
143143
'dateFilter' => $this->dateMock,
144144
'couponGenerator' => $this->couponGenerator,
145145
'generationSpecFactory' => $this->couponGenerationSpec,
146-
'validateCouponLengthWithQuantity' => $this->validateCouponLengthWithQuantity
146+
'getCouponCodeLength' => $this->getCouponCodeLength
147147
]
148148
);
149149
}
@@ -182,16 +182,16 @@ public function testExecuteWithCouponTypeAuto()
182182
$ruleMock->expects($this->once())
183183
->method('getCouponType')
184184
->willReturn(\Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO);
185-
$this->requestMock->expects($this->exactly(3))
185+
$this->requestMock->expects($this->once())
186186
->method('getParams')
187187
->willReturn($requestData);
188188
$requestData['quantity'] = isset($requestData['qty']) ? $requestData['qty'] : null;
189189
$this->couponGenerationSpec->expects($this->once())
190190
->method('create')
191191
->with(['data' => $requestData])
192192
->willReturn(['some_data', 'some_data_2']);
193-
$this->validateCouponLengthWithQuantity->expects($this->once())
194-
->method('validateCouponCodeLengthWithQuantity')
193+
$this->getCouponCodeLength->expects($this->once())
194+
->method('fetchCouponCodeLength')
195195
->willReturn(10);
196196
$this->messageManager->expects($this->once())
197197
->method('addSuccessMessage');
@@ -257,10 +257,10 @@ public function testExecuteWithAutoGenerationEnabled()
257257
$ruleMock->expects($this->once())
258258
->method('getUseAutoGeneration')
259259
->willReturn(1);
260-
$this->validateCouponLengthWithQuantity->expects($this->once())
261-
->method('validateCouponCodeLengthWithQuantity')
260+
$this->getCouponCodeLength->expects($this->once())
261+
->method('fetchCouponCodeLength')
262262
->willReturn(10);
263-
$this->requestMock->expects($this->exactly(3))
263+
$this->requestMock->expects($this->once())
264264
->method('getParams')
265265
->willReturn($requestData);
266266
$requestData['quantity'] = isset($requestData['qty']) ? $requestData['qty'] : null;
@@ -336,8 +336,7 @@ public function testExecuteWithCouponTypeNotAutoAndAutoGenerationNotEnabled()
336336
$helperData->expects($this->once())
337337
->method('jsonEncode')
338338
->with([
339-
'error' =>
340-
__('The rule coupon settings changed. Please save the rule before using auto-generation.')
339+
'error' => __('The rule coupon settings changed. Please save the rule before using auto-generation.')
341340
]);
342341
$this->model->execute();
343342
}

app/code/Magento/SalesRule/etc/adminhtml/di.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323
for="Magento\SalesRule\Model\Spi\CodeLimitManagerInterface"
2424
type="Magento\SalesRule\Model\Coupon\AdminCodeLimitManager" />
2525
<preference
26-
for="Magento\SalesRule\Model\Quote\ValidateCouponLengthWithQuantityInterface"
27-
type="Magento\SalesRule\Model\Quote\ValidateCouponLengthWithQuantity" />
26+
for="Magento\SalesRule\Model\Quote\GetCouponCodeLengthInterface"
27+
type="Magento\SalesRule\Model\Quote\GetCouponCodeLength" />
2828
</config>

0 commit comments

Comments
 (0)