7
7
8
8
namespace Magento \GraphQl \Catalog ;
9
9
10
- use Magento \TestFramework \ TestCase \ GraphQlAbstract ;
10
+ use Magento \Catalog \ Api \ CategoryLinkManagementInterface ;
11
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
- use Magento \TestFramework \Helper \Bootstrap ;
13
- use Magento \Catalog \Model \Product \Visibility ;
14
12
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 ;
15
20
16
21
/**
17
22
* Class CategoryProductsCountTest
@@ -25,10 +30,39 @@ class CategoryProductsCountTest extends GraphQlAbstract
25
30
*/
26
31
private $ productRepository ;
27
32
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
+ */
28
56
protected function setUp ()
29
57
{
30
- $ objectManager = Bootstrap::getObjectManager ();
58
+ parent ::setUp ();
59
+
60
+ $ objectManager = ObjectManager::getInstance ();
31
61
$ 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);
32
66
}
33
67
34
68
/**
@@ -82,6 +116,15 @@ public function testCategoryWithInvisibleProduct()
82
116
public function testCategoryWithOutOfStockProductManageStockEnabled ()
83
117
{
84
118
$ 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 );
85
128
86
129
$ query = <<<QUERY
87
130
{
@@ -93,15 +136,27 @@ public function testCategoryWithOutOfStockProductManageStockEnabled()
93
136
QUERY ;
94
137
$ response = $ this ->graphQlQuery ($ query );
95
138
139
+ $ this ->resourceConfig ->saveConfig (Configuration::XML_PATH_MANAGE_STOCK , $ manageStock );
140
+ $ this ->reinitConfig ->reinit ();
141
+
96
142
self ::assertEquals (0 , $ response ['category ' ]['product_count ' ]);
97
143
}
98
144
99
145
/**
100
- * @magentoApiDataFixture Magento/Catalog/_files/category_product .php
146
+ * @magentoApiDataFixture Magento/Catalog/_files/product_simple_out_of_stock .php
101
147
*/
102
148
public function testCategoryWithOutOfStockProductManageStockDisabled ()
103
149
{
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 );
105
160
106
161
$ query = <<<QUERY
107
162
{
@@ -113,6 +168,9 @@ public function testCategoryWithOutOfStockProductManageStockDisabled()
113
168
QUERY ;
114
169
$ response = $ this ->graphQlQuery ($ query );
115
170
171
+ $ this ->resourceConfig ->saveConfig (Configuration::XML_PATH_MANAGE_STOCK , $ manageStock );
172
+ $ this ->reinitConfig ->reinit ();
173
+
116
174
self ::assertEquals (1 , $ response ['category ' ]['product_count ' ]);
117
175
}
118
176
@@ -140,4 +198,84 @@ public function testCategoryWithDisabledProduct()
140
198
141
199
self ::assertEquals (0 , $ response ['category ' ]['product_count ' ]);
142
200
}
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
+ }
143
281
}
0 commit comments