Skip to content

Commit ed7832a

Browse files
committed
MC-42571: Unexpected results with graphql search query
- commit with test
1 parent 8599e3c commit ed7832a

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/ProductSearch.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider;
99

1010
use Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory;
11+
use Magento\Catalog\Model\Product\Visibility;
1112
use Magento\Catalog\Model\ResourceModel\Product\Collection;
1213
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
1314
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionPostProcessor;
@@ -18,6 +19,7 @@
1819
use Magento\Framework\Api\Search\SearchResultInterface;
1920
use Magento\Framework\Api\SearchCriteriaInterface;
2021
use Magento\Framework\Api\SearchResultsInterface;
22+
use Magento\Framework\App\ObjectManager;
2123
use Magento\GraphQl\Model\Query\ContextInterface;
2224

2325
/**
@@ -55,28 +57,36 @@ class ProductSearch
5557
*/
5658
private $searchCriteriaBuilder;
5759

60+
/**
61+
* @var Visibility
62+
*/
63+
private $catalogProductVisibility;
64+
5865
/**
5966
* @param CollectionFactory $collectionFactory
6067
* @param ProductSearchResultsInterfaceFactory $searchResultsFactory
6168
* @param CollectionProcessorInterface $collectionPreProcessor
6269
* @param CollectionPostProcessor $collectionPostProcessor
6370
* @param SearchResultApplierFactory $searchResultsApplierFactory
6471
* @param ProductCollectionSearchCriteriaBuilder $searchCriteriaBuilder
72+
* @param Visibility $catalogProductVisibility
6573
*/
6674
public function __construct(
6775
CollectionFactory $collectionFactory,
6876
ProductSearchResultsInterfaceFactory $searchResultsFactory,
6977
CollectionProcessorInterface $collectionPreProcessor,
7078
CollectionPostProcessor $collectionPostProcessor,
7179
SearchResultApplierFactory $searchResultsApplierFactory,
72-
ProductCollectionSearchCriteriaBuilder $searchCriteriaBuilder
80+
ProductCollectionSearchCriteriaBuilder $searchCriteriaBuilder,
81+
Visibility $catalogProductVisibility
7382
) {
7483
$this->collectionFactory = $collectionFactory;
7584
$this->searchResultsFactory = $searchResultsFactory;
7685
$this->collectionPreProcessor = $collectionPreProcessor;
7786
$this->collectionPostProcessor = $collectionPostProcessor;
7887
$this->searchResultApplierFactory = $searchResultsApplierFactory;
7988
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
89+
$this->catalogProductVisibility = $catalogProductVisibility;
8090
}
8191

8292
/**
@@ -106,6 +116,7 @@ public function getList(
106116
$this->getSortOrderArray($searchCriteriaForCollection)
107117
)->apply();
108118

119+
$collection->setVisibility($this->catalogProductVisibility->getVisibleInSiteIds());
109120
$this->collectionPreProcessor->process($collection, $searchCriteriaForCollection, $attributes, $context);
110121
$collection->load();
111122
$this->collectionPostProcessor->process($collection, $attributes);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
namespace Magento\GraphQl\CatalogGraphQl;
9+
10+
use Magento\GraphQl\GetCustomerAuthenticationHeader;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\ObjectManager;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
/**
16+
* Test class to verify product search, used for GraphQL resolver
17+
* for configurable product returns only visible products.
18+
*/
19+
class ProductSearchTest extends GraphQlAbstract
20+
{
21+
/**
22+
* @var ObjectManager|null
23+
*/
24+
private $objectManager;
25+
26+
/**
27+
* @var GetCustomerAuthenticationHeader
28+
*/
29+
private $getCustomerAuthenticationHeader;
30+
31+
protected function setUp(): void
32+
{
33+
$this->objectManager = Bootstrap::getObjectManager();
34+
$this->getCustomerAuthenticationHeader = $this->objectManager->get(GetCustomerAuthenticationHeader::class);
35+
}
36+
37+
/**
38+
* Test for checking if graphQL query fpr configurable product returns
39+
* expected visible items
40+
*
41+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_products_with_different_super_attribute.php
42+
*/
43+
public function testCheckIfConfigurableProductVisibilityReturnsExpectedItem(): void
44+
{
45+
$productName = 'Configurable Product';
46+
$productSku = 'configurable';
47+
$query = $this->getProductSearchQuery($productName, $productSku);
48+
49+
$response = $this->graphQlQuery($query);
50+
51+
$this->assertNotEmpty($response['products']);
52+
$this->assertEquals(1, $response['products']['total_count']);
53+
$this->assertNotEmpty($response['products']['items']);
54+
$this->assertEquals($productName, $response['products']['items'][0]['name']);
55+
$this->assertEquals($productSku, $response['products']['items'][0]['sku']);
56+
}
57+
58+
/**
59+
* Get a query which user filter for product sku and search by product name
60+
*
61+
* @param string $productName
62+
* @param string $productSku
63+
* @return string
64+
*/
65+
private function getProductSearchQuery(string $productName, string $productSku): string
66+
{
67+
return <<<QUERY
68+
{
69+
products(filter: {sku: {eq: "{$productSku}"}}, search: "$productName", sort: {}, pageSize: 200, currentPage: 1) {
70+
total_count
71+
page_info {
72+
total_pages
73+
current_page
74+
page_size
75+
}
76+
items {
77+
name
78+
sku
79+
}
80+
}
81+
}
82+
QUERY;
83+
}
84+
}

0 commit comments

Comments
 (0)