Skip to content

Commit 9150a7a

Browse files
committed
Merge branch 'MAGETWO-90576' into MPI-PR
2 parents 2917519 + c9c08a3 commit 9150a7a

File tree

2 files changed

+82
-28
lines changed
  • app/code/Magento/Catalog
    • Test/Unit/Ui/DataProvider/Product/Form/Modifier
    • Ui/DataProvider/Product/Form/Modifier

2 files changed

+82
-28
lines changed

app/code/Magento/Catalog/Test/Unit/Ui/DataProvider/Product/Form/Modifier/EavTest.php

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\Catalog\Model\Product\Type;
99
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav;
1010
use Magento\Eav\Model\Config;
11+
use Magento\Eav\Model\Entity\Attribute\Source\SourceInterface;
1112
use Magento\Framework\App\RequestInterface;
1213
use Magento\Framework\EntityManager\EventManager;
1314
use Magento\Store\Model\StoreManagerInterface;
@@ -256,7 +257,15 @@ protected function setUp()
256257
$this->searchResultsMock = $this->getMockBuilder(SearchResultsInterface::class)
257258
->getMockForAbstractClass();
258259
$this->eavAttributeMock = $this->getMockBuilder(Attribute::class)
259-
->setMethods(['load', 'getAttributeGroupCode', 'getApplyTo', 'getFrontendInput', 'getAttributeCode'])
260+
->setMethods([
261+
'load',
262+
'getAttributeGroupCode',
263+
'getApplyTo',
264+
'getFrontendInput',
265+
'getAttributeCode',
266+
'usesSource',
267+
'getSource'
268+
])
260269
->disableOriginalConstructor()
261270
->getMock();
262271
$this->productAttributeMock = $this->getMockBuilder(ProductAttributeInterface::class)
@@ -450,72 +459,100 @@ public function testModifyData()
450459
}
451460

452461
/**
453-
* @param int $productId
462+
* @param int|null $productId
454463
* @param bool $productRequired
455-
* @param string $attrValue
464+
* @param string|null $attrValue
456465
* @param array $expected
457466
* @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav::isProductExists
458467
* @covers \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav::setupAttributeMeta
459468
* @dataProvider setupAttributeMetaDataProvider
460469
*/
461-
public function testSetupAttributeMetaDefaultAttribute($productId, $productRequired, $attrValue, $expected)
462-
{
463-
$configPath = 'arguments/data/config';
470+
public function testSetupAttributeMetaDefaultAttribute(
471+
$productId,
472+
$productRequired,
473+
$attrValue,
474+
$expected
475+
) {
476+
$configPath = 'arguments/data/config';
464477
$groupCode = 'product-details';
465478
$sortOrder = '0';
479+
$attributeOptions = [
480+
['value' => 1, 'label' => 'Int label'],
481+
['value' => 1.5, 'label' => 'Float label'],
482+
['value' => true, 'label' => 'Boolean label'],
483+
['value' => 'string', 'label' => 'String label'],
484+
['value' => ['test1', 'test2'], 'label' => 'Array label']
485+
];
486+
$attributeOptionsExpected = [
487+
['value' => '1', 'label' => 'Int label'],
488+
['value' => '1.5', 'label' => 'Float label'],
489+
['value' => '1', 'label' => 'Boolean label'],
490+
['value' => 'string', 'label' => 'String label'],
491+
['value' => ['test1', 'test2'], 'label' => 'Array label']
492+
];
466493

467-
$this->productMock->expects($this->any())
468-
->method('getId')
494+
$this->productMock->method('getId')
469495
->willReturn($productId);
470496

471-
$this->productAttributeMock->expects($this->any())
472-
->method('getIsRequired')
497+
$this->productAttributeMock->method('getIsRequired')
473498
->willReturn($productRequired);
474499

475-
$this->productAttributeMock->expects($this->any())
476-
->method('getDefaultValue')
500+
$this->productAttributeMock->method('getDefaultValue')
477501
->willReturn('required_value');
478502

479-
$this->productAttributeMock->expects($this->any())
480-
->method('getAttributeCode')
503+
$this->productAttributeMock->method('getAttributeCode')
481504
->willReturn('code');
482505

483-
$this->productAttributeMock->expects($this->any())
484-
->method('getValue')
506+
$this->productAttributeMock->method('getValue')
485507
->willReturn('value');
486508

487509
$attributeMock = $this->getMockBuilder(AttributeInterface::class)
488510
->setMethods(['getValue'])
489511
->disableOriginalConstructor()
490512
->getMockForAbstractClass();
491513

492-
$attributeMock->expects($this->any())
493-
->method('getValue')
514+
$attributeMock->method('getValue')
494515
->willReturn($attrValue);
495516

496-
$this->productMock->expects($this->any())
497-
->method('getCustomAttribute')
517+
$this->productMock->method('getCustomAttribute')
498518
->willReturn($attributeMock);
519+
$this->eavAttributeMock->method('usesSource')
520+
->willReturn(true);
521+
522+
$attributeSource = $this->getMockBuilder(SourceInterface::class)
523+
->getMockForAbstractClass();
524+
$attributeSource->method('getAllOptions')
525+
->willReturn($attributeOptions);
499526

500-
$this->arrayManagerMock->expects($this->any())
501-
->method('set')
527+
$this->eavAttributeMock->method('getSource')
528+
->willReturn($attributeSource);
529+
530+
$this->arrayManagerMock->method('set')
502531
->with(
503532
$configPath,
504533
[],
505534
$expected
506535
)
507536
->willReturn($expected);
508537

509-
$this->arrayManagerMock->expects($this->any())
538+
$this->arrayManagerMock->expects($this->once())
510539
->method('merge')
540+
->with(
541+
$this->anything(),
542+
$this->anything(),
543+
$this->callback(
544+
function ($value) use ($attributeOptionsExpected) {
545+
return $value['options'] === $attributeOptionsExpected;
546+
}
547+
)
548+
)
511549
->willReturn($expected);
512550

513-
$this->arrayManagerMock->expects($this->any())
514-
->method('get')
551+
$this->arrayManagerMock->method('get')
515552
->willReturn([]);
516553

517-
$this->arrayManagerMock->expects($this->any())
518-
->method('exists');
554+
$this->arrayManagerMock->method('exists')
555+
->willReturn(true);
519556

520557
$this->assertEquals(
521558
$expected,

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,9 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
591591
// TODO: Refactor to $attribute->getOptions() when MAGETWO-48289 is done
592592
$attributeModel = $this->getAttributeModel($attribute);
593593
if ($attributeModel->usesSource()) {
594+
$options = $attributeModel->getSource()->getAllOptions();
594595
$meta = $this->arrayManager->merge($configPath, $meta, [
595-
'options' => $attributeModel->getSource()->getAllOptions(),
596+
'options' => $this->convertOptionsValueToString($options),
596597
]);
597598
}
598599

@@ -645,6 +646,22 @@ public function setupAttributeMeta(ProductAttributeInterface $attribute, $groupC
645646
return $meta;
646647
}
647648

649+
/**
650+
* Convert options value to string
651+
*
652+
* @param array $options
653+
* @return array
654+
*/
655+
private function convertOptionsValueToString(array $options): array
656+
{
657+
array_walk($options, function (&$value) {
658+
if (isset($value['value']) && is_scalar($value['value'])) {
659+
$value['value'] = (string)$value['value'];
660+
}
661+
});
662+
return $options;
663+
}
664+
648665
/**
649666
* @param ProductAttributeInterface $attribute
650667
* @param array $meta

0 commit comments

Comments
 (0)