Skip to content

Commit c9e33ed

Browse files
committed
MAGETWO-39705: It's possible to specify discount more than 100% in Catalog Price Rule
1 parent 5389341 commit c9e33ed

File tree

9 files changed

+191
-4
lines changed

9 files changed

+191
-4
lines changed

app/code/Magento/CatalogRule/Model/Rule.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
*/
4343
class Rule extends \Magento\Rule\Model\AbstractModel
4444
{
45+
const PERCENTAGE_VALIDATION_ERR_MSG = 'Percentage discount should be between 0 and 100.';
46+
const NOT_NEGATIVE_VALIDATION_ERR_MSG = 'Discount value should be 0 or greater.';
47+
const INCORRECT_ACTION_ERR_MSG = 'Unknown action.';
48+
4549
/**
4650
* Prefix of model events names
4751
*
@@ -347,6 +351,58 @@ protected function _getWebsitesMap()
347351
return $map;
348352
}
349353

354+
/**
355+
* {@inheritdoc}
356+
*/
357+
public function validateData(\Magento\Framework\Object $object)
358+
{
359+
$result = parent::validateData($object);
360+
if ($result === true) {
361+
$result = [];
362+
}
363+
364+
$action = $object->getData('simple_action');
365+
$discount = $object->getData('discount_amount');
366+
$result = array_merge($result, $this->validateDiscount($action, $discount));
367+
if ($object->getData('sub_is_enable') == 1) {
368+
$action = $object->getData('sub_simple_action');
369+
$discount = $object->getData('sub_discount_amount');
370+
$result = array_merge($result, $this->validateDiscount($action, $discount));
371+
}
372+
373+
return !empty($result) ? $result : true;
374+
}
375+
376+
/**
377+
* Validate discount based on action
378+
*
379+
* @param string $action
380+
* @param string|int|float $discount
381+
*
382+
* @return array Validation errors
383+
*/
384+
protected function validateDiscount($action, $discount)
385+
{
386+
$result = [];
387+
switch ($action) {
388+
case 'by_percent':
389+
case 'to_percent':
390+
if ($discount < 0 || $discount > 100) {
391+
$result[] = __(self::PERCENTAGE_VALIDATION_ERR_MSG);
392+
};
393+
break;
394+
case 'by_fixed':
395+
case 'to_fixed':
396+
if ($discount < 0) {
397+
$result[] = __(self::NOT_NEGATIVE_VALIDATION_ERR_MSG);
398+
};
399+
break;
400+
default:
401+
$result[] = __(self::INCORRECT_ACTION_ERR_MSG);
402+
}
403+
return $result;
404+
}
405+
350406
/**
351407
* Calculate price using catalog price rule of product
352408
*

app/code/Magento/CatalogRule/Test/Unit/Model/RuleTest.php

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,110 @@ public function dataProviderCallbackValidateProduct()
200200
];
201201
}
202202

203+
/**
204+
* Test validateData action
205+
*
206+
* @dataProvider validateDataDataProvider
207+
* @param array $data Data for the rule actions
208+
* @param bool|array $expected True or an array of errors
209+
*
210+
* @return void
211+
*/
212+
public function testValidateData($data, $expected)
213+
{
214+
$result = $this->rule->validateData(new \Magento\Framework\Object($data));
215+
$this->assertEquals($result, $expected);
216+
}
217+
218+
/**
219+
* Data provider for testValidateData test
220+
*
221+
* @return array
222+
*/
223+
public function validateDataDataProvider()
224+
{
225+
return [
226+
[
227+
[
228+
'simple_action' => 'by_fixed',
229+
'discount_amount' => '123',
230+
'sub_is_enable' => '0',
231+
'sub_simple_action' => 'by_percent',
232+
'sub_discount_amount' => '123',
233+
],
234+
true
235+
],
236+
[
237+
[
238+
'simple_action' => 'by_percent',
239+
'discount_amount' => '9,99',
240+
'sub_is_enable' => '0',
241+
],
242+
true
243+
],
244+
[
245+
[
246+
'simple_action' => 'by_fixed',
247+
'discount_amount' => '123',
248+
'sub_is_enable' => '1',
249+
'sub_simple_action' => 'by_percent',
250+
'sub_discount_amount' => '123',
251+
],
252+
[
253+
'Percentage discount should be between 0 and 100.',
254+
]
255+
],
256+
[
257+
[
258+
'simple_action' => 'by_percent',
259+
'discount_amount' => '123.12',
260+
'sub_is_enable' => '1',
261+
'sub_simple_action' => 'to_percent',
262+
'sub_discount_amount' => '123.001',
263+
],
264+
[
265+
'Percentage discount should be between 0 and 100.',
266+
'Percentage discount should be between 0 and 100.',
267+
]
268+
],
269+
[
270+
[
271+
'simple_action' => 'to_percent',
272+
'discount_amount' => '-12',
273+
'sub_is_enable' => '1',
274+
'sub_simple_action' => 'to_fixed',
275+
'sub_discount_amount' => '567.8901',
276+
],
277+
[
278+
'Percentage discount should be between 0 and 100.',
279+
]
280+
],
281+
[
282+
[
283+
'simple_action' => 'to_fixed',
284+
'discount_amount' => '-1234567890',
285+
'sub_is_enable' => '1',
286+
'sub_simple_action' => 'by_fixed',
287+
'sub_discount_amount' => '-5',
288+
],
289+
[
290+
'Discount value should be 0 or greater.',
291+
'Discount value should be 0 or greater.',
292+
]
293+
],
294+
[
295+
[
296+
'simple_action' => 'invalid action',
297+
'discount_amount' => '12',
298+
'sub_is_enable' => '0',
299+
],
300+
[
301+
'Unknown action.',
302+
]
303+
],
304+
];
305+
}
306+
203307
/**
204308
* Test after delete action
205309
*
@@ -228,9 +332,9 @@ public function testAfterUpdate()
228332
}
229333

230334
/**
231-
* Test IsRuleBehaviorChanged action
335+
* Test isRuleBehaviorChanged action
232336
*
233-
* @dataProvider ruleData
337+
* @dataProvider isRuleBehaviorChangedDataProvider
234338
* @param array $dataArray
235339
* @param array $originDataArray
236340
* @param bool $isObjectNew
@@ -258,11 +362,11 @@ public function testIsRuleBehaviorChanged($dataArray, $originDataArray, $isObjec
258362
}
259363

260364
/**
261-
* Data provider for isRuleBehaviorChanged test
365+
* Data provider for testIsRuleBehaviorChanged test
262366
*
263367
* @return array
264368
*/
265-
public function ruleData()
369+
public function isRuleBehaviorChangedDataProvider()
266370
{
267371
return [
268372
[['new name', 'new description'], ['name', 'description'], false, false],

app/code/Magento/CatalogRule/i18n/de_DE.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ Promo,Promo
6868
"Conditions Combination",Bedingungenkombination
6969
"Product Attribute",Produktattribut
7070
"The rules have been applied.","Die Regeln wurden angewendet."
71+
"The rules have been applied.","The rules have been applied."
72+
"Percentage discount should be between 0 and 100.","Percentage discount should be between 0 and 100."
73+
"Discount value should be 0 or greater.","Discount value should be 0 or greater."
74+
"Unknown action.","Unknown action."

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ Promo,Promo
6868
"Conditions Combination","Conditions Combination"
6969
"Product Attribute","Product Attribute"
7070
"The rules have been applied.","The rules have been applied."
71+
"Percentage discount should be between 0 and 100.","Percentage discount should be between 0 and 100."
72+
"Discount value should be 0 or greater.","Discount value should be 0 or greater."
73+
"Unknown action.","Unknown action."

app/code/Magento/CatalogRule/i18n/es_ES.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ Promo,Promo
6868
"Conditions Combination","Combinación de condiciones"
6969
"Product Attribute","Atributos del producto"
7070
"The rules have been applied.","Las reglas han sido aplicadas."
71+
"The rules have been applied.","The rules have been applied."
72+
"Percentage discount should be between 0 and 100.","Percentage discount should be between 0 and 100."
73+
"Discount value should be 0 or greater.","Discount value should be 0 or greater."
74+
"Unknown action.","Unknown action."

app/code/Magento/CatalogRule/i18n/fr_FR.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ Promo,Promo
6868
"Conditions Combination","Combinaison de conditions"
6969
"Product Attribute","Attribut de produit"
7070
"The rules have been applied.","Les règles ont été appliquées."
71+
"The rules have been applied.","The rules have been applied."
72+
"Percentage discount should be between 0 and 100.","Percentage discount should be between 0 and 100."
73+
"Discount value should be 0 or greater.","Discount value should be 0 or greater."
74+
"Unknown action.","Unknown action."

app/code/Magento/CatalogRule/i18n/nl_NL.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ Promo,Promo
6868
"Conditions Combination",Voorwaardencombinaties
6969
"Product Attribute","Product attribuut"
7070
"The rules have been applied.","De regels zijn ingesteld."
71+
"The rules have been applied.","The rules have been applied."
72+
"Percentage discount should be between 0 and 100.","Percentage discount should be between 0 and 100."
73+
"Discount value should be 0 or greater.","Discount value should be 0 or greater."
74+
"Unknown action.","Unknown action."

app/code/Magento/CatalogRule/i18n/pt_BR.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ Promo,Promo
6868
"Conditions Combination","Combinação de condições"
6969
"Product Attribute","Atributo de Produto"
7070
"The rules have been applied.","As regras foram aplicadas."
71+
"The rules have been applied.","The rules have been applied."
72+
"Percentage discount should be between 0 and 100.","Percentage discount should be between 0 and 100."
73+
"Discount value should be 0 or greater.","Discount value should be 0 or greater."
74+
"Unknown action.","Unknown action."

app/code/Magento/CatalogRule/i18n/zh_CN.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ Promo,Promo
6868
"Conditions Combination",条件组合
6969
"Product Attribute",产品属性
7070
"The rules have been applied.",规则已应用。
71+
"The rules have been applied.","The rules have been applied."
72+
"Percentage discount should be between 0 and 100.","Percentage discount should be between 0 and 100."
73+
"Discount value should be 0 or greater.","Discount value should be 0 or greater."
74+
"Unknown action.","Unknown action."

0 commit comments

Comments
 (0)