Skip to content
Snippets Groups Projects
Commit ecd6a67b authored by Leonid Poluyanov's avatar Leonid Poluyanov
Browse files

MAGETWO-56862: [Github] Saving Product does not update URL rewrite in Magento...

MAGETWO-56862: [Github] Saving Product does not update URL rewrite in Magento 2.1.0 with single-store mode #5929
parents 1b719efb 94dc702a
No related merge requests found
...@@ -131,12 +131,7 @@ class Helper ...@@ -131,12 +131,7 @@ class Helper
$productData[$field] = []; $productData[$field] = [];
} }
} }
$productData['website_ids'] = $this->filterWebsiteIds($productData['website_ids']);
foreach ($productData['website_ids'] as $websiteId => $checkboxValue) {
if (!$checkboxValue) {
unset($productData['website_ids'][$websiteId]);
}
}
$wasLockedMedia = false; $wasLockedMedia = false;
if ($product->isLockedAttribute('media')) { if ($product->isLockedAttribute('media')) {
...@@ -422,4 +417,23 @@ class Helper ...@@ -422,4 +417,23 @@ class Helper
} }
return $this->dateTimeFilter; return $this->dateTimeFilter;
} }
/**
* Remove ids of non selected websites from $websiteIds array and return filtered data
* $websiteIds parameter expects array with website ids as keys and 1 (selected) or 0 (non selected) as values
* Only one id (default website ID) will be set to $websiteIds array when the single store mode is turned on
*
* @param array $websiteIds
* @return array
*/
private function filterWebsiteIds($websiteIds)
{
if (!$this->storeManager->isSingleStoreMode()) {
$websiteIds = array_filter((array)$websiteIds);
} else {
$websiteIds[$this->storeManager->getWebsite(true)->getId()] = 1;
}
return $websiteIds;
}
} }
...@@ -5,18 +5,14 @@ ...@@ -5,18 +5,14 @@
*/ */
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Initialization; namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Initialization;
use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper;
use Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter; use Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter;
use Magento\Catalog\Model\Product; use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Option; use Magento\Catalog\Model\Product\Option;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\App\RequestInterface; use Magento\Framework\App\RequestInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\Data\WebsiteInterface; use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Model\StoreManagerInterface; use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Stdlib\DateTime\Filter\Date as DateFilter;
use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory; use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory;
use Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks; use Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks;
...@@ -34,11 +30,6 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -34,11 +30,6 @@ class HelperTest extends \PHPUnit_Framework_TestCase
*/ */
protected $objectManager; protected $objectManager;
/**
* @var int
*/
protected $websiteId = 1;
/** /**
* @var Helper * @var Helper
*/ */
...@@ -64,106 +55,54 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -64,106 +55,54 @@ class HelperTest extends \PHPUnit_Framework_TestCase
*/ */
protected $productMock; protected $productMock;
/**
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storeMock;
/**
* @var WebsiteInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $websiteMock;
/**
* @var DateFilter|\PHPUnit_Framework_MockObject_MockObject
*/
protected $dateFilterMock;
/**
* @var ProductLinkInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $productLinkFactoryMock;
/**
* @var ProductRepository|\PHPUnit_Framework_MockObject_MockObject
*/
protected $productRepositoryMock;
/** /**
* @var ProductCustomOptionInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject * @var ProductCustomOptionInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
*/ */
protected $customOptionFactoryMock; protected $customOptionFactoryMock;
/** /**
* @var Option|\PHPUnit_Framework_MockObject_MockObject * @var \Magento\Catalog\Model\Product\Link\Resolver|\PHPUnit_Framework_MockObject_MockObject
*/
protected $customOptionMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/ */
protected $linkResolverMock; protected $linkResolverMock;
/** /**
* @var ProductLinks * @var ProductLinks|\PHPUnit_Framework_MockObject_MockObject
*/ */
protected $productLinksMock; protected $productLinksMock;
protected function setUp() protected function setUp()
{ {
$this->objectManager = new ObjectManager($this); $this->objectManager = new ObjectManager($this);
$this->productLinkFactoryMock = $this->getMockBuilder(ProductLinkInterfaceFactory::class)
->disableOriginalConstructor()
->getMock();
$this->productRepositoryMock = $this->getMockBuilder(ProductRepository::class)
->disableOriginalConstructor()
->getMock();
$this->requestMock = $this->getMockBuilder(RequestInterface::class) $this->requestMock = $this->getMockBuilder(RequestInterface::class)
->setMethods(['getPost']) ->setMethods(['getPost'])
->getMockForAbstractClass(); ->getMockForAbstractClass();
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
->setMethods(['getWebsite'])
->getMockForAbstractClass();
$this->websiteMock = $this->getMockBuilder(WebsiteInterface::class)
->getMockForAbstractClass();
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass(); ->getMockForAbstractClass();
$this->dateFilterMock = $this->getMockBuilder(DateFilter::class)
->disableOriginalConstructor()
->getMock();
$this->stockFilterMock = $this->getMockBuilder(StockDataFilter::class) $this->stockFilterMock = $this->getMockBuilder(StockDataFilter::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->productMock = $this->getMockBuilder(Product::class) $this->productMock = $this->getMockBuilder(Product::class)
->setMethods([ ->setMethods(
'getId', [
'setWebsiteIds', 'getId',
'isLockedAttribute', 'isLockedAttribute',
'lockAttribute', 'lockAttribute',
'getAttributes', 'getAttributes',
'unlockAttribute', 'unlockAttribute',
'getOptionsReadOnly', 'getOptionsReadOnly',
'setCanSaveCustomOptions', 'getSku',
'__sleep', 'getProductLinks',
'__wakeup', ]
'getSku', )
'getProductLinks',
'getWebsiteIds'
])
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMockForAbstractClass();
$this->customOptionFactoryMock = $this->getMockBuilder(ProductCustomOptionInterfaceFactory::class) $this->customOptionFactoryMock = $this->getMockBuilder(ProductCustomOptionInterfaceFactory::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->setMethods(['create']) ->setMethods(['create'])
->getMock(); ->getMock();
$this->customOptionMock = $this->getMockBuilder(Option::class)
->disableOriginalConstructor()
->setMethods(null)
->getMock();
$this->productLinksMock = $this->getMockBuilder(ProductLinks::class) $this->productLinksMock = $this->getMockBuilder(ProductLinks::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->productLinksMock->expects($this->any()) $this->productLinksMock->expects($this->any())
->method('initializeLinks') ->method('initializeLinks')
->willReturn($this->productMock); ->willReturn($this->productMock);
...@@ -173,10 +112,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -173,10 +112,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase
'storeManager' => $this->storeManagerMock, 'storeManager' => $this->storeManagerMock,
'stockFilter' => $this->stockFilterMock, 'stockFilter' => $this->stockFilterMock,
'productLinks' => $this->productLinksMock, 'productLinks' => $this->productLinksMock,
'dateFilter' => $this->dateFilterMock,
'customOptionFactory' => $this->customOptionFactoryMock, 'customOptionFactory' => $this->customOptionFactoryMock,
'productLinkFactory' => $this->productLinkFactoryMock,
'productRepository' => $this->productRepositoryMock,
]); ]);
$this->linkResolverMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Link\Resolver::class) $this->linkResolverMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Link\Resolver::class)
...@@ -190,9 +126,13 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -190,9 +126,13 @@ class HelperTest extends \PHPUnit_Framework_TestCase
/** /**
* @covers \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize * @covers \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::initialize
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @param bool $isSingleStore
* @param array $websiteIds
* @param array $expWebsiteIds
*
* @dataProvider initializeDataProvider
*/ */
public function testInitialize() public function testInitialize($isSingleStore, $websiteIds, $expWebsiteIds)
{ {
$optionsData = [ $optionsData = [
'option1' => ['is_delete' => true, 'name' => 'name1', 'price' => 'price1', 'option_id' => ''], 'option1' => ['is_delete' => true, 'name' => 'name1', 'price' => 'price1', 'option_id' => ''],
...@@ -202,6 +142,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -202,6 +142,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase
$productData = [ $productData = [
'stock_data' => ['stock_data'], 'stock_data' => ['stock_data'],
'options' => $optionsData, 'options' => $optionsData,
'website_ids' => $websiteIds
]; ];
$attributeNonDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class) $attributeNonDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
...@@ -218,69 +159,39 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -218,69 +159,39 @@ class HelperTest extends \PHPUnit_Framework_TestCase
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$attributeNonDate->expects($this->any()) $attributeNonDate->expects($this->any())->method('getBackend')->willReturn($attributeNonDateBackEnd);
->method('getBackend') $attributeDate->expects($this->any())->method('getBackend')->willReturn($attributeDateBackEnd);
->willReturn($attributeNonDateBackEnd); $this->productMock->expects($this->any())->method('getProductLinks')->willReturn([]);
$attributeDate->expects($this->any()) $attributeNonDateBackEnd->expects($this->any())->method('getType')->willReturn('non-datetime');
->method('getBackend') $attributeDateBackEnd->expects($this->any())->method('getType')->willReturn('datetime');
->willReturn($attributeDateBackEnd);
$this->productMock->expects($this->any())
->method('getProductLinks')
->willReturn([]);
$attributeNonDateBackEnd->expects($this->any())
->method('getType')
->willReturn('non-datetime');
$attributeDateBackEnd->expects($this->any())
->method('getType')
->willReturn('datetime');
$attributesArray = [
$attributeNonDate,
$attributeDate
];
$useDefaults = ['attributeCode1', 'attributeCode2']; $useDefaults = ['attributeCode1', 'attributeCode2'];
$this->requestMock->expects($this->at(0)) $this->requestMock->expects($this->any())->method('getPost')->willReturnMap(
->method('getPost') [
->with('product') ['product', [], $productData],
->willReturn($productData); ['use_default', null, $useDefaults]
$this->requestMock->expects($this->at(1)) ]
->method('getPost') );
->with('use_default')
->willReturn($useDefaults);
$this->linkResolverMock->expects($this->once())->method('getLinks')->willReturn([]); $this->linkResolverMock->expects($this->once())->method('getLinks')->willReturn([]);
$this->stockFilterMock->expects($this->once()) $this->stockFilterMock->expects($this->once())->method('filter')->with(['stock_data'])
->method('filter')
->with(['stock_data'])
->willReturn(['stock_data']); ->willReturn(['stock_data']);
$this->productMock->expects($this->once()) $this->productMock->expects($this->once())->method('isLockedAttribute')->with('media')->willReturn(true);
->method('isLockedAttribute') $this->productMock->expects($this->once())->method('unlockAttribute')->with('media');
->with('media') $this->productMock->expects($this->any())->method('getProductLinks')->willReturn([]);
->willReturn(true); $this->productMock->expects($this->once())->method('lockAttribute')->with('media');
$this->productMock->expects($this->once()) $this->productMock->expects($this->once())->method('getAttributes')
->method('unlockAttribute') ->willReturn([$attributeNonDate, $attributeDate]);
->with('media'); $this->productMock->expects($this->any())->method('getSku')->willReturn('sku');
$this->productMock->expects($this->any()) $this->productMock->expects($this->any())->method('getOptionsReadOnly')->willReturn(false);
->method('getProductLinks')
->willReturn([]); $customOptionMock = $this->getMockBuilder(Option::class)
$this->productMock->expects($this->once()) ->disableOriginalConstructor()
->method('lockAttribute') ->setMethods(null)
->with('media'); ->getMock();
$this->productMock->expects($this->once()) $firstExpectedCustomOption = clone $customOptionMock;
->method('getAttributes')
->willReturn($attributesArray);
$this->productMock->expects($this->any())
->method('getSku')
->willReturn('sku');
$this->productMock->expects($this->any())
->method('getOptionsReadOnly')
->willReturn(false);
$firstExpectedCustomOption = clone $this->customOptionMock;
$firstExpectedCustomOption->setData($optionsData['option2']); $firstExpectedCustomOption->setData($optionsData['option2']);
$secondExpectedCustomOption = clone $this->customOptionMock; $secondExpectedCustomOption = clone $customOptionMock;
$secondExpectedCustomOption->setData($optionsData['option3']); $secondExpectedCustomOption->setData($optionsData['option3']);
$this->customOptionFactoryMock->expects($this->any()) $this->customOptionFactoryMock->expects($this->any())
->method('create') ->method('create')
...@@ -293,8 +204,13 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -293,8 +204,13 @@ class HelperTest extends \PHPUnit_Framework_TestCase
$secondExpectedCustomOption $secondExpectedCustomOption
] ]
]); ]);
$website = $this->getMockBuilder(WebsiteInterface::class)->getMockForAbstractClass();
$website->expects($this->any())->method('getId')->willReturn(1);
$this->storeManagerMock->expects($this->once())->method('isSingleStoreMode')->willReturn($isSingleStore);
$this->storeManagerMock->expects($this->any())->method('getWebsite')->willReturn($website);
$this->assertEquals($this->productMock, $this->helper->initialize($this->productMock)); $this->assertEquals($this->productMock, $this->helper->initialize($this->productMock));
$this->assertEquals($expWebsiteIds, $this->productMock->getDataByKey('website_ids'));
$productOptions = $this->productMock->getOptions(); $productOptions = $this->productMock->getOptions();
$this->assertTrue(2 == count($productOptions)); $this->assertTrue(2 == count($productOptions));
...@@ -305,6 +221,35 @@ class HelperTest extends \PHPUnit_Framework_TestCase ...@@ -305,6 +221,35 @@ class HelperTest extends \PHPUnit_Framework_TestCase
$this->assertTrue('sku' == $option2->getData('product_sku')); $this->assertTrue('sku' == $option2->getData('product_sku'));
} }
/**
* @return array
*/
public function initializeDataProvider()
{
return [
[
'single_store' => false,
'website_ids' => ['1' => 1, '2' => 1],
'expected_website_ids' => ['1' => 1, '2' => 1]
],
[
'single_store' => false,
'website_ids' => ['1' => 1, '2' => 0],
'expected_website_ids' => ['1' => 1]
],
[
'single_store' => false,
'website_ids' => ['1' => 0, '2' => 0],
'expected_website_ids' => []
],
[
'single_store' => true,
'website_ids' => [],
'expected_website_ids' => ['1' => 1]
],
];
}
/** /**
* Data provider for testMergeProductOptions * Data provider for testMergeProductOptions
* *
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment