Skip to content

Commit

Permalink
Merge pull request #5797 from magento-tsg-csl3/2.3-develop-pr49
Browse files Browse the repository at this point in the history
[TSG-CSL3] For 2.3 (pr49)
  • Loading branch information
zakdma authored Jun 16, 2020
2 parents a68e299 + 78c211f commit 8159f43
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;

/**
* {@inheritdoc}
* @inheritdoc
*/
class ProductLinksTypeResolver implements TypeResolverInterface
{
Expand All @@ -20,9 +20,9 @@ class ProductLinksTypeResolver implements TypeResolverInterface
private $linkTypes = ['related', 'upsell', 'crosssell'];

/**
* {@inheritdoc}
* @inheritdoc
*/
public function resolveType(array $data) : string
public function resolveType(array $data): string
{
if (isset($data['link_type'])) {
$linkType = $data['link_type'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ class BatchProductLinks implements BatchServiceContractResolverInterface
/**
* @var string[]
*/
private static $linkTypes = ['related', 'upsell', 'crosssell'];
private $linkTypes;

/**
* @param array $linkTypes
*/
public function __construct(array $linkTypes)
{
$this->linkTypes = $linkTypes;
}

/**
* @inheritDoc
Expand All @@ -44,7 +52,7 @@ public function convertToServiceArgument(ResolveRequestInterface $request)
/** @var \Magento\Catalog\Model\Product $product */
$product = $value['model'];

return new ListCriteria((string)$product->getId(), self::$linkTypes, $product);
return new ListCriteria((string)$product->getId(), $this->linkTypes, $product);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/CatalogGraphQl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,14 @@
<preference type="\Magento\CatalogGraphQl\Model\Resolver\Product\Price\Provider" for="\Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderInterface"/>

<preference type="Magento\CatalogGraphQl\Model\Resolver\Products\Query\Search" for="Magento\CatalogGraphQl\Model\Resolver\Products\Query\ProductQueryInterface"/>

<type name="\Magento\CatalogGraphQl\Model\Resolver\Product\BatchProductLinks">
<arguments>
<argument name="linkTypes" xsi:type="array">
<item name="related" xsi:type="string">related</item>
<item name="upsell" xsi:type="string">upsell</item>
<item name="crosssell" xsi:type="string">crosssell</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
<!-- ko if: Array.isArray(option.value) -->
<span data-bind="html: option.value.join('<br>')"></span>
<!-- /ko -->
<!-- ko if: (!Array.isArray(option.value) && option.option_type == 'file') -->
<!-- ko if: (!Array.isArray(option.value) && ['file', 'html'].includes(option.option_type)) -->
<span data-bind="html: option.value"></span>
<!-- /ko -->
<!-- ko if: (!Array.isArray(option.value) && option.option_type != 'file') -->
<span data-bind="text: option.value"></span>
<!-- ko if: (!Array.isArray(option.value) && !['file', 'html'].includes(option.option_type)) -->
<span data-bind="text: option.value"></span>
<!-- /ko -->
</dd>
<!-- /ko -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
namespace Magento\GroupedProductGraphQl\Model;

use Magento\Framework\GraphQl\Query\Resolver\TypeResolverInterface;
use Magento\CatalogGraphQl\Model\ProductLinksTypeResolver;

/**
* {@inheritdoc}
* @inheritdoc
*/
class GroupedProductLinksTypeResolver implements TypeResolverInterface
{
Expand All @@ -20,14 +21,14 @@ class GroupedProductLinksTypeResolver implements TypeResolverInterface
private $linkTypes = ['associated'];

/**
* {@inheritdoc}
* @inheritdoc
*/
public function resolveType(array $data) : string
public function resolveType(array $data): string
{
if (isset($data['link_type'])) {
$linkType = $data['link_type'];
if (in_array($linkType, $this->linkTypes)) {
return 'GroupedProductLinks';
return 'ProductLinks';
}
}
return '';
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/GroupedProductGraphQl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@
</argument>
</arguments>
</type>
<type name="\Magento\CatalogGraphQl\Model\Resolver\Product\BatchProductLinks">
<arguments>
<argument name="linkTypes" xsi:type="array">
<item name="associated" xsi:type="string">associated</item>
</argument>
</arguments>
</type>
</config>
39 changes: 23 additions & 16 deletions app/code/Magento/Quote/Model/ResourceModel/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ public function subtractProductFromQuotes($product)
'items_qty' => new \Zend_Db_Expr(
$connection->quoteIdentifier('q.items_qty') . ' - ' . $connection->quoteIdentifier('qi.qty')
),
'items_count' => new \Zend_Db_Expr($ifSql)
'items_count' => new \Zend_Db_Expr($ifSql),
'updated_at' => 'q.updated_at',
]
)->join(
['qi' => $this->getTable('quote_item')],
Expand Down Expand Up @@ -277,21 +278,27 @@ public function markQuotesRecollect($productIds)
{
$tableQuote = $this->getTable('quote');
$tableItem = $this->getTable('quote_item');
$subSelect = $this->getConnection()->select()->from(
$tableItem,
['entity_id' => 'quote_id']
)->where(
'product_id IN ( ? )',
$productIds
)->group(
'quote_id'
);

$select = $this->getConnection()->select()->join(
['t2' => $subSelect],
't1.entity_id = t2.entity_id',
['trigger_recollect' => new \Zend_Db_Expr('1')]
);
$subSelect = $this->getConnection()
->select()
->from(
$tableItem,
['entity_id' => 'quote_id']
)->where(
'product_id IN ( ? )',
$productIds
)->group(
'quote_id'
);
$select = $this->getConnection()
->select()
->join(
['t2' => $subSelect],
't1.entity_id = t2.entity_id',
[
'trigger_recollect' => new \Zend_Db_Expr('1'),
'updated_at' => 't1.updated_at',
]
);
$updateQuery = $select->crossUpdateFromSelect(['t1' => $tableQuote]);
$this->getConnection()->query($updateQuery);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,46 @@

namespace Magento\GraphQl\GroupedProduct;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Class to test GraphQl response with grouped products
*/
class GroupedProductViewTest extends GraphQlAbstract
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
}

/**
* @magentoApiDataFixture Magento/GroupedProduct/_files/product_grouped.php
*/
public function testAllFieldsGroupedProduct()
{
$productSku = 'grouped-product';
$query
= <<<QUERY
$query = <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}}) {
items {
items {
id
attribute_set_id
created_at
name
sku
type_id
type_id
... on GroupedProduct {
items{
qty
Expand All @@ -39,57 +55,92 @@ public function testAllFieldsGroupedProduct()
sku
name
type_id
url_key
url_key
}
}
product_links{
linked_product_sku
position
link_type
}
}
}
}
}
QUERY;

$response = $this->graphQlQuery($query);
/** @var ProductRepositoryInterface $productRepository */
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
$groupedProduct = $productRepository->get($productSku, false, null, true);
$groupedProduct = $this->productRepository->get($productSku, false, null, true);

$this->assertGroupedProductItems($groupedProduct, $response['products']['items'][0]);
$this->assertNotEmpty(
$response['products']['items'][0]['items'],
"Precondition failed: 'Grouped product items' must not be empty"
);
$this->assertGroupedProductItems($groupedProduct, $response['products']['items'][0]['items']);
$this->assertNotEmpty(
$response['products']['items'][0]['product_links'],
"Precondition failed: 'Linked product items' must not be empty"
);
$this->assertProductLinks($groupedProduct, $response['products']['items'][0]['product_links']);
}

private function assertGroupedProductItems($product, $actualResponse)
/**
* @param ProductInterface $product
* @param array $items
*/
private function assertGroupedProductItems(ProductInterface $product, array $items): void
{
$this->assertNotEmpty(
$actualResponse['items'],
"Precondition failed: 'grouped product items' must not be empty"
);
$this->assertEquals(2, count($actualResponse['items']));
$this->assertEquals(2, count($items));
$groupedProductLinks = $product->getProductLinks();
foreach ($actualResponse['items'] as $itemIndex => $bundleItems) {
$this->assertNotEmpty($bundleItems);
foreach ($items as $itemIndex => $bundleItem) {
$this->assertNotEmpty($bundleItem);
$associatedProductSku = $groupedProductLinks[$itemIndex]->getLinkedProductSku();

$productsRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
/** @var \Magento\Catalog\Model\Product $associatedProduct */
$associatedProduct = $productsRepository->get($associatedProductSku);
$associatedProduct = $this->productRepository->get($associatedProductSku);

$this->assertEquals(
$groupedProductLinks[$itemIndex]->getExtensionAttributes()->getQty(),
$actualResponse['items'][$itemIndex]['qty']
$bundleItem['qty']
);
$this->assertEquals(
$groupedProductLinks[$itemIndex]->getPosition(),
$actualResponse['items'][$itemIndex]['position']
$bundleItem['position']
);
$this->assertResponseFields(
$actualResponse['items'][$itemIndex]['product'],
$bundleItem['product'],
[
'sku' => $associatedProductSku,
'type_id' => $groupedProductLinks[$itemIndex]->getLinkedProductType(),
'url_key'=> $associatedProduct->getUrlKey(),
'name' => $associatedProduct->getName()
'sku' => $associatedProductSku,
'type_id' => $groupedProductLinks[$itemIndex]->getLinkedProductType(),
'url_key'=> $associatedProduct->getUrlKey(),
'name' => $associatedProduct->getName()

]
);
}
}

/**
* @param ProductInterface $product
* @param array $links
* @return void
*/
private function assertProductLinks(ProductInterface $product, array $links): void
{
$this->assertEquals(2, count($links));
$productLinks = $product->getProductLinks();
foreach ($links as $itemIndex => $linkedItem) {
$this->assertNotEmpty($linkedItem);
$this->assertEquals(
$productLinks[$itemIndex]->getPosition(),
$linkedItem['position']
);
$this->assertEquals(
$productLinks[$itemIndex]->getLinkedProductSku(),
$linkedItem['linked_product_sku']
);
$this->assertEquals(
$productLinks[$itemIndex]->getLinkType(),
$linkedItem['link_type']
);
}
}
}
Loading

0 comments on commit 8159f43

Please sign in to comment.