Skip to content

Commit

Permalink
MAGETWO-49595: Category page problems after pull request #367
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksym Aposov committed Feb 24, 2016
1 parent fb909a9 commit ca5d13f
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Model/Category/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function getAttributesMeta(Type $entityType)
$meta[$code]['validation'] = $rules;
}

$meta[$code]['scope_label'] = $this->getScopeLabel($attribute);
$meta[$code]['scopeLabel'] = $this->getScopeLabel($attribute);
$meta[$code]['componentType'] = Field::NAME;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ protected function getAttributesMeta(array $attributes, $groupCode)
}

// TODO: getAttributeModel() should not be used when MAGETWO-48284 is complete
if (($rules = $this->eavValidationRules->build($this->getAttributeModel($attribute), $child))) {
$childData = $child['arguments']['data']['config'];
if (($rules = $this->eavValidationRules->build($this->getAttributeModel($attribute), $childData))) {
$child['arguments']['data']['config']['validation'] = $rules;
}

Expand Down Expand Up @@ -585,7 +586,6 @@ protected function getValue($attributeCode)
return $product->getData($attributeCode);
}


/**
* Retrieve scope label
*
Expand Down
41 changes: 16 additions & 25 deletions app/code/Magento/Ui/DataProvider/EavValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ class EavValidationRules
/**
* @var array
*/
protected $validationRul = [
'input_validation' => [
'email' => ['validate-email' => true],
'date' => ['validate-date' => true],
],
protected $validationRules = [
'email' => ['validate-email' => true],
'date' => ['validate-date' => true],
];

/**
Expand All @@ -31,30 +29,23 @@ class EavValidationRules
*/
public function build(AbstractAttribute $attribute, array $data)
{
$rules = [];
if (!empty($data['arguments']['data']['config']['required'])) {
$rules['required-entry'] = true;
$validation = [];
if (isset($data['required']) && $data['required'] == 1) {
$validation = array_merge($validation, ['required-entry' => true]);
}
if ($attribute->getFrontendInput() === 'price') {
$rules['validate-zero-or-greater'] = true;
$validation = array_merge($validation, ['validate-zero-or-greater' => true]);
}

$validation = $attribute->getValidateRules();
if (!empty($validation)) {
foreach ($validation as $type => $ruleName) {
switch ($type) {
case 'input_validation':
if (isset($this->validationRul[$type][$ruleName])) {
$rules = array_merge($rules, $this->validationRul[$type][$ruleName]);
}
break;
case 'min_text_length':
case 'max_text_length':
$rules = array_merge($rules, [$type => $ruleName]);
break;
}

if ($attribute->getValidateRules()) {
$validation = array_merge($validation, $attribute->getValidateRules());
}
$rules = [];
foreach ($validation as $type => $ruleName) {
$rule = [$type => $ruleName];
if ($type === 'input_validation') {
$rule = isset($this->validationRules[$ruleName]) ? $this->validationRules[$ruleName] : [];
}
$rules = array_merge($rules, $rule);
}

return $rules;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Ui\Test\Unit\DataProvider;

use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Ui\DataProvider\EavValidationRules;

/**
* Class EavValidationRulesTest
*/
class EavValidationRulesTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ObjectManager
*/
protected $objectManager;

/**
* @var \Magento\Ui\DataProvider\EavValidationRules
*/
protected $subject;

/**
* @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute|\PHPUnit_Framework_MockObject_MockObject
*/
protected $attributeMock;

protected function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->attributeMock =
$this->getMockBuilder(AbstractAttribute::class)
->setMethods(['getFrontendInput', 'getValidateRules'])
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->subject = new EavValidationRules();
}

/**
* @param array $data
* @param array $expected
* @dataProvider buildDataProvider
*/
public function testBuild($attributeInputType, $validateRules, $data, $expected)
{
$this->attributeMock->expects($this->once())->method('getFrontendInput')->willReturn($attributeInputType);
$this->attributeMock->expects($this->any())->method('getValidateRules')->willReturn($validateRules);
$validationRules = $this->subject->build($this->attributeMock, $data);
$this->assertEquals($expected, $validationRules);
}

/**
* @return array
*/
public function buildDataProvider()
{
return [
['', '', [], []],
['', null, [], []],
['', false, [], []],
['', [], [], []],
['', '', ['required' => 1], ['required-entry' => true]],
['price', '', [], ['validate-zero-or-greater' => true]],
['price', '', ['required' => 1], ['validate-zero-or-greater' => true, 'required-entry' => true]],
['', ['input_validation' => 'email'], [], ['validate-email' => true]],
['', ['input_validation' => 'date'], [], ['validate-date' => true]],
['', ['input_validation' => 'other'], [], []],
['', ['max_text_length' => '254'], ['required' => 1], ['max_text_length' => 254, 'required-entry' => true]],
['', ['max_text_length' => '254', 'min_text_length' => 1], [],
['max_text_length' => 254, 'min_text_length' => 1]],
['', ['max_text_length' => '254', 'input_validation' => 'date'], [],
['max_text_length' => 254, 'validate-date' => true]],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Model\Category;

use Magento\Catalog\Model\Category\DataProvider;
use Magento\Eav\Model\Config as EavConfig;
use Magento\TestFramework\Helper\Bootstrap;

class DataProviderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var DataProvider
*/
private $dataProvider;

/**
* @var \Magento\Eav\Model\Entity\Type
*/
private $entityType;

/**
* {@inheritDoc}
*/
protected function setUp()
{
parent::setUp();
$objectManager = Bootstrap::getObjectManager();
$this->dataProvider = $objectManager->create(
DataProvider::class,
[
'name' => 'category_form_data_source',
'primaryFieldName' => 'entity_id',
'requestFieldName' => 'id'
]
);

$this->entityType = $objectManager->create(EavConfig::class)->getEntityType('catalog_category');
}

/**
* @return void
*/
public function testGetMetaRequiredAttributes()
{
$requiredAttributes = [
'general' => ['name'],
'display_settings' => ['available_sort_by', 'default_sort_by'],
];
$meta = $this->dataProvider->getMeta();
$this->assertArrayHasKey('url_key', $meta['search_engine_optimization']['children']);
foreach ($requiredAttributes as $scope => $attributes) {
foreach ($attributes as $attribute) {
$this->assertArrayHasKey($attribute, $meta[$scope]['children']);
$data = $meta[$scope]['children'][$attribute];
$this->assertTrue($data['arguments']['data']['config']['validation']['required-entry']);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ protected function setUp()
*/
public function testGetAttributesMeta()
{
$meta = $this->dataProvider->getAttributesMeta($this->entityType);
$this->assertArrayHasKey('url_key', $meta);
$this->assertEquals('text', $meta['url_key']['dataType']);
$this->assertEquals('input', $meta['url_key']['formElement']);
$this->assertEquals('1', $meta['url_key']['visible']);
$this->assertEquals('0', $meta['url_key']['required']);
$this->assertEquals('[STORE VIEW]', $meta['url_key']['scope_label']);
$meta = $this->dataProvider->getMeta();
$this->assertArrayHasKey('url_key', $meta['search_engine_optimization']['children']);
$urlKeyData = $meta['search_engine_optimization']['children']['url_key']['arguments']['data']['config'];
$this->assertEquals('text', $urlKeyData['dataType']);
$this->assertEquals('input', $urlKeyData['formElement']);
$this->assertEquals('1', $urlKeyData['visible']);
$this->assertEquals('0', $urlKeyData['required']);
$this->assertEquals('[STORE VIEW]', $urlKeyData['scopeLabel']);
}
}

0 comments on commit ca5d13f

Please sign in to comment.