Skip to content

Commit

Permalink
Merge remote-tracking branch 'mainline/2.2-develop' into MAGETWO-72477-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan Korablov committed Sep 25, 2017
2 parents f866101 + 96f9822 commit 0f6795d
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 27 deletions.
6 changes: 5 additions & 1 deletion app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,11 @@ public function reindex()
if ($this->flatState->isFlatEnabled()) {
$flatIndexer = $this->indexerRegistry->get(Indexer\Category\Flat\State::INDEXER_ID);
if (!$flatIndexer->isScheduled()) {
$flatIndexer->reindexRow($this->getId());
$idsList = [$this->getId()];
if ($this->dataHasChangedFor('url_key')) {
$idsList = array_merge($idsList, explode(',', $this->getAllChildren()));
}
$flatIndexer->reindexList($idsList);
}
}
$productIndexer = $this->indexerRegistry->get(Indexer\Category\Product::INDEXER_ID);
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public function testReindexFlatEnabled($flatScheduled, $productScheduled, $expec
->will($this->returnValue(true));

$this->flatIndexer->expects($this->exactly(1))->method('isScheduled')->will($this->returnValue($flatScheduled));
$this->flatIndexer->expects($this->exactly($expectedFlatReindexCalls))->method('reindexRow')->with('123');
$this->flatIndexer->expects($this->exactly($expectedFlatReindexCalls))->method('reindexList')->with(['123']);

$this->productIndexer->expects($this->exactly(1))->method('isScheduled')->will($this->returnValue($productScheduled));
$this->productIndexer->expects($this->exactly($expectedProductReindexCall))->method('reindexList')->with($pathIds);
Expand Down
39 changes: 38 additions & 1 deletion app/code/Magento/Customer/Helper/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
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 @@ -93,27 +95,35 @@ 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
\Magento\Customer\Model\Address\Config $addressConfig,
AttributeResolver $attributeResolver = null
) {
$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 @@ -391,4 +401,31 @@ 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;
}
}
91 changes: 82 additions & 9 deletions app/code/Magento/Customer/Test/Unit/Helper/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 @@ -35,6 +36,9 @@ 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 @@ -51,6 +55,7 @@ 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 @@ -322,9 +327,11 @@ public function testGetFormatTypeRenderer($code, $result)
$this->addressConfig->expects($this->once())
->method('getFormatByCode')
->with($code)
->will($this->returnValue(
new \Magento\Framework\DataObject($result !== null ? ['renderer' => $result] : [])
));
->will(
$this->returnValue(
new \Magento\Framework\DataObject($result !== null ? ['renderer' => $result] : [])
)
);
$this->assertEquals($result, $this->helper->getFormatTypeRenderer($code));
}

Expand All @@ -334,7 +341,7 @@ public function getFormatTypeRendererDataProvider()
->disableOriginalConstructor()->getMock();
return [
['valid_code', $renderer],
['invalid_code', null]
['invalid_code', null],
];
}

Expand All @@ -355,9 +362,11 @@ public function testGetFormat($code, $result)
$this->addressConfig->expects($this->once())
->method('getFormatByCode')
->with($code)
->will($this->returnValue(
new \Magento\Framework\DataObject(!empty($result) ? ['renderer' => $renderer] : [])
));
->will(
$this->returnValue(
new \Magento\Framework\DataObject(!empty($result) ? ['renderer' => $renderer] : [])
)
);

$this->assertEquals($result, $this->helper->getFormat($code));
}
Expand All @@ -366,7 +375,7 @@ public function getFormatDataProvider()
{
return [
['valid_code', ['key' => 'value']],
['invalid_code', '']
['invalid_code', ''],
];
}

Expand Down Expand Up @@ -396,7 +405,71 @@ public function isAttributeVisibleDataProvider()
{
return [
['fax', true],
['invalid_code', false]
['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'],
],
];
}
}
32 changes: 32 additions & 0 deletions app/code/Magento/Swagger/Block/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Swagger\Block;

use Magento\Framework\View\Element\Template;

/**
* Class Index
*
* @api
*/
class Index extends Template
{
/**
* @return mixed|string
*/
private function getParamStore()
{
return ($this->getRequest()->getParam('store')) ? $this->getRequest()->getParam('store') : 'all';
}

/**
* @return string
*/
public function getSchemaUrl()
{
return rtrim($this->getBaseUrl(), '/') . '/rest/' . $this->getParamStore() . '/schema?services=all';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<referenceContainer name="page.wrapper" remove="true"/>
<referenceBlock name="requirejs-config" remove="true"/>
<referenceContainer name="root">
<block name="swaggerUiContent" class="Magento\Framework\View\Element\Template" template="Magento_Swagger::swagger-ui/index.phtml"/>
<block name="swaggerUiContent" class="Magento\Swagger\Block\Index" template="Magento_Swagger::swagger-ui/index.phtml"/>
</referenceContainer>
</body>
</page>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/** @var \Magento\Framework\View\Element\Template $block */

$schemaUrl = rtrim($block->getBaseUrl(), '/') . '/rest/all/schema?services=all';
$schemaUrl = $block->getSchemaUrl();
?>

<div id='header'>
Expand Down
47 changes: 37 additions & 10 deletions app/code/Magento/Theme/Model/Design/Config/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,8 @@ public function validate(DesignConfigInterface $designConfig)
}

foreach ($elements as $name => $data) {
// Load template object by configured template id
$template = $this->templateFactory->create();
$template->emulateDesign($designConfig->getScopeId());
$templateId = $data['value'];
if (is_numeric($templateId)) {
$template->load($templateId);
} else {
$template->loadDefault($templateId);
}
$text = $template->getTemplateText();
$template->revertDesign();
$text = $this->getTemplateText($templateId, $designConfig);
// Check if template body has a reference to the same config path
if (preg_match_all(Template::CONSTRUCTION_TEMPLATE_PATTERN, $text, $constructions, PREG_SET_ORDER)) {
foreach ($constructions as $construction) {
Expand All @@ -94,6 +85,42 @@ public function validate(DesignConfigInterface $designConfig)
}
}

/**
* Returns store identifier if is store scope
*
* @param DesignConfigInterface $designConfig
* @return string|bool
*/
private function getScopeId(DesignConfigInterface $designConfig)
{
if ($designConfig->getScope() == 'stores') {
return $designConfig->getScopeId();
}
return false;
}

/**
* Load template text in configured scope
*
* @param integer|string $templateId
* @param DesignConfigInterface $designConfig
* @return string
*/
private function getTemplateText($templateId, DesignConfigInterface $designConfig)
{
// Load template object by configured template id
$template = $this->templateFactory->create();
$template->emulateDesign($this->getScopeId($designConfig));
if (is_numeric($templateId)) {
$template->load($templateId);
} else {
$template->loadDefault($templateId);
}
$text = $template->getTemplateText();
$template->revertDesign();
return $text;
}

/**
* Return associative array of parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class Edit extends Form
*/
protected $vatFieldId = 'vat_id';

/**
* Locator for address simple (input, textarea, not multiple fields) attribute
*
* @var string
*/
private $addressSimpleAttribute = "[name='%s']";

/**
* Edit customer address
*
Expand Down Expand Up @@ -77,4 +84,15 @@ protected function dataMapping(array $fields = null, $parent = null)
}
return parent::dataMapping($fields, $parent);
}

/**
* Check if Customer Address Simple(input, textarea, not multiple fields) Attribute visible
*
* @param string $attributeCode
* @return bool
*/
public function isAddressSimpleAttributeVisible($attributeCode)
{
return $this->_rootElement->find(sprintf($this->addressSimpleAttribute, $attributeCode))->isVisible();
}
}
Loading

0 comments on commit 0f6795d

Please sign in to comment.