From d0a17ce30894e47406ea4f391af53c685167c4a5 Mon Sep 17 00:00:00 2001 From: Kamil Grygierzec Date: Tue, 13 Aug 2024 17:07:14 +0200 Subject: [PATCH] Add missing tests for product taxons filtering --- ...ering_listed_products_from_a_taxon.feature | 26 ++++++++ .../Admin/ManagingProductTaxonsContext.php | 65 +++++++++++++++++++ .../Ui/Admin/ManagingProductsContext.php | 31 +++++++++ .../Page/Admin/Product/IndexPerTaxonPage.php | 6 ++ .../Product/IndexPerTaxonPageInterface.php | 2 + 5 files changed, 130 insertions(+) create mode 100644 features/admin/product/managing_products/filtering_listed_products_from_a_taxon.feature diff --git a/features/admin/product/managing_products/filtering_listed_products_from_a_taxon.feature b/features/admin/product/managing_products/filtering_listed_products_from_a_taxon.feature new file mode 100644 index 0000000000..ecdc436d1c --- /dev/null +++ b/features/admin/product/managing_products/filtering_listed_products_from_a_taxon.feature @@ -0,0 +1,26 @@ +@managing_products +Feature: Filtering listed products from a taxon + In order to quickly find products from a specific category + As an Administrator + I want to filter listed products from a taxon + + Background: + Given the store operates on a single channel in "United States" + And the store classifies its products as "Jeans" and "T-Shirts" + And the store has a product "Old t-shirt" belonging to the "T-Shirts" taxon + And the store has a product "Small jeans" belonging to the "Jeans" taxon + And the store has a product "Big jeans" belonging to the "Jeans" taxon + And I am logged in as an administrator + + @api @no-ui + Scenario: Filtering listed products by taxon + When I am browsing products from "T-Shirts" taxon + Then I should see the "T-Shirts" taxon + But I should not see the "Jeans" taxon + + @api @ui + Scenario: Filtering listed products by product + When I am browsing products from "Jeans" taxon + And I filter them by "Small jeans" product + Then I should see the "Small jeans" product + But I should not see the "Big jeans" product diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingProductTaxonsContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingProductTaxonsContext.php index 0aba8e274a..427b2ca915 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingProductTaxonsContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingProductTaxonsContext.php @@ -60,6 +60,17 @@ final class ManagingProductTaxonsContext implements Context $this->sharedStorage->set('response', $this->client->getLastResponse()); } + /** + * @When I filter them by :product product + */ + public function iFilterThemByProduct(ProductInterface $product): void + { + $this->client->addFilter('product.code', $product->getCode()); + $this->client->filter(); + + $this->sharedStorage->set('response', $this->client->getLastResponse()); + } + /** * @When I set the position of :product to :position */ @@ -188,4 +199,58 @@ final class ManagingProductTaxonsContext implements Context 'The type of the "position" attribute must be "int", "string" given.', ); } + + /** + * @Then I should see the :taxon taxon + */ + public function iShouldSeeTheTaxon(TaxonInterface $taxon): void + { + Assert::true( + $this->isTaxonVisible($taxon), + sprintf('Taxon with code %s does not exist, but it should', $taxon->getCode()), + ); + } + + /** + * @Then I should see the :product product + */ + public function iShouldSeeTheProduct(ProductInterface $product): void + { + Assert::true( + $this->isProductVisible($product), + sprintf('Product with code %s does not exist, but it should', $product->getCode()), + ); + } + + /** + * @Then I should not see the :taxon taxon + */ + public function iShouldNotSeeTheTaxon(TaxonInterface $taxon): void + { + Assert::false( + $this->isTaxonVisible($taxon), + sprintf('Taxon with code %s exists, but it should not', $taxon->getCode()), + ); + } + + /** + * @Then I should not see the :product product + */ + public function iShouldNotSeeTheProduct(ProductInterface $product): void + { + Assert::false( + $this->isProductVisible($product), + sprintf('Product with code %s does not exist, but it should not', $product->getCode()), + ); + } + + private function isTaxonVisible(TaxonInterface $taxon): bool + { + return in_array($this->iriConverter->getIriFromResource($taxon), array_column($this->responseChecker->getCollection($this->sharedStorage->get('response')), 'taxon')); + } + + private function isProductVisible(ProductInterface $product): bool + { + return in_array($this->iriConverter->getIriFromResource($product), array_column($this->responseChecker->getCollection($this->sharedStorage->get('response')), 'product')); + } } diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php index 91fac8aebd..6a93ef9b82 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php @@ -294,6 +294,15 @@ final readonly class ManagingProductsContext implements Context $this->indexPage->filter(); } + /** + * @When I filter them by :productName product + */ + public function iFilterThemByProduct(string $productName): void + { + $this->indexPerTaxonPage->filterByName($productName); + $this->indexPerTaxonPage->filter(); + } + /** * @When I filter them by :taxonName main taxon */ @@ -1436,6 +1445,28 @@ final readonly class ManagingProductsContext implements Context Assert::false($this->updateSimpleProductPage->hasGenerateVariantsButton(), 'Generate variants button should not be visible'); } + /** + * @Then I should see the :product product + */ + public function iShouldSeeTheProduct(ProductInterface $product): void + { + Assert::true( + $this->indexPerTaxonPage->isSingleResourceOnPage(['name' => $product->getName()]), + sprintf('Product with code %s does not exist, but it should', $product->getCode()), + ); + } + + /** + * @Then I should not see the :product product + */ + public function iShouldNotSeeTheProduct(ProductInterface $product): void + { + Assert::false( + $this->indexPerTaxonPage->isSingleResourceOnPage(['name' => $product->getName()]), + sprintf('Product with code %s does not exist, but it should', $product->getCode()), + ); + } + private function assertValidationMessage(string $element, string $message): void { /** @var CreatePageInterface|UpdatePageInterface $currentPage */ diff --git a/src/Sylius/Behat/Page/Admin/Product/IndexPerTaxonPage.php b/src/Sylius/Behat/Page/Admin/Product/IndexPerTaxonPage.php index 52fc31fd57..1fbce5f23f 100644 --- a/src/Sylius/Behat/Page/Admin/Product/IndexPerTaxonPage.php +++ b/src/Sylius/Behat/Page/Admin/Product/IndexPerTaxonPage.php @@ -58,10 +58,16 @@ class IndexPerTaxonPage extends CrudIndexPage implements IndexPerTaxonPageInterf $this->getDocument()->waitFor(5, fn () => null === $saveConfigurationButton->find('css', '.loading')); } + public function filterByName(string $name): void + { + $this->getElement('name_filter')->setValue($name); + } + /** @return array */ protected function getDefinedElements(): array { return array_merge(parent::getDefinedElements(), [ + 'name_filter' => '#criteria_search_value', 'save_configuration_button' => '[data-test-save-configuration-button]', ]); } diff --git a/src/Sylius/Behat/Page/Admin/Product/IndexPerTaxonPageInterface.php b/src/Sylius/Behat/Page/Admin/Product/IndexPerTaxonPageInterface.php index e325aa2fb5..8b9802e572 100644 --- a/src/Sylius/Behat/Page/Admin/Product/IndexPerTaxonPageInterface.php +++ b/src/Sylius/Behat/Page/Admin/Product/IndexPerTaxonPageInterface.php @@ -24,4 +24,6 @@ interface IndexPerTaxonPageInterface extends CrudIndexPageInterface public function setPositionOfProduct(string $productName, string $position): void; public function savePositions(): void; + + public function filterByName(string $name): void; }