Skip to content

Commit 3cf16dc

Browse files
MAGETWO-99838: Error message '"customer address attribute" is a required value.' is absent on Storefront
- Fixed bug - Added unit test
1 parent d6ef3ef commit 3cf16dc

File tree

2 files changed

+77
-14
lines changed
  • app/code/Magento/Eav

2 files changed

+77
-14
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: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,43 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
namespace Magento\Eav\Test\Unit\Model\Validator\Attribute;
8+
79
/**
810
* Test for \Magento\Eav\Model\Validator\Attribute\Data
911
*/
10-
namespace Magento\Eav\Test\Unit\Model\Validator\Attribute;
11-
1212
class DataTest extends \PHPUnit\Framework\TestCase
1313
{
14+
/**
15+
* @var \Magento\Eav\Model\AttributeDataFactory|\PHPUnit_Framework_MockObject_MockObject
16+
*/
17+
private $attrDataFactory;
18+
19+
/**
20+
* @var \Magento\Eav\Model\Validator\Attribute\Data
21+
*/
22+
private $model;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function setUp()
28+
{
29+
$this->attrDataFactory = $this->getMockBuilder(\Magento\Eav\Model\AttributeDataFactory::class)
30+
->setMethods(['create'])
31+
->setConstructorArgs(
32+
[
33+
'objectManager' => $this->createMock(\Magento\Framework\ObjectManagerInterface::class),
34+
'string' => $this->createMock(\Magento\Framework\Stdlib\StringUtils::class)
35+
]
36+
)
37+
->getMock();
38+
39+
$this->model = new \Magento\Eav\Model\Validator\Attribute\Data(
40+
$this->attrDataFactory
41+
);
42+
}
43+
1444
/**
1545
* Testing \Magento\Eav\Model\Validator\Attribute\Data::isValid
1646
*
@@ -381,13 +411,15 @@ public function testAddErrorMessages()
381411
protected function _getAttributeMock($attributeData)
382412
{
383413
$attribute = $this->getMockBuilder(\Magento\Eav\Model\Attribute::class)
384-
->setMethods([
385-
'getAttributeCode',
386-
'getDataModel',
387-
'getFrontendInput',
388-
'__wakeup',
389-
'getIsVisible',
390-
])
414+
->setMethods(
415+
[
416+
'getAttributeCode',
417+
'getDataModel',
418+
'getFrontendInput',
419+
'__wakeup',
420+
'getIsVisible',
421+
]
422+
)
391423
->disableOriginalConstructor()
392424
->getMock();
393425

@@ -466,4 +498,35 @@ protected function _getEntityMock()
466498
)->disableOriginalConstructor()->getMock();
467499
return $entity;
468500
}
501+
502+
/**
503+
* Test for isValid() without data for attribute.
504+
*
505+
* @return void
506+
*/
507+
public function testIsValidWithoutData() : void
508+
{
509+
$attributeData = ['attribute_code' => 'attribute', 'frontend_input' => 'text', 'is_visible' => true];
510+
$entity = $this->_getEntityMock();
511+
$attribute = $this->_getAttributeMock($attributeData);
512+
$this->model->setAttributes([$attribute])->setData([]);
513+
$dataModel = $this->getMockBuilder(\Magento\Eav\Model\Attribute\Data\AbstractData::class)
514+
->disableOriginalConstructor()
515+
->setMethods(['validateValue'])
516+
->getMockForAbstractClass();
517+
$dataModel->expects($this->once())
518+
->method('validateValue')
519+
// only empty string
520+
->with(
521+
$this->logicalAnd(
522+
$this->isEmpty(),
523+
$this->isType('string')
524+
)
525+
)->willReturn(true);
526+
$this->attrDataFactory->expects($this->once())
527+
->method('create')
528+
->with($attribute, $entity)
529+
->willReturn($dataModel);
530+
$this->assertEquals(true, $this->model->isValid($entity));
531+
}
469532
}

0 commit comments

Comments
 (0)