Skip to content

Commit

Permalink
ENGCOM-4136: graphQl-255: Test coverage for PR-243 #341
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav authored Feb 5, 2019
2 parents 3ce8aa6 + e0575c4 commit 474d117
Showing 1 changed file with 144 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

namespace Magento\GraphQl\Catalog;

use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Catalog\Api\CategoryLinkManagementInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\Product\Attribute\Source\Status as productStatus;
use Magento\Catalog\Model\Product\Visibility;
use Magento\CatalogInventory\Model\Configuration;
use Magento\Config\Model\ResourceModel\Config;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Class CategoryProductsCountTest
Expand All @@ -25,10 +30,39 @@ class CategoryProductsCountTest extends GraphQlAbstract
*/
private $productRepository;

/**
* @var Config $config
*/
private $resourceConfig;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var ReinitableConfigInterface
*/
private $reinitConfig;

/**
* @var CategoryLinkManagementInterface
*/
private $categoryLinkManagement;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
parent::setUp();

$objectManager = ObjectManager::getInstance();
$this->productRepository = $objectManager->create(ProductRepositoryInterface::class);
$this->resourceConfig = $objectManager->get(Config::class);
$this->scopeConfig = $objectManager->get(ScopeConfigInterface::class);
$this->reinitConfig = $objectManager->get(ReinitableConfigInterface::class);
$this->categoryLinkManagement = $objectManager->get(CategoryLinkManagementInterface::class);
}

/**
Expand Down Expand Up @@ -82,6 +116,15 @@ public function testCategoryWithInvisibleProduct()
public function testCategoryWithOutOfStockProductManageStockEnabled()
{
$categoryId = 2;
$sku = 'simple-out-of-stock';
$manageStock = $this->scopeConfig->getValue(Configuration::XML_PATH_MANAGE_STOCK);

$this->resourceConfig->saveConfig(Configuration::XML_PATH_MANAGE_STOCK, 1);
$this->reinitConfig->reinit();

// need to resave product to reindex it with new configuration.
$product = $this->productRepository->get($sku);
$this->productRepository->save($product);

$query = <<<QUERY
{
Expand All @@ -93,15 +136,27 @@ public function testCategoryWithOutOfStockProductManageStockEnabled()
QUERY;
$response = $this->graphQlQuery($query);

$this->resourceConfig->saveConfig(Configuration::XML_PATH_MANAGE_STOCK, $manageStock);
$this->reinitConfig->reinit();

self::assertEquals(0, $response['category']['product_count']);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/category_product.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
*/
public function testCategoryWithOutOfStockProductManageStockDisabled()
{
$categoryId = 333;
$categoryId = 2;
$sku = 'simple-out-of-stock';
$manageStock = $this->scopeConfig->getValue(Configuration::XML_PATH_MANAGE_STOCK);

$this->resourceConfig->saveConfig(Configuration::XML_PATH_MANAGE_STOCK, 0);
$this->reinitConfig->reinit();

// need to resave product to reindex it with new configuration.
$product = $this->productRepository->get($sku);
$this->productRepository->save($product);

$query = <<<QUERY
{
Expand All @@ -113,6 +168,9 @@ public function testCategoryWithOutOfStockProductManageStockDisabled()
QUERY;
$response = $this->graphQlQuery($query);

$this->resourceConfig->saveConfig(Configuration::XML_PATH_MANAGE_STOCK, $manageStock);
$this->reinitConfig->reinit();

self::assertEquals(1, $response['category']['product_count']);
}

Expand Down Expand Up @@ -140,4 +198,84 @@ public function testCategoryWithDisabledProduct()

self::assertEquals(0, $response['category']['product_count']);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
*/
public function testCategoryWithOutOfStockProductShowOutOfStockProduct()
{
$showOutOfStock = $this->scopeConfig->getValue(Configuration::XML_PATH_SHOW_OUT_OF_STOCK);

$this->resourceConfig->saveConfig(Configuration::XML_PATH_SHOW_OUT_OF_STOCK, 1);
$this->reinitConfig->reinit();

$categoryId = 2;

$query = <<<QUERY
{
category(id: {$categoryId}) {
id
product_count
}
}
QUERY;
$response = $this->graphQlQuery($query);

$this->resourceConfig->saveConfig(Configuration::XML_PATH_SHOW_OUT_OF_STOCK, $showOutOfStock);
$this->reinitConfig->reinit();

self::assertEquals(1, $response['category']['product_count']);
}

/**
* @magentoApiDataFixture Magento/CatalogRule/_files/configurable_product.php
*/
public function testCategoryWithConfigurableChildrenOutOfStock()
{
$categoryId = 2;

$this->categoryLinkManagement->assignProductToCategories('configurable', [$categoryId]);

foreach (['simple1', 'simple2'] as $sku) {
$product = $this->productRepository->get($sku);
$product->setStockData(['is_in_stock' => 0]);
$this->productRepository->save($product);
}

$query = <<<QUERY
{
category(id: {$categoryId}) {
id
product_count
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertEquals(0, $response['category']['product_count']);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/category_product.php
*/
public function testCategoryWithProductNotAvailableOnWebsite()
{
$product = $this->productRepository->getById(333);
$product->setWebsiteIds([]);
$this->productRepository->save($product);

$categoryId = 333;

$query = <<<QUERY
{
category(id: {$categoryId}) {
id
product_count
}
}
QUERY;
$response = $this->graphQlQuery($query);

self::assertEquals(0, $response['category']['product_count']);
}
}

0 comments on commit 474d117

Please sign in to comment.