Skip to content

Commit e121fba

Browse files
faraz-glsreenia806
authored andcommitted
AC-8753: Coupon quantity generation checks
* Disable option for coupon quantities added
1 parent 7a97ee2 commit e121fba

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@ public function execute(): void
166166
self::XML_CONFIG_COUPON_QUANTITY_LIMIT,
167167
ScopeInterface::SCOPE_STORE
168168
);
169-
if ($data['quantity'] > 0 && $data['quantity'] <= $couponQuantityLimit) {
169+
// @codingStandardsIgnoreStart
170+
if ($data['quantity'] > 0 && ($data['quantity'] <= $couponQuantityLimit || $couponQuantityLimit === 0)) {
170171
$couponSpec = $this->generationSpecFactory->create(['data' => $data]);
171172

172173
$this->messagePublisher->publish('sales_rule.codegenerator', $couponSpec);
173174
$this->messageManager->addSuccessMessage(
174-
__('Message is added to queue, wait to get your coupons soon')
175+
__('Message is added to queue, wait to get your coupons soon.')
175176
);
176177
} else {
177-
// @codingStandardsIgnoreStart
178178
$this->messageManager->addErrorMessage(
179179
__(
180180
'Coupon quantity should be less than or equal to the coupon quantity in the store configuration.'

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class GenerateTest extends TestCase
4444
/** @const XML_COUPON_QUANTITY_LIMIT_VALUE_TEST */
4545
private const XML_COUPON_QUANTITY_LIMIT_VALUE_TEST = 250000;
4646

47+
/** @const XML_COUPON_QUANTITY_LIMIT_DISABLE_VALUE_TEST */
48+
private const XML_COUPON_QUANTITY_LIMIT_DISABLE_VALUE_TEST = 0;
49+
4750
/**
4851
* @var array
4952
*/
@@ -446,4 +449,79 @@ public function testExecuteWithInvalidCouponQuantity()
446449
->willReturn(__('Coupon qty should be less than or equal to the coupon qty in the store configuration.'));
447450
$this->model->execute();
448451
}
452+
453+
/**
454+
* @covers \Magento\SalesRule\Controller\Adminhtml\Promo\Quote::execute
455+
*/
456+
public function testExecuteWithDisableCouponQuantity()
457+
{
458+
$helperData = $this->getMockBuilder(Data::class)
459+
->disableOriginalConstructor()
460+
->getMock();
461+
$this->objectManagerMock->expects($this->any())
462+
->method('get')
463+
->with(Data::class)
464+
->willReturn($helperData);
465+
$this->requestMock->expects($this->once())
466+
->method('isAjax')
467+
->willReturn(true);
468+
$ruleMock = $this->getMockBuilder(Rule::class)
469+
->addMethods(['getCouponType'])
470+
->onlyMethods(['getId'])
471+
->disableOriginalConstructor()
472+
->getMock();
473+
$this->registryMock->expects($this->once())
474+
->method('registry')
475+
->willReturn($ruleMock);
476+
$ruleMock->expects($this->once())
477+
->method('getId')
478+
->willReturn(1);
479+
$ruleMock->expects($this->once())
480+
->method('getCouponType')
481+
->willReturn(Rule::COUPON_TYPE_AUTO);
482+
$this->requestMock->expects($this->once())
483+
->method('getParams')
484+
->willReturn($this->requestMockDataWithInvalidCouponQuantity);
485+
$this->scopeConfigMock
486+
->expects($this->once())
487+
->method('getValue')
488+
->with(self::XML_COUPON_QUANTITY_LIMIT_PATH_TEST)
489+
->willReturn(self::XML_COUPON_QUANTITY_LIMIT_DISABLE_VALUE_TEST);
490+
$this->requestMockDataWithInvalidCouponQuantity['quantity'] = $this->requestMockDataWithInvalidCouponQuantity['qty'] ?? 0;
491+
$this->couponGenerationSpec->expects($this->any())
492+
->method('create')
493+
->with(['data' => $this->requestMockDataWithInvalidCouponQuantity])
494+
->willReturn(['some_data', 'some_data_2']);
495+
$this->getCouponCodeLength->expects($this->once())
496+
->method('fetchCouponCodeLength')
497+
->willReturn(10);
498+
$this->messageManager->expects($this->any())
499+
->method('addSuccessMessage');
500+
$this->responseMock->expects($this->once())
501+
->method('representJson')
502+
->with();
503+
$helperData->expects($this->once())
504+
->method('jsonEncode')
505+
->with([
506+
'messages' => __('%1 coupon(s) have been generated.', 2)
507+
]);
508+
$layout = $this->getMockBuilder(Layout::class)
509+
->disableOriginalConstructor()
510+
->getMock();
511+
$this->view->expects($this->any())
512+
->method('getLayout')
513+
->willReturn($layout);
514+
$messageBlock = $this->getMockBuilder(Messages::class)
515+
->disableOriginalConstructor()
516+
->getMock();
517+
$layout->expects($this->once())
518+
->method('initMessages');
519+
$layout->expects($this->once())
520+
->method('getMessagesBlock')
521+
->willReturn($messageBlock);
522+
$messageBlock->expects($this->once())
523+
->method('getGroupedHtml')
524+
->willReturn(__('%1 coupon(s) have been generated.', 2));
525+
$this->model->execute();
526+
}
449527
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
</field>
2222
<field id="quantity_limit" translate="label comment" type="text" sortOrder="20" showInDefault="1" canRestore="1">
2323
<label>Code Quantity Limit</label>
24-
<comment>For better performance max value allowed is 250,000.</comment>
25-
<frontend_class>required-entry validate-number-range number-range-1-250000</frontend_class>
24+
<comment>For better performance max value allowed is 250,000. Set 0 to disable it.</comment>
25+
<frontend_class>required-entry validate-number-range number-range-0-250000</frontend_class>
2626
</field>
2727
<field id="format" translate="label" type="select" sortOrder="30" showInDefault="1" canRestore="1">
2828
<label>Code Format</label>

app/code/Magento/SalesRule/i18n/en_US.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,4 @@ Apply,Apply
171171
"Discount amount is distributed among subtotal and shipping amount. Cases when multiple discounts applied to shipping amount are not supported. The option is going to be removed in future releases.ly","Discount amount is distributed among subtotal and shipping amount. Cases when multiple discounts applied to shipping amount are not supported. The option is going to be removed in future releases."
172172
"Coupon quantity should be less than or equal to the coupon quantity in the store configuration.","Coupon quantity should be less than or equal to the coupon quantity in the store configuration."
173173
"Code Quantity Limit","Code Quantity Limit"
174-
"For better performance max value allowed is 250,000.","For better performance max value allowed is 250,000."
174+
"For better performance max value allowed is 250,000. Set 0 to disable it.","For better performance max value allowed is 250,000. Set 0 to disable it."

0 commit comments

Comments
 (0)