Skip to content

Commit 5b9d3e8

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-99838' into EPAM-PR-81
2 parents 868e50e + dfb0c46 commit 5b9d3e8

File tree

2 files changed

+78
-15
lines changed
  • app/code/Magento/Eav

2 files changed

+78
-15
lines changed

app/code/Magento/Eav/Model/Validator/Attribute/Data.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
namespace Magento\Eav\Model\Validator\Attribute;
8+
9+
use Magento\Eav\Model\Attribute;
10+
711
/**
812
* EAV attribute data validator
913
*
1014
* @author Magento Core Team <[email protected]>
1115
*/
12-
namespace Magento\Eav\Model\Validator\Attribute;
13-
14-
use Magento\Eav\Model\Attribute;
15-
1616
class Data extends \Magento\Framework\Validator\AbstractValidator
1717
{
1818
/**
@@ -126,7 +126,7 @@ public function isValid($entity)
126126
$dataModel = $this->_attrDataFactory->create($attribute, $entity);
127127
$dataModel->setExtractedData($data);
128128
if (!isset($data[$attributeCode])) {
129-
$data[$attributeCode] = null;
129+
$data[$attributeCode] = '';
130130
}
131131
$result = $dataModel->validateValue($data[$attributeCode]);
132132
if (true !== $result) {

app/code/Magento/Eav/Test/Unit/Model/Validator/Attribute/DataTest.php

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,54 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
9+
namespace Magento\Eav\Test\Unit\Model\Validator\Attribute;
10+
711
/**
812
* Test for \Magento\Eav\Model\Validator\Attribute\Data
913
*/
10-
namespace Magento\Eav\Test\Unit\Model\Validator\Attribute;
11-
1214
class DataTest extends \PHPUnit\Framework\TestCase
1315
{
16+
/**
17+
* @var \Magento\Eav\Model\AttributeDataFactory|\PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
private $attrDataFactory;
20+
21+
/**
22+
* @var \Magento\Eav\Model\Validator\Attribute\Data
23+
*/
24+
private $model;
25+
26+
/**
27+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
28+
*/
29+
private $objectManager;
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
protected function setUp()
35+
{
36+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
37+
$this->attrDataFactory = $this->getMockBuilder(\Magento\Eav\Model\AttributeDataFactory::class)
38+
->setMethods(['create'])
39+
->setConstructorArgs(
40+
[
41+
'objectManager' => $this->createMock(\Magento\Framework\ObjectManagerInterface::class),
42+
'string' => $this->createMock(\Magento\Framework\Stdlib\StringUtils::class)
43+
]
44+
)
45+
->getMock();
46+
47+
$this->model = $this->objectManager->getObject(
48+
\Magento\Eav\Model\Validator\Attribute\Data::class,
49+
[
50+
'_attrDataFactory' => $this->attrDataFactory
51+
]
52+
);
53+
}
54+
1455
/**
1556
* Testing \Magento\Eav\Model\Validator\Attribute\Data::isValid
1657
*
@@ -381,13 +422,15 @@ public function testAddErrorMessages()
381422
protected function _getAttributeMock($attributeData)
382423
{
383424
$attribute = $this->getMockBuilder(\Magento\Eav\Model\Attribute::class)
384-
->setMethods([
385-
'getAttributeCode',
386-
'getDataModel',
387-
'getFrontendInput',
388-
'__wakeup',
389-
'getIsVisible',
390-
])
425+
->setMethods(
426+
[
427+
'getAttributeCode',
428+
'getDataModel',
429+
'getFrontendInput',
430+
'__wakeup',
431+
'getIsVisible',
432+
]
433+
)
391434
->disableOriginalConstructor()
392435
->getMock();
393436

@@ -436,7 +479,7 @@ protected function _getDataModelMock($returnValue, $argument = null)
436479
$dataModel = $this->getMockBuilder(
437480
\Magento\Eav\Model\Attribute\Data\AbstractData::class
438481
)->disableOriginalConstructor()->setMethods(
439-
['validateValue']
482+
['setExtractedData', 'validateValue']
440483
)->getMockForAbstractClass();
441484
if ($argument) {
442485
$dataModel->expects(
@@ -466,4 +509,24 @@ protected function _getEntityMock()
466509
)->disableOriginalConstructor()->getMock();
467510
return $entity;
468511
}
512+
513+
/**
514+
* Test for isValid() without data for attribute.
515+
*
516+
* @return void
517+
*/
518+
public function testIsValidWithoutData() : void
519+
{
520+
$attributeData = ['attribute_code' => 'attribute', 'frontend_input' => 'text', 'is_visible' => true];
521+
$entity = $this->_getEntityMock();
522+
$attribute = $this->_getAttributeMock($attributeData);
523+
$dataModel = $this->_getDataModelMock(true, $this->logicalAnd($this->isEmpty(), $this->isType('string')));
524+
$dataModel->expects($this->once())->method('setExtractedData')->with([])->willReturnSelf();
525+
$this->attrDataFactory->expects($this->once())
526+
->method('create')
527+
->with($attribute, $entity)
528+
->willReturn($dataModel);
529+
$this->model->setAttributes([$attribute])->setData([]);
530+
$this->assertTrue($this->model->isValid($entity));
531+
}
469532
}

0 commit comments

Comments
 (0)