From 489019f3d5254a45d7e81ccc7e8e444b5446dae8 Mon Sep 17 00:00:00 2001 From: Rafal Janicki Date: Mon, 30 Jan 2023 17:10:32 +0000 Subject: [PATCH] LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module (#82) * LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module * LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module * LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module * LYNX-63: Refactored tests for ProductAttributeFilterInput of products query in CatalogGraphQl module * LYNX-63: Fix static test in ProductSearchTest.php * LYNX-63: Fix for Product Search test with search suggestions * LYNX-63: Fixture for Indexer --- .../Magento/Indexer/Test/Fixture/Indexer.php | 44 +++++ .../CatalogGraphQl/ProductSearchTest.php | 187 ++++++++++++------ 2 files changed, 169 insertions(+), 62 deletions(-) create mode 100644 app/code/Magento/Indexer/Test/Fixture/Indexer.php diff --git a/app/code/Magento/Indexer/Test/Fixture/Indexer.php b/app/code/Magento/Indexer/Test/Fixture/Indexer.php new file mode 100644 index 0000000000000..7b0ea1197c42f --- /dev/null +++ b/app/code/Magento/Indexer/Test/Fixture/Indexer.php @@ -0,0 +1,44 @@ +indexerCollection = $indexerCollection; + } + + /** + * {@inheritdoc} + * @param array $data Parameters + */ + public function apply(array $data = []): ?DataObject + { + $this->indexerCollection->load(); + /** @var IndexerModel $indexer */ + foreach ($this->indexerCollection->getItems() as $indexer) { + $indexer->reindexAll(); + } + return null; + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogGraphQl/ProductSearchTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogGraphQl/ProductSearchTest.php index b66ce63f06e15..e8d63d8cf64c7 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogGraphQl/ProductSearchTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogGraphQl/ProductSearchTest.php @@ -7,10 +7,12 @@ namespace Magento\GraphQl\CatalogGraphQl; -use Magento\GraphQl\GetCustomerAuthenticationHeader; use Magento\TestFramework\Helper\Bootstrap; -use Magento\TestFramework\ObjectManager; use Magento\TestFramework\TestCase\GraphQlAbstract; +use Magento\TestFramework\Fixture\DataFixtureStorageManager; +use Magento\TestFramework\Fixture\DataFixture; +use Magento\Catalog\Test\Fixture\Product; +use Magento\Indexer\Test\Fixture\Indexer; /** * Test class to verify product search, used for GraphQL resolver @@ -24,35 +26,11 @@ class ProductSearchTest extends GraphQlAbstract private $objectManager; /** - * @var GetCustomerAuthenticationHeader + * Test setup */ - private $getCustomerAuthenticationHeader; - protected function setUp(): void { $this->objectManager = Bootstrap::getObjectManager(); - $this->getCustomerAuthenticationHeader = $this->objectManager->get(GetCustomerAuthenticationHeader::class); - } - - /** - * Test for checking if graphQL query fpr configurable product returns - * expected visible items - * - * @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_products_with_different_super_attribute.php - */ - public function testCheckIfConfigurableProductVisibilityReturnsExpectedItem(): void - { - $productName = 'Configurable Product'; - $productSku = 'configurable'; - $query = $this->getProductSearchQuery($productName, $productSku); - - $response = $this->graphQlQuery($query); - - $this->assertNotEmpty($response['products']); - $this->assertEquals(1, $response['products']['total_count']); - $this->assertNotEmpty($response['products']['items']); - $this->assertEquals($productName, $response['products']['items'][0]['name']); - $this->assertEquals($productSku, $response['products']['items'][0]['sku']); } /** @@ -65,33 +43,68 @@ public function testCheckIfConfigurableProductVisibilityReturnsExpectedItem(): v private function getProductSearchQuery(string $productName, string $productSku): string { return <<getSearchQueryWithSuggestions(); - $response = $this->graphQlQuery($query); - $this->assertNotEmpty($response['products']); - $this->assertEmpty($response['products']['items']); - $this->assertNotEmpty($response['products']['suggestions']); + return <<getSku()}", + "{$products[1]->getSku()}", + "{$products[2]->getSku()}" + ] + }}, + search: "$product", + sort: {}, + pageSize: 200, + currentPage: 1) + { + total_count + page_info { + total_pages + current_page + page_size + } + items { + name + sku + } + } + } + QUERY; } /** @@ -99,22 +112,72 @@ public function testSearchSuggestions() : void * * @return string */ - private function getSearchQueryWithSuggestions() : string + private function getSearchQueryWithSuggestions(): string { return <<get('product'); + $response = $this->graphQlQuery($this->getProductSearchQuery($product->getName(), $product->getSku())); + + $this->assertNotEmpty($response['products']); + $this->assertEquals(1, $response['products']['total_count']); + $this->assertNotEmpty($response['products']['items']); + $this->assertEquals($product->getName(), $response['products']['items'][0]['name']); + $this->assertEquals($product->getSku(), $response['products']['items'][0]['sku']); } - } -} -QUERY; + + #[ + DataFixture(Product::class, as: 'product1'), + DataFixture(Product::class, as: 'product2'), + DataFixture(Product::class, as: 'product3') + ] + public function testSearchProductsWithMultipleSkuInFilterQuery(): void + { + /** @var \Magento\Catalog\Model\Product $product */ + $response = $this->graphQlQuery( + $this->getProductSearchQueryWithMultipleSkusFilter([ + DataFixtureStorageManager::getStorage()->get('product1'), + DataFixtureStorageManager::getStorage()->get('product2'), + DataFixtureStorageManager::getStorage()->get('product3') + ], "simple") + ); + + $this->assertNotEmpty($response['products']); + $this->assertEquals(3, $response['products']['total_count']); + $this->assertNotEmpty($response['products']['items']); + } + + #[ + DataFixture(Product::class, as: 'product1'), + DataFixture(Product::class, as: 'product2'), + DataFixture(Indexer::class, as: 'indexer') + ] + public function testSearchSuggestions(): void + { + $response = $this->graphQlQuery($this->getSearchQueryWithSuggestions()); + $this->assertNotEmpty($response['products']); + $this->assertEmpty($response['products']['items']); + $this->assertNotEmpty($response['products']['suggestions']); } }