Skip to content

Commit

Permalink
Merge pull request #738 from magento-tsg/2.0-pr7
Browse files Browse the repository at this point in the history
MAGETWO-62647: "Invalid option value" Error message is shown when delete option row without data
MAGETWO-62680: Restricted Admin User mustn't see info from other store
  • Loading branch information
rganin authored Jan 13, 2017
2 parents ea6f29c + c627661 commit b3a5c0a
Show file tree
Hide file tree
Showing 21 changed files with 706 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Controller\Adminhtml\Product\Initialization;

/**
* Product initialization helper class
*/
class Helper
{
/**
Expand Down Expand Up @@ -71,7 +75,7 @@ public function __construct(
*/
public function initialize(\Magento\Catalog\Model\Product $product)
{
$productData = $this->request->getPost('product');
$productData = $this->request->getPost('product', []);
unset($productData['custom_attributes']);
unset($productData['extension_attributes']);

Expand Down Expand Up @@ -144,6 +148,13 @@ public function initialize(\Magento\Catalog\Model\Product $product)
$productData['options'],
$this->request->getPost('options_use_default')
);
foreach ($options as &$customOptionData) {
if (isset($customOptionData['values'])) {
$customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) {
return !($valueData['option_type_id'] == '-1' && !empty($valueData['is_delete']));
});
}
}
$product->setProductOptions($options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Magento\Backend\App\Action;
use Magento\Catalog\Controller\Adminhtml\Product;

/**
* Class that handle new product creation
*/
class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,59 @@
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Initialization;

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\Model\Product;
use Magento\Catalog\Model\Product\Option;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks;

/**
* Class HelperTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
class HelperTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ObjectManager
*/
protected $objectManager;

/**
* @var Helper
*/
protected $helper;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $requestMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storeManagerMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var StockDataFilter|\PHPUnit_Framework_MockObject_MockObject
*/
protected $stockFilterMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var ProductLinks|\PHPUnit_Framework_MockObject_MockObject
*/
protected $productLinksMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var Product|\PHPUnit_Framework_MockObject_MockObject
*/
protected $productMock;

Expand All @@ -55,43 +80,45 @@ class HelperTest extends \PHPUnit_Framework_TestCase
protected $websiteId = 1;

/**
* @var \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper
*/
protected $helper;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
* @var \Magento\Backend\Helper\Js|\PHPUnit_Framework_MockObject_MockObject
*/
protected $jsHelperMock;

protected function setUp()
{
$this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
$this->jsHelperMock = $this->getMock('Magento\Backend\Helper\Js', [], [], '', false);
$this->storeMock = $this->getMock('Magento\Store\Model\Store', [], [], '', false);
$this->websiteMock = $this->getMock('Magento\Store\Model\Website', [], [], '', false);
$this->storeManagerMock = $this->getMock('Magento\Store\Model\StoreManagerInterface');
$this->dateFilterMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\Filter\Date', [], [], '', false);

$this->stockFilterMock = $this->getMock(
'Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter',
$this->objectManager = new ObjectManager($this);
$this->requestMock = $this->getMockBuilder(RequestInterface::class)
->setMethods(['getPost'])
->getMockForAbstractClass();

$this->jsHelperMock = $this->getMock(\Magento\Backend\Helper\Js::class, [], [], '', false);
$this->storeMock = $this->getMock(\Magento\Store\Model\Store::class, [], [], '', false);
$this->websiteMock = $this->getMock(\Magento\Store\Model\Website::class, [], [], '', false);
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
->getMockForAbstractClass();
$this->dateFilterMock = $this->getMock(
\Magento\Framework\Stdlib\DateTime\Filter\Date::class,
[],
[],
'',
false
);

$this->stockFilterMock = $this->getMockBuilder(StockDataFilter::class)
->disableOriginalConstructor()
->getMock();

$this->productLinksMock = $this->getMock(
'Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks',
\Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks::class,
[],
[],
'',
false
);

$this->productMock = $this->getMock(
'Magento\Catalog\Model\Product',
\Magento\Catalog\Model\Product::class,
[
'setData',
'addData',
'getId',
'setWebsiteIds',
Expand All @@ -100,7 +127,6 @@ protected function setUp()
'getAttributes',
'unlockAttribute',
'getOptionsReadOnly',
'setProductOptions',
'setCanSaveCustomOptions',
'__sleep',
'__wakeup'
Expand All @@ -109,6 +135,15 @@ protected function setUp()
'',
false
);

$this->helper = $this->objectManager->getObject(Helper::class, [
'request' => $this->requestMock,
'storeManager' => $this->storeManagerMock,
'stockFilter' => $this->stockFilterMock,
'productLinks' => $this->productLinksMock,
'jsHelper' => $this->jsHelperMock,
'dateFilter' => $this->dateFilterMock,
]);
}

/**
Expand All @@ -117,6 +152,20 @@ protected function setUp()
*/
public function testInitialize()
{
$optionsData = [
'option1' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1', 'option_id' => '13'],
'option2' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1', 'option_id' => '14',
'values' => [
'value1' => ['option_type_id' => 1, 'is_delete' =>''],
'value2' => ['option_type_id' => '-1', 'is_delete' =>'1']
]
],
];
$productData = [
'stock_data' => ['stock_data'],
'options' => $optionsData,
];

$this->websiteMock->expects($this->once())
->method('getId')
->will($this->returnValue($this->websiteId));
Expand All @@ -130,28 +179,20 @@ public function testInitialize()
->with(true)
->will($this->returnValue($this->storeMock));

$this->jsHelperMock = $this->getMock('\Magento\Backend\Helper\Js', [], [], '', false);
$this->helper = new Helper(
$this->requestMock,
$this->storeManagerMock,
$this->stockFilterMock,
$this->productLinksMock,
$this->jsHelperMock,
$this->dateFilterMock
);

$productData = [
'stock_data' => ['stock_data'],
'options' => ['option1', 'option2']
];

$attributeNonDate = $this->getMock('Magento\Catalog\Model\ResourceModel\Eav\Attribute', [], [], '', false);
$attributeDate = $this->getMock('Magento\Catalog\Model\ResourceModel\Eav\Attribute', [], [], '', false);
$attributeNonDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
->disableOriginalConstructor()
->getMock();
$attributeDate = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class)
->disableOriginalConstructor()
->getMock();

$attributeNonDateBackEnd =
$this->getMock('Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend', [], [], '', false);
$attributeDateBackEnd =
$this->getMock('Magento\Eav\Model\Entity\Attribute\Backend\Datetime', [], [], '', false);
$this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\DefaultBackend::class)
->disableOriginalConstructor()
->getMock();
$attributeDateBackEnd = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Backend\Datetime::class)
->disableOriginalConstructor()
->getMock();

$attributeNonDate->expects($this->any())
->method('getBackend')
Expand All @@ -177,6 +218,13 @@ public function testInitialize()

$useDefaults = ['attributeCode1', 'attributeCode2'];

$this->requestMock->expects($this->any())->method('getPost')->willReturnMap(
[
['product', [], $productData],
['use_default', null, $useDefaults]
]
);

$this->requestMock->expects($this->at(0))
->method('getPost')
->with('product')
Expand Down Expand Up @@ -230,6 +278,7 @@ public function testInitialize()

$productData['category_ids'] = [];
$productData['website_ids'] = [];

$this->productMock->expects($this->once())
->method('addData')
->with($productData);
Expand All @@ -242,15 +291,11 @@ public function testInitialize()
->method('getOptionsReadOnly')
->will($this->returnValue(false));

$this->productMock->expects($this->once())
->method('setProductOptions')
->with($productData['options']);

$this->productMock->expects($this->once())
->method('setCanSaveCustomOptions')
->with(true);

$this->assertEquals($this->productMock, $this->helper->initialize($this->productMock));

$productOptions = $this->productMock->getProductOptions();
$this->assertTrue(2 == count($productOptions));
$this->assertTrue(1 == count($productOptions['option2']['values']));
}

/**
Expand Down Expand Up @@ -292,15 +337,6 @@ public function mergeProductOptionsDataProvider()
*/
public function testMergeProductOptions($productOptions, $defaultOptions, $expectedResults)
{
$this->jsHelperMock = $this->getMock('\Magento\Backend\Helper\Js', [], [], '', false);
$this->helper = new Helper(
$this->requestMock,
$this->storeManagerMock,
$this->stockFilterMock,
$this->productLinksMock,
$this->jsHelperMock,
$this->dateFilterMock
);
$result = $this->helper->mergeProductOptions($productOptions, $defaultOptions);
$this->assertEquals($expectedResults, $result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product;

/**
* Class to test new product creation
*/
class NewActionTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest
{
/** @var \Magento\Catalog\Controller\Adminhtml\Product\NewAction */
Expand All @@ -22,46 +26,50 @@ class NewActionTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\Prod
protected function setUp()
{
$this->productBuilder = $this->getMock(
'Magento\Catalog\Controller\Adminhtml\Product\Builder',
\Magento\Catalog\Controller\Adminhtml\Product\Builder::class,
['build'],
[],
'',
false
);
$this->product = $this->getMockBuilder('Magento\Catalog\Model\Product')->disableOriginalConstructor()
$this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->setMethods(['addData', 'getTypeId', 'getStoreId', '__sleep', '__wakeup'])->getMock();
$this->product->expects($this->any())->method('getTypeId')->will($this->returnValue('simple'));
$this->product->expects($this->any())->method('getStoreId')->will($this->returnValue('1'));
$this->productBuilder->expects($this->any())->method('build')->will($this->returnValue($this->product));

$this->resultPage = $this->getMockBuilder('Magento\Backend\Model\View\Result\Page')
$this->resultPage = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Page::class)
->disableOriginalConstructor()
->getMock();

$resultPageFactory = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory')
$resultPageFactory = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$resultPageFactory->expects($this->atLeastOnce())
->method('create')
->willReturn($this->resultPage);

$this->resultForward = $this->getMockBuilder('Magento\Backend\Model\View\Result\Forward')
$this->resultForward = $this->getMockBuilder(\Magento\Backend\Model\View\Result\Forward::class)
->disableOriginalConstructor()
->getMock();
$resultForwardFactory = $this->getMockBuilder('Magento\Backend\Model\View\Result\ForwardFactory')
$resultForwardFactory = $this->getMockBuilder(\Magento\Backend\Model\View\Result\ForwardFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$resultForwardFactory->expects($this->any())
->method('create')
->willReturn($this->resultForward);

$stockFilter = $this->getMockBuilder(
\Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter::class
)->disableOriginalConstructor()->getMock();

$this->action = new \Magento\Catalog\Controller\Adminhtml\Product\NewAction(
$this->initContext(),
$this->productBuilder,
$this->getMockBuilder('Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter')
->disableOriginalConstructor()->getMock(),
$stockFilter,
$resultPageFactory,
$resultForwardFactory
);
Expand Down
Loading

0 comments on commit b3a5c0a

Please sign in to comment.