Skip to content

Commit

Permalink
Merge 760bb52 into PR_08062017
Browse files Browse the repository at this point in the history
  • Loading branch information
Magento CICD authored Jun 9, 2017
2 parents 4a14208 + 760bb52 commit b7f8145
Show file tree
Hide file tree
Showing 53 changed files with 2,344 additions and 871 deletions.
72 changes: 69 additions & 3 deletions app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -2509,13 +2509,15 @@ public function setTypeId($typeId)
/**
* {@inheritdoc}
*
* @return \Magento\Catalog\Api\Data\ProductExtensionInterface|null
* @return \Magento\Catalog\Api\Data\ProductExtensionInterface
*/
public function getExtensionAttributes()
{
$extensionAttributes = $this->_getExtensionAttributes();
if (!$extensionAttributes) {
return $this->extensionAttributesFactory->create(\Magento\Catalog\Api\Data\ProductInterface::class);
if (null === $extensionAttributes) {
/** @var \Magento\Catalog\Api\Data\ProductExtensionInterface $extensionAttributes */
$extensionAttributes = $this->extensionAttributesFactory->create(ProductInterface::class);
$this->setExtensionAttributes($extensionAttributes);
}
return $extensionAttributes;
}
Expand Down Expand Up @@ -2639,4 +2641,68 @@ public function setAssociatedProductIds(array $productIds)
$this->getExtensionAttributes()->setConfigurableProductLinks($productIds);
return $this;
}

/**
* Get quantity and stock status data
*
* @return array|null
*
* @deprecated as Product model shouldn't be responsible for stock status
* @see StockItemInterface when you want to change the stock data
* @see StockStatusInterface when you want to read the stock data for representation layer (storefront)
* @see StockItemRepositoryInterface::save as extension point for customization of saving process
*/
public function getQuantityAndStockStatus()
{
return $this->getData('quantity_and_stock_status');
}

/**
* Set quantity and stock status data
*
* @param array $quantityAndStockStatusData
* @return $this
*
* @deprecated as Product model shouldn't be responsible for stock status
* @see StockItemInterface when you want to change the stock data
* @see StockStatusInterface when you want to read the stock data for representation layer (storefront)
* @see StockItemRepositoryInterface::save as extension point for customization of saving process
*/
public function setQuantityAndStockStatus($quantityAndStockStatusData)
{
$this->setData('quantity_and_stock_status', $quantityAndStockStatusData);
return $this;
}

/**
* Get stock data
*
* @return array|null
*
* @deprecated as Product model shouldn't be responsible for stock status
* @see StockItemInterface when you want to change the stock data
* @see StockStatusInterface when you want to read the stock data for representation layer (storefront)
* @see StockItemRepositoryInterface::save as extension point for customization of saving process
*/
public function getStockData()
{
return $this->getData('stock_data');
}

/**
* Set stock data
*
* @param array $stockData
* @return $this
*
* @deprecated as Product model shouldn't be responsible for stock status
* @see StockItemInterface when you want to change the stock data
* @see StockStatusInterface when you want to read the stock data for representation layer (storefront)
* @see StockItemRepositoryInterface::save as extension point for customization of saving process
*/
public function setStockData($stockData)
{
$this->setData('stock_data', $stockData);
return $this;
}
}
25 changes: 5 additions & 20 deletions app/code/Magento/Catalog/Model/Product/Attribute/Backend/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Model\Product\Attribute\Backend;

use Magento\Catalog\Model\Product;

/**
* Quantity and Stock Status attribute processing
*
* @deprecated as this attribute should be removed
* @see StockItemInterface when you want to change the stock data
* @see StockStatusInterface when you want to read the stock data for representation layer (storefront)
* @see StockItemRepositoryInterface::save as extension point for customization of saving process
*/
class Stock extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
{
Expand Down Expand Up @@ -47,25 +51,6 @@ public function afterLoad($object)
return parent::afterLoad($object);
}

/**
* Prepare inventory data from custom attribute
*
* @param Product $object
* @return void
*/
public function beforeSave($object)
{
$stockData = $object->getData($this->getAttribute()->getAttributeCode());
if (isset($stockData['qty']) && $stockData['qty'] === '') {
$stockData['qty'] = null;
}
if ($object->getStockData() !== null && $stockData !== null) {
$object->setStockData(array_replace((array)$object->getStockData(), (array)$stockData));
}
$object->unsetData($this->getAttribute()->getAttributeCode());
parent::beforeSave($object);
}

/**
* Validate
*
Expand Down
21 changes: 20 additions & 1 deletion app/code/Magento/Catalog/Model/Product/Copier.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public function copy(\Magento\Catalog\Model\Product $product)

/** @var \Magento\Catalog\Model\Product $duplicate */
$duplicate = $this->productFactory->create();
$duplicate->setData($product->getData());
$productData = $product->getData();
$productData = $this->removeStockItem($productData);
$duplicate->setData($productData);
$duplicate->setOptions([]);
$duplicate->setIsDuplicate(true);
$duplicate->setOriginalLinkId($product->getData($metadata->getLinkField()));
Expand Down Expand Up @@ -116,4 +118,21 @@ private function getMetadataPool()
}
return $this->metadataPool;
}

/**
* Remove stock item
*
* @param array $productData
* @return array
*/
private function removeStockItem(array $productData)
{
if (isset($productData[ProductInterface::EXTENSION_ATTRIBUTES_KEY])) {
$extensionAttributes = $productData[ProductInterface::EXTENSION_ATTRIBUTES_KEY];
if (null !== $extensionAttributes->getStockItem()) {
$extensionAttributes->setData('stock_item', null);
}
}
return $productData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,68 +74,4 @@ public function testAfterLoad()
$this->assertEquals(1, $data[self::ATTRIBUTE_NAME]['is_in_stock']);
$this->assertEquals(5, $data[self::ATTRIBUTE_NAME]['qty']);
}

public function testBeforeSave()
{
$object = new \Magento\Framework\DataObject(
[
self::ATTRIBUTE_NAME => ['is_in_stock' => 1, 'qty' => 5],
'stock_data' => ['is_in_stock' => 2, 'qty' => 2],
]
);
$stockData = $object->getStockData();
$this->assertEquals(2, $stockData['is_in_stock']);
$this->assertEquals(2, $stockData['qty']);
$this->assertNotEmpty($object->getData(self::ATTRIBUTE_NAME));

$this->model->beforeSave($object);

$stockData = $object->getStockData();
$this->assertEquals(1, $stockData['is_in_stock']);
$this->assertEquals(5, $stockData['qty']);
$this->assertNull($object->getData(self::ATTRIBUTE_NAME));
}

public function testBeforeSaveQtyIsEmpty()
{
$object = new \Magento\Framework\DataObject(
[
self::ATTRIBUTE_NAME => ['is_in_stock' => 1, 'qty' => ''],
'stock_data' => ['is_in_stock' => 2, 'qty' => ''],
]
);

$this->model->beforeSave($object);

$stockData = $object->getStockData();
$this->assertNull($stockData['qty']);
}

public function testBeforeSaveQtyIsZero()
{
$object = new \Magento\Framework\DataObject(
[
self::ATTRIBUTE_NAME => ['is_in_stock' => 1, 'qty' => 0],
'stock_data' => ['is_in_stock' => 2, 'qty' => 0],
]
);

$this->model->beforeSave($object);

$stockData = $object->getStockData();
$this->assertEquals(0, $stockData['qty']);
}

public function testBeforeSaveNoStockData()
{
$object = new \Magento\Framework\DataObject(
[
self::ATTRIBUTE_NAME => ['is_in_stock' => 1, 'qty' => 0]
]
);

$this->model->beforeSave($object);
$this->assertNull($object->getStockData());
$this->assertNull($object->getData(self::ATTRIBUTE_NAME));
}
}
26 changes: 24 additions & 2 deletions app/code/Magento/Catalog/Test/Unit/Model/Product/CopierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
*/
namespace Magento\Catalog\Test\Unit\Model\Product;

use Magento\Catalog\Api\Data\ProductInterface;
use \Magento\Catalog\Model\Product\Copier;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CopierTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -80,10 +84,28 @@ protected function setUp()

public function testCopy()
{
$stockItem = $this->getMockBuilder(\Magento\CatalogInventory\Api\Data\StockItemInterface::class)
->getMock();
$extensionAttributes = $this->getMockBuilder(\Magento\Catalog\Api\Data\ProductExtension::class)
->setMethods(['getStockItem', 'setData'])
->getMock();
$extensionAttributes
->expects($this->once())
->method('getStockItem')
->willReturn($stockItem);
$extensionAttributes
->expects($this->once())
->method('setData')
->with('stock_item', null);

$productData = [
'product data' => ['product data'],
ProductInterface::EXTENSION_ATTRIBUTES_KEY => $extensionAttributes,
];
$this->productMock->expects($this->atLeastOnce())->method('getWebsiteIds');
$this->productMock->expects($this->atLeastOnce())->method('getCategoryIds');
$this->productMock->expects($this->any())->method('getData')->willReturnMap([
['', null, 'product data'],
['', null, $productData],
['linkField', null, '1'],
]);

Expand Down Expand Up @@ -135,7 +157,7 @@ public function testCopy()
)->with(
\Magento\Store\Model\Store::DEFAULT_STORE_ID
);
$duplicateMock->expects($this->once())->method('setData')->with('product data');
$duplicateMock->expects($this->once())->method('setData')->with($productData);
$this->copyConstructorMock->expects($this->once())->method('build')->with($this->productMock, $duplicateMock);
$duplicateMock->expects($this->once())->method('getUrlKey')->willReturn('urk-key-1');
$duplicateMock->expects($this->once())->method('setUrlKey')->with('urk-key-2');
Expand Down
15 changes: 14 additions & 1 deletion app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ class ProductTest extends \PHPUnit_Framework_TestCase
*/
private $collectionFactoryMock;

/**
* @var ProductExtensionInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $extensionAttributes;

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
Expand Down Expand Up @@ -392,8 +397,16 @@ protected function setUp()
->setMethods(['create'])
->getMock();
$this->mediaConfig = $this->getMock(\Magento\Catalog\Model\Product\Media\Config::class, [], [], '', false);
$this->objectManagerHelper = new ObjectManagerHelper($this);

$this->extensionAttributes = $this->getMockBuilder(ProductExtensionInterface::class)
->setMethods(['getStockItem'])
->getMock();
$this->extensionAttributesFactory
->expects($this->any())
->method('create')
->willReturn($this->extensionAttributes);

$this->objectManagerHelper = new ObjectManagerHelper($this);
$this->model = $this->objectManagerHelper->getObject(
\Magento\Catalog\Model\Product::class,
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ public function __construct(
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->request = $request;
$this->collection = $collectionFactory->create();
$this->collection->setExcludeSetFilter((int)$this->request->getParam('template_id', 0));
}

/**
* {@inheritdoc}
*/
public function getData()
{
$this->collection->setExcludeSetFilter((int)$this->request->getParam('template_id', 0));
$this->collection->getSelect()->setPart('order', []);

$items = [];
foreach ($this->getCollection()->getItems() as $attribute) {
$items[] = $attribute->toArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,28 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CatalogInventory\Model\Plugin;

/**
* @deprecated Stock Item as a part of ExtensionAttributes is deprecated
* @see StockItemInterface when you want to change the stock data
* @see StockStatusInterface when you want to read the stock data for representation layer (storefront)
* @see StockItemRepositoryInterface::save as extension point for customization of saving process
*/
class AfterProductLoad
{
/**
* @var \Magento\CatalogInventory\Api\StockRegistryInterface
*/
protected $stockRegistry;

/**
* @var \Magento\Catalog\Api\Data\ProductExtensionFactory
*/
protected $productExtensionFactory;
private $stockRegistry;

/**
* @param \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
* @param \Magento\Catalog\Api\Data\ProductExtensionFactory $productExtensionFactory
*/
public function __construct(
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
\Magento\Catalog\Api\Data\ProductExtensionFactory $productExtensionFactory
\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
) {
$this->stockRegistry = $stockRegistry;
$this->productExtensionFactory = $productExtensionFactory;
}

/**
Expand All @@ -40,10 +37,6 @@ public function __construct(
public function afterLoad(\Magento\Catalog\Model\Product $product)
{
$productExtension = $product->getExtensionAttributes();
if ($productExtension === null) {
$productExtension = $this->productExtensionFactory->create();
}
// stockItem := \Magento\CatalogInventory\Api\Data\StockItemInterface
$productExtension->setStockItem($this->stockRegistry->getStockItem($product->getId()));
$product->setExtensionAttributes($productExtension);
return $product;
Expand Down
Loading

0 comments on commit b7f8145

Please sign in to comment.