Skip to content

Commit d6e97e3

Browse files
committed
MC-18514: API functional test to cover filterable custom attributes in layered navigation
- added test coverage for filtering by url_key
1 parent 8807fe4 commit d6e97e3

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductSearchTest.php

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,205 @@ public function testFilterByCategoryIdAndCustomAttribute()
701701
}
702702
}
703703

704+
/**
705+
* Filter by exact match of product url key
706+
*
707+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
708+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
709+
*/
710+
public function testFilterBySingleProductUrlKey()
711+
{
712+
/** @var ProductRepositoryInterface $productRepository */
713+
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
714+
/** @var Product $product */
715+
$product = $productRepository->get('simple-4');
716+
$urlKey = $product->getUrlKey();
717+
718+
$query = <<<QUERY
719+
{
720+
products(filter:{
721+
url_key:{eq:"{$urlKey}"}
722+
}
723+
pageSize: 3
724+
currentPage: 1
725+
)
726+
{
727+
total_count
728+
items
729+
{
730+
name
731+
sku
732+
url_key
733+
}
734+
page_info{
735+
current_page
736+
page_size
737+
total_pages
738+
}
739+
filters{
740+
name
741+
request_var
742+
filter_items_count
743+
filter_items{
744+
label
745+
items_count
746+
value_string
747+
__typename
748+
}
749+
}
750+
aggregations
751+
{
752+
attribute_code
753+
count
754+
label
755+
options
756+
{
757+
count
758+
label
759+
value
760+
}
761+
}
762+
}
763+
}
764+
QUERY;
765+
$response = $this->graphQlQuery($query);
766+
$this->assertEquals(1, $response['products']['total_count'], 'More than 1 product found');
767+
$this->assertCount(2, $response['products']['aggregations']);
768+
$this->assertResponseFields($response['products']['items'][0],
769+
[
770+
'name' => $product->getName(),
771+
'sku' => $product->getSku(),
772+
'url_key'=> $product->getUrlKey()
773+
]
774+
);
775+
$this->assertEquals('Price', $response['products']['aggregations'][0]['label']);
776+
$this->assertEquals('Category', $response['products']['aggregations'][1]['label']);
777+
//Disable the product
778+
$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
779+
$productRepository->save($product);
780+
$query2 = <<<QUERY
781+
{
782+
products(filter:{
783+
url_key:{eq:"{$urlKey}"}
784+
}
785+
pageSize: 3
786+
currentPage: 1
787+
)
788+
{
789+
total_count
790+
items
791+
{
792+
name
793+
sku
794+
url_key
795+
}
796+
797+
filters{
798+
name
799+
request_var
800+
filter_items_count
801+
}
802+
aggregations
803+
{
804+
attribute_code
805+
count
806+
label
807+
options
808+
{
809+
count
810+
label
811+
value
812+
}
813+
}
814+
}
815+
}
816+
QUERY;
817+
$response = $this->graphQlQuery($query2);
818+
$this->assertEquals(0, $response['products']['total_count'], 'Total count should be zero');
819+
$this->assertEmpty($response['products']['items']);
820+
$this->assertEmpty($response['products']['aggregations']);
821+
}
822+
823+
/**
824+
* Filter by multiple product url keys
825+
*
826+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
827+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
828+
*/
829+
public function testFilterByMultipleProductUrlKeys()
830+
{
831+
/** @var ProductRepositoryInterface $productRepository */
832+
$productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class);
833+
/** @var Product $product */
834+
$product1 = $productRepository->get('simple');
835+
$product2 = $productRepository->get('12345');
836+
$product3 = $productRepository->get('simple-4');
837+
$filteredProducts = [$product1, $product2, $product3];
838+
$urlKey =[];
839+
foreach ($filteredProducts as $product) {
840+
$urlKey[] = $product->getUrlKey();
841+
}
842+
843+
$query = <<<QUERY
844+
{
845+
products(filter:{
846+
url_key:{in:["{$urlKey[0]}", "{$urlKey[1]}", "{$urlKey[2]}"]}
847+
}
848+
pageSize: 3
849+
currentPage: 1
850+
)
851+
{
852+
total_count
853+
items
854+
{
855+
name
856+
sku
857+
url_key
858+
}
859+
page_info{
860+
current_page
861+
page_size
862+
863+
}
864+
filters{
865+
name
866+
request_var
867+
filter_items_count
868+
}
869+
aggregations
870+
{
871+
attribute_code
872+
count
873+
label
874+
options
875+
{
876+
count
877+
label
878+
value
879+
}
880+
}
881+
}
882+
}
883+
QUERY;
884+
$response = $this->graphQlQuery($query);
885+
$this->assertEquals(3, $response['products']['total_count'], 'Total count is incorrect');
886+
$this->assertCount(2, $response['products']['aggregations']);
887+
888+
$productItemsInResponse = array_map(null, $response['products']['items'], $filteredProducts);
889+
//phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
890+
for ($itemIndex = 0; $itemIndex < count($filteredProducts); $itemIndex++) {
891+
$this->assertNotEmpty($productItemsInResponse[$itemIndex]);
892+
//validate that correct products are returned
893+
$this->assertResponseFields(
894+
$productItemsInResponse[$itemIndex][0],
895+
[ 'name' => $filteredProducts[$itemIndex]->getName(),
896+
'sku' => $filteredProducts[$itemIndex]->getSku(),
897+
'url_key'=> $filteredProducts[$itemIndex]->getUrlKey()
898+
]
899+
);
900+
}
901+
}
902+
704903
/**
705904
* Get array with expected data for layered navigation filters
706905
*

0 commit comments

Comments
 (0)