From b953300925ac4f514b34fcea65620c5b09418307 Mon Sep 17 00:00:00 2001 From: Andrii Meysar Date: Tue, 27 Dec 2016 19:51:49 +0200 Subject: [PATCH 1/6] MAGETWO-62647: "Invalid option value" Error message is shown when delete option row without data --- .../Product/Initialization/Helper.php | 32 +++- .../Adminhtml/Product/NewAction.php | 25 ++- .../Controller/Adminhtml/Product/Validate.php | 35 ++-- .../Product/Initialization/HelperTest.php | 157 +++++++++++------- .../Adminhtml/Product/NewActionTest.php | 59 +++++-- .../Adminhtml/Product/ValidateTest.php | 127 ++++++++++---- 6 files changed, 307 insertions(+), 128 deletions(-) diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php index 49559ba330b6e..8278a7d244513 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php @@ -5,6 +5,11 @@ */ namespace Magento\Catalog\Controller\Adminhtml\Product\Initialization; +/** + * Product initialization helper class + * + * @package Magento\Catalog\Controller\Adminhtml\Product\Initialization + */ class Helper { /** @@ -62,16 +67,18 @@ public function __construct( } /** - * Initialize product before saving + * Initialize product from data * * @param \Magento\Catalog\Model\Product $product + * @param array $productData * @return \Magento\Catalog\Model\Product + * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function initialize(\Magento\Catalog\Model\Product $product) + public function initializeFromData(\Magento\Catalog\Model\Product $product, array $productData = null) { - $productData = $this->request->getPost('product'); + $productData = $productData ?: $this->request->getPost('product', []); unset($productData['custom_attributes']); unset($productData['extension_attributes']); @@ -144,6 +151,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 empty($valueData['is_delete']); + }); + } + } $product->setProductOptions($options); } @@ -154,6 +168,18 @@ public function initialize(\Magento\Catalog\Model\Product $product) return $product; } + /** + * Initialize product before saving + * + * @param \Magento\Catalog\Model\Product $product + * @return \Magento\Catalog\Model\Product + */ + public function initialize(\Magento\Catalog\Model\Product $product) + { + $productData = $this->request->getPost('product', []); + return $this->initializeFromData($product, $productData); + } + /** * Merge product and default options for product * diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php index 110029cdda0a8..229696fa5a862 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php @@ -8,7 +8,13 @@ use Magento\Backend\App\Action; use Magento\Catalog\Controller\Adminhtml\Product; +use Magento\Framework\App\ObjectManager; +/** + * Class that handle new product creation + * + * @package Magento\Catalog\Controller\Adminhtml\Product + */ class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product { /** @@ -26,6 +32,11 @@ class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product */ protected $resultForwardFactory; + /** + * @var Initialization\Helper + */ + protected $initializationHelper; + /** * @param Action\Context $context * @param Builder $productBuilder @@ -69,7 +80,7 @@ public function execute() if ($productData) { $stockData = isset($productData['stock_data']) ? $productData['stock_data'] : []; $productData['stock_data'] = $this->stockFilter->filter($stockData); - $product->addData($productData); + $product = $this->getInitializationHelper()->initializeFromData($product, $productData); } $this->_eventManager->dispatch('catalog_product_new_action', ['product' => $product]); @@ -92,4 +103,16 @@ public function execute() return $resultPage; } + + /** + * @deprecated + * @return Initialization\Helper + */ + protected function getInitializationHelper() + { + if (null === $this->initializationHelper) { + $this->initializationHelper = ObjectManager::getInstance()->get(Initialization\Helper::class); + } + return $this->initializationHelper; + } } diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php index ae7b4b3d451d7..7c61b0cf17865 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php @@ -8,6 +8,7 @@ use Magento\Backend\App\Action; use Magento\Catalog\Controller\Adminhtml\Product; +use Magento\Framework\App\ObjectManager; /** * Product validate @@ -18,6 +19,7 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product { /** * @var \Magento\Framework\Stdlib\DateTime\Filter\Date + * @deprecated */ protected $_dateFilter; @@ -39,6 +41,11 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product /** @var \Magento\Catalog\Model\ProductFactory */ protected $productFactory; + /** + * @var Initialization\Helper + */ + protected $initializationHelper; + /** * @param Action\Context $context * @param Builder $productBuilder @@ -78,7 +85,7 @@ public function execute() $response->setError(false); try { - $productData = $this->getRequest()->getPost('product'); + $productData = $this->getRequest()->getPost('product', []); if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) { $productData['stock_data']['use_config_manage_stock'] = 0; @@ -102,19 +109,7 @@ public function execute() if ($productId) { $product->load($productId); } - - $dateFieldFilters = []; - $attributes = $product->getAttributes(); - foreach ($attributes as $attrKey => $attribute) { - if ($attribute->getBackend()->getType() == 'datetime') { - if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') { - $dateFieldFilters[$attrKey] = $this->_dateFilter; - } - } - } - $inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData); - $productData = $inputFilter->getUnescaped(); - $product->addData($productData); + $product = $this->getInitializationHelper()->initializeFromData($product, $productData); /* set restrictions for date ranges */ $resource = $product->getResource(); @@ -140,4 +135,16 @@ public function execute() return $this->resultJsonFactory->create()->setJsonData($response->toJson()); } + + /** + * @return Initialization\Helper + * @deprecated + */ + protected function getInitializationHelper() + { + if (null === $this->initializationHelper) { + $this->initializationHelper = ObjectManager::getInstance()->get(Initialization\Helper::class); + } + return $this->initializationHelper; + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php index 2a407a67fd428..750462b388042 100644 --- a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php @@ -5,32 +5,56 @@ */ 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; @@ -55,33 +79,36 @@ 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, [], [], '', @@ -89,9 +116,8 @@ protected function setUp() ); $this->productMock = $this->getMock( - 'Magento\Catalog\Model\Product', + \Magento\Catalog\Model\Product::class, [ - 'setData', 'addData', 'getId', 'setWebsiteIds', @@ -100,7 +126,6 @@ protected function setUp() 'getAttributes', 'unlockAttribute', 'getOptionsReadOnly', - 'setProductOptions', 'setCanSaveCustomOptions', '__sleep', '__wakeup' @@ -109,6 +134,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, + ]); } /** @@ -117,6 +151,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' => ['is_delete' =>''], + 'value2' => ['is_delete' =>'1'] + ] + ], + ]; + $productData = [ + 'stock_data' => ['stock_data'], + 'options' => $optionsData, + ]; + $this->websiteMock->expects($this->once()) ->method('getId') ->will($this->returnValue($this->websiteId)); @@ -130,28 +178,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') @@ -177,6 +217,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') @@ -230,6 +277,7 @@ public function testInitialize() $productData['category_ids'] = []; $productData['website_ids'] = []; + $this->productMock->expects($this->once()) ->method('addData') ->with($productData); @@ -242,15 +290,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'])); } /** @@ -292,15 +336,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); } diff --git a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/NewActionTest.php b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/NewActionTest.php index 187c4e6bba0ff..a2fb859e20f9b 100644 --- a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/NewActionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/NewActionTest.php @@ -6,6 +6,15 @@ */ namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product; +use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; +use Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter; +use Magento\Catalog\Controller\Adminhtml\Product\NewAction; + +/** + * Class to test new product creation + * + * @package Magento\Catalog\Test\Unit\Controller\Adminhtml\Product + */ class NewActionTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest { /** @var \Magento\Catalog\Controller\Adminhtml\Product\NewAction */ @@ -19,26 +28,31 @@ class NewActionTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\Prod /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */ protected $product; + /** + * @var Helper|\PHPUnit_Framework_MockObject_MockObject + */ + protected $initializationHelper; + 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(); @@ -46,26 +60,34 @@ protected function setUp() ->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); + $this->initializationHelper = $this->getMock(Helper::class, [], [], '', false); + $stockDataFilter = $this->getMockBuilder(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(), - $resultPageFactory, - $resultForwardFactory - ); + $this->action = $this->getMockBuilder(NewAction::class) + ->setMethods(['getInitializationHelper']) + ->setConstructorArgs( + [ + 'context' => $this->initContext(), + 'productBuilder' => $this->productBuilder, + 'stockFilter' => $stockDataFilter, + 'resultPageFactory' => $resultPageFactory, + 'resultForwardFactory' => $resultForwardFactory, + ] + )->getMock(); + $this->action->method('getInitializationHelper')->willReturn($this->initializationHelper); + $this->action->method('getRequest')->willReturn($this->request); $this->resultPage->expects($this->atLeastOnce()) ->method('getLayout') ->willReturn($this->layout); @@ -85,10 +107,15 @@ public function testExecuteObtainsProductDataFromSession() $this->action->getRequest()->expects($this->any())->method('getFullActionName') ->willReturn('catalog_product_new'); + $productData = ['name' => 'test-name', 'stock_data' => null]; $this->session->expects($this->any())->method('getProductData') - ->willReturn(['product' => ['name' => 'test-name']]); + ->willReturn(['product' => $productData]); - $this->product->expects($this->once())->method('addData')->with(['name' => 'test-name', 'stock_data' => null]); + $this->initializationHelper + ->expects($this->once()) + ->method('initializeFromData') + ->with($this->product, $productData) + ->willReturn($this->product); $this->action->execute(); } diff --git a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/ValidateTest.php b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/ValidateTest.php index b24546b9e87da..4a5f6d45767c6 100644 --- a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/ValidateTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/ValidateTest.php @@ -3,46 +3,71 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product; use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +/** + * Class to test product validation before save + * + * @package Magento\Catalog\Test\Unit\Controller\Adminhtml\Product + */ class ValidateTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest { /** @var \Magento\Catalog\Controller\Adminhtml\Product\Validate */ protected $action; + /** @var \Magento\Backend\Model\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject */ protected $resultPage; + /** @var \Magento\Backend\Model\View\Result\Forward|\PHPUnit_Framework_MockObject_MockObject */ protected $resultForward; + /** @var \Magento\Catalog\Controller\Adminhtml\Product\Builder|\PHPUnit_Framework_MockObject_MockObject */ protected $productBuilder; + /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */ protected $product; + /** @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $resultRedirectFactory; + /** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */ protected $resultRedirect; + /** @var Helper|\PHPUnit_Framework_MockObject_MockObject */ protected $initializationHelper; + /** @var \Magento\Catalog\Model\ProductFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $productFactory; + /** @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject */ protected $resultJson; + /** @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $resultJsonFactory; + /** @var \Magento\Framework\Stdlib\DateTime\Filter\Date|\PHPUnit_Framework_MockObject_MockObject */ + protected $dateFilter; + + /** @var \Magento\Catalog\Model\Product\Validator|\PHPUnit_Framework_MockObject_MockObject */ + protected $productValidator; + + /** @var */ + protected $layoutFactory; + 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', 'getSku', 'getTypeId', 'getStoreId', '__sleep', '__wakeup', 'getAttributes', 'setAttributeSetId', @@ -53,20 +78,20 @@ protected function setUp() $this->product->expects($this->any())->method('getAttributes')->will($this->returnValue([])); $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->any())->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(); @@ -75,14 +100,14 @@ protected function setUp() ->willReturn($this->resultForward); $this->resultPage->expects($this->any())->method('getLayout')->willReturn($this->layout); $this->resultRedirectFactory = $this->getMock( - 'Magento\Backend\Model\View\Result\RedirectFactory', + \Magento\Backend\Model\View\Result\RedirectFactory::class, ['create'], [], '', false ); $this->resultRedirect = $this->getMock( - 'Magento\Backend\Model\View\Result\Redirect', + \Magento\Backend\Model\View\Result\Redirect::class, [], [], '', @@ -90,47 +115,61 @@ protected function setUp() ); $this->resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect); - $this->initializationHelper = $this->getMock( - 'Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper', - [], - [], - '', - false - ); + $this->initializationHelper = $this->getMock(Helper::class, [], [], '', false); - $this->productFactory = $this->getMockBuilder('Magento\Catalog\Model\ProductFactory') + $this->productFactory = $this->getMockBuilder(\Magento\Catalog\Model\ProductFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); $this->productFactory->expects($this->any())->method('create')->willReturn($this->product); - $this->resultJson = $this->getMock('Magento\Framework\Controller\Result\Json', [], [], '', false); - $this->resultJsonFactory = $this->getMockBuilder('Magento\Framework\Controller\Result\JsonFactory') + $this->resultJson = $this->getMock(\Magento\Framework\Controller\Result\Json::class, [], [], '', false); + $this->resultJsonFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class) ->disableOriginalConstructor() ->setMethods(['create']) ->getMock(); $this->resultJsonFactory->expects($this->any())->method('create')->willReturn($this->resultJson); + $this->dateFilter = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\Filter\Date::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->productValidator = $this->getMockBuilder(\Magento\Catalog\Model\Product\Validator::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->layoutFactory = $this->getMockBuilder(\Magento\Framework\View\LayoutFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $additionalParams = ['resultRedirectFactory' => $this->resultRedirectFactory]; - $this->action = (new ObjectManagerHelper($this))->getObject( - 'Magento\Catalog\Controller\Adminhtml\Product\Validate', - [ - 'context' => $this->initContext($additionalParams), - 'productBuilder' => $this->productBuilder, - 'resultPageFactory' => $resultPageFactory, - 'resultForwardFactory' => $resultForwardFactory, - 'initializationHelper' => $this->initializationHelper, - 'resultJsonFactory' => $this->resultJsonFactory, - 'productFactory' => $this->productFactory, - ] - ); + $this->action = $this->getMockBuilder(\Magento\Catalog\Controller\Adminhtml\Product\Validate::class) + ->setMethods(['getInitializationHelper']) + ->setConstructorArgs( + [ + 'context' => $this->initContext($additionalParams), + 'productBuilder' => $this->productBuilder, + 'dateFilter' => $this->dateFilter, + 'productValidator' => $this->productValidator, + 'resultJsonFactory' => $this->resultJsonFactory, + 'layoutFactory' => $this->layoutFactory, + 'productFactory' => $this->productFactory, + + ] + )->getMock(); + $this->action->method('getInitializationHelper')->willReturn($this->initializationHelper); } public function testAttributeSetIsObtainedFromPostByDefault() { $this->request->expects($this->any())->method('getParam')->willReturnMap([['set', null, 4]]); - $this->request->expects($this->any())->method('getPost')->willReturnMap([['set', null, 9]]); + $this->request->expects($this->any())->method('getPost')->willReturnMap([ + ['set', null, 9], + ['product', [], []], + ]); $this->product->expects($this->once())->method('setAttributeSetId')->with(9); + $this->initializationHelper->expects($this->any())->method('initializeFromData') + ->willReturn($this->product); $this->action->execute(); } @@ -138,8 +177,30 @@ public function testAttributeSetIsObtainedFromPostByDefault() public function testAttributeSetIsObtainedFromGetWhenThereIsNoOneInPost() { $this->request->expects($this->any())->method('getParam')->willReturnMap([['set', null, 4]]); - $this->request->expects($this->any())->method('getPost')->willReturnMap([['set', null, null]]); + $this->request->expects($this->any())->method('getPost')->willReturnMap([ + ['set', null, null], + ['product', [], []], + ]); $this->product->expects($this->once())->method('setAttributeSetId')->with(4); + $this->initializationHelper->expects($this->any()) + ->method('initializeFromData') + ->willReturn($this->product); + + $this->action->execute(); + } + + public function testInitializeFromData() + { + $productData = ['name' => 'test-name', 'stock_data' => ['use_config_manage_stock' => 0]]; + $this->request->expects($this->any())->method('getPost')->willReturnMap([ + ['product', [], $productData], + ]); + + $this->initializationHelper + ->expects($this->once()) + ->method('initializeFromData') + ->with($this->product, $productData) + ->willReturn($this->product); $this->action->execute(); } From a70142121ef2fffa11da8be5afcc6111331865a2 Mon Sep 17 00:00:00 2001 From: Andrii Meysar Date: Thu, 29 Dec 2016 09:15:14 +0200 Subject: [PATCH 2/6] MAGETWO-62647: "Invalid option value" Error message is shown when delete option row without data --- .../Product/Initialization/Helper.php | 25 +---- .../Adminhtml/Product/NewAction.php | 22 +--- .../Controller/Adminhtml/Product/Validate.php | 35 +++--- .../Product/Initialization/HelperTest.php | 5 +- .../Adminhtml/Product/NewActionTest.php | 51 +++------ .../Adminhtml/Product/ValidateTest.php | 103 +++++------------- 6 files changed, 64 insertions(+), 177 deletions(-) diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php index 8278a7d244513..3e45111fb6982 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php @@ -3,12 +3,11 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Catalog\Controller\Adminhtml\Product\Initialization; /** * Product initialization helper class - * - * @package Magento\Catalog\Controller\Adminhtml\Product\Initialization */ class Helper { @@ -67,18 +66,16 @@ public function __construct( } /** - * Initialize product from data + * Initialize product before saving * * @param \Magento\Catalog\Model\Product $product - * @param array $productData * @return \Magento\Catalog\Model\Product - * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ - public function initializeFromData(\Magento\Catalog\Model\Product $product, array $productData = null) + public function initialize(\Magento\Catalog\Model\Product $product) { - $productData = $productData ?: $this->request->getPost('product', []); + $productData = $this->request->getPost('product', []); unset($productData['custom_attributes']); unset($productData['extension_attributes']); @@ -154,7 +151,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra foreach ($options as &$customOptionData) { if (isset($customOptionData['values'])) { $customOptionData['values'] = array_filter($customOptionData['values'], function ($valueData) { - return empty($valueData['is_delete']); + return !($valueData['option_type_id'] == '-1' && !empty($valueData['is_delete'])); }); } } @@ -168,18 +165,6 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra return $product; } - /** - * Initialize product before saving - * - * @param \Magento\Catalog\Model\Product $product - * @return \Magento\Catalog\Model\Product - */ - public function initialize(\Magento\Catalog\Model\Product $product) - { - $productData = $this->request->getPost('product', []); - return $this->initializeFromData($product, $productData); - } - /** * Merge product and default options for product * diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php index 229696fa5a862..cc671cfd8d2f9 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/NewAction.php @@ -8,12 +8,9 @@ use Magento\Backend\App\Action; use Magento\Catalog\Controller\Adminhtml\Product; -use Magento\Framework\App\ObjectManager; /** * Class that handle new product creation - * - * @package Magento\Catalog\Controller\Adminhtml\Product */ class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product { @@ -32,11 +29,6 @@ class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product */ protected $resultForwardFactory; - /** - * @var Initialization\Helper - */ - protected $initializationHelper; - /** * @param Action\Context $context * @param Builder $productBuilder @@ -80,7 +72,7 @@ public function execute() if ($productData) { $stockData = isset($productData['stock_data']) ? $productData['stock_data'] : []; $productData['stock_data'] = $this->stockFilter->filter($stockData); - $product = $this->getInitializationHelper()->initializeFromData($product, $productData); + $product->addData($productData); } $this->_eventManager->dispatch('catalog_product_new_action', ['product' => $product]); @@ -103,16 +95,4 @@ public function execute() return $resultPage; } - - /** - * @deprecated - * @return Initialization\Helper - */ - protected function getInitializationHelper() - { - if (null === $this->initializationHelper) { - $this->initializationHelper = ObjectManager::getInstance()->get(Initialization\Helper::class); - } - return $this->initializationHelper; - } } diff --git a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php index 7c61b0cf17865..ae7b4b3d451d7 100644 --- a/app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php +++ b/app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php @@ -8,7 +8,6 @@ use Magento\Backend\App\Action; use Magento\Catalog\Controller\Adminhtml\Product; -use Magento\Framework\App\ObjectManager; /** * Product validate @@ -19,7 +18,6 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product { /** * @var \Magento\Framework\Stdlib\DateTime\Filter\Date - * @deprecated */ protected $_dateFilter; @@ -41,11 +39,6 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product /** @var \Magento\Catalog\Model\ProductFactory */ protected $productFactory; - /** - * @var Initialization\Helper - */ - protected $initializationHelper; - /** * @param Action\Context $context * @param Builder $productBuilder @@ -85,7 +78,7 @@ public function execute() $response->setError(false); try { - $productData = $this->getRequest()->getPost('product', []); + $productData = $this->getRequest()->getPost('product'); if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) { $productData['stock_data']['use_config_manage_stock'] = 0; @@ -109,7 +102,19 @@ public function execute() if ($productId) { $product->load($productId); } - $product = $this->getInitializationHelper()->initializeFromData($product, $productData); + + $dateFieldFilters = []; + $attributes = $product->getAttributes(); + foreach ($attributes as $attrKey => $attribute) { + if ($attribute->getBackend()->getType() == 'datetime') { + if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') { + $dateFieldFilters[$attrKey] = $this->_dateFilter; + } + } + } + $inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $productData); + $productData = $inputFilter->getUnescaped(); + $product->addData($productData); /* set restrictions for date ranges */ $resource = $product->getResource(); @@ -135,16 +140,4 @@ public function execute() return $this->resultJsonFactory->create()->setJsonData($response->toJson()); } - - /** - * @return Initialization\Helper - * @deprecated - */ - protected function getInitializationHelper() - { - if (null === $this->initializationHelper) { - $this->initializationHelper = ObjectManager::getInstance()->get(Initialization\Helper::class); - } - return $this->initializationHelper; - } } diff --git a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php index 750462b388042..dc6ee45b79f25 100644 --- a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php @@ -3,6 +3,7 @@ * 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; @@ -155,8 +156,8 @@ public function testInitialize() 'option1' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1', 'option_id' => '13'], 'option2' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1', 'option_id' => '14', 'values' => [ - 'value1' => ['is_delete' =>''], - 'value2' => ['is_delete' =>'1'] + 'value1' => ['option_type_id' => 1, 'is_delete' =>''], + 'value2' => ['option_type_id' => '-1', 'is_delete' =>'1'] ] ], ]; diff --git a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/NewActionTest.php b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/NewActionTest.php index a2fb859e20f9b..d887b33f17ef6 100644 --- a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/NewActionTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/NewActionTest.php @@ -4,16 +4,11 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product; -use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; -use Magento\Catalog\Controller\Adminhtml\Product\Initialization\StockDataFilter; -use Magento\Catalog\Controller\Adminhtml\Product\NewAction; +namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product; /** * Class to test new product creation - * - * @package Magento\Catalog\Test\Unit\Controller\Adminhtml\Product */ class NewActionTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest { @@ -28,11 +23,6 @@ class NewActionTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\Prod /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */ protected $product; - /** - * @var Helper|\PHPUnit_Framework_MockObject_MockObject - */ - protected $initializationHelper; - protected function setUp() { $this->productBuilder = $this->getMock( @@ -42,7 +32,8 @@ protected function setUp() '', false ); - $this->product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)->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')); @@ -70,24 +61,19 @@ protected function setUp() $resultForwardFactory->expects($this->any()) ->method('create') ->willReturn($this->resultForward); - $this->initializationHelper = $this->getMock(Helper::class, [], [], '', false); - $stockDataFilter = $this->getMockBuilder(StockDataFilter::class) - ->disableOriginalConstructor()->getMock(); - $this->action = $this->getMockBuilder(NewAction::class) - ->setMethods(['getInitializationHelper']) - ->setConstructorArgs( - [ - 'context' => $this->initContext(), - 'productBuilder' => $this->productBuilder, - 'stockFilter' => $stockDataFilter, - 'resultPageFactory' => $resultPageFactory, - 'resultForwardFactory' => $resultForwardFactory, - ] - )->getMock(); + $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, + $stockFilter, + $resultPageFactory, + $resultForwardFactory + ); - $this->action->method('getInitializationHelper')->willReturn($this->initializationHelper); - $this->action->method('getRequest')->willReturn($this->request); $this->resultPage->expects($this->atLeastOnce()) ->method('getLayout') ->willReturn($this->layout); @@ -107,15 +93,10 @@ public function testExecuteObtainsProductDataFromSession() $this->action->getRequest()->expects($this->any())->method('getFullActionName') ->willReturn('catalog_product_new'); - $productData = ['name' => 'test-name', 'stock_data' => null]; $this->session->expects($this->any())->method('getProductData') - ->willReturn(['product' => $productData]); + ->willReturn(['product' => ['name' => 'test-name']]); - $this->initializationHelper - ->expects($this->once()) - ->method('initializeFromData') - ->with($this->product, $productData) - ->willReturn($this->product); + $this->product->expects($this->once())->method('addData')->with(['name' => 'test-name', 'stock_data' => null]); $this->action->execute(); } diff --git a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/ValidateTest.php b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/ValidateTest.php index 4a5f6d45767c6..d98b115f56608 100644 --- a/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/ValidateTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/ValidateTest.php @@ -7,58 +7,41 @@ namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product; use Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper; +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; /** * Class to test product validation before save - * - * @package Magento\Catalog\Test\Unit\Controller\Adminhtml\Product */ class ValidateTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\ProductTest { /** @var \Magento\Catalog\Controller\Adminhtml\Product\Validate */ protected $action; - /** @var \Magento\Backend\Model\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject */ protected $resultPage; - /** @var \Magento\Backend\Model\View\Result\Forward|\PHPUnit_Framework_MockObject_MockObject */ protected $resultForward; - /** @var \Magento\Catalog\Controller\Adminhtml\Product\Builder|\PHPUnit_Framework_MockObject_MockObject */ protected $productBuilder; - /** @var \Magento\Catalog\Model\Product|\PHPUnit_Framework_MockObject_MockObject */ protected $product; - /** @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $resultRedirectFactory; - /** @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject */ protected $resultRedirect; - /** @var Helper|\PHPUnit_Framework_MockObject_MockObject */ protected $initializationHelper; - /** @var \Magento\Catalog\Model\ProductFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $productFactory; - /** @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject */ protected $resultJson; - /** @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $resultJsonFactory; - - /** @var \Magento\Framework\Stdlib\DateTime\Filter\Date|\PHPUnit_Framework_MockObject_MockObject */ - protected $dateFilter; - - /** @var \Magento\Catalog\Model\Product\Validator|\PHPUnit_Framework_MockObject_MockObject */ - protected $productValidator; - - /** @var */ - protected $layoutFactory; + /** @var ObjectManagerHelper */ + protected $objectManagerHelper; protected function setUp() { + $this->objectManagerHelper = new ObjectManagerHelper($this); $this->productBuilder = $this->getMock( \Magento\Catalog\Controller\Adminhtml\Product\Builder::class, ['build'], @@ -115,7 +98,13 @@ protected function setUp() ); $this->resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect); - $this->initializationHelper = $this->getMock(Helper::class, [], [], '', false); + $this->initializationHelper = $this->getMock( + \Magento\Catalog\Controller\Adminhtml\Product\Initialization\Helper::class, + [], + [], + '', + false + ); $this->productFactory = $this->getMockBuilder(\Magento\Catalog\Model\ProductFactory::class) ->disableOriginalConstructor() @@ -130,46 +119,26 @@ protected function setUp() ->getMock(); $this->resultJsonFactory->expects($this->any())->method('create')->willReturn($this->resultJson); - $this->dateFilter = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\Filter\Date::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->productValidator = $this->getMockBuilder(\Magento\Catalog\Model\Product\Validator::class) - ->disableOriginalConstructor() - ->getMock(); - - $this->layoutFactory = $this->getMockBuilder(\Magento\Framework\View\LayoutFactory::class) - ->disableOriginalConstructor() - ->getMock(); - $additionalParams = ['resultRedirectFactory' => $this->resultRedirectFactory]; - $this->action = $this->getMockBuilder(\Magento\Catalog\Controller\Adminhtml\Product\Validate::class) - ->setMethods(['getInitializationHelper']) - ->setConstructorArgs( - [ - 'context' => $this->initContext($additionalParams), - 'productBuilder' => $this->productBuilder, - 'dateFilter' => $this->dateFilter, - 'productValidator' => $this->productValidator, - 'resultJsonFactory' => $this->resultJsonFactory, - 'layoutFactory' => $this->layoutFactory, - 'productFactory' => $this->productFactory, - - ] - )->getMock(); - $this->action->method('getInitializationHelper')->willReturn($this->initializationHelper); + $this->action = $this->objectManagerHelper->getObject( + \Magento\Catalog\Controller\Adminhtml\Product\Validate::class, + [ + 'context' => $this->initContext($additionalParams), + 'productBuilder' => $this->productBuilder, + 'resultPageFactory' => $resultPageFactory, + 'resultForwardFactory' => $resultForwardFactory, + 'initializationHelper' => $this->initializationHelper, + 'resultJsonFactory' => $this->resultJsonFactory, + 'productFactory' => $this->productFactory, + ] + ); } public function testAttributeSetIsObtainedFromPostByDefault() { $this->request->expects($this->any())->method('getParam')->willReturnMap([['set', null, 4]]); - $this->request->expects($this->any())->method('getPost')->willReturnMap([ - ['set', null, 9], - ['product', [], []], - ]); + $this->request->expects($this->any())->method('getPost')->willReturnMap([['set', null, 9]]); $this->product->expects($this->once())->method('setAttributeSetId')->with(9); - $this->initializationHelper->expects($this->any())->method('initializeFromData') - ->willReturn($this->product); $this->action->execute(); } @@ -177,30 +146,8 @@ public function testAttributeSetIsObtainedFromPostByDefault() public function testAttributeSetIsObtainedFromGetWhenThereIsNoOneInPost() { $this->request->expects($this->any())->method('getParam')->willReturnMap([['set', null, 4]]); - $this->request->expects($this->any())->method('getPost')->willReturnMap([ - ['set', null, null], - ['product', [], []], - ]); + $this->request->expects($this->any())->method('getPost')->willReturnMap([['set', null, null]]); $this->product->expects($this->once())->method('setAttributeSetId')->with(4); - $this->initializationHelper->expects($this->any()) - ->method('initializeFromData') - ->willReturn($this->product); - - $this->action->execute(); - } - - public function testInitializeFromData() - { - $productData = ['name' => 'test-name', 'stock_data' => ['use_config_manage_stock' => 0]]; - $this->request->expects($this->any())->method('getPost')->willReturnMap([ - ['product', [], $productData], - ]); - - $this->initializationHelper - ->expects($this->once()) - ->method('initializeFromData') - ->with($this->product, $productData) - ->willReturn($this->product); $this->action->execute(); } From e18caf41cef0e6e4af1a991c1bf4bef7e71bd996 Mon Sep 17 00:00:00 2001 From: OlgaVasyltsun Date: Thu, 29 Dec 2016 16:58:46 +0200 Subject: [PATCH 3/6] MAGETWO-62680: Restricted Admin User mustn't see info from other store --- .../Order/Creditmemo/Grid/Collection.php | 36 +++++++++++++++++++ .../Creditmemo/Order/Grid/Collection.php | 36 +++++++++++++++++++ .../Order/Invoice/Grid/Collection.php | 36 +++++++++++++++++++ .../Order/Invoice/Orders/Grid/Collection.php | 36 +++++++++++++++++++ .../Order/Shipment/Grid/Collection.php | 36 +++++++++++++++++++ .../Order/Shipment/Order/Grid/Collection.php | 36 +++++++++++++++++++ app/code/Magento/Sales/etc/di.xml | 36 ------------------- 7 files changed, 216 insertions(+), 36 deletions(-) create mode 100644 app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Grid/Collection.php create mode 100644 app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Order/Grid/Collection.php create mode 100644 app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Grid/Collection.php create mode 100644 app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Orders/Grid/Collection.php create mode 100644 app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Grid/Collection.php create mode 100644 app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Order/Grid/Collection.php diff --git a/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Grid/Collection.php b/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Grid/Collection.php new file mode 100644 index 0000000000000..d5c0689c14022 --- /dev/null +++ b/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Grid/Collection.php @@ -0,0 +1,36 @@ +Magento\Framework\App\State\Proxy - - - sales_invoice_grid - Magento\Sales\Model\ResourceModel\Order\Invoice - - - - - sales_shipment_grid - Magento\Sales\Model\ResourceModel\Order\Shipment - - - - - sales_creditmemo_grid - Magento\Sales\Model\ResourceModel\Order\Creditmemo - - - - - sales_invoice_grid - Magento\Sales\Model\ResourceModel\Order\Invoice - - - - - sales_shipment_grid - Magento\Sales\Model\ResourceModel\Order\Shipment - - - - - sales_creditmemo_grid - Magento\Sales\Model\ResourceModel\Order\Creditmemo - - Magento\Sales\Model\ResourceModel\Order From ef13c54ec707cdcd599cc0aca1c1727479e6d507 Mon Sep 17 00:00:00 2001 From: OlgaVasyltsun Date: Wed, 4 Jan 2017 10:06:50 +0200 Subject: [PATCH 4/6] MAGETWO-62680: Creation real classes from virtual --- .../Order/Creditmemo/Grid/Collection.php | 2 +- .../Creditmemo/Order/Grid/Collection.php | 2 +- .../Order/Invoice/Grid/Collection.php | 2 +- .../Order/Invoice/Orders/Grid/Collection.php | 2 +- .../Order/Shipment/Grid/Collection.php | 2 +- .../Order/Shipment/Order/Grid/Collection.php | 2 +- .../Magento/Catalog/_files/product_simple.php | 2 +- .../_files/product_simple_multiwebsite.php | 34 ++++++++++ .../testsuite/Magento/Sales/_files/order.php | 10 +-- .../_files/order_fixture_second_store.php | 66 +++++++++++++++++++ ...ice_shipment_creditmemo_for_two_stores.php | 59 +++++++++++++++++ .../Store/_files/second_website_store.php | 55 ++++++++++++++++ 12 files changed, 226 insertions(+), 12 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_multiwebsite.php create mode 100644 dev/tests/integration/testsuite/Magento/Sales/_files/order_fixture_second_store.php create mode 100644 dev/tests/integration/testsuite/Magento/Sales/_files/order_invoice_shipment_creditmemo_for_two_stores.php create mode 100644 dev/tests/integration/testsuite/Magento/Store/_files/second_website_store.php diff --git a/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Grid/Collection.php b/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Grid/Collection.php index d5c0689c14022..9c68b48319713 100644 --- a/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Grid/Collection.php +++ b/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Grid/Collection.php @@ -29,7 +29,7 @@ public function __construct( FetchStrategy $fetchStrategy, EventManager $eventManager, $mainTable = 'sales_creditmemo_grid', - $resourceModel = '\Magento\Sales\Model\ResourceModel\Order\Creditmemo' + $resourceModel = \Magento\Sales\Model\ResourceModel\Order\Creditmemo::class ) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel); } diff --git a/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Order/Grid/Collection.php b/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Order/Grid/Collection.php index 4455879910ac8..96ee1a67c3dc7 100644 --- a/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Order/Grid/Collection.php +++ b/app/code/Magento/Sales/Model/ResourceModel/Order/Creditmemo/Order/Grid/Collection.php @@ -29,7 +29,7 @@ public function __construct( FetchStrategy $fetchStrategy, EventManager $eventManager, $mainTable = 'sales_creditmemo_grid', - $resourceModel = '\Magento\Sales\Model\ResourceModel\Order\Creditmemo' + $resourceModel = \Magento\Sales\Model\ResourceModel\Order\Creditmemo::class ) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel); } diff --git a/app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Grid/Collection.php b/app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Grid/Collection.php index 97a8cda9ab1eb..874bb9f6ae672 100644 --- a/app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Grid/Collection.php +++ b/app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Grid/Collection.php @@ -29,7 +29,7 @@ public function __construct( FetchStrategy $fetchStrategy, EventManager $eventManager, $mainTable = 'sales_invoice_grid', - $resourceModel = '\Magento\Sales\Model\ResourceModel\Order\Invoice' + $resourceModel = \Magento\Sales\Model\ResourceModel\Order\Invoice::class ) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel); } diff --git a/app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Orders/Grid/Collection.php b/app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Orders/Grid/Collection.php index ae4788c4daea3..f8c1197440fd1 100644 --- a/app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Orders/Grid/Collection.php +++ b/app/code/Magento/Sales/Model/ResourceModel/Order/Invoice/Orders/Grid/Collection.php @@ -29,7 +29,7 @@ public function __construct( FetchStrategy $fetchStrategy, EventManager $eventManager, $mainTable = 'sales_invoice_grid', - $resourceModel = '\Magento\Sales\Model\ResourceModel\Order\Invoice' + $resourceModel = \Magento\Sales\Model\ResourceModel\Order\Invoice::class ) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel); } diff --git a/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Grid/Collection.php b/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Grid/Collection.php index f3141f521623f..066f617f28bd5 100644 --- a/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Grid/Collection.php +++ b/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Grid/Collection.php @@ -29,7 +29,7 @@ public function __construct( FetchStrategy $fetchStrategy, EventManager $eventManager, $mainTable = 'sales_shipment_grid', - $resourceModel = '\Magento\Sales\Model\ResourceModel\Order\Shipment' + $resourceModel = \Magento\Sales\Model\ResourceModel\Order\Shipment::class ) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel); } diff --git a/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Order/Grid/Collection.php b/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Order/Grid/Collection.php index 941b510a7f632..799173e1af170 100644 --- a/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Order/Grid/Collection.php +++ b/app/code/Magento/Sales/Model/ResourceModel/Order/Shipment/Order/Grid/Collection.php @@ -29,7 +29,7 @@ public function __construct( FetchStrategy $fetchStrategy, EventManager $eventManager, $mainTable = 'sales_shipment_grid', - $resourceModel = '\Magento\Sales\Model\ResourceModel\Order\Shipment' + $resourceModel = \Magento\Sales\Model\ResourceModel\Order\Shipment::class ) { parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel); } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple.php index 66766153dcf31..d382506222bd2 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple.php @@ -6,7 +6,7 @@ /** @var $product \Magento\Catalog\Model\Product */ $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager() - ->create('Magento\Catalog\Model\Product'); + ->create(\Magento\Catalog\Model\Product::class); $product->isObjectNew(true); $product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) ->setId(1) diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_multiwebsite.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_multiwebsite.php new file mode 100644 index 0000000000000..27b5a6f7fba31 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_multiwebsite.php @@ -0,0 +1,34 @@ +create(\Magento\Catalog\Model\Product::class); +$product->isObjectNew(true); +$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) + ->setAttributeSetId(4) + ->setWebsiteIds([1,2]) + ->setName('Simple Product Two') + ->setSku('simple_two') + ->setPrice(10) + ->setWeight(2) + ->setStockData( + [ + 'use_config_manage_stock' => 1, + 'qty' => 100, + 'is_qty_decimal' => 0, + 'is_in_stock' => 1, + ] + ) + ->setCategoryIds([2]) + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) + ->save(); diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/order.php b/dev/tests/integration/testsuite/Magento/Sales/_files/order.php index 4ac29374ef55f..c62b44106c938 100644 --- a/dev/tests/integration/testsuite/Magento/Sales/_files/order.php +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/order.php @@ -14,17 +14,17 @@ $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); -$billingAddress = $objectManager->create('Magento\Sales\Model\Order\Address', ['data' => $addressData]); +$billingAddress = $objectManager->create(\Magento\Sales\Model\Order\Address::class, ['data' => $addressData]); $billingAddress->setAddressType('billing'); $shippingAddress = clone $billingAddress; $shippingAddress->setId(null)->setAddressType('shipping'); -$payment = $objectManager->create('Magento\Sales\Model\Order\Payment'); +$payment = $objectManager->create(\Magento\Sales\Model\Order\Payment::class); $payment->setMethod('checkmo'); /** @var \Magento\Sales\Model\Order\Item $orderItem */ -$orderItem = $objectManager->create('Magento\Sales\Model\Order\Item'); +$orderItem = $objectManager->create(\Magento\Sales\Model\Order\Item::class); $orderItem->setProductId($product->getId())->setQtyOrdered(2); $orderItem->setBasePrice($product->getPrice()); $orderItem->setPrice($product->getPrice()); @@ -32,7 +32,7 @@ $orderItem->setProductType('simple'); /** @var \Magento\Sales\Model\Order $order */ -$order = $objectManager->create('Magento\Sales\Model\Order'); +$order = $objectManager->create(\Magento\Sales\Model\Order::class); $order->setIncrementId( '100000001' )->setState( @@ -56,7 +56,7 @@ )->setShippingAddress( $shippingAddress )->setStoreId( - $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId() + $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->getStore()->getId() )->addItem( $orderItem )->setPayment( diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/order_fixture_second_store.php b/dev/tests/integration/testsuite/Magento/Sales/_files/order_fixture_second_store.php new file mode 100644 index 0000000000000..a19a9b47bb290 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/order_fixture_second_store.php @@ -0,0 +1,66 @@ +create(\Magento\Sales\Model\Order\Address::class, ['data' => $addressData]); +$billingAddress->setAddressType('billing'); + +/** @var \Magento\Sales\Model\Order\Address $shippingAddress */ +$shippingAddress = clone $billingAddress; +$shippingAddress->setId(null)->setAddressType('shipping'); + +/** @var \Magento\Sales\Model\Order\Payment $payment */ +$payment = $objectManager->create(\Magento\Sales\Model\Order\Payment::class); +$payment->setMethod('checkmo'); + +/** @var \Magento\Sales\Model\Order\Item $orderItem */ +$orderItem = $objectManager->create(\Magento\Sales\Model\Order\Item::class); +$orderItem->setProductId($product->getId())->setQtyOrdered(2); +$orderItem->setBasePrice($product->getPrice()); +$orderItem->setPrice($product->getPrice()); +$orderItem->setRowTotal($product->getPrice()); +$orderItem->setProductType('simple'); + +$storeId = $store->getStoreId(); +$incrementId = $storeId . '00000001'; +/** @var \Magento\Sales\Model\Order $order */ +$order = $objectManager->create(\Magento\Sales\Model\Order::class); +$order->setIncrementId( + $incrementId +)->setState( + \Magento\Sales\Model\Order::STATE_PROCESSING +)->setStatus( + $order->getConfig()->getStateDefaultStatus(\Magento\Sales\Model\Order::STATE_PROCESSING) +)->setSubtotal( + 100 +)->setBaseSubtotal( + 100 +)->setCustomerIsGuest( + true +)->setCustomerEmail( + 'customer@null.com' +)->setBillingAddress( + $billingAddress +)->setShippingAddress( + $shippingAddress +)->setStoreId( + $storeId +)->addItem( + $orderItem +)->setPayment( + $payment +); +$order->save(); diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/order_invoice_shipment_creditmemo_for_two_stores.php b/dev/tests/integration/testsuite/Magento/Sales/_files/order_invoice_shipment_creditmemo_for_two_stores.php new file mode 100644 index 0000000000000..53b651724106e --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/order_invoice_shipment_creditmemo_for_two_stores.php @@ -0,0 +1,59 @@ +create( + \Magento\Sales\Api\InvoiceManagementInterface::class + ); + $orderIncrementId = $order->getIncrementId(); + /** @var \Magento\Sales\Model\Order\Invoice $invoice */ + $invoice = $orderService->prepareInvoice($order); + $invoice->register(); + $order->setIsInProcess(true); + /** @var \Magento\Framework\DB\Transaction $transactionSave */ + $transactionSave = $objectManager + ->create(\Magento\Framework\DB\Transaction::class); + $transactionSave->addObject($invoice)->addObject($order)->save(); + + /** @var Magento\Sales\Model\Order\Payment $payment */ + $payment = $order->getPayment(); + $paymentInfoBlock = $objectManager + ->get(\Magento\Payment\Helper\Data::class) + ->getInfoBlock($payment); + $payment->setBlockMock($paymentInfoBlock); + + /** @var \Magento\Sales\Model\Order\Shipment $shipment */ + $shipment = $objectManager->create(\Magento\Sales\Model\Order\Shipment::class); + $shipment->setOrder($order); + + /** @var \Magento\Sales\Model\Order\Shipment\Item $shipmentItem */ + $shipmentItem = $objectManager->create(\Magento\Sales\Model\Order\Shipment\Item::class); + $shipmentItem->setOrderItem($orderItem); + $shipment->addItem($shipmentItem); + $shipment->setPackages([['1'], ['2']]); + $shipment->setShipmentStatus(\Magento\Sales\Model\Order\Shipment::STATUS_NEW); + + $shipment->save(); + + /** @var \Magento\Sales\Model\Order\CreditmemoFactory $creditmemoFactory */ + $creditmemoFactory = $objectManager->get(\Magento\Sales\Model\Order\CreditmemoFactory::class); + /** @var Magento\Sales\Model\Order\Creditmemo $creditmemo */ + $creditmemo = $creditmemoFactory->createByOrder($order, $order->getData()); + $creditmemo->setOrder($order); + $creditmemo->setState(Magento\Sales\Model\Order\Creditmemo::STATE_OPEN); + $creditmemo->setIncrementId($orderIncrementId); + $creditmemo->save(); +} diff --git a/dev/tests/integration/testsuite/Magento/Store/_files/second_website_store.php b/dev/tests/integration/testsuite/Magento/Store/_files/second_website_store.php new file mode 100644 index 0000000000000..23a8af03d8760 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Store/_files/second_website_store.php @@ -0,0 +1,55 @@ +create(\Magento\Store\Model\Website::class); + +if (!$website->load('second_website', 'code')->getId()) { + $website->setData(['code' => 'second_website', 'name' => 'Second Website', 'is_default' => '0']); + $website->save(); +} + +$websiteId = $website->getId(); + +$group = $objectManager->create(\Magento\Store\Model\Group::class); + +if (!$group->load('Second Group', 'name')->getId()) { + $group->setData(['website_id' => $websiteId, 'name' => 'Second Group', 'root_category_id' => '2']); + $group->save(); +} + +$groupId = $group->getId(); + +$store = $objectManager->create(\Magento\Store\Model\Store::class); +$storeId = $store->load('fixture_second_store', 'code')->getId(); + +if (!$storeId) { + $store->setCode( + 'fixture_second_store' + )->setWebsiteId( + $websiteId + )->setGroupId( + $groupId + )->setName( + 'Fixture Store' + )->setSortOrder( + 10 + )->setIsActive( + 1 + ); + $store->save(); + + $eventManager = $objectManager->create(\Magento\Framework\Event\ManagerInterface::class); + $eventName = 'store_add'; + $eventManager->dispatch($eventName, ['store' => $store]); + + /* Refresh stores memory cache */ + $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->reinitStores(); +} From 41584ed397dd8c7cba4d8ebc26e28f92178a3416 Mon Sep 17 00:00:00 2001 From: OlgaVasyltsun Date: Wed, 4 Jan 2017 15:41:34 +0200 Subject: [PATCH 5/6] MAGETWO-62680: Creation real classes from virtual --- ...ent_creditmemo_for_two_stores_rollback.php | 28 +++++++++++++++++++ .../_files/second_website_store_rollback.php | 22 +++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 dev/tests/integration/testsuite/Magento/Sales/_files/order_invoice_shipment_creditmemo_for_two_stores_rollback.php create mode 100644 dev/tests/integration/testsuite/Magento/Store/_files/second_website_store_rollback.php diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/order_invoice_shipment_creditmemo_for_two_stores_rollback.php b/dev/tests/integration/testsuite/Magento/Sales/_files/order_invoice_shipment_creditmemo_for_two_stores_rollback.php new file mode 100644 index 0000000000000..03e9cd9428167 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/order_invoice_shipment_creditmemo_for_two_stores_rollback.php @@ -0,0 +1,28 @@ +get('Magento\Framework\Registry'); +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +/** @var \Magento\Sales\Model\ResourceModel\Order\Invoice\Grid\Collection $invoiceGridCollection */ +$invoiceGridCollection = $objectManager->create(\Magento\Sales\Model\ResourceModel\Order\Invoice\Grid\Collection::class); +$invoiceGridCollection->getConnection()->truncateTable($invoiceGridCollection->getMainTable()); + +/** @var \Magento\Sales\Model\ResourceModel\Order\Shipment\Grid\Collection $shipmentCollection */ +$shipmentCollection = $objectManager->create(\Magento\Sales\Model\ResourceModel\Order\Shipment\Grid\Collection::class); +$shipmentCollection->getConnection()->truncateTable($shipmentCollection->getMainTable()); + +/** @var \Magento\Sales\Model\ResourceModel\Order\Creditmemo\Grid\Collection $creditmemoCollection */ +$creditmemoCollection = $objectManager->create(\Magento\Sales\Model\ResourceModel\Order\Creditmemo\Grid\Collection::class); +$creditmemoCollection->getConnection()->truncateTable($creditmemoCollection->getMainTable()); + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false); \ No newline at end of file diff --git a/dev/tests/integration/testsuite/Magento/Store/_files/second_website_store_rollback.php b/dev/tests/integration/testsuite/Magento/Store/_files/second_website_store_rollback.php new file mode 100644 index 0000000000000..2e252c34481f9 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Store/_files/second_website_store_rollback.php @@ -0,0 +1,22 @@ +get('Magento\Framework\Registry'); + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +/** @var \Magento\Store\Model\Website $website */ +$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Store\Model\Website'); +$website->load('second_website'); + +if ($website->getId()) { + $website->delete(); +} + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false); From e9264ef0d25fc0914501b70953ee055a13dfd9ad Mon Sep 17 00:00:00 2001 From: Andrii Meysar Date: Thu, 5 Jan 2017 22:37:31 +0200 Subject: [PATCH 6/6] MAGETWO-62647: "Invalid option value" Error message is shown when delete option row without data - Add integration test --- .../Controller/Adminhtml/ProductTest.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/ProductTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/ProductTest.php index 4242163acd5b9..65016818c0780 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/ProductTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/ProductTest.php @@ -109,4 +109,74 @@ public function testEditAction() '"Save & Duplicate" button isn\'t present on Edit Product page' ); } + + /** + * @dataProvider saveWithInvalidCustomOptionDataProvider + * @magentoDataFixture Magento/Catalog/_files/product_without_options.php + */ + public function testSaveWithInvalidCustomOption($postData) + { + $this->getRequest()->setPostValue($postData); + $this->dispatch('backend/catalog/product/save/id/1'); + + $this->assertSessionMessages( + $this->contains('You saved the product.'), + \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS + ); + } + + /** + * Data Provider for save + * + * @return array + */ + public function saveWithInvalidCustomOptionDataProvider() + { + return [ + [ + [ + 'product' => [ + 'options' => [ + [ + 'title' => 'drop_down option', + 'type' => 'drop_down', + 'is_require' => true, + 'sort_order' => 0, + 'values' => [ + [ + 'title' => 'drop_down option 1', + 'price' => 10, + 'price_type' => 'fixed', + 'sku' => 'drop_down option 1 sku', + 'option_type_id' => '-1', + 'is_delete' => '', + 'sort_order' => 0, + ], + [ + 'title' => 'drop_down option 2', + 'price' => 20, + 'price_type' => 'fixed', + 'sku' => 'drop_down option 2 sku', + 'option_type_id' => '-1', + 'is_delete' => '', + 'sort_order' => 1, + ], + [ + 'title' => '', + 'price' => '', + 'price_type' => 'fixed', + 'sku' => '', + 'option_type_id' => '-1', + 'is_delete' => '1', + 'sort_order' => 2, + ] + ], + ] + ], + ], + 'affect_product_custom_options' => 1, + ] + ], + ]; + } }