Skip to content

Commit 1bbeb21

Browse files
committed
graphQl-255: Test coverage for PR-243
1 parent 8e2ca94 commit 1bbeb21

File tree

1 file changed

+144
-6
lines changed

1 file changed

+144
-6
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/CategoryProductsCountTest.php

+144-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77

88
namespace Magento\GraphQl\Catalog;
99

10-
use Magento\TestFramework\TestCase\GraphQlAbstract;
10+
use Magento\Catalog\Api\CategoryLinkManagementInterface;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
12-
use Magento\TestFramework\Helper\Bootstrap;
13-
use Magento\Catalog\Model\Product\Visibility;
1412
use Magento\Catalog\Model\Product\Attribute\Source\Status as productStatus;
13+
use Magento\Catalog\Model\Product\Visibility;
14+
use Magento\CatalogInventory\Model\Configuration;
15+
use Magento\Config\Model\ResourceModel\Config;
16+
use Magento\Framework\App\Config\ReinitableConfigInterface;
17+
use Magento\Framework\App\Config\ScopeConfigInterface;
18+
use Magento\TestFramework\ObjectManager;
19+
use Magento\TestFramework\TestCase\GraphQlAbstract;
1520

1621
/**
1722
* Class CategoryProductsCountTest
@@ -25,10 +30,39 @@ class CategoryProductsCountTest extends GraphQlAbstract
2530
*/
2631
private $productRepository;
2732

33+
/**
34+
* @var Config $config
35+
*/
36+
private $resourceConfig;
37+
38+
/**
39+
* @var ScopeConfigInterface
40+
*/
41+
private $scopeConfig;
42+
43+
/**
44+
* @var ReinitableConfigInterface
45+
*/
46+
private $reinitConfig;
47+
48+
/**
49+
* @var CategoryLinkManagementInterface
50+
*/
51+
private $categoryLinkManagement;
52+
53+
/**
54+
* @inheritdoc
55+
*/
2856
protected function setUp()
2957
{
30-
$objectManager = Bootstrap::getObjectManager();
58+
parent::setUp();
59+
60+
$objectManager = ObjectManager::getInstance();
3161
$this->productRepository = $objectManager->create(ProductRepositoryInterface::class);
62+
$this->resourceConfig = $objectManager->get(Config::class);
63+
$this->scopeConfig = $objectManager->get(ScopeConfigInterface::class);
64+
$this->reinitConfig = $objectManager->get(ReinitableConfigInterface::class);
65+
$this->categoryLinkManagement = $objectManager->get(CategoryLinkManagementInterface::class);
3266
}
3367

3468
/**
@@ -82,6 +116,15 @@ public function testCategoryWithInvisibleProduct()
82116
public function testCategoryWithOutOfStockProductManageStockEnabled()
83117
{
84118
$categoryId = 2;
119+
$sku = 'simple-out-of-stock';
120+
$manageStock = $this->scopeConfig->getValue(Configuration::XML_PATH_MANAGE_STOCK);
121+
122+
$this->resourceConfig->saveConfig(Configuration::XML_PATH_MANAGE_STOCK, 1);
123+
$this->reinitConfig->reinit();
124+
125+
// need to resave product to reindex it with new configuration.
126+
$product = $this->productRepository->get($sku);
127+
$this->productRepository->save($product);
85128

86129
$query = <<<QUERY
87130
{
@@ -93,15 +136,27 @@ public function testCategoryWithOutOfStockProductManageStockEnabled()
93136
QUERY;
94137
$response = $this->graphQlQuery($query);
95138

139+
$this->resourceConfig->saveConfig(Configuration::XML_PATH_MANAGE_STOCK, $manageStock);
140+
$this->reinitConfig->reinit();
141+
96142
self::assertEquals(0, $response['category']['product_count']);
97143
}
98144

99145
/**
100-
* @magentoApiDataFixture Magento/Catalog/_files/category_product.php
146+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
101147
*/
102148
public function testCategoryWithOutOfStockProductManageStockDisabled()
103149
{
104-
$categoryId = 333;
150+
$categoryId = 2;
151+
$sku = 'simple-out-of-stock';
152+
$manageStock = $this->scopeConfig->getValue(Configuration::XML_PATH_MANAGE_STOCK);
153+
154+
$this->resourceConfig->saveConfig(Configuration::XML_PATH_MANAGE_STOCK, 0);
155+
$this->reinitConfig->reinit();
156+
157+
// need to resave product to reindex it with new configuration.
158+
$product = $this->productRepository->get($sku);
159+
$this->productRepository->save($product);
105160

106161
$query = <<<QUERY
107162
{
@@ -113,6 +168,9 @@ public function testCategoryWithOutOfStockProductManageStockDisabled()
113168
QUERY;
114169
$response = $this->graphQlQuery($query);
115170

171+
$this->resourceConfig->saveConfig(Configuration::XML_PATH_MANAGE_STOCK, $manageStock);
172+
$this->reinitConfig->reinit();
173+
116174
self::assertEquals(1, $response['category']['product_count']);
117175
}
118176

@@ -140,4 +198,84 @@ public function testCategoryWithDisabledProduct()
140198

141199
self::assertEquals(0, $response['category']['product_count']);
142200
}
201+
202+
/**
203+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
204+
*/
205+
public function testCategoryWithOutOfStockProductShowOutOfStockProduct()
206+
{
207+
$showOutOfStock = $this->scopeConfig->getValue(Configuration::XML_PATH_SHOW_OUT_OF_STOCK);
208+
209+
$this->resourceConfig->saveConfig(Configuration::XML_PATH_SHOW_OUT_OF_STOCK, 1);
210+
$this->reinitConfig->reinit();
211+
212+
$categoryId = 2;
213+
214+
$query = <<<QUERY
215+
{
216+
category(id: {$categoryId}) {
217+
id
218+
product_count
219+
}
220+
}
221+
QUERY;
222+
$response = $this->graphQlQuery($query);
223+
224+
$this->resourceConfig->saveConfig(Configuration::XML_PATH_SHOW_OUT_OF_STOCK, $showOutOfStock);
225+
$this->reinitConfig->reinit();
226+
227+
self::assertEquals(1, $response['category']['product_count']);
228+
}
229+
230+
/**
231+
* @magentoApiDataFixture Magento/CatalogRule/_files/configurable_product.php
232+
*/
233+
public function testCategoryWithConfigurableChildrenOutOfStock()
234+
{
235+
$categoryId = 2;
236+
237+
$this->categoryLinkManagement->assignProductToCategories('configurable', [$categoryId]);
238+
239+
foreach (['simple1', 'simple2'] as $sku) {
240+
$product = $this->productRepository->get($sku);
241+
$product->setStockData(['is_in_stock' => 0]);
242+
$this->productRepository->save($product);
243+
}
244+
245+
$query = <<<QUERY
246+
{
247+
category(id: {$categoryId}) {
248+
id
249+
product_count
250+
}
251+
}
252+
QUERY;
253+
$response = $this->graphQlQuery($query);
254+
255+
self::assertEquals(0, $response['category']['product_count']);
256+
}
257+
258+
/**
259+
* @magentoApiDataFixture Magento/Catalog/_files/category_product.php
260+
*/
261+
public function testCategoryWithProductNotAvailableOnWebsite()
262+
{
263+
$product = $this->productRepository->getById(333);
264+
$product->setWebsiteIds([]);
265+
$this->productRepository->save($product);
266+
267+
$categoryId = 333;
268+
269+
$query = <<<QUERY
270+
{
271+
category(id: {$categoryId}) {
272+
id
273+
product_count
274+
}
275+
}
276+
QUERY;
277+
$response = $this->graphQlQuery($query);
278+
279+
self::assertEquals(0, $response['category']['product_count']);
280+
}
143281
}

0 commit comments

Comments
 (0)