Skip to content

Commit

Permalink
MAGETWO-98729: [2.3] [Magento Cloud] Translated Arabic URL's are retu…
Browse files Browse the repository at this point in the history
…rning 404's

- fixed
- modified tests
- reduced nesting level
- eliminated cyclomatic complexity
- eliminated npath complexity
- eliminated long function
  • Loading branch information
odubovyk committed Apr 24, 2019
1 parent 645941c commit 6f4f511
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 179 deletions.
343 changes: 197 additions & 146 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogUrlRewrite\Model;

use Magento\Store\Model\Store;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product;
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class ProductUrlPathGenerator
*/
class ProductUrlPathGenerator
{
const XML_PATH_PRODUCT_URL_SUFFIX = 'catalog/seo/product_url_suffix';
Expand All @@ -17,36 +30,36 @@ class ProductUrlPathGenerator
protected $productUrlSuffix = [];

/**
* @var \Magento\Store\Model\StoreManagerInterface
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator
* @var CategoryUrlPathGenerator
*/
protected $categoryUrlPathGenerator;

/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
* @var ProductRepositoryInterface
*/
protected $productRepository;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
* @param ScopeConfigInterface $scopeConfig
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig,
CategoryUrlPathGenerator $categoryUrlPathGenerator,
ProductRepositoryInterface $productRepository
) {
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
Expand All @@ -57,8 +70,8 @@ public function __construct(
/**
* Retrieve Product Url path (with category if exists)
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\Category $category
* @param Product $product
* @param Category $category
*
* @return string
*/
Expand All @@ -78,10 +91,10 @@ public function getUrlPath($product, $category = null)
/**
* Prepare URL Key with stored product data (fallback for "Use Default Value" logic)
*
* @param \Magento\Catalog\Model\Product $product
* @param Product $product
* @return string
*/
protected function prepareProductDefaultUrlKey(\Magento\Catalog\Model\Product $product)
protected function prepareProductDefaultUrlKey(Product $product)
{
$storedProduct = $this->productRepository->getById($product->getId());
$storedUrlKey = $storedProduct->getUrlKey();
Expand All @@ -91,9 +104,9 @@ protected function prepareProductDefaultUrlKey(\Magento\Catalog\Model\Product $p
/**
* Retrieve Product Url path with suffix
*
* @param \Magento\Catalog\Model\Product $product
* @param Product $product
* @param int $storeId
* @param \Magento\Catalog\Model\Category $category
* @param Category $category
* @return string
*/
public function getUrlPathWithSuffix($product, $storeId, $category = null)
Expand All @@ -104,8 +117,8 @@ public function getUrlPathWithSuffix($product, $storeId, $category = null)
/**
* Get canonical product url path
*
* @param \Magento\Catalog\Model\Product $product
* @param \Magento\Catalog\Model\Category|null $category
* @param Product $product
* @param Category|null $category
* @return string
*/
public function getCanonicalUrlPath($product, $category = null)
Expand All @@ -117,7 +130,7 @@ public function getCanonicalUrlPath($product, $category = null)
/**
* Generate product url key based on url_key entered by merchant or product name
*
* @param \Magento\Catalog\Model\Product $product
* @param Product $product
* @return string|null
*/
public function getUrlKey($product)
Expand All @@ -129,13 +142,15 @@ public function getUrlKey($product)
/**
* Prepare url key for product
*
* @param \Magento\Catalog\Model\Product $product
* @param Product $product
* @return string
*/
protected function prepareProductUrlKey(\Magento\Catalog\Model\Product $product)
protected function prepareProductUrlKey(Product $product)
{
$urlKey = $product->getUrlKey();
return $product->formatUrlKey($urlKey === '' || $urlKey === null ? $product->getName() : $urlKey);
$urlKey = (string)$product->getUrlKey();
$urlKey = trim(strtolower($urlKey));

return $urlKey ?: $product->formatUrlKey($product->getName());
}

/**
Expand All @@ -153,7 +168,7 @@ protected function getProductUrlSuffix($storeId = null)
if (!isset($this->productUrlSuffix[$storeId])) {
$this->productUrlSuffix[$storeId] = $this->scopeConfig->getValue(
self::XML_PATH_PRODUCT_URL_SUFFIX,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\ScopeInterface;

/**
* Class ProductUrlPathGeneratorTest
*/
class ProductUrlPathGeneratorTest extends \PHPUnit\Framework\TestCase
{
/** @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator */
Expand Down Expand Up @@ -77,27 +80,30 @@ protected function setUp(): void
public function getUrlPathDataProvider(): array
{
return [
'path based on url key' => ['url-key', null, 'url-key'],
'path based on product name 1' => ['', 'product-name', 'product-name'],
'path based on product name 2' => [null, 'product-name', 'product-name'],
'path based on product name 3' => [false, 'product-name', 'product-name']
'path based on url key uppercase' => ['Url-Key', null, 0, 'url-key'],
'path based on url key' => ['url-key', null, 0, 'url-key'],
'path based on product name 1' => ['', 'product-name', 1, 'product-name'],
'path based on product name 2' => [null, 'product-name', 1, 'product-name'],
'path based on product name 3' => [false, 'product-name', 1, 'product-name']
];
}

/**
* @dataProvider getUrlPathDataProvider
* @param string|null|bool $urlKey
* @param string|null|bool $productName
* @param int $formatterCalled
* @param string $result
* @return void
*/
public function testGetUrlPath($urlKey, $productName, $result): void
public function testGetUrlPath($urlKey, $productName, $formatterCalled, $result): void
{
$this->product->expects($this->once())->method('getData')->with('url_path')
->will($this->returnValue(null));
$this->product->expects($this->any())->method('getUrlKey')->will($this->returnValue($urlKey));
$this->product->expects($this->any())->method('getName')->will($this->returnValue($productName));
$this->product->expects($this->once())->method('formatUrlKey')->will($this->returnArgument(0));
$this->product->expects($this->exactly($formatterCalled))
->method('formatUrlKey')->will($this->returnArgument(0));

$this->assertEquals($result, $this->productUrlPathGenerator->getUrlPath($this->product, null));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;

$stockDataConfig = [
'use_config_manage_stock' => 1,
'qty' => 100,
'is_qty_decimal' => 0,
'is_in_stock' => 1
];

/** @var ObjectManagerInterface $objectManager */
$objectManager = Bootstrap::getObjectManager();

/** @var ProductRepositoryInterface $productRepository */
$productRepository = $objectManager->create(ProductRepositoryInterface::class);

/** @var ProductInterface $product */
$product = $objectManager->create(ProductInterface::class);
$product->setTypeId(Type::TYPE_SIMPLE)
->setAttributeSetId(4)
->setWebsiteIds([1])
->setName('Чудовий продукт без Url Key')
->setSku('ukrainian-without-url-key')
->setPrice(10)
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStatus(Status::STATUS_ENABLED)
->setCategoryIds([2])
->setStockData($stockDataConfig);
try {
$productRepository->save($product);
} catch (\Exception $e) {
// problems during save
};

/** @var ProductInterface $product */
$product = $objectManager->create(ProductInterface::class);
$product->setTypeId(Type::TYPE_SIMPLE)
->setAttributeSetId(4)
->setWebsiteIds([1])
->setName('Надзвичайний продукт з Url Key')
->setSku('ukrainian-with-url-key')
->setPrice(10)
->setVisibility(Visibility::VISIBILITY_BOTH)
->setStatus(Status::STATUS_ENABLED)
->setCategoryIds([2])
->setStockData($stockDataConfig)
->setUrlKey('надзвичайний продукт на кожен день');
try {
$productRepository->save($product);
} catch (\Exception $e) {
// problems during save
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\TestFramework\Helper\Bootstrap;

Bootstrap::getInstance()->getInstance()->reinitialize();

/** @var Registry $registry */
$registry = Bootstrap::getObjectManager()->get(Registry::class);
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);

/** @var ProductRepositoryInterface $productRepository */
$productRepository = Bootstrap::getObjectManager()
->get(ProductRepositoryInterface::class);

$productSkus = [
'ukrainian-with-url-key',
'ukrainian-without-url-key',
];
try {
foreach ($productSkus as $sku) {
$product = $productRepository->get($sku, false, null, true);
$productRepository->delete($product);
}
} catch (NoSuchEntityException $e) {
// nothing to delete
}

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
Loading

0 comments on commit 6f4f511

Please sign in to comment.