Skip to content

Commit

Permalink
MAGETWO-81153: Backward incompatible changes of 2.2.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
viktym committed Oct 18, 2017
1 parent 0edacbc commit a872f40
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 107 deletions.
39 changes: 1 addition & 38 deletions app/code/Magento/Customer/Helper/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\CustomerMetadataInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Model\Metadata\AttributeResolver;
use Magento\Directory\Model\Country\Format;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NoSuchEntityException;

/**
Expand Down Expand Up @@ -95,35 +93,27 @@ class Address extends \Magento\Framework\App\Helper\AbstractHelper
*/
protected $_addressConfig;

/**
* @var AttributeResolver
*/
private $attributeResolver;

/**
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Framework\View\Element\BlockFactory $blockFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param CustomerMetadataInterface $customerMetadataService
* @param AddressMetadataInterface $addressMetadataService
* @param \Magento\Customer\Model\Address\Config $addressConfig
* @param AttributeResolver|null $attributeResolver
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\View\Element\BlockFactory $blockFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
CustomerMetadataInterface $customerMetadataService,
AddressMetadataInterface $addressMetadataService,
\Magento\Customer\Model\Address\Config $addressConfig,
AttributeResolver $attributeResolver = null
\Magento\Customer\Model\Address\Config $addressConfig
) {
$this->_blockFactory = $blockFactory;
$this->_storeManager = $storeManager;
$this->_customerMetadataService = $customerMetadataService;
$this->_addressMetadataService = $addressMetadataService;
$this->_addressConfig = $addressConfig;
$this->attributeResolver = $attributeResolver ?: ObjectManager::getInstance()->get(AttributeResolver::class);
parent::__construct($context);
}

Expand Down Expand Up @@ -401,31 +391,4 @@ public function isAttributeVisible($code)
}
return false;
}

/**
* Checks whether it is allowed to show an attribute on the form
*
* This check relies on the attribute's property 'getUsedInForms' which contains a list of forms
* where allowed to render specified attribute.
*
* @param string $attributeCode
* @param string $formName
* @return bool
*/
public function isAttributeAllowedOnForm($attributeCode, $formName)
{
$isAllowed = false;
$attributeMetadata = $this->_addressMetadataService->getAttributeMetadata($attributeCode);
if ($attributeMetadata) {
/** @var \Magento\Customer\Model\Attribute $attribute */
$attribute = $this->attributeResolver->getModelByAttribute(
\Magento\Customer\Api\AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS,
$attributeMetadata
);
$usedInForms = $attribute->getUsedInForms();
$isAllowed = in_array($formName, $usedInForms, true);
}

return $isAllowed;
}
}
71 changes: 71 additions & 0 deletions app/code/Magento/Customer/Helper/AttributeChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Helper;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\AddressMetadataManagementInterface;
use Magento\Customer\Model\Attribute;
use Magento\Customer\Model\Metadata\AttributeResolver;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

/**
* Customer attribute checker.
*/
class AttributeChecker extends AbstractHelper
{
/**
* @var AddressMetadataInterface
*/
private $addressMetadata;

/**
* @var AttributeResolver
*/
private $attributeResolver;

/**
* @param Context $context
* @param AddressMetadataInterface $addressMetadata
* @param AttributeResolver $attributeResolver
*/
public function __construct(
Context $context,
AddressMetadataInterface $addressMetadata,
AttributeResolver $attributeResolver
) {
$this->addressMetadata = $addressMetadata;
$this->attributeResolver = $attributeResolver;
parent::__construct($context);
}

/**
* Checks whether it is allowed to show an attribute on the form
*
* This check relies on the attribute's property 'getUsedInForms' which contains a list of forms
* where allowed to render specified attribute.
*
* @param string $attributeCode
* @param string $formName
* @return bool
*/
public function isAttributeAllowedOnForm($attributeCode, $formName)
{
$isAllowed = false;
$attributeMetadata = $this->addressMetadata->getAttributeMetadata($attributeCode);
if ($attributeMetadata) {
/** @var Attribute $attribute */
$attribute = $this->attributeResolver->getModelByAttribute(
AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS,
$attributeMetadata
);
$usedInForms = $attribute->getUsedInForms();
$isAllowed = in_array($formName, $usedInForms, true);
}

return $isAllowed;
}
}
69 changes: 0 additions & 69 deletions app/code/Magento/Customer/Test/Unit/Helper/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
namespace Magento\Customer\Test\Unit\Helper;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\AddressMetadataManagementInterface;
use Magento\Customer\Api\CustomerMetadataInterface;

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

/** @var \Magento\Customer\Model\Metadata\AttributeResolver|\PHPUnit_Framework_MockObject_MockObject */
protected $attributeResolver;

/** @var \PHPUnit_Framework_MockObject_MockObject|AddressMetadataInterface */
private $addressMetadataService;

Expand All @@ -55,7 +51,6 @@ protected function setUp()
$this->customerMetadataService = $arguments['customerMetadataService'];
$this->addressConfig = $arguments['addressConfig'];
$this->addressMetadataService = $arguments['addressMetadataService'];
$this->attributeResolver = $arguments['attributeResolver'];

$this->helper = $objectManagerHelper->getObject($className, $arguments);
}
Expand Down Expand Up @@ -408,68 +403,4 @@ public function isAttributeVisibleDataProvider()
['invalid_code', false],
];
}

/**
* @dataProvider attributeOnFormDataProvider
* @param bool $isAllowed
* @param bool $isMetadataExists
* @param string $attributeCode
* @param string $formName
* @param array $attributeFormsList
*/
public function testIsAttributeAllowedOnForm(
$isAllowed,
$isMetadataExists,
$attributeCode,
$formName,
array $attributeFormsList
) {
$attributeMetadata = null;
if ($isMetadataExists) {
$attributeMetadata = $this->getMockBuilder(\Magento\Customer\Api\Data\AttributeMetadataInterface::class)
->getMockForAbstractClass();
$attribute = $this->getMockBuilder(\Magento\Customer\Model\Attribute::class)
->disableOriginalConstructor()
->getMock();
$this->attributeResolver->expects($this->once())
->method('getModelByAttribute')
->with(AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS, $attributeMetadata)
->willReturn($attribute);
$attribute->expects($this->once())
->method('getUsedInForms')
->willReturn($attributeFormsList);
}
$this->addressMetadataService->expects($this->once())
->method('getAttributeMetadata')
->with($attributeCode)
->willReturn($attributeMetadata);
$this->assertEquals($isAllowed, $this->helper->isAttributeAllowedOnForm($attributeCode, $formName));
}

public function attributeOnFormDataProvider()
{
return [
'metadata not exists' => [
'isAllowed' => false,
'isMetadataExists' => false,
'attributeCode' => 'attribute_code',
'formName' => 'form_name',
'attributeFormsList' => [],
],
'form not in the list' => [
'isAllowed' => false,
'isMetadataExists' => true,
'attributeCode' => 'attribute_code',
'formName' => 'form_name',
'attributeFormsList' => ['form_1', 'form_2'],
],
'allowed' => [
'isAllowed' => true,
'isMetadataExists' => true,
'attributeCode' => 'attribute_code',
'formName' => 'form_name',
'attributeFormsList' => ['form_name', 'form_1', 'form_2'],
],
];
}
}
112 changes: 112 additions & 0 deletions app/code/Magento/Customer/Test/Unit/Helper/AttributeCheckerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Customer\Test\Unit\Helper;

use Magento\Customer\Api\AddressMetadataInterface;
use Magento\Customer\Api\AddressMetadataManagementInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Helper\AttributeChecker;
use Magento\Customer\Model\Attribute;
use Magento\Customer\Model\Metadata\AttributeResolver;
use Magento\Framework\App\Helper\Context;

class AttributeCheckerTest extends \PHPUnit\Framework\TestCase
{
/** @var AttributeChecker|\PHPUnit_Framework_MockObject_MockObject */
private $model;

/** @var Context */
private $context;

/** @var AttributeResolver|\PHPUnit_Framework_MockObject_MockObject */
private $attributeResolver;

/** @var AddressMetadataInterface|\PHPUnit_Framework_MockObject_MockObject */
private $addressMetadataService;

protected function setUp()
{
$this->context = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
$this->addressMetadataService = $this->getMockForAbstractClass(AddressMetadataInterface::class);
$this->attributeResolver = $this->getMockBuilder(AttributeResolver::class)
->disableOriginalConstructor()
->getMock();

$this->model = new AttributeChecker(
$this->context,
$this->addressMetadataService,
$this->attributeResolver
);
}

/**
* @param bool $isAllowed
* @param bool $isMetadataExists
* @param string $attributeCode
* @param string $formName
* @param array $attributeFormsList
*
* @dataProvider attributeOnFormDataProvider
*/
public function testIsAttributeAllowedOnForm(
$isAllowed,
$isMetadataExists,
$attributeCode,
$formName,
array $attributeFormsList
) {
$attributeMetadata = null;
if ($isMetadataExists) {
$attributeMetadata = $this->getMockForAbstractClass(AttributeMetadataInterface::class);
$attribute = $this->getMockBuilder(Attribute::class)
->disableOriginalConstructor()
->getMock();
$this->attributeResolver->expects($this->once())
->method('getModelByAttribute')
->with(AddressMetadataManagementInterface::ENTITY_TYPE_ADDRESS, $attributeMetadata)
->willReturn($attribute);
$attribute->expects($this->once())
->method('getUsedInForms')
->willReturn($attributeFormsList);
}
$this->addressMetadataService->expects($this->once())
->method('getAttributeMetadata')
->with($attributeCode)
->willReturn($attributeMetadata);

$this->assertEquals($isAllowed, $this->model->isAttributeAllowedOnForm($attributeCode, $formName));
}

public function attributeOnFormDataProvider()
{
return [
'metadata not exists' => [
'isAllowed' => false,
'isMetadataExists' => false,
'attributeCode' => 'attribute_code',
'formName' => 'form_name',
'attributeFormsList' => [],
],
'form not in the list' => [
'isAllowed' => false,
'isMetadataExists' => true,
'attributeCode' => 'attribute_code',
'formName' => 'form_name',
'attributeFormsList' => ['form_1', 'form_2'],
],
'allowed' => [
'isAllowed' => true,
'isMetadataExists' => true,
'attributeCode' => 'attribute_code',
'formName' => 'form_name',
'attributeFormsList' => ['form_name', 'form_1', 'form_2'],
],
];
}
}

0 comments on commit a872f40

Please sign in to comment.