Skip to content

Commit 96f9822

Browse files
author
Alexander Akimov
authored
Merge pull request #1510 from magento-plankton/2.2-bugs
[Plankton] Bugs
2 parents 540f467 + 8d3fa00 commit 96f9822

File tree

5 files changed

+171
-12
lines changed

5 files changed

+171
-12
lines changed

app/code/Magento/Customer/Helper/Address.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use Magento\Customer\Api\AddressMetadataInterface;
99
use Magento\Customer\Api\CustomerMetadataInterface;
1010
use Magento\Customer\Api\Data\AttributeMetadataInterface;
11+
use Magento\Customer\Model\Metadata\AttributeResolver;
1112
use Magento\Directory\Model\Country\Format;
13+
use Magento\Framework\App\ObjectManager;
1214
use Magento\Framework\Exception\NoSuchEntityException;
1315

1416
/**
@@ -93,27 +95,35 @@ class Address extends \Magento\Framework\App\Helper\AbstractHelper
9395
*/
9496
protected $_addressConfig;
9597

98+
/**
99+
* @var AttributeResolver
100+
*/
101+
private $attributeResolver;
102+
96103
/**
97104
* @param \Magento\Framework\App\Helper\Context $context
98105
* @param \Magento\Framework\View\Element\BlockFactory $blockFactory
99106
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
100107
* @param CustomerMetadataInterface $customerMetadataService
101108
* @param AddressMetadataInterface $addressMetadataService
102109
* @param \Magento\Customer\Model\Address\Config $addressConfig
110+
* @param AttributeResolver|null $attributeResolver
103111
*/
104112
public function __construct(
105113
\Magento\Framework\App\Helper\Context $context,
106114
\Magento\Framework\View\Element\BlockFactory $blockFactory,
107115
\Magento\Store\Model\StoreManagerInterface $storeManager,
108116
CustomerMetadataInterface $customerMetadataService,
109117
AddressMetadataInterface $addressMetadataService,
110-
\Magento\Customer\Model\Address\Config $addressConfig
118+
\Magento\Customer\Model\Address\Config $addressConfig,
119+
AttributeResolver $attributeResolver = null
111120
) {
112121
$this->_blockFactory = $blockFactory;
113122
$this->_storeManager = $storeManager;
114123
$this->_customerMetadataService = $customerMetadataService;
115124
$this->_addressMetadataService = $addressMetadataService;
116125
$this->_addressConfig = $addressConfig;
126+
$this->attributeResolver = $attributeResolver ?: ObjectManager::getInstance()->get(AttributeResolver::class);
117127
parent::__construct($context);
118128
}
119129

@@ -391,4 +401,31 @@ public function isAttributeVisible($code)
391401
}
392402
return false;
393403
}
404+
405+
/**
406+
* Checks whether it is allowed to show an attribute on the form
407+
*
408+
* This check relies on the attribute's property 'getUsedInForms' which contains a list of forms
409+
* where allowed to render specified attribute.
410+
*
411+
* @param string $attributeCode
412+
* @param string $formName
413+
* @return bool
414+
*/
415+
public function isAttributeAllowedOnForm($attributeCode, $formName)
416+
{
417+
$isAllowed = false;
418+
$attributeMetadata = $this->_addressMetadataService->getAttributeMetadata($attributeCode);
419+
if ($attributeMetadata) {
420+
/** @var \Magento\Customer\Model\Attribute $attribute */
421+
$attribute = $this->attributeResolver->getModelByAttribute(
422+
\Magento\Customer\Api\AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS,
423+
$attributeMetadata
424+
);
425+
$usedInForms = $attribute->getUsedInForms();
426+
$isAllowed = in_array($formName, $usedInForms, true);
427+
}
428+
429+
return $isAllowed;
430+
}
394431
}

app/code/Magento/Customer/Test/Unit/Helper/AddressTest.php

Lines changed: 82 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\Customer\Test\Unit\Helper;
88

99
use Magento\Customer\Api\AddressMetadataInterface;
10+
use Magento\Customer\Api\AddressMetadataManagementInterface;
1011
use Magento\Customer\Api\CustomerMetadataInterface;
1112

1213
/**
@@ -35,6 +36,9 @@ class AddressTest extends \PHPUnit\Framework\TestCase
3536
/** @var \Magento\Customer\Model\Address\Config|\PHPUnit_Framework_MockObject_MockObject */
3637
protected $addressConfig;
3738

39+
/** @var \Magento\Customer\Model\Metadata\AttributeResolver|\PHPUnit_Framework_MockObject_MockObject */
40+
protected $attributeResolver;
41+
3842
/** @var \PHPUnit_Framework_MockObject_MockObject|AddressMetadataInterface */
3943
private $addressMetadataService;
4044

@@ -51,6 +55,7 @@ protected function setUp()
5155
$this->customerMetadataService = $arguments['customerMetadataService'];
5256
$this->addressConfig = $arguments['addressConfig'];
5357
$this->addressMetadataService = $arguments['addressMetadataService'];
58+
$this->attributeResolver = $arguments['attributeResolver'];
5459

5560
$this->helper = $objectManagerHelper->getObject($className, $arguments);
5661
}
@@ -322,9 +327,11 @@ public function testGetFormatTypeRenderer($code, $result)
322327
$this->addressConfig->expects($this->once())
323328
->method('getFormatByCode')
324329
->with($code)
325-
->will($this->returnValue(
326-
new \Magento\Framework\DataObject($result !== null ? ['renderer' => $result] : [])
327-
));
330+
->will(
331+
$this->returnValue(
332+
new \Magento\Framework\DataObject($result !== null ? ['renderer' => $result] : [])
333+
)
334+
);
328335
$this->assertEquals($result, $this->helper->getFormatTypeRenderer($code));
329336
}
330337

@@ -334,7 +341,7 @@ public function getFormatTypeRendererDataProvider()
334341
->disableOriginalConstructor()->getMock();
335342
return [
336343
['valid_code', $renderer],
337-
['invalid_code', null]
344+
['invalid_code', null],
338345
];
339346
}
340347

@@ -355,9 +362,11 @@ public function testGetFormat($code, $result)
355362
$this->addressConfig->expects($this->once())
356363
->method('getFormatByCode')
357364
->with($code)
358-
->will($this->returnValue(
359-
new \Magento\Framework\DataObject(!empty($result) ? ['renderer' => $renderer] : [])
360-
));
365+
->will(
366+
$this->returnValue(
367+
new \Magento\Framework\DataObject(!empty($result) ? ['renderer' => $renderer] : [])
368+
)
369+
);
361370

362371
$this->assertEquals($result, $this->helper->getFormat($code));
363372
}
@@ -366,7 +375,7 @@ public function getFormatDataProvider()
366375
{
367376
return [
368377
['valid_code', ['key' => 'value']],
369-
['invalid_code', '']
378+
['invalid_code', ''],
370379
];
371380
}
372381

@@ -396,7 +405,71 @@ public function isAttributeVisibleDataProvider()
396405
{
397406
return [
398407
['fax', true],
399-
['invalid_code', false]
408+
['invalid_code', false],
409+
];
410+
}
411+
412+
/**
413+
* @dataProvider attributeOnFormDataProvider
414+
* @param bool $isAllowed
415+
* @param bool $isMetadataExists
416+
* @param string $attributeCode
417+
* @param string $formName
418+
* @param array $attributeFormsList
419+
*/
420+
public function testIsAttributeAllowedOnForm(
421+
$isAllowed,
422+
$isMetadataExists,
423+
$attributeCode,
424+
$formName,
425+
array $attributeFormsList
426+
) {
427+
$attributeMetadata = null;
428+
if ($isMetadataExists) {
429+
$attributeMetadata = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
430+
->getMockForAbstractClass();
431+
$attribute = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
432+
->disableOriginalConstructor()
433+
->getMock();
434+
$this->attributeResolver->expects($this->once())
435+
->method('getModelByAttribute')
436+
->with(AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS, $attributeMetadata)
437+
->willReturn($attribute);
438+
$attribute->expects($this->once())
439+
->method('getUsedInForms')
440+
->willReturn($attributeFormsList);
441+
}
442+
$this->addressMetadataService->expects($this->once())
443+
->method('getAttributeMetadata')
444+
->with($attributeCode)
445+
->willReturn($attributeMetadata);
446+
$this->assertEquals($isAllowed, $this->helper->isAttributeAllowedOnForm($attributeCode, $formName));
447+
}
448+
449+
public function attributeOnFormDataProvider()
450+
{
451+
return [
452+
'metadata not exists' => [
453+
'isAllowed' => false,
454+
'isMetadataExists' => false,
455+
'attributeCode' => 'attribute_code',
456+
'formName' => 'form_name',
457+
'attributeFormsList' => [],
458+
],
459+
'form not in the list' => [
460+
'isAllowed' => false,
461+
'isMetadataExists' => true,
462+
'attributeCode' => 'attribute_code',
463+
'formName' => 'form_name',
464+
'attributeFormsList' => ['form_1', 'form_2'],
465+
],
466+
'allowed' => [
467+
'isAllowed' => true,
468+
'isMetadataExists' => true,
469+
'attributeCode' => 'attribute_code',
470+
'formName' => 'form_name',
471+
'attributeFormsList' => ['form_name', 'form_1', 'form_2'],
472+
],
400473
];
401474
}
402475
}

dev/tests/functional/tests/app/Magento/Customer/Test/Block/Address/Edit.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class Edit extends Form
3030
*/
3131
protected $vatFieldId = 'vat_id';
3232

33+
/**
34+
* Locator for address simple (input, textarea, not multiple fields) attribute
35+
*
36+
* @var string
37+
*/
38+
private $addressSimpleAttribute = "[name='%s']";
39+
3340
/**
3441
* Edit customer address
3542
*
@@ -77,4 +84,15 @@ protected function dataMapping(array $fields = null, $parent = null)
7784
}
7885
return parent::dataMapping($fields, $parent);
7986
}
87+
88+
/**
89+
* Check if Customer Address Simple(input, textarea, not multiple fields) Attribute visible
90+
*
91+
* @param string $attributeCode
92+
* @return bool
93+
*/
94+
public function isAddressSimpleAttributeVisible($attributeCode)
95+
{
96+
return $this->_rootElement->find(sprintf($this->addressSimpleAttribute, $attributeCode))->isVisible();
97+
}
8098
}

lib/internal/Magento/Framework/Data/Form/Element/Multiselect.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public function getElementHtml()
5959
if ($this->getCanBeEmpty()) {
6060
$html .= '<input type="hidden" name="' . parent::getName() . '" value="" />';
6161
}
62+
if (!empty($this->_data['disabled'])) {
63+
$html .= '<input type="hidden" name="' . parent::getName() . '_disabled" value="" />';
64+
}
65+
6266
$html .= '<select id="' . $this->getHtmlId() . '" name="' . $this->getName() . '" ' . $this->serialize(
6367
$this->getHtmlAttributes()
6468
) . $this->_getUiId() . ' multiple="multiple">' . "\n";

lib/internal/Magento/Framework/Data/Test/Unit/Form/Element/MultiselectTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,37 @@ protected function setUp()
2626
*/
2727
public function testHiddenFieldPresentInMultiSelect()
2828
{
29-
$this->_model->setDisabled(true);
29+
$fieldName = 'fieldName';
3030
$this->_model->setCanBeEmpty(true);
31+
$this->_model->setName($fieldName);
32+
$elementHtml = $this->_model->getElementHtml();
33+
$this->assertContains('<input type="hidden" name="' . $fieldName . '"', $elementHtml);
34+
}
35+
36+
/**
37+
* Verify that hidden input is present in multiselect and it allow indicate is multiselect is disabled.
38+
*/
39+
public function testHiddenDisabledFieldPresentInMultiSelect()
40+
{
41+
$fieldName = 'fieldName';
42+
$this->_model->setDisabled(true);
43+
$this->_model->setName($fieldName);
44+
$elementHtml = $this->_model->getElementHtml();
45+
$this->assertContains('<input type="hidden" name="' . $fieldName . '_disabled"', $elementHtml);
46+
}
47+
48+
/**
49+
* Verify that hidden input doesn't present in multiselect and it allow indicate is multiselect is disabled.
50+
*
51+
* @covers \Magento\Framework\Data\Form\Element\Multiselect::getElementHtml
52+
*/
53+
public function testHiddenDisabledFieldNotPresentInMultiSelect()
54+
{
55+
$fieldName = 'fieldName';
56+
$this->_model->setDisabled(false);
57+
$this->_model->setName($fieldName);
3158
$elementHtml = $this->_model->getElementHtml();
32-
$this->assertContains('<input type="hidden"', $elementHtml);
59+
$this->assertNotContains('<input type="hidden" name="' . $fieldName . '_disabled"', $elementHtml);
3360
}
3461

3562
/**

0 commit comments

Comments
 (0)