Skip to content

Commit

Permalink
Merge pull request #4647 from magento-mpi/pr_vk_2019_08_16
Browse files Browse the repository at this point in the history
[mpi] fixes 2.3 - 2019_08_16
  • Loading branch information
dhorytskyi authored Aug 17, 2019
2 parents 034806f + 807402c commit 2bc471e
Show file tree
Hide file tree
Showing 48 changed files with 1,542 additions and 391 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ $numColumns = $block->getColumns() !== null ? count($block->getColumns()) : 0;
<?php if ($block->canDisplayContainer()) : ?>
<div id="<?= $block->escapeHtml($block->getId()) ?>" data-grid-id="<?= $block->escapeHtml($block->getId()) ?>">
<?php else : ?>
<?= $block->getLayout()->getMessagesBlock()->getGroupedHtml() ?>
<?= $block->getLayout()->getMessagesBlock()->getGroupedHtml() ?>
<?php endif; ?>

<div class="admin__data-grid-header admin__data-grid-toolbar">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,6 @@ public function getItems($attributeCode)
*/
public function add($attributeCode, $option)
{
/** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $currentOptions */
$currentOptions = $this->getItems($attributeCode);
if (is_array($currentOptions)) {
array_walk($currentOptions, function (&$attributeOption) {
/** @var \Magento\Eav\Api\Data\AttributeOptionInterface $attributeOption */
$attributeOption = $attributeOption->getLabel();
});
if (in_array($option->getLabel(), $currentOptions, true)) {
return false;
}
}
return $this->eavOptionManagement->add(
\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE,
$attributeCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\Event\Observer;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\Store;
Expand Down Expand Up @@ -68,13 +67,15 @@ public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var Category $category */
$category = $observer->getEvent()->getCategory();
$useDefaultAttribute = !$category->isObjectNew() && !empty($category->getData('use_default')['url_key']);
$useDefaultAttribute = !empty($category->getData('use_default')['url_key']);
if ($category->getUrlKey() !== false && !$useDefaultAttribute) {
$resultUrlKey = $this->categoryUrlPathGenerator->getUrlKey($category);
$this->updateUrlKey($category, $resultUrlKey);
} else if ($useDefaultAttribute) {
$resultUrlKey = $category->formatUrlKey($category->getOrigData('name'));
$this->updateUrlKey($category, $resultUrlKey);
} elseif ($useDefaultAttribute) {
if (!$category->isObjectNew()) {
$resultUrlKey = $category->formatUrlKey($category->getOrigData('name'));
$this->updateUrlKey($category, $resultUrlKey);
}
$category->setUrlKey(null)->setUrlPath(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,67 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogUrlRewrite\Test\Unit\Observer;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

class CategoryUrlPathAutogeneratorObserverTest extends \PHPUnit\Framework\TestCase
{
/** @var \Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver */
protected $categoryUrlPathAutogeneratorObserver;
/**
* @var \Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver
*/
private $categoryUrlPathAutogeneratorObserver;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $categoryUrlPathGenerator;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $categoryUrlPathGenerator;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $childrenCategoriesProvider;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $childrenCategoriesProvider;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $observer;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $observer;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $category;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $category;

/**
* @var \Magento\CatalogUrlRewrite\Service\V1\StoreViewService|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storeViewService;
private $storeViewService;

/**
* @var \Magento\Catalog\Model\ResourceModel\Category|\PHPUnit_Framework_MockObject_MockObject
*/
protected $categoryResource;
private $categoryResource;

/**
* @inheritDoc
*/
protected function setUp()
{
$this->observer = $this->createPartialMock(
\Magento\Framework\Event\Observer::class,
['getEvent', 'getCategory']
);
$this->categoryResource = $this->createMock(\Magento\Catalog\Model\ResourceModel\Category::class);
$this->category = $this->createPartialMock(\Magento\Catalog\Model\Category::class, [
'setUrlKey',
'setUrlPath',
$this->category = $this->createPartialMock(
\Magento\Catalog\Model\Category::class,
[
'dataHasChangedFor',
'isObjectNew',
'getResource',
'getUrlKey',
'getStoreId',
'getData'
]);
'formatUrlKey'
]
);
$this->category->expects($this->any())->method('getResource')->willReturn($this->categoryResource);
$this->observer->expects($this->any())->method('getEvent')->willReturnSelf();
$this->observer->expects($this->any())->method('getCategory')->willReturn($this->category);
Expand All @@ -73,106 +86,125 @@ protected function setUp()
);
}

public function testSetCategoryUrlAndCategoryPath()
/**
* @param $isObjectNew
* @throws \Magento\Framework\Exception\LocalizedException
* @dataProvider shouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValueDataProvider
*/
public function testShouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValue($isObjectNew)
{
$this->category->expects($this->once())->method('getUrlKey')->willReturn('category');
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlKey')->willReturn('urk_key');
$this->category->expects($this->once())->method('setUrlKey')->with('urk_key')->willReturnSelf();
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->willReturn('url_path');
$this->category->expects($this->once())->method('setUrlPath')->with('url_path')->willReturnSelf();
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);

$expectedUrlKey = 'formatted_url_key';
$expectedUrlPath = 'generated_url_path';
$categoryData = ['use_default' => ['url_key' => 0], 'url_key' => 'some_key', 'url_path' => ''];
$this->category->setData($categoryData);
$this->category->isObjectNew($isObjectNew);
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlKey')->willReturn($expectedUrlKey);
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->willReturn($expectedUrlPath);
$this->assertEquals($categoryData['url_key'], $this->category->getUrlKey());
$this->assertEquals($categoryData['url_path'], $this->category->getUrlPath());
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
$this->assertEquals($expectedUrlKey, $this->category->getUrlKey());
$this->assertEquals($expectedUrlPath, $this->category->getUrlPath());
$this->categoryResource->expects($this->never())->method('saveAttribute');
}

public function testExecuteWithoutUrlKeyAndUrlPathUpdating()
/**
* @return array
*/
public function shouldFormatUrlKeyAndGenerateUrlPathIfUrlKeyIsNotUsingDefaultValueDataProvider()
{
$this->category->expects($this->once())->method('getUrlKey')->willReturn(false);
$this->category->expects($this->never())->method('setUrlKey');
$this->category->expects($this->never())->method('setUrlPath');
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
return [
[true],
[false],
];
}

/**
* @expectedException \Magento\Framework\Exception\LocalizedException
* @expectedExceptionMessage Invalid URL key
* @param $isObjectNew
* @throws \Magento\Framework\Exception\LocalizedException
* @dataProvider shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvider
*/
public function testExecuteWithException()
public function testShouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValue($isObjectNew)
{
$categoryName = 'test';
$categoryData = ['url_key' => 0];
$this->category->expects($this->once())->method('getUrlKey')->willReturn($categoryName);
$this->category->expects($this->once())
->method('getData')
->with('use_default')
->willReturn($categoryData);
$this->categoryUrlPathGenerator->expects($this->once())
->method('getUrlKey')
->with($this->category)
->willReturn(null);
$categoryData = ['use_default' => ['url_key' => 1], 'url_key' => 'some_key', 'url_path' => 'some_path'];
$this->category->setData($categoryData);
$this->category->isObjectNew($isObjectNew);
$this->category->expects($this->any())->method('formatUrlKey')->willReturn('formatted_key');
$this->assertEquals($categoryData['url_key'], $this->category->getUrlKey());
$this->assertEquals($categoryData['url_path'], $this->category->getUrlPath());
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
$this->assertNull($this->category->getUrlKey());
$this->assertNull($this->category->getUrlPath());
}

public function testUrlKeyAndUrlPathUpdating()
/**
* @return array
*/
public function shouldResetUrlPathAndUrlKeyIfUrlKeyIsUsingDefaultValueDataProvider()
{
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlKey')->with($this->category)
->willReturn('url_key');
$this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->with($this->category)
->willReturn('url_path');

$this->category->expects($this->once())->method('getUrlKey')->willReturn('not_formatted_url_key');
$this->category->expects($this->once())->method('setUrlKey')->with('url_key')->willReturnSelf();
$this->category->expects($this->once())->method('setUrlPath')->with('url_path')->willReturnSelf();
// break code execution
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
return [
[true],
[false],
];
}

/**
* @param $useDefaultUrlKey
* @param $isObjectNew
* @throws \Magento\Framework\Exception\LocalizedException
* @dataProvider shouldThrowExceptionIfUrlKeyIsEmptyDataProvider
*/
public function testShouldThrowExceptionIfUrlKeyIsEmpty($useDefaultUrlKey, $isObjectNew)
{
$this->expectExceptionMessage('Invalid URL key');
$categoryData = ['use_default' => ['url_key' => $useDefaultUrlKey], 'url_key' => '', 'url_path' => ''];
$this->category->setData($categoryData);
$this->category->isObjectNew($isObjectNew);
$this->assertEquals($isObjectNew, $this->category->isObjectNew());
$this->assertEquals($categoryData['url_key'], $this->category->getUrlKey());
$this->assertEquals($categoryData['url_path'], $this->category->getUrlPath());
$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
$this->assertEquals($categoryData['url_key'], $this->category->getUrlKey());
$this->assertEquals($categoryData['url_path'], $this->category->getUrlPath());
}

public function testUrlPathAttributeNoUpdatingIfCategoryIsNew()
/**
* @return array
*/
public function shouldThrowExceptionIfUrlKeyIsEmptyDataProvider()
{
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('url_key');
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('url_path');

$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();

$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(true);
$this->categoryResource->expects($this->never())->method('saveAttribute');

$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
return [
[0, false],
[0, true],
[1, false],
];
}

public function testUrlPathAttributeUpdating()
{
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('url_key');
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('url_path');

$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);

$categoryData = ['url_key' => 'some_key', 'url_path' => ''];
$this->category->setData($categoryData);
$this->category->isObjectNew(false);
$expectedUrlKey = 'formatted_url_key';
$expectedUrlPath = 'generated_url_path';
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn($expectedUrlKey);
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn($expectedUrlPath);
$this->categoryResource->expects($this->once())->method('saveAttribute')->with($this->category, 'url_path');

// break code execution
$this->category->expects($this->once())->method('dataHasChangedFor')->with('url_path')->willReturn(false);

$this->categoryUrlPathAutogeneratorObserver->execute($this->observer);
}

public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChanged()
{
$categoryData = ['url_key' => 'some_key', 'url_path' => ''];
$this->category->setData($categoryData);
$this->category->isObjectNew(false);

$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('url_key');
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('url_path');

$this->categoryResource->expects($this->once())->method('saveAttribute')->with($this->category, 'url_path');

$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
// break code execution
$this->category->expects($this->once())->method('dataHasChangedFor')->with('url_path')->willReturn(false);

Expand All @@ -181,29 +213,31 @@ public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChange

public function testChildrenUrlPathAttributeUpdatingForSpecificStore()
{
$categoryData = ['url_key' => 'some_key', 'url_path' => ''];
$this->category->setData($categoryData);
$this->category->isObjectNew(false);

$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlKey')->willReturn('generated_url_key');
$this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('generated_url_path');

$this->category->expects($this->any())->method('getUrlKey')->willReturn('not_formatted_url_key');
$this->category->expects($this->any())->method('setUrlKey')->willReturnSelf();
$this->category->expects($this->any())->method('setUrlPath')->willReturnSelf();
$this->category->expects($this->exactly(2))->method('isObjectNew')->willReturn(false);
$this->category->expects($this->any())->method('dataHasChangedFor')->willReturn(true);
// only for specific store
$this->category->expects($this->atLeastOnce())->method('getStoreId')->willReturn(1);

$childCategoryResource = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Category::class)
->disableOriginalConstructor()->getMock();
$childCategory = $this->getMockBuilder(\Magento\Catalog\Model\Category::class)
->setMethods([
'getUrlPath',
'setUrlPath',
'getResource',
'getStore',
'getStoreId',
'setStoreId'
])
->disableOriginalConstructor()->getMock();
->setMethods(
[
'getUrlPath',
'setUrlPath',
'getResource',
'getStore',
'getStoreId',
'setStoreId'
]
)
->disableOriginalConstructor()
->getMock();
$childCategory->expects($this->any())->method('getResource')->willReturn($childCategoryResource);
$childCategory->expects($this->once())->method('setStoreId')->with(1);

Expand Down
Loading

0 comments on commit 2bc471e

Please sign in to comment.