Skip to content

Commit

Permalink
MAGETWO-47698: [Github] Custom options not displayed correctly on a s…
Browse files Browse the repository at this point in the history
…tore view level #2908 #5885

- tests stabilization and fixes
  • Loading branch information
Yurii Hryhoriev committed Sep 16, 2016
1 parent 8fa7764 commit ecf2c1f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 41 deletions.
4 changes: 4 additions & 0 deletions app/code/Magento/Catalog/Model/Product/Option/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,16 @@ public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $opt
if (!$productSku) {
throw new CouldNotSaveException(__('ProductSku should be specified'));
}
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->productRepository->get($productSku);
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
$option->setData('product_id', $product->getData($metadata->getLinkField()));
$option->setData('store_id', $product->getStoreId());

if ($option->getOptionId()) {
if (!$product->getOptions()) {
$product->getResource()->load($product, $product->getId());
}
$persistedOption = $product->getOptionById($option->getOptionId());
if (!$persistedOption) {
throw new NoSuchEntityException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product\Initialization;

use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
use Magento\Catalog\Api\ProductRepositoryInterface\Proxy as ProductRepository;
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\Catalog\Model\ProductRepository;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\Data\WebsiteInterface;
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\ProductCustomOptionInterface;
use Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks;

/**
Expand Down Expand Up @@ -95,7 +95,7 @@ class HelperTest extends \PHPUnit_Framework_TestCase
protected $customOptionFactoryMock;

/**
* @var ProductCustomOptionInterface|\PHPUnit_Framework_MockObject_MockObject
* @var Option|\PHPUnit_Framework_MockObject_MockObject
*/
protected $customOptionMock;

Expand Down Expand Up @@ -136,16 +136,13 @@ protected function setUp()
->getMock();
$this->productMock = $this->getMockBuilder(Product::class)
->setMethods([
'setData',
'addData',
'getId',
'setWebsiteIds',
'isLockedAttribute',
'lockAttribute',
'getAttributes',
'unlockAttribute',
'getOptionsReadOnly',
'setOptions',
'setCanSaveCustomOptions',
'__sleep',
'__wakeup',
Expand All @@ -159,9 +156,10 @@ protected function setUp()
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$this->customOptionMock = $this->getMockBuilder(ProductCustomOptionInterface::class)
$this->customOptionMock = $this->getMockBuilder(Option::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
->setMethods(null)
->getMock();
$this->productLinksMock = $this->getMockBuilder(ProductLinks::class)
->disableOriginalConstructor()
->getMock();
Expand Down Expand Up @@ -196,12 +194,10 @@ protected function setUp()
*/
public function testInitialize()
{
$this->customOptionMock->expects($this->once())
->method('setProductSku');

$optionsData = [
'option1' => ['is_delete' => true, 'name' => 'name1', 'price' => 'price1', 'option_id' => ''],
'option2' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1', 'option_id' => '13'],
'option3' => ['is_delete' => false, 'name' => 'name1', 'price' => 'price1', 'option_id' => '14']
];
$productData = [
'stock_data' => ['stock_data'],
Expand Down Expand Up @@ -275,14 +271,7 @@ public function testInitialize()
->method('getAttributes')
->willReturn($attributesArray);

$productData['category_ids'] = [];
$productData['website_ids'] = [];
unset($productData['options']);

$this->productMock->expects($this->once())
->method('addData')
->with($productData);
$this->productMock->expects($this->once())
$this->productMock->expects($this->any())
->method('getSku')
->willReturn('sku');
$this->productMock->expects($this->any())
Expand All @@ -291,13 +280,25 @@ public function testInitialize()

$this->customOptionFactoryMock->expects($this->any())
->method('create')
->with(['data' => $optionsData['option2']])
->willReturn($this->customOptionMock);
$this->productMock->expects($this->once())
->method('setOptions')
->with([$this->customOptionMock]);
->willReturnMap([
[
['data' => $optionsData['option2']],
(clone $this->customOptionMock)->setData($optionsData['option2'])
], [
['data' => $optionsData['option3']],
(clone $this->customOptionMock)->setData($optionsData['option3'])
]
]);

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

$productOptions = $this->productMock->getOptions();
$this->assertTrue(2 == count($productOptions));
list($option2, $option3) = $productOptions;
$this->assertTrue($option2->getOptionId() == $optionsData['option2']['option_id']);
$this->assertTrue('sku' == $option2->getData('product_sku'));
$this->assertTrue($option3->getOptionId() == $optionsData['option3']['option_id']);
$this->assertTrue('sku' == $option2->getData('product_sku'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@
'sort_order' => 0,
'values' => [
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 1',
'price' => 3,
'price_type' => 'fixed',
'sku' => '3-1-select',
],
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 2',
'price' => 3,
'price_type' => 'fixed',
Expand All @@ -183,14 +183,14 @@
'sort_order' => 0,
'values' => [
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 1',
'price' => 3,
'price_type' => 'fixed',
'sku' => '4-1-radio',
],
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 2',
'price' => 3,
'price_type' => 'fixed',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@
'sort_order' => 0,
'values' => [
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 1',
'price' => 3,
'price_type' => 'fixed',
'sku' => '3-1-select',
],
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 2',
'price' => 3,
'price_type' => 'fixed',
Expand All @@ -121,14 +121,14 @@
'sort_order' => 0,
'values' => [
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 1',
'price' => 3,
'price_type' => 'fixed',
'sku' => '4-1-radio',
],
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 2',
'price' => 3,
'price_type' => 'fixed',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,30 @@

$customOptions = [
[
'id' => 'test_option_code_1',
'option_id' => '0',
'option_id' => null,
'sort_order' => '0',
'title' => 'Option 1',
'type' => 'drop_down',
'is_require' => 1,
'values' => [
1 => ['option_type_id' => -1, 'title' => 'Option 1 & Value 1"', 'price' => '1.00', 'price_type' => 'fixed'],
2 => ['option_type_id' => -1, 'title' => 'Option 1 & Value 2"', 'price' => '2.00', 'price_type' => 'fixed'],
3 => ['option_type_id' => -1, 'title' => 'Option 1 & Value 3"', 'price' => '3.00', 'price_type' => 'fixed']
1 => [
'option_type_id' => null,
'title' => 'Option 1 & Value 1"',
'price' => '1.00',
'price_type' => 'fixed'
],
2 => [
'option_type_id' => null,
'title' => 'Option 1 & Value 2"',
'price' => '2.00',
'price_type' => 'fixed'
],
3 => [
'option_type_id' => null,
'title' => 'Option 1 & Value 3"',
'price' => '3.00',
'price_type' => 'fixed'
]
]
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@
'sort_order' => 0,
'values' => [
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 1',
'price' => 3,
'price_type' => 'fixed',
'sku' => '3-1-select',
],
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 2',
'price' => 3,
'price_type' => 'fixed',
Expand All @@ -127,14 +127,14 @@
'sort_order' => 0,
'values' => [
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 1',
'price' => 3,
'price_type' => 'fixed',
'sku' => '4-1-radio',
],
[
'option_type_id' => -1,
'option_type_id' => null,
'title' => 'Option 2',
'price' => 3,
'price_type' => 'fixed',
Expand Down

0 comments on commit ecf2c1f

Please sign in to comment.