Skip to content

Commit 8fd5c78

Browse files
Vahalenaorobei
authored andcommitted
magento/graphql-ce#808: [Test coverage] Add disabled variation of Configurable Product to cart
1 parent 5fcac84 commit 8fd5c78

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/ConfigurableProduct/AddConfigurableProductToCartTest.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function testAddVariationFromAnotherConfigurableProductWithDifferentSuper
199199
/**
200200
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_sku.php
201201
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
202-
* @expectedException \Exception
202+
* @expectedException Exception
203203
* @expectedExceptionMessage The requested qty is not available
204204
*/
205205
public function testAddProductIfQuantityIsNotAvailable()
@@ -224,7 +224,7 @@ public function testAddProductIfQuantityIsNotAvailable()
224224
/**
225225
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_sku.php
226226
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
227-
* @expectedException \Exception
227+
* @expectedException Exception
228228
* @expectedExceptionMessage Could not find a product with SKU "configurable_no_exist"
229229
*/
230230
public function testAddNonExistentConfigurableProductParentToCart()
@@ -263,7 +263,7 @@ public function testAddNonExistentConfigurableProductVariationToCart()
263263
2000
264264
);
265265

266-
$this->expectException(\Exception::class);
266+
$this->expectException(Exception::class);
267267
$this->expectExceptionMessage(
268268
'Could not add the product with SKU configurable to the shopping cart: The product that was requested ' .
269269
'doesn\'t exist. Verify the product and try again.'
@@ -272,6 +272,60 @@ public function testAddNonExistentConfigurableProductVariationToCart()
272272
$this->graphQlMutation($query);
273273
}
274274

275+
/**
276+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_sku.php
277+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_disable_first_child.php
278+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
279+
*/
280+
public function testAddDisabledVariationToCart()
281+
{
282+
$searchResponse = $this->graphQlQuery($this->getFetchProductQuery('configurable'));
283+
$product = current($searchResponse['products']['items']);
284+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
285+
$parentSku = $product['sku'];
286+
$sku = 'simple_10';
287+
$query = $this->getQuery(
288+
$maskedQuoteId,
289+
$parentSku,
290+
$sku,
291+
1
292+
);
293+
294+
$this->expectException(Exception::class);
295+
$this->expectExceptionMessage(
296+
'Could not add the product with SKU configurable to the shopping cart: This product is out of stock.'
297+
);
298+
299+
$this->graphQlMutation($query);
300+
}
301+
302+
/**
303+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_sku.php
304+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_zero_qty_first_child.php
305+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
306+
*/
307+
public function testOutOfStockVariationToCart()
308+
{
309+
$searchResponse = $this->graphQlQuery($this->getFetchProductQuery('configurable'));
310+
$product = current($searchResponse['products']['items']);
311+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
312+
$parentSku = $product['sku'];
313+
$sku = 'simple_10';
314+
$query = $this->getQuery(
315+
$maskedQuoteId,
316+
$parentSku,
317+
$sku,
318+
1
319+
);
320+
321+
$this->expectException(Exception::class);
322+
$this->expectExceptionMessage(
323+
'Could not add the product with SKU configurable to the shopping cart: This product is out of stock.'
324+
);
325+
326+
$this->graphQlMutation($query);
327+
}
328+
275329
/**
276330
* @param string $maskedQuoteId
277331
* @param string $parentSku
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Catalog\Model\Product;
11+
use Magento\Catalog\Model\Product\Action;
12+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
13+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
16+
/** @var ProductRepositoryInterface $productRepository */
17+
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
18+
19+
try {
20+
/** @var Product $configurableProduct */
21+
$configurableProduct = $productRepository->get('configurable');
22+
/** @var Configurable $productTypeInstance */
23+
$productTypeInstance = $configurableProduct->getTypeInstance();
24+
/** @var Product $child */
25+
foreach ($productTypeInstance->getUsedProducts($configurableProduct) as $child) {
26+
27+
$productAction = Bootstrap::getObjectManager()->get(Action::class);
28+
$productAction->updateAttributes(
29+
[$child->getId()],
30+
[ProductAttributeInterface::CODE_STATUS => Status::STATUS_DISABLED],
31+
$child->getStoreId()
32+
);
33+
break;
34+
}
35+
} catch (Exception $e) {
36+
// Nothing to remove
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Catalog\Model\Product as ProductModel;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
/** @var ProductRepositoryInterface $productRepository */
13+
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
14+
15+
try {
16+
$configurableProduct = $productRepository->get('configurable');
17+
$productTypeInstance = $configurableProduct->getTypeInstance();
18+
19+
/** @var ProductModel $child */
20+
foreach ($productTypeInstance->getUsedProducts($configurableProduct) as $child) {
21+
$childProduct = $productRepository->getById($child->getId());
22+
$childProduct->setStockData(['use_config_manage_stock' => 1, 'qty' => 0, 'is_qty_decimal' => 0, 'is_in_stock' => 0]);
23+
$productRepository->save($childProduct);
24+
break;
25+
}
26+
} catch (Exception $e) {
27+
// Nothing to remove
28+
}

0 commit comments

Comments
 (0)