Skip to content

Commit

Permalink
Merge pull request #4168 from magento-tango/PR-05
Browse files Browse the repository at this point in the history
Tango PR-05
  • Loading branch information
dhorytskyi authored May 7, 2019
2 parents 6a9860f + a12ecfd commit bd82ed4
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 107 deletions.
9 changes: 3 additions & 6 deletions app/code/Magento/Catalog/Block/Product/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ public function getAddToCartUrl($product, $additional = [])
}

/**
* Get JSON encoded configuration array which can be used for JS dynamic
* price calculation depending on product options
* Get JSON encoded configuration which can be used for JS dynamic price calculation depending on product options
*
* @return string
*/
Expand Down Expand Up @@ -262,6 +261,7 @@ public function isStartCustomization()

/**
* Get default qty - either as preconfigured, or as 1.
*
* Also restricts it by minimal qty.
*
* @param null|\Magento\Catalog\Model\Product $product
Expand Down Expand Up @@ -323,10 +323,7 @@ public function getQuantityValidators()
public function getIdentities()
{
$identities = $this->getProduct()->getIdentities();
$category = $this->_coreRegistry->registry('current_category');
if ($category) {
$identities[] = Category::CACHE_TAG . '_' . $category->getId();
}

return $identities;
}

Expand Down
19 changes: 13 additions & 6 deletions app/code/Magento/Catalog/Test/Unit/Block/Product/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Magento\Catalog\Test\Unit\Block\Product;

/**
* Class ViewTest
*/
class ViewTest extends \PHPUnit\Framework\TestCase
{
/**
Expand All @@ -25,6 +28,9 @@ class ViewTest extends \PHPUnit\Framework\TestCase
*/
protected $registryMock;

/**
* @inheritDoc
*/
protected function setUp()
{
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand All @@ -36,6 +42,9 @@ protected function setUp()
);
}

/**
* @return void
*/
public function testShouldRenderQuantity()
{
$productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
Expand All @@ -61,28 +70,26 @@ public function testShouldRenderQuantity()
$this->assertEquals(false, $this->view->shouldRenderQuantity());
}

/**
* @return void
*/
public function testGetIdentities()
{
$productTags = ['cat_p_1'];
$product = $this->createMock(\Magento\Catalog\Model\Product::class);
$category = $this->createMock(\Magento\Catalog\Model\Category::class);

$product->expects($this->once())
->method('getIdentities')
->will($this->returnValue($productTags));
$category->expects($this->once())
->method('getId')
->will($this->returnValue(1));
$this->registryMock->expects($this->any())
->method('registry')
->will(
$this->returnValueMap(
[
['product', $product],
['current_category', $category],
]
)
);
$this->assertEquals(['cat_p_1', 'cat_c_1'], $this->view->getIdentities());
$this->assertEquals($productTags, $this->view->getIdentities());
}
}
4 changes: 2 additions & 2 deletions app/code/Magento/Customer/Model/Address/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

/**
* Dataprovider of customer addresses for customer address grid.
*
* @property \Magento\Customer\Model\ResourceModel\Address\Collection $collection
*/
class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
Expand Down Expand Up @@ -222,8 +223,7 @@ private function getAttributesMeta(Type $entityType): array
$meta[$attribute->getAttributeCode()] = $this->attributeMetadataResolver->getAttributesMeta(
$attribute,
$entityType,
$this->allowToShowHiddenAttributes,
$this->getRequestFieldName()
$this->allowToShowHiddenAttributes
);
}
$this->attributeMetadataResolver->processWebsiteMeta($meta);
Expand Down
43 changes: 4 additions & 39 deletions app/code/Magento/Customer/Model/AttributeMetadataResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,13 @@ public function __construct(
* @param AbstractAttribute $attribute
* @param Type $entityType
* @param bool $allowToShowHiddenAttributes
* @param string $requestFieldName
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAttributesMeta(
AbstractAttribute $attribute,
Type $entityType,
bool $allowToShowHiddenAttributes,
string $requestFieldName
bool $allowToShowHiddenAttributes
): array {
$meta = $this->modifyBooleanAttributeMeta($attribute);
// use getDataUsingMethod, since some getters are defined and apply additional processing of returning value
Expand Down Expand Up @@ -138,7 +136,6 @@ public function getAttributesMeta(
$meta['arguments']['data']['config']['componentType'] = Field::NAME;
$meta['arguments']['data']['config']['visible'] = $this->canShowAttribute(
$attribute,
$requestFieldName,
$allowToShowHiddenAttributes
);

Expand All @@ -155,48 +152,16 @@ public function getAttributesMeta(
* Detect can we show attribute on specific form or not
*
* @param AbstractAttribute $customerAttribute
* @param string $requestFieldName
* @param bool $allowToShowHiddenAttributes
* @return bool
*/
private function canShowAttribute(
AbstractAttribute $customerAttribute,
string $requestFieldName,
bool $allowToShowHiddenAttributes
) {
$userDefined = (bool)$customerAttribute->getIsUserDefined();
if (!$userDefined) {
return $customerAttribute->getIsVisible();
}

$canShowOnForm = $this->canShowAttributeInForm($customerAttribute, $requestFieldName);

return ($allowToShowHiddenAttributes && $canShowOnForm) ||
(!$allowToShowHiddenAttributes && $canShowOnForm && $customerAttribute->getIsVisible());
}

/**
* Check whether the specific attribute can be shown in form: customer registration, customer edit, etc...
*
* @param AbstractAttribute $customerAttribute
* @param string $requestFieldName
* @return bool
*/
private function canShowAttributeInForm(AbstractAttribute $customerAttribute, string $requestFieldName): bool
{
$isRegistration = $this->context->getRequestParam($requestFieldName) === null;

if ($customerAttribute->getEntityType()->getEntityTypeCode() === 'customer') {
return \is_array($customerAttribute->getUsedInForms()) &&
(
(\in_array('customer_account_create', $customerAttribute->getUsedInForms(), true)
&& $isRegistration) ||
(\in_array('customer_account_edit', $customerAttribute->getUsedInForms(), true)
&& !$isRegistration)
);
}
return \is_array($customerAttribute->getUsedInForms()) &&
\in_array('customer_address_edit', $customerAttribute->getUsedInForms(), true);
return $allowToShowHiddenAttributes && (bool) $customerAttribute->getIsUserDefined()
? true
: (bool) $customerAttribute->getIsVisible();
}

/**
Expand Down
36 changes: 4 additions & 32 deletions app/code/Magento/Customer/Model/Customer/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,45 +307,17 @@ protected function getAttributesMeta(Type $entityType)
return $meta;
}

/**
* Check whether the specific attribute can be shown in form: customer registration, customer edit, etc...
*
* @param Attribute $customerAttribute
* @return bool
*/
private function canShowAttributeInForm(AbstractAttribute $customerAttribute)
{
$isRegistration = $this->context->getRequestParam($this->getRequestFieldName()) === null;

if ($customerAttribute->getEntityType()->getEntityTypeCode() === 'customer') {
return is_array($customerAttribute->getUsedInForms()) &&
(
(in_array('customer_account_create', $customerAttribute->getUsedInForms()) && $isRegistration) ||
(in_array('customer_account_edit', $customerAttribute->getUsedInForms()) && !$isRegistration)
);
} else {
return is_array($customerAttribute->getUsedInForms()) &&
in_array('customer_address_edit', $customerAttribute->getUsedInForms());
}
}

/**
* Detect can we show attribute on specific form or not
*
* @param Attribute $customerAttribute
* @return bool
*/
private function canShowAttribute(AbstractAttribute $customerAttribute)
private function canShowAttribute(AbstractAttribute $customerAttribute): bool
{
$userDefined = (bool) $customerAttribute->getIsUserDefined();
if (!$userDefined) {
return $customerAttribute->getIsVisible();
}

$canShowOnForm = $this->canShowAttributeInForm($customerAttribute);

return ($this->allowToShowHiddenAttributes && $canShowOnForm) ||
(!$this->allowToShowHiddenAttributes && $canShowOnForm && $customerAttribute->getIsVisible());
return $this->allowToShowHiddenAttributes && (bool) $customerAttribute->getIsUserDefined()
? true
: (bool) $customerAttribute->getIsVisible();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ private function getAttributesMeta(Type $entityType): array
$meta[$attribute->getAttributeCode()] = $this->attributeMetadataResolver->getAttributesMeta(
$attribute,
$entityType,
$this->allowToShowHiddenAttributes,
$this->getRequestFieldName()
$this->allowToShowHiddenAttributes
);
}
$this->attributeMetadataResolver->processWebsiteMeta($meta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1087,16 +1087,15 @@ public function testGetDataWithVisibleAttributesWithAccountEdit()

$meta = $dataProvider->getMeta();
$this->assertNotEmpty($meta);
$this->assertEquals($this->getExpectationForVisibleAttributes(false), $meta);
$this->assertEquals($this->getExpectationForVisibleAttributes(), $meta);
}

/**
* Retrieve all customer variations of attributes with all variations of visibility
*
* @param bool $isRegistration
* @return array
*/
private function getCustomerAttributeExpectations($isRegistration)
private function getCustomerAttributeExpectations()
{
return [
self::ATTRIBUTE_CODE . "_1" => [
Expand All @@ -1106,7 +1105,7 @@ private function getCustomerAttributeExpectations($isRegistration)
'dataType' => 'frontend_input',
'formElement' => 'frontend_input',
'options' => 'test-options',
'visible' => !$isRegistration,
'visible' => true,
'required' => 'is_required',
'label' => __('frontend_label'),
'sortOrder' => 'sort_order',
Expand Down Expand Up @@ -1143,7 +1142,7 @@ private function getCustomerAttributeExpectations($isRegistration)
'config' => [
'dataType' => 'frontend_input',
'formElement' => 'frontend_input',
'visible' => $isRegistration,
'visible' => true,
'required' => 'is_required',
'label' => __('frontend_label'),
'sortOrder' => 'sort_order',
Expand All @@ -1166,7 +1165,7 @@ private function getCustomerAttributeExpectations($isRegistration)
'config' => [
'dataType' => 'frontend_input',
'formElement' => 'frontend_input',
'visible' => $isRegistration,
'visible' => true,
'required' => 'is_required',
'label' => __('frontend_label'),
'sortOrder' => 'sort_order',
Expand All @@ -1189,14 +1188,13 @@ private function getCustomerAttributeExpectations($isRegistration)
/**
* Retrieve all variations of attributes with all variations of visibility
*
* @param bool $isRegistration
* @return array
*/
private function getExpectationForVisibleAttributes($isRegistration = true)
private function getExpectationForVisibleAttributes()
{
return [
'customer' => [
'children' => $this->getCustomerAttributeExpectations($isRegistration),
'children' => $this->getCustomerAttributeExpectations(),
],
'address' => [
'children' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if ($block->getIsShipping()):
require(["Magento_Sales/order/create/form"], function(){

order.shippingAddressContainer = '<?= /* @escapeNotVerified */ $_fieldsContainerId ?>';
order.setAddresses(<?= /* @escapeNotVerified */ $customerAddressFormatter->getAddressesJson($addressArray) ?>);
order.setAddresses(<?= /* @escapeVerfied */ $block->getAddressCollectionJson() ?>);

});
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
height: 36px;
margin-top: -7px;
text-align: center;
width: 45px;
width: 60px;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@
.item-qty {
margin-right: @indent__s;
text-align: center;
width: 45px;
width: 60px;
}

.update-cart-item {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,30 @@ public function toNestedArray(
}
$dataObjectArray = $this->dataObjectProcessor->buildOutputDataArray($dataObject, $dataObjectType);
//process custom attributes if present
$dataObjectArray = $this->processCustomAttributes($dataObjectArray, $skipAttributes);

if (!empty($dataObjectArray[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY])) {
/** @var array $extensionAttributes */
$extensionAttributes = $dataObjectArray[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY];
unset($dataObjectArray[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
foreach ($extensionAttributes as $attributeKey => $attributeValue) {
if (!in_array($attributeKey, $skipAttributes)) {
$dataObjectArray[$attributeKey] = $attributeValue;
}
}
}
return $dataObjectArray;
}

/**
* Recursive process array to process customer attributes
*
* @param array $dataObjectArray
* @param array $skipAttributes
* @return array
*/
private function processCustomAttributes(array $dataObjectArray, array $skipAttributes): array
{
if (!empty($dataObjectArray[AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY])) {
/** @var AttributeValue[] $customAttributes */
$customAttributes = $dataObjectArray[AbstractExtensibleObject::CUSTOM_ATTRIBUTES_KEY];
Expand All @@ -56,14 +80,9 @@ public function toNestedArray(
}
}
}
if (!empty($dataObjectArray[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY])) {
/** @var array $extensionAttributes */
$extensionAttributes = $dataObjectArray[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY];
unset($dataObjectArray[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
foreach ($extensionAttributes as $attributeKey => $attributeValue) {
if (!in_array($attributeKey, $skipAttributes)) {
$dataObjectArray[$attributeKey] = $attributeValue;
}
foreach ($dataObjectArray as $key => $value) {
if (is_array($value)) {
$dataObjectArray[$key] = $this->processCustomAttributes($value, $skipAttributes);
}
}
return $dataObjectArray;
Expand Down
Loading

0 comments on commit bd82ed4

Please sign in to comment.