Skip to content

Commit a872f40

Browse files
committed
MAGETWO-81153: Backward incompatible changes of 2.2.1 release
1 parent 0edacbc commit a872f40

File tree

4 files changed

+184
-107
lines changed

4 files changed

+184
-107
lines changed

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

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
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;
1211
use Magento\Directory\Model\Country\Format;
13-
use Magento\Framework\App\ObjectManager;
1412
use Magento\Framework\Exception\NoSuchEntityException;
1513

1614
/**
@@ -95,35 +93,27 @@ class Address extends \Magento\Framework\App\Helper\AbstractHelper
9593
*/
9694
protected $_addressConfig;
9795

98-
/**
99-
* @var AttributeResolver
100-
*/
101-
private $attributeResolver;
102-
10396
/**
10497
* @param \Magento\Framework\App\Helper\Context $context
10598
* @param \Magento\Framework\View\Element\BlockFactory $blockFactory
10699
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
107100
* @param CustomerMetadataInterface $customerMetadataService
108101
* @param AddressMetadataInterface $addressMetadataService
109102
* @param \Magento\Customer\Model\Address\Config $addressConfig
110-
* @param AttributeResolver|null $attributeResolver
111103
*/
112104
public function __construct(
113105
\Magento\Framework\App\Helper\Context $context,
114106
\Magento\Framework\View\Element\BlockFactory $blockFactory,
115107
\Magento\Store\Model\StoreManagerInterface $storeManager,
116108
CustomerMetadataInterface $customerMetadataService,
117109
AddressMetadataInterface $addressMetadataService,
118-
\Magento\Customer\Model\Address\Config $addressConfig,
119-
AttributeResolver $attributeResolver = null
110+
\Magento\Customer\Model\Address\Config $addressConfig
120111
) {
121112
$this->_blockFactory = $blockFactory;
122113
$this->_storeManager = $storeManager;
123114
$this->_customerMetadataService = $customerMetadataService;
124115
$this->_addressMetadataService = $addressMetadataService;
125116
$this->_addressConfig = $addressConfig;
126-
$this->attributeResolver = $attributeResolver ?: ObjectManager::getInstance()->get(AttributeResolver::class);
127117
parent::__construct($context);
128118
}
129119

@@ -401,31 +391,4 @@ public function isAttributeVisible($code)
401391
}
402392
return false;
403393
}
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-
}
431394
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Helper;
7+
8+
use Magento\Customer\Api\AddressMetadataInterface;
9+
use Magento\Customer\Api\AddressMetadataManagementInterface;
10+
use Magento\Customer\Model\Attribute;
11+
use Magento\Customer\Model\Metadata\AttributeResolver;
12+
use Magento\Framework\App\Helper\AbstractHelper;
13+
use Magento\Framework\App\Helper\Context;
14+
15+
/**
16+
* Customer attribute checker.
17+
*/
18+
class AttributeChecker extends AbstractHelper
19+
{
20+
/**
21+
* @var AddressMetadataInterface
22+
*/
23+
private $addressMetadata;
24+
25+
/**
26+
* @var AttributeResolver
27+
*/
28+
private $attributeResolver;
29+
30+
/**
31+
* @param Context $context
32+
* @param AddressMetadataInterface $addressMetadata
33+
* @param AttributeResolver $attributeResolver
34+
*/
35+
public function __construct(
36+
Context $context,
37+
AddressMetadataInterface $addressMetadata,
38+
AttributeResolver $attributeResolver
39+
) {
40+
$this->addressMetadata = $addressMetadata;
41+
$this->attributeResolver = $attributeResolver;
42+
parent::__construct($context);
43+
}
44+
45+
/**
46+
* Checks whether it is allowed to show an attribute on the form
47+
*
48+
* This check relies on the attribute's property 'getUsedInForms' which contains a list of forms
49+
* where allowed to render specified attribute.
50+
*
51+
* @param string $attributeCode
52+
* @param string $formName
53+
* @return bool
54+
*/
55+
public function isAttributeAllowedOnForm($attributeCode, $formName)
56+
{
57+
$isAllowed = false;
58+
$attributeMetadata = $this->addressMetadata->getAttributeMetadata($attributeCode);
59+
if ($attributeMetadata) {
60+
/** @var Attribute $attribute */
61+
$attribute = $this->attributeResolver->getModelByAttribute(
62+
AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS,
63+
$attributeMetadata
64+
);
65+
$usedInForms = $attribute->getUsedInForms();
66+
$isAllowed = in_array($formName, $usedInForms, true);
67+
}
68+
69+
return $isAllowed;
70+
}
71+
}

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

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

99
use Magento\Customer\Api\AddressMetadataInterface;
10-
use Magento\Customer\Api\AddressMetadataManagementInterface;
1110
use Magento\Customer\Api\CustomerMetadataInterface;
1211

1312
/**
@@ -36,9 +35,6 @@ class AddressTest extends \PHPUnit\Framework\TestCase
3635
/** @var \Magento\Customer\Model\Address\Config|\PHPUnit_Framework_MockObject_MockObject */
3736
protected $addressConfig;
3837

39-
/** @var \Magento\Customer\Model\Metadata\AttributeResolver|\PHPUnit_Framework_MockObject_MockObject */
40-
protected $attributeResolver;
41-
4238
/** @var \PHPUnit_Framework_MockObject_MockObject|AddressMetadataInterface */
4339
private $addressMetadataService;
4440

@@ -55,7 +51,6 @@ protected function setUp()
5551
$this->customerMetadataService = $arguments['customerMetadataService'];
5652
$this->addressConfig = $arguments['addressConfig'];
5753
$this->addressMetadataService = $arguments['addressMetadataService'];
58-
$this->attributeResolver = $arguments['attributeResolver'];
5954

6055
$this->helper = $objectManagerHelper->getObject($className, $arguments);
6156
}
@@ -408,68 +403,4 @@ public function isAttributeVisibleDataProvider()
408403
['invalid_code', false],
409404
];
410405
}
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-
],
473-
];
474-
}
475406
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Test\Unit\Helper;
8+
9+
use Magento\Customer\Api\AddressMetadataInterface;
10+
use Magento\Customer\Api\AddressMetadataManagementInterface;
11+
use Magento\Customer\Api\Data\AttributeMetadataInterface;
12+
use Magento\Customer\Helper\AttributeChecker;
13+
use Magento\Customer\Model\Attribute;
14+
use Magento\Customer\Model\Metadata\AttributeResolver;
15+
use Magento\Framework\App\Helper\Context;
16+
17+
class AttributeCheckerTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/** @var AttributeChecker|\PHPUnit_Framework_MockObject_MockObject */
20+
private $model;
21+
22+
/** @var Context */
23+
private $context;
24+
25+
/** @var AttributeResolver|\PHPUnit_Framework_MockObject_MockObject */
26+
private $attributeResolver;
27+
28+
/** @var AddressMetadataInterface|\PHPUnit_Framework_MockObject_MockObject */
29+
private $addressMetadataService;
30+
31+
protected function setUp()
32+
{
33+
$this->context = $this->getMockBuilder(Context::class)
34+
->disableOriginalConstructor()
35+
->getMock();
36+
$this->addressMetadataService = $this->getMockForAbstractClass(AddressMetadataInterface::class);
37+
$this->attributeResolver = $this->getMockBuilder(AttributeResolver::class)
38+
->disableOriginalConstructor()
39+
->getMock();
40+
41+
$this->model = new AttributeChecker(
42+
$this->context,
43+
$this->addressMetadataService,
44+
$this->attributeResolver
45+
);
46+
}
47+
48+
/**
49+
* @param bool $isAllowed
50+
* @param bool $isMetadataExists
51+
* @param string $attributeCode
52+
* @param string $formName
53+
* @param array $attributeFormsList
54+
*
55+
* @dataProvider attributeOnFormDataProvider
56+
*/
57+
public function testIsAttributeAllowedOnForm(
58+
$isAllowed,
59+
$isMetadataExists,
60+
$attributeCode,
61+
$formName,
62+
array $attributeFormsList
63+
) {
64+
$attributeMetadata = null;
65+
if ($isMetadataExists) {
66+
$attributeMetadata = $this->getMockForAbstractClass(AttributeMetadataInterface::class);
67+
$attribute = $this->getMockBuilder(Attribute::class)
68+
->disableOriginalConstructor()
69+
->getMock();
70+
$this->attributeResolver->expects($this->once())
71+
->method('getModelByAttribute')
72+
->with(AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS, $attributeMetadata)
73+
->willReturn($attribute);
74+
$attribute->expects($this->once())
75+
->method('getUsedInForms')
76+
->willReturn($attributeFormsList);
77+
}
78+
$this->addressMetadataService->expects($this->once())
79+
->method('getAttributeMetadata')
80+
->with($attributeCode)
81+
->willReturn($attributeMetadata);
82+
83+
$this->assertEquals($isAllowed, $this->model->isAttributeAllowedOnForm($attributeCode, $formName));
84+
}
85+
86+
public function attributeOnFormDataProvider()
87+
{
88+
return [
89+
'metadata not exists' => [
90+
'isAllowed' => false,
91+
'isMetadataExists' => false,
92+
'attributeCode' => 'attribute_code',
93+
'formName' => 'form_name',
94+
'attributeFormsList' => [],
95+
],
96+
'form not in the list' => [
97+
'isAllowed' => false,
98+
'isMetadataExists' => true,
99+
'attributeCode' => 'attribute_code',
100+
'formName' => 'form_name',
101+
'attributeFormsList' => ['form_1', 'form_2'],
102+
],
103+
'allowed' => [
104+
'isAllowed' => true,
105+
'isMetadataExists' => true,
106+
'attributeCode' => 'attribute_code',
107+
'formName' => 'form_name',
108+
'attributeFormsList' => ['form_name', 'form_1', 'form_2'],
109+
],
110+
];
111+
}
112+
}

0 commit comments

Comments
 (0)