From b365bf3a8149e49ca3d3e3f165ff1975c1ed34b6 Mon Sep 17 00:00:00 2001 From: Volodymyr Kholoshenko Date: Thu, 10 Sep 2015 19:07:12 +0300 Subject: [PATCH] MAGETWO-42468: Related products block displaying all products including ones that are out of stock --- .../Magento/Catalog/Model/Product/Link.php | 9 +++++ .../Test/Unit/Model/Product/LinkTest.php | 32 ++++++++++++--- .../Magento/Checkout/Block/Cart/Crosssell.php | 2 - .../Bootstrap/WebapiDocBlock.php | 2 +- .../ProductLinkManagementInterfaceTest.php | 5 ++- .../Api/ProductRepositoryInterfaceTest.php | 39 +++++++++++++++++++ .../Api/ProductLinkManagementTest.php | 3 ++ .../Api/ProductRepositoryInterfaceTest.php | 36 +++++++++++++++++ .../_files/product_virtual_in_stock.php | 20 ++++++++++ .../product_virtual_in_stock_rollback.php | 21 ++++++++++ .../GroupedProduct/_files/product_grouped.php | 2 +- 11 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock.php create mode 100644 dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock_rollback.php diff --git a/app/code/Magento/Catalog/Model/Product/Link.php b/app/code/Magento/Catalog/Model/Product/Link.php index 7ca13f75821ec..a088df34cf7df 100644 --- a/app/code/Magento/Catalog/Model/Product/Link.php +++ b/app/code/Magento/Catalog/Model/Product/Link.php @@ -49,11 +49,17 @@ class Link extends \Magento\Framework\Model\AbstractModel */ protected $_linkCollectionFactory; + /** + * @var \Magento\CatalogInventory\Helper\Stock + */ + protected $stockHelper; + /** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Catalog\Model\Resource\Product\Link\CollectionFactory $linkCollectionFactory * @param \Magento\Catalog\Model\Resource\Product\Link\Product\CollectionFactory $productCollectionFactory + * @param \Magento\CatalogInventory\Helper\Stock $stockHelper * @param \Magento\Framework\Model\Resource\AbstractResource $resource * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection * @param array $data @@ -63,12 +69,14 @@ public function __construct( \Magento\Framework\Registry $registry, \Magento\Catalog\Model\Resource\Product\Link\CollectionFactory $linkCollectionFactory, \Magento\Catalog\Model\Resource\Product\Link\Product\CollectionFactory $productCollectionFactory, + \Magento\CatalogInventory\Helper\Stock $stockHelper, \Magento\Framework\Model\Resource\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, array $data = [] ) { $this->_linkCollectionFactory = $linkCollectionFactory; $this->_productCollectionFactory = $productCollectionFactory; + $this->stockHelper = $stockHelper; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } @@ -128,6 +136,7 @@ public function getAttributeTypeTable($type) public function getProductCollection() { $collection = $this->_productCollectionFactory->create()->setLinkModel($this); + $this->stockHelper->addInStockFilterToCollection($collection); return $collection; } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Product/LinkTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Product/LinkTest.php index a8d1676440ee1..a806c515918ce 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Product/LinkTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Product/LinkTest.php @@ -19,6 +19,16 @@ class LinkTest extends \PHPUnit_Framework_TestCase */ protected $resource; + /** + * @var \Magento\CatalogInventory\Helper\Stock|\PHPUnit_Framework_MockObject_MockObject + */ + protected $stockHelperMock; + + /** + * @var \Magento\Catalog\Model\Resource\Product\Link\Product\Collection|\PHPUnit_Framework_MockObject_MockObject + */ + protected $productCollection; + protected function setUp() { $linkCollection = $this->getMockBuilder( @@ -35,12 +45,12 @@ protected function setUp() $linkCollectionFactory->expects($this->any()) ->method('create') ->will($this->returnValue($linkCollection)); - $productCollection = $this->getMockBuilder( + $this->productCollection = $this->getMockBuilder( 'Magento\Catalog\Model\Resource\Product\Link\Product\Collection' )->disableOriginalConstructor()->setMethods( ['setLinkModel'] )->getMock(); - $productCollection->expects($this->any())->method('setLinkModel')->will($this->returnSelf()); + $this->productCollection->expects($this->any())->method('setLinkModel')->will($this->returnSelf()); $productCollectionFactory = $this->getMockBuilder( 'Magento\Catalog\Model\Resource\Product\Link\Product\CollectionFactory' )->disableOriginalConstructor()->setMethods( @@ -48,7 +58,7 @@ protected function setUp() )->getMock(); $productCollectionFactory->expects($this->any()) ->method('create') - ->will($this->returnValue($productCollection)); + ->will($this->returnValue($this->productCollection)); $this->resource = $this->getMock( 'Magento\Framework\Model\Resource\AbstractResource', @@ -64,11 +74,19 @@ protected function setUp() ] ); + $this->stockHelperMock = $this->getMockBuilder('Magento\CatalogInventory\Helper\Stock') + ->disableOriginalConstructor() + ->getMock(); + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->model = $objectManager->getObject( 'Magento\Catalog\Model\Product\Link', - ['linkCollectionFactory' => $linkCollectionFactory, 'productCollectionFactory' => $productCollectionFactory, - 'resource' => $this->resource] + [ + 'linkCollectionFactory' => $linkCollectionFactory, + 'productCollectionFactory' => $productCollectionFactory, + 'resource' => $this->resource, + 'stockHelper' => $this->stockHelperMock + ] ); } @@ -110,6 +128,10 @@ public function testGetAttributeTypeTable() public function testGetProductCollection() { + $this->stockHelperMock + ->expects($this->once()) + ->method('addInStockFilterToCollection') + ->with($this->productCollection); $this->assertInstanceOf( 'Magento\Catalog\Model\Resource\Product\Link\Product\Collection', $this->model->getProductCollection() diff --git a/app/code/Magento/Checkout/Block/Cart/Crosssell.php b/app/code/Magento/Checkout/Block/Cart/Crosssell.php index 508bbd6857994..d15b017ddf7bb 100644 --- a/app/code/Magento/Checkout/Block/Cart/Crosssell.php +++ b/app/code/Magento/Checkout/Block/Cart/Crosssell.php @@ -195,8 +195,6 @@ protected function _getCollection() ); $this->_addProductAttributesAndPrices($collection); - $this->stockHelper->addInStockFilterToCollection($collection); - return $collection; } } diff --git a/dev/tests/api-functional/framework/Magento/TestFramework/Bootstrap/WebapiDocBlock.php b/dev/tests/api-functional/framework/Magento/TestFramework/Bootstrap/WebapiDocBlock.php index b8f7c54ad70f1..831becca6f3d2 100644 --- a/dev/tests/api-functional/framework/Magento/TestFramework/Bootstrap/WebapiDocBlock.php +++ b/dev/tests/api-functional/framework/Magento/TestFramework/Bootstrap/WebapiDocBlock.php @@ -18,7 +18,7 @@ class WebapiDocBlock extends \Magento\TestFramework\Bootstrap\DocBlock protected function _getSubscribers(\Magento\TestFramework\Application $application) { $subscribers = parent::_getSubscribers($application); - array_unshift($subscribers, new \Magento\TestFramework\Annotation\ApiDataFixture($this->_fixturesBaseDir)); + $subscribers[] = new \Magento\TestFramework\Annotation\ApiDataFixture($this->_fixturesBaseDir); return $subscribers; } } diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductLinkManagementInterfaceTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductLinkManagementInterfaceTest.php index 3aa38f8cdb53f..ff7e2abfbe54f 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductLinkManagementInterfaceTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductLinkManagementInterfaceTest.php @@ -9,6 +9,9 @@ use Magento\TestFramework\Helper\Bootstrap; use Magento\TestFramework\TestCase\WebapiAbstract; +/** + * @magentoAppIsolation enabled + */ class ProductLinkManagementInterfaceTest extends WebapiAbstract { const SERVICE_NAME = 'catalogProductLinkManagementV1'; @@ -85,7 +88,7 @@ protected function assertLinkedProducts($productSku, $linkType) /** * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php - * @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php + * @magentoApiDataFixture Magento/Catalog/_files/product_virtual_in_stock.php */ public function testAssign() { diff --git a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php index d33e1456bc363..4cbfcf59b2c53 100644 --- a/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php @@ -6,9 +6,13 @@ namespace Magento\Catalog\Api; use Magento\Catalog\Api\Data\ProductInterface; +use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\TestFramework\TestCase\WebapiAbstract; use Magento\Framework\Webapi\Exception as HTTPExceptionCodes; +/** + * @magentoAppIsolation enabled + */ class ProductRepositoryInterfaceTest extends WebapiAbstract { const SERVICE_NAME = 'catalogProductRepositoryV1'; @@ -139,6 +143,9 @@ public function testProductLinks() ProductInterface::STATUS => 1, ProductInterface::TYPE_ID => 'simple', ProductInterface::ATTRIBUTE_SET_ID => 4, + ProductInterface::EXTENSION_ATTRIBUTES_KEY => [ + 'stock_item' => $this->getStockItemData() + ] ]; $this->saveProduct($productData); @@ -778,4 +785,36 @@ public function testTierPrices() $response = $this->deleteProduct($productData[ProductInterface::SKU]); $this->assertTrue($response); } + + /** + * @return array + */ + private function getStockItemData() + { + return [ + StockItemInterface::IS_IN_STOCK => 1, + StockItemInterface::QTY => 100500, + StockItemInterface::IS_QTY_DECIMAL => 1, + StockItemInterface::SHOW_DEFAULT_NOTIFICATION_MESSAGE => 0, + StockItemInterface::USE_CONFIG_MIN_QTY => 0, + StockItemInterface::USE_CONFIG_MIN_SALE_QTY => 0, + StockItemInterface::MIN_QTY => 1, + StockItemInterface::MIN_SALE_QTY => 1, + StockItemInterface::MAX_SALE_QTY => 100, + StockItemInterface::USE_CONFIG_MAX_SALE_QTY => 0, + StockItemInterface::USE_CONFIG_BACKORDERS => 0, + StockItemInterface::BACKORDERS => 0, + StockItemInterface::USE_CONFIG_NOTIFY_STOCK_QTY => 0, + StockItemInterface::NOTIFY_STOCK_QTY => 0, + StockItemInterface::USE_CONFIG_QTY_INCREMENTS => 0, + StockItemInterface::QTY_INCREMENTS => 0, + StockItemInterface::USE_CONFIG_ENABLE_QTY_INC => 0, + StockItemInterface::ENABLE_QTY_INCREMENTS => 0, + StockItemInterface::USE_CONFIG_MANAGE_STOCK => 1, + StockItemInterface::MANAGE_STOCK => 1, + StockItemInterface::LOW_STOCK_DATE => null, + StockItemInterface::IS_DECIMAL_DIVIDED => 0, + StockItemInterface::STOCK_STATUS_CHANGED_AUTO => 0, + ]; + } } diff --git a/dev/tests/api-functional/testsuite/Magento/GroupedProduct/Api/ProductLinkManagementTest.php b/dev/tests/api-functional/testsuite/Magento/GroupedProduct/Api/ProductLinkManagementTest.php index 88160057b0700..5522d62bd0259 100644 --- a/dev/tests/api-functional/testsuite/Magento/GroupedProduct/Api/ProductLinkManagementTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GroupedProduct/Api/ProductLinkManagementTest.php @@ -6,6 +6,9 @@ */ namespace Magento\GroupedProduct\Api; +/** + * @magentoAppIsolation enabled + */ class ProductLinkManagementTest extends \Magento\TestFramework\TestCase\WebapiAbstract { const SERVICE_NAME = 'catalogProductLinkManagementV1'; diff --git a/dev/tests/api-functional/testsuite/Magento/GroupedProduct/Api/ProductRepositoryInterfaceTest.php b/dev/tests/api-functional/testsuite/Magento/GroupedProduct/Api/ProductRepositoryInterfaceTest.php index 8adffb82f7054..f4a8bab17e58d 100644 --- a/dev/tests/api-functional/testsuite/Magento/GroupedProduct/Api/ProductRepositoryInterfaceTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GroupedProduct/Api/ProductRepositoryInterfaceTest.php @@ -6,6 +6,7 @@ namespace Magento\GroupedProduct\Api; use Magento\Catalog\Api\Data\ProductInterface; +use Magento\CatalogInventory\Api\Data\StockItemInterface; use Magento\TestFramework\TestCase\WebapiAbstract; class ProductRepositoryInterfaceTest extends WebapiAbstract @@ -126,6 +127,9 @@ public function testProductLinks() ProductInterface::STATUS => 1, ProductInterface::TYPE_ID => 'simple', ProductInterface::ATTRIBUTE_SET_ID => 4, + ProductInterface::EXTENSION_ATTRIBUTES_KEY => [ + 'stock_item' => $this->getStockItemData() + ] ]; $this->saveProduct($productData); @@ -200,4 +204,36 @@ public function testProductLinks() $this->deleteProduct("product_simple_500"); $this->deleteProduct("group_product_500"); } + + /** + * @return array + */ + private function getStockItemData() + { + return [ + StockItemInterface::IS_IN_STOCK => 1, + StockItemInterface::QTY => 100500, + StockItemInterface::IS_QTY_DECIMAL => 1, + StockItemInterface::SHOW_DEFAULT_NOTIFICATION_MESSAGE => 0, + StockItemInterface::USE_CONFIG_MIN_QTY => 0, + StockItemInterface::USE_CONFIG_MIN_SALE_QTY => 0, + StockItemInterface::MIN_QTY => 1, + StockItemInterface::MIN_SALE_QTY => 1, + StockItemInterface::MAX_SALE_QTY => 100, + StockItemInterface::USE_CONFIG_MAX_SALE_QTY => 0, + StockItemInterface::USE_CONFIG_BACKORDERS => 0, + StockItemInterface::BACKORDERS => 0, + StockItemInterface::USE_CONFIG_NOTIFY_STOCK_QTY => 0, + StockItemInterface::NOTIFY_STOCK_QTY => 0, + StockItemInterface::USE_CONFIG_QTY_INCREMENTS => 0, + StockItemInterface::QTY_INCREMENTS => 0, + StockItemInterface::USE_CONFIG_ENABLE_QTY_INC => 0, + StockItemInterface::ENABLE_QTY_INCREMENTS => 0, + StockItemInterface::USE_CONFIG_MANAGE_STOCK => 1, + StockItemInterface::MANAGE_STOCK => 1, + StockItemInterface::LOW_STOCK_DATE => null, + StockItemInterface::IS_DECIMAL_DIVIDED => 0, + StockItemInterface::STOCK_STATUS_CHANGED_AUTO => 0, + ]; + } } diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock.php new file mode 100644 index 0000000000000..9f5be07e7658d --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock.php @@ -0,0 +1,20 @@ +create('Magento\Catalog\Model\Product'); +$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL) + ->setId(21) + ->setAttributeSetId(4) + ->setWebsiteIds([1]) + ->setName('Virtual Product') + ->setSku('virtual-product') + ->setPrice(10) + ->setTaxClassId(0) + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) + ->setStockData(['is_in_stock' => 1]) + ->save(); diff --git a/dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock_rollback.php b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock_rollback.php new file mode 100644 index 0000000000000..bed07e6d07bdd --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock_rollback.php @@ -0,0 +1,21 @@ +get('Magento\Framework\Registry'); + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +/** @var $product \Magento\Catalog\Model\Product */ +$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Catalog\Model\Product'); +$product->load(21); +if ($product->getId()) { + $product->delete(); +} + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false); diff --git a/dev/tests/integration/testsuite/Magento/GroupedProduct/_files/product_grouped.php b/dev/tests/integration/testsuite/Magento/GroupedProduct/_files/product_grouped.php index d8ac7ad7a8ce3..eb9f71d2f896f 100644 --- a/dev/tests/integration/testsuite/Magento/GroupedProduct/_files/product_grouped.php +++ b/dev/tests/integration/testsuite/Magento/GroupedProduct/_files/product_grouped.php @@ -5,7 +5,7 @@ */ require realpath(__DIR__ . '/../../') . '/Catalog/_files/product_simple_duplicated.php'; -require realpath(__DIR__ . '/../../') . '/Catalog/_files/product_virtual.php'; +require realpath(__DIR__ . '/../../') . '/Catalog/_files/product_virtual_in_stock.php'; /** @var $product \Magento\Catalog\Model\Product */ $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Catalog\Model\Product');