Skip to content

Commit 380619c

Browse files
author
Bohdan Korablov
committed
Merge remote-tracking branch 'tango/MAGETWO-49191' into MAGETWO-51827
2 parents 7608464 + 8cb2463 commit 380619c

File tree

3 files changed

+173
-7
lines changed

3 files changed

+173
-7
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Ui\DataProvider;
7+
8+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
9+
use Magento\Catalog\Ui\DataProvider\CatalogEavValidationRules;
10+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
11+
12+
class CatalogEavValidationRulesTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var ObjectManagerHelper
16+
*/
17+
protected $objectManagerHelper;
18+
19+
/**
20+
* @var CatalogEavValidationRules
21+
*/
22+
protected $catalogEavValidationRules;
23+
24+
/**
25+
* @return void
26+
*/
27+
protected function setUp()
28+
{
29+
$this->objectManagerHelper = new ObjectManagerHelper($this);
30+
$this->catalogEavValidationRules = $this->objectManagerHelper->getObject(CatalogEavValidationRules::class);
31+
}
32+
33+
/**
34+
* @param $frontendInput
35+
* @param $frontendClass
36+
* @param array $eavConfig
37+
* @param array $expectedResult
38+
* @return void
39+
* @dataProvider buildDataProvider
40+
*/
41+
public function testBuild($frontendInput, $frontendClass, array $eavConfig, array $expectedResult)
42+
{
43+
/** @var \Magento\Catalog\Api\Data\ProductAttributeInterface|MockObject $attribute */
44+
$attribute = $this->getMock(\Magento\Catalog\Api\Data\ProductAttributeInterface::class);
45+
46+
$attribute->expects($this->once())
47+
->method('getFrontendInput')
48+
->willReturn($frontendInput);
49+
$attribute->expects($this->once())
50+
->method('getFrontendClass')
51+
->willReturn($frontendClass);
52+
53+
$this->assertEquals($expectedResult, $this->catalogEavValidationRules->build($attribute, $eavConfig));
54+
}
55+
56+
public function buildDataProvider()
57+
{
58+
$data['required'] = true;
59+
60+
return [
61+
[
62+
'frontendInput' => 'input',
63+
'frontendClass' => '',
64+
'eavConfig' => [],
65+
'expectedResult' => [],
66+
],
67+
[
68+
'frontendInput' => 'price',
69+
'frontendClass' => '',
70+
'eavConfig' => $data,
71+
'expectedResult' => [
72+
'required-entry' => true,
73+
'validate-zero-or-greater' => true,
74+
],
75+
],
76+
[
77+
'frontendInput' => 'input',
78+
'frontendClass' => 'maximum-length-20 minimum-length-10 validate-number validate-digits'
79+
. ' validate-email validate-url validate-alpha validate-alphanum',
80+
'eavConfig' => [],
81+
'expectedResult' => [
82+
'max_text_length' => 20,
83+
'min_text_length' => 10,
84+
'validate-number' => true,
85+
'validate-digits' => true,
86+
'validate-email' => true,
87+
'validate-url' => true,
88+
'validate-alpha' => true,
89+
'validate-alphanum' => true,
90+
],
91+
],
92+
];
93+
}
94+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Ui\DataProvider;
7+
8+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
9+
10+
/**
11+
* Class build validation rules for catalog EAV attributes
12+
*/
13+
class CatalogEavValidationRules
14+
{
15+
/**
16+
* Build validation rules
17+
*
18+
* @param ProductAttributeInterface $attribute
19+
* @param array $data
20+
* @return array
21+
*/
22+
public function build(ProductAttributeInterface $attribute, array $data)
23+
{
24+
$rules = [];
25+
if (!empty($data['required'])) {
26+
$rules['required-entry'] = true;
27+
}
28+
if ($attribute->getFrontendInput() === 'price') {
29+
$rules['validate-zero-or-greater'] = true;
30+
}
31+
32+
$validationClasses = explode(' ', $attribute->getFrontendClass());
33+
34+
foreach ($validationClasses as $class) {
35+
if (preg_match('/^maximum-length-(\d+)$/', $class, $matches)) {
36+
$rules = array_merge($rules, ['max_text_length' => $matches[1]]);
37+
continue;
38+
}
39+
if (preg_match('/^minimum-length-(\d+)$/', $class, $matches)) {
40+
$rules = array_merge($rules, ['min_text_length' => $matches[1]]);
41+
continue;
42+
}
43+
44+
$rules = $this->mapRules($class, $rules);
45+
}
46+
47+
return $rules;
48+
}
49+
50+
/**
51+
* Map classes w. rules
52+
*
53+
* @param string $class
54+
* @param array $rules
55+
* @return array
56+
*/
57+
protected function mapRules($class, array $rules)
58+
{
59+
switch ($class) {
60+
case 'validate-number':
61+
case 'validate-digits':
62+
case 'validate-email':
63+
case 'validate-url':
64+
case 'validate-alpha':
65+
case 'validate-alphanum':
66+
$rules = array_merge($rules, [$class => true]);
67+
break;
68+
}
69+
70+
return $rules;
71+
}
72+
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use Magento\Store\Model\StoreManagerInterface;
2626
use Magento\Ui\Component\Form\Field;
2727
use Magento\Ui\Component\Form\Fieldset;
28-
use Magento\Ui\DataProvider\EavValidationRules;
28+
use Magento\Catalog\Ui\DataProvider\CatalogEavValidationRules;
2929
use Magento\Ui\DataProvider\Mapper\FormElement as FormElementMapper;
3030
use Magento\Ui\DataProvider\Mapper\MetaProperties as MetaPropertiesMapper;
3131
use Magento\Ui\Component\Form\Element\Wysiwyg as WysiwygElement;
@@ -52,9 +52,9 @@ class Eav extends AbstractModifier
5252
protected $eavConfig;
5353

5454
/**
55-
* @var EavValidationRules
55+
* @var CatalogEavValidationRules
5656
*/
57-
protected $eavValidationRules;
57+
protected $catalogEavValidationRules;
5858

5959
/**
6060
* @var RequestInterface
@@ -163,7 +163,7 @@ class Eav extends AbstractModifier
163163

164164
/**
165165
* @param LocatorInterface $locator
166-
* @param EavValidationRules $eavValidationRules
166+
* @param CatalogEavValidationRules $catalogEavValidationRules
167167
* @param Config $eavConfig
168168
* @param RequestInterface $request
169169
* @param GroupCollectionFactory $groupCollectionFactory
@@ -185,7 +185,7 @@ class Eav extends AbstractModifier
185185
*/
186186
public function __construct(
187187
LocatorInterface $locator,
188-
EavValidationRules $eavValidationRules,
188+
CatalogEavValidationRules $catalogEavValidationRules,
189189
Config $eavConfig,
190190
RequestInterface $request,
191191
GroupCollectionFactory $groupCollectionFactory,
@@ -205,7 +205,7 @@ public function __construct(
205205
$attributesToEliminate = []
206206
) {
207207
$this->locator = $locator;
208-
$this->eavValidationRules = $eavValidationRules;
208+
$this->catalogEavValidationRules = $catalogEavValidationRules;
209209
$this->eavConfig = $eavConfig;
210210
$this->request = $request;
211211
$this->groupCollectionFactory = $groupCollectionFactory;
@@ -575,7 +575,7 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
575575

576576
// TODO: getAttributeModel() should not be used when MAGETWO-48284 is complete
577577
$childData = $this->arrayManager->get($configPath, $meta, []);
578-
if (($rules = $this->eavValidationRules->build($this->getAttributeModel($attribute), $childData))) {
578+
if (($rules = $this->catalogEavValidationRules->build($this->getAttributeModel($attribute), $childData))) {
579579
$meta = $this->arrayManager->merge($configPath, $meta, [
580580
'validation' => $rules,
581581
]);

0 commit comments

Comments
 (0)