Skip to content

Commit 8fc2cde

Browse files
committed
Merge pull request #621 from magento-troll/bugfixes
[Troll] Bugfixes
2 parents 6a99883 + 0f72ad3 commit 8fc2cde

File tree

12 files changed

+236
-15
lines changed

12 files changed

+236
-15
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,58 @@ protected function _getWebsitesMap()
347347
return $map;
348348
}
349349

350+
/**
351+
* {@inheritdoc}
352+
*/
353+
public function validateData(\Magento\Framework\DataObject $dataObject)
354+
{
355+
$result = parent::validateData($dataObject);
356+
if ($result === true) {
357+
$result = [];
358+
}
359+
360+
$action = $dataObject->getData('simple_action');
361+
$discount = $dataObject->getData('discount_amount');
362+
$result = array_merge($result, $this->validateDiscount($action, $discount));
363+
if ($dataObject->getData('sub_is_enable') == 1) {
364+
$action = $dataObject->getData('sub_simple_action');
365+
$discount = $dataObject->getData('sub_discount_amount');
366+
$result = array_merge($result, $this->validateDiscount($action, $discount));
367+
}
368+
369+
return !empty($result) ? $result : true;
370+
}
371+
372+
/**
373+
* Validate discount based on action
374+
*
375+
* @param string $action
376+
* @param string|int|float $discount
377+
*
378+
* @return array Validation errors
379+
*/
380+
protected function validateDiscount($action, $discount)
381+
{
382+
$result = [];
383+
switch ($action) {
384+
case 'by_percent':
385+
case 'to_percent':
386+
if ($discount < 0 || $discount > 100) {
387+
$result[] = __('Percentage discount should be between 0 and 100.');
388+
};
389+
break;
390+
case 'by_fixed':
391+
case 'to_fixed':
392+
if ($discount < 0) {
393+
$result[] = __('Discount value should be 0 or greater.');
394+
};
395+
break;
396+
default:
397+
$result[] = __('Unknown action.');
398+
}
399+
return $result;
400+
}
401+
350402
/**
351403
* Calculate price using catalog price rule of product
352404
*

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

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1010

11+
/**
12+
* Class RuleTest
13+
* @package Magento\CatalogRule\Test\Unit\Model
14+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
15+
*/
1116
class RuleTest extends \PHPUnit_Framework_TestCase
1217
{
1318
/** @var \Magento\CatalogRule\Model\Rule */
@@ -200,6 +205,110 @@ public function dataProviderCallbackValidateProduct()
200205
];
201206
}
202207

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

230339
/**
231-
* Test IsRuleBehaviorChanged action
340+
* Test isRuleBehaviorChanged action
341+
*
342+
* @dataProvider isRuleBehaviorChangedDataProvider
232343
*
233-
* @dataProvider ruleData
234344
* @param array $dataArray
235345
* @param array $originDataArray
236346
* @param bool $isObjectNew
@@ -258,11 +368,11 @@ public function testIsRuleBehaviorChanged($dataArray, $originDataArray, $isObjec
258368
}
259369

260370
/**
261-
* Data provider for isRuleBehaviorChanged test
371+
* Data provider for testIsRuleBehaviorChanged test
262372
*
263373
* @return array
264374
*/
265-
public function ruleData()
375+
public function isRuleBehaviorChangedDataProvider()
266376
{
267377
return [
268378
[['new name', 'new description'], ['name', 'description'], false, false],

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ Promo,Promo
6868
"Conditions Combination",Bedingungenkombination
6969
"Product Attribute",Produktattribut
7070
"The rules have been applied.","Die Regeln wurden angewendet."
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/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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ 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+
"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/fr_FR.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ 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+
"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/nl_NL.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ Promo,Promo
6868
"Conditions Combination",Voorwaardencombinaties
6969
"Product Attribute","Product attribuut"
7070
"The rules have been applied.","De regels zijn ingesteld."
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/pt_BR.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ 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+
"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/zh_CN.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ Promo,Promo
6868
"Conditions Combination",条件组合
6969
"Product Attribute",产品属性
7070
"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/view/adminhtml/templates/promo/js.phtml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,53 @@ function hideShowSubproductOptions()
1313
$('rule_sub_simple_action').up('div.field').show();
1414
$('rule_sub_discount_amount').up('div.field').show();
1515
$('rule_sub_discount_amount').addClassName('required-entry validate-not-negative-number');
16+
changeValidationForSubDiscountPercent();
1617
} else {
18+
$('rule_sub_discount_amount').
19+
removeClassName('required-entry').
20+
removeClassName('validate-not-negative-number').
21+
removeClassName('validate-number-range').
22+
removeClassName('number-range-0.00-100.00');
1723
$('rule_sub_simple_action').up('div.field').hide();
1824
$('rule_sub_discount_amount').up('div.field').hide();
19-
$('rule_sub_discount_amount').removeClassName('required-entry').removeClassName('validate-not-negative-number');
2025
}
2126

2227
return true;
2328
}
29+
30+
function changeValidationForDiscountPercent()
31+
{
32+
if ($('rule_simple_action').value == 'by_percent' || $('rule_simple_action').value == 'to_percent') {
33+
$('rule_discount_amount').addClassName('validate-number-range number-range-0.00-100.00');
34+
} else {
35+
$('rule_discount_amount').removeClassName('validate-number-range').removeClassName('number-range-0.00-100.00');
36+
}
37+
38+
return true;
39+
}
40+
41+
function changeValidationForSubDiscountPercent()
42+
{
43+
if ($('rule_sub_is_enable').value == 1) {
44+
if ($('rule_sub_simple_action').value == 'by_percent' || $('rule_sub_simple_action').value == 'to_percent') {
45+
$('rule_sub_discount_amount').addClassName('validate-number-range number-range-0.00-100.00');
46+
} else {
47+
$('rule_sub_discount_amount').
48+
removeClassName('validate-number-range').
49+
removeClassName('number-range-0.00-100.00');
50+
}
51+
}
52+
53+
return true;
54+
}
55+
2456
jQuery(document).ready(hideShowSubproductOptions);
57+
jQuery(document).ready(changeValidationForDiscountPercent);
58+
jQuery(document).on('change', '#rule_sub_is_enable', hideShowSubproductOptions);
59+
jQuery(document).on('change', '#rule_simple_action', changeValidationForDiscountPercent);
60+
jQuery(document).on('change', '#rule_sub_simple_action', changeValidationForSubDiscountPercent);
2561
window.hideShowSubproductOptions = hideShowSubproductOptions;
62+
window.changeValidationForDiscountPercent = changeValidationForDiscountPercent;
63+
window.changeValidationForSubDiscountPercent = changeValidationForSubDiscountPercent;
2664
});
2765
</script>

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,19 +350,19 @@ public function validate(\Magento\Framework\DataObject $object)
350350
/**
351351
* Validate rule data
352352
*
353-
* @param \Magento\Framework\DataObject $object
353+
* @param \Magento\Framework\DataObject $dataObject
354354
* @return bool|string[] - return true if validation passed successfully. Array with errors description otherwise
355355
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
356356
* @SuppressWarnings(PHPMD.NPathComplexity)
357357
*/
358-
public function validateData(\Magento\Framework\DataObject $object)
358+
public function validateData(\Magento\Framework\DataObject $dataObject)
359359
{
360360
$result = [];
361361
$fromDate = $toDate = null;
362362

363-
if ($object->hasFromDate() && $object->hasToDate()) {
364-
$fromDate = $object->getFromDate();
365-
$toDate = $object->getToDate();
363+
if ($dataObject->hasFromDate() && $dataObject->hasToDate()) {
364+
$fromDate = $dataObject->getFromDate();
365+
$toDate = $dataObject->getToDate();
366366
}
367367

368368
if ($fromDate && $toDate) {
@@ -374,14 +374,14 @@ public function validateData(\Magento\Framework\DataObject $object)
374374
}
375375
}
376376

377-
if ($object->hasWebsiteIds()) {
378-
$websiteIds = $object->getWebsiteIds();
377+
if ($dataObject->hasWebsiteIds()) {
378+
$websiteIds = $dataObject->getWebsiteIds();
379379
if (empty($websiteIds)) {
380380
$result[] = __('Please specify a website.');
381381
}
382382
}
383-
if ($object->hasCustomerGroupIds()) {
384-
$customerGroupIds = $object->getCustomerGroupIds();
383+
if ($dataObject->hasCustomerGroupIds()) {
384+
$customerGroupIds = $dataObject->getCustomerGroupIds();
385385
if (empty($customerGroupIds)) {
386386
$result[] = __('Please specify Customer Groups.');
387387
}

app/code/Magento/Sales/Block/Adminhtml/Report/Filter/Form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected function _prepareForm()
6767
'name' => 'show_order_statuses',
6868
'label' => __('Order Status'),
6969
'options' => ['0' => __('Any'), '1' => __('Specified')],
70-
'note' => __('Applies to Any of the Specified Order Statuses')
70+
'note' => __('Applies to Any of the Specified Order Statuses except canceled orders')
7171
],
7272
'to'
7373
);

0 commit comments

Comments
 (0)