Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

namespace Magento\GraphQl\Quote;

use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

class AddSimpleProductToCartTest extends GraphQlAbstract
{
Expand All @@ -30,6 +31,11 @@ class AddSimpleProductToCartTest extends GraphQlAbstract
*/
private $quoteIdToMaskedId;

/**
* @var ProductCustomOptionRepositoryInterface
*/
private $productCustomOptionsRepository;

/**
* @inheritdoc
*/
Expand All @@ -39,6 +45,73 @@ protected function setUp()
$this->quoteResource = $objectManager->get(QuoteResource::class);
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
$this->productCustomOptionsRepository = $objectManager->get(ProductCustomOptionRepositoryInterface::class);
}

/**
* Test adding a simple product to the shopping cart with all supported
* customizable options assigned
*
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
public function testAddSimpleProductWithOptions()
{
$sku = 'simple';
$qty = 1;

$customOptionsValues = $this->getCustomOptionsValuesForQuery($sku);

/* Generate customizable options fragment for GraphQl request */
$queryCustomizableOptions = preg_replace('/"([^"]+)"\s*:\s*/', '$1:', json_encode($customOptionsValues));

$maskedQuoteId = $this->getMaskedQuoteId();

$query = <<<QUERY
mutation {
addSimpleProductsToCart(
input: {
cart_id: "{$maskedQuoteId}",
cartItems: [
{
data: {
qty: $qty
sku: "$sku"
},
customizable_options: $queryCustomizableOptions
}
]
}
) {
cart {
items {
... on SimpleCartItem {
customizable_options {
label
values {
value
}
}
}
}
}
}
}
QUERY;

$response = $this->graphQlQuery($query);

self::assertArrayHasKey('items', $response['addSimpleProductsToCart']['cart']);
self::assertCount(1, $response['addSimpleProductsToCart']['cart']);

$customizableOptionsOutput = $response['addSimpleProductsToCart']['cart']['items'][0]['customizable_options'];
$assignedOptionsCount = count($customOptionsValues);
for ($counter = 0; $counter < $assignedOptionsCount; $counter++) {
self::assertEquals(
$customOptionsValues[$counter]['value'],
$customizableOptionsOutput[$counter]['values'][0]['value']
);
}
}

/**
Expand Down Expand Up @@ -78,7 +151,7 @@ public function testAddSimpleProductToCartWithNegativeQty()
/**
* @return string
*/
public function getMaskedQuoteId() : string
private function getMaskedQuoteId() : string
{
$quote = $this->quoteFactory->create();
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
Expand Down Expand Up @@ -160,4 +233,86 @@ public function testAddProductWithWrongCartHash()

$this->graphQlQuery($query);
}

/**
* Test adding a simple product with empty values for required options
*
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_options.php
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
*/
public function testAddSimpleProductWithNoRequiredOptionsSet()
{
$sku = 'simple';
$qty = 1;

$maskedQuoteId = $this->getMaskedQuoteId();

$query = <<<QUERY
mutation {
addSimpleProductsToCart(
input: {
cart_id: "{$maskedQuoteId}",
cartItems: [
{
data: {
qty: $qty
sku: "$sku"
}
}
]
}
) {
cart {
items {
... on SimpleCartItem {
customizable_options {
label
values {
value
}
}
}
}
}
}
}
QUERY;

self::expectExceptionMessage(
'The product\'s required option(s) weren\'t entered. Make sure the options are entered and try again.'
);

$this->graphQlQuery($query);
}

/**
* Generate an array with test values for customizable options
* based on the option type
*
* @param string $sku
* @return array
*/
private function getCustomOptionsValuesForQuery(string $sku): array
{
$customOptions = $this->productCustomOptionsRepository->getList($sku);
$customOptionsValues = [];

foreach ($customOptions as $customOption) {
$optionType = $customOption->getType();
if ($optionType == 'field' || $optionType == 'area') {
$customOptionsValues[] = [
'id' => (int) $customOption->getOptionId(),
'value' => 'test'
];
} elseif ($optionType == 'drop_down') {
$optionSelectValues = $customOption->getValues();
$customOptionsValues[] = [
'id' => (int) $customOption->getOptionId(),
'value' => reset($optionSelectValues)->getOptionTypeId()
];
}
}

return $customOptionsValues;
}
}
Loading