Skip to content

Commit

Permalink
merge magento/2.4-develop into magento-tsg/2.4-develop-pr39
Browse files Browse the repository at this point in the history
  • Loading branch information
magento-mts-svc authored Jun 2, 2020
2 parents ea532db + 134ef81 commit 5b6121b
Show file tree
Hide file tree
Showing 34 changed files with 1,558 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*
* @see \Magento\Store\Model\ResourceModel\Store
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @package Magento\CatalogUrlRewrite\Model\Category\Plugin\Store
*/
class View
{
Expand All @@ -39,7 +38,7 @@ class View
protected $productFactory;

/**
* @var \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator
* @var CategoryUrlRewriteGenerator
*/
protected $categoryUrlRewriteGenerator;

Expand Down Expand Up @@ -75,6 +74,8 @@ public function __construct(
}

/**
* Setter for Orig Store data
*
* @param \Magento\Store\Model\ResourceModel\Store $object
* @param AbstractModel $store
* @return void
Expand All @@ -100,14 +101,13 @@ public function afterSave(
\Magento\Store\Model\ResourceModel\Store $store
) {
if ($this->origStore->isObjectNew() || $this->origStore->dataHasChangedFor('group_id')) {
if (!$this->origStore->isObjectNew()) {
$this->urlPersist->deleteByData([UrlRewrite::STORE_ID => $this->origStore->getId()]);
}

$this->urlPersist->replace(
$this->generateCategoryUrls($this->origStore->getRootCategoryId(), $this->origStore->getId())
$categoryRewriteUrls = $this->generateCategoryUrls(
$this->origStore->getRootCategoryId(),
$this->origStore->getId()
);

$this->urlPersist->replace($categoryRewriteUrls);

$this->urlPersist->replace(
$this->generateProductUrls(
$this->origStore->getWebsiteId(),
Expand Down Expand Up @@ -139,33 +139,34 @@ protected function generateProductUrls($websiteId, $originWebsiteId, $storeId)
->addAttributeToSelect(['name', 'url_path', 'url_key', 'visibility'])
->addWebsiteFilter($websiteIds);
foreach ($collection as $product) {
$product->setStoreId($storeId);
/** @var \Magento\Catalog\Model\Product $product */
$urls = array_merge(
$urls,
$this->productUrlRewriteGenerator->generate($product)
);
$product->setStoreId($storeId);
$urls[] = $this->productUrlRewriteGenerator->generate($product);
}
$urls = array_merge([], ...$urls);

return $urls;
}

/**
* Generate url rewrites for categories
*
* @param int $rootCategoryId
* @param int $storeId
* @return array
*/
protected function generateCategoryUrls($rootCategoryId, $storeId)
{
$urls = [];
$categories = $this->categoryFactory->create()->getCategories($rootCategoryId, 1, false, true);
$categories = $this->categoryFactory->create()->getCategories($rootCategoryId, 1, false, true, false);
$categories->setStoreId($storeId);
foreach ($categories as $category) {
/** @var \Magento\Catalog\Model\Category $category */
/** @var Category $category */
$category->setStoreId($storeId);
$urls = array_merge(
$urls,
$this->categoryUrlRewriteGenerator->generate($category)
);
$urls[] = $this->categoryUrlRewriteGenerator->generate($category);
}
$urls = array_merge([], ...$urls);

return $urls;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category\Plugin\Store;

use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\Product;
Expand Down Expand Up @@ -87,6 +88,9 @@ class ViewTest extends TestCase
*/
private $productMock;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = new ObjectManager($this);
Expand Down Expand Up @@ -139,7 +143,12 @@ protected function setUp(): void
);
}

public function testAfterSave()
/**
* Test after save
*
* @return void
*/
public function testAfterSave(): void
{
$origStoreMock = $this->getMockBuilder(\Magento\Store\Model\Store::class)
->disableOriginalConstructor()
Expand All @@ -155,9 +164,16 @@ public function testAfterSave()
$this->abstractModelMock->expects($this->any())
->method('isObjectNew')
->willReturn(true);
$categoryCollection = $this->getMockBuilder(CategoryCollection::class)
->disableOriginalConstructor()
->setMethods(['getIterator'])
->getMock();
$categoryCollection->expects($this->any())
->method('getIterator')
->willReturn(new \ArrayIterator([]));
$this->categoryMock->expects($this->once())
->method('getCategories')
->willReturn([]);
->willReturn($categoryCollection);
$this->categoryFactoryMock->expects($this->once())
->method('create')
->willReturn($this->categoryMock);
Expand Down Expand Up @@ -191,7 +207,12 @@ public function testAfterSave()
);
}

public function testAfterDelete()
/**
* Test after delete
*
* @return void
*/
public function testAfterDelete(): void
{
$this->urlPersistMock->expects($this->once())
->method('deleteByData');
Expand Down
51 changes: 28 additions & 23 deletions app/code/Magento/Checkout/Controller/Cart/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Cart as CustomerCart;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Exception\NoSuchEntityException;

/**
Expand Down Expand Up @@ -77,7 +79,7 @@ protected function _initProduct()
/**
* Add product to shopping cart action
*
* @return \Magento\Framework\Controller\Result\Redirect
* @return ResponseInterface|ResultInterface
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
Expand All @@ -90,7 +92,6 @@ public function execute()
}

$params = $this->getRequest()->getParams();

try {
if (isset($params['qty'])) {
$filter = new \Zend_Filter_LocalizedToNormalized(
Expand All @@ -104,9 +105,7 @@ public function execute()
$product = $this->_initProduct();
$related = $this->getRequest()->getParam('related_product');

/**
* Check product availability
*/
/** Check product availability */
if (!$product) {
return $this->goBack();
}
Expand All @@ -115,7 +114,6 @@ public function execute()
if (!empty($related)) {
$this->cart->addProductsByIds(explode(',', $related));
}

$this->cart->save();

/**
Expand All @@ -127,21 +125,25 @@ public function execute()
);

if (!$this->_checkoutSession->getNoCartRedirect(true)) {
if (!$this->cart->getQuote()->getHasError()) {
if ($this->shouldRedirectToCart()) {
$message = __(
'You added %1 to your shopping cart.',
$product->getName()
);
$this->messageManager->addSuccessMessage($message);
} else {
$this->messageManager->addComplexSuccessMessage(
'addCartSuccessMessage',
[
'product_name' => $product->getName(),
'cart_url' => $this->getCartUrl(),
]
);
if ($this->shouldRedirectToCart()) {
$message = __(
'You added %1 to your shopping cart.',
$product->getName()
);
$this->messageManager->addSuccessMessage($message);
} else {
$this->messageManager->addComplexSuccessMessage(
'addCartSuccessMessage',
[
'product_name' => $product->getName(),
'cart_url' => $this->getCartUrl(),
]
);
}
if ($this->cart->getQuote()->getHasError()) {
$errors = $this->cart->getQuote()->getErrors();
foreach ($errors as $error) {
$this->messageManager->addErrorMessage($error->getText());
}
}
return $this->goBack(null, $product);
Expand All @@ -161,7 +163,6 @@ public function execute()
}

$url = $this->_checkoutSession->getRedirectUrl(true);

if (!$url) {
$url = $this->_redirect->getRedirectUrl($this->getCartUrl());
}
Expand All @@ -175,14 +176,16 @@ public function execute()
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
return $this->goBack();
}

return $this->getResponse();
}

/**
* Resolve response
*
* @param string $backUrl
* @param \Magento\Catalog\Model\Product $product
* @return $this|\Magento\Framework\Controller\Result\Redirect
* @return ResponseInterface|ResultInterface
*/
protected function goBack($backUrl = null, $product = null)
{
Expand All @@ -205,6 +208,8 @@ protected function goBack($backUrl = null, $product = null)
$this->getResponse()->representJson(
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($result)
);

return $this->getResponse();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,6 @@ define([
* @returns {Boolean} - initial visibility state.
*/
resolveInitialPasswordVisibility: function () {
if (checkoutData.getInputFieldEmailValue() !== '' && checkoutData.getCheckedEmailValue() === '') {
return true;
}

if (checkoutData.getInputFieldEmailValue() !== '') {
return checkoutData.getInputFieldEmailValue() === checkoutData.getCheckedEmailValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
data-bind="
textInput: email,
hasFocus: emailFocused,
afterRender: emailHasChanged,
mageInit: {'mage/trim-input':{}}"
name="username"
data-validate="{required:true, 'validate-email':true}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private function convertToProductData(int $productId, array $indexData, int $sto
$attributeValues = [$productId => $attributeValues];
}
$attributeValues = $this->prepareAttributeValues($productId, $attribute, $attributeValues, $storeId);
$productAttributes += $this->convertAttribute($attribute, $attributeValues);
$productAttributes += $this->convertAttribute($attribute, $attributeValues, $storeId);
}

return $productAttributes;
Expand All @@ -201,9 +201,10 @@ private function convertToProductData(int $productId, array $indexData, int $sto
*
* @param Attribute $attribute
* @param array $attributeValues
* @param int $storeId
* @return array
*/
private function convertAttribute(Attribute $attribute, array $attributeValues): array
private function convertAttribute(Attribute $attribute, array $attributeValues, int $storeId): array
{
$productAttributes = [];

Expand All @@ -212,7 +213,7 @@ private function convertAttribute(Attribute $attribute, array $attributeValues):
$productAttributes[$attribute->getAttributeCode()] = $retrievedValue;

if ($attribute->getIsSearchable()) {
$attributeLabels = $this->getValuesLabels($attribute, $attributeValues);
$attributeLabels = $this->getValuesLabels($attribute, $attributeValues, $storeId);
$retrievedLabel = $this->retrieveFieldValue($attributeLabels);
if ($retrievedLabel) {
$productAttributes[$attribute->getAttributeCode() . '_value'] = $retrievedLabel;
Expand Down Expand Up @@ -299,20 +300,21 @@ private function isAttributeDate(Attribute $attribute): bool
*
* @param Attribute $attribute
* @param array $attributeValues
* @param int $storeId
* @return array
*/
private function getValuesLabels(Attribute $attribute, array $attributeValues): array
private function getValuesLabels(Attribute $attribute, array $attributeValues, int $storeId): array
{
$attributeLabels = [];

$options = $this->getAttributeOptions($attribute);
$options = $this->getAttributeOptions($attribute, $storeId);
if (empty($options)) {
return $attributeLabels;
}

foreach ($attributeValues as $attributeValue) {
if (isset($options[$attributeValue])) {
$attributeLabels[] = $options[$attributeValue]->getLabel();
foreach ($options as $option) {
if (\in_array($option['value'], $attributeValues)) {
$attributeLabels[] = $option['label'];
}
}

Expand All @@ -323,20 +325,23 @@ private function getValuesLabels(Attribute $attribute, array $attributeValues):
* Retrieve options for attribute
*
* @param Attribute $attribute
* @param int $storeId
* @return array
*/
private function getAttributeOptions(Attribute $attribute): array
private function getAttributeOptions(Attribute $attribute, int $storeId): array
{
if (!isset($this->attributeOptionsCache[$attribute->getId()])) {
$options = $attribute->getOptions() ?? [];
$optionsByValue = [];
foreach ($options as $option) {
$optionsByValue[$option->getValue()] = $option;
}
$this->attributeOptionsCache[$attribute->getId()] = $optionsByValue;
if (!isset($this->attributeOptionsCache[$storeId][$attribute->getId()])) {
$attributeStoreId = $attribute->getStoreId();
/**
* Load array format of options.
* $attribute->getOptions() loads options into data objects which can be costly.
*/
$options = $attribute->usesSource() ? $attribute->setStoreId($storeId)->getSource()->getAllOptions() : [];
$this->attributeOptionsCache[$storeId][$attribute->getId()] = $options;
$attribute->setStoreId($attributeStoreId);
}

return $this->attributeOptionsCache[$attribute->getId()];
return $this->attributeOptionsCache[$storeId][$attribute->getId()];
}

/**
Expand Down
Loading

0 comments on commit 5b6121b

Please sign in to comment.