From cbe90e093acfb8d9b76522df3072baad34b3d789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Wojda=C5=82owicz?= Date: Fri, 23 Feb 2024 08:31:18 +0100 Subject: [PATCH] OP-237 - write behat tests, mock api responses, create examples of renderers --- composer.json | 2 +- features/caching_sulu_request.feature | 50 +++++ features/render_sulu_page.feature | 27 +++ .../show_featured_pages_on_homepage.feature | 20 ++ src/Entity/SuluChannelConfigurationTrait.php | 10 + src/Twig/Extension/SuluPageExtension.php | 1 + src/Twig/Runtime/SuluRuntime.php | 19 +- src/Twig/Runtime/SuluRuntimeInterface.php | 2 + .../Grid/Action/invalidateSuluCache.html.twig | 2 +- tests/Application/.env | 4 + tests/Application/config/bundles.php | 2 + .../config/doctrine/Channel.Channel.orm.xml | 12 ++ .../Application/config/packages/_sylius.yaml | 32 +++ .../Application/config/packages/doctrine.yaml | 11 ++ .../config/packages/framework.yaml | 5 + .../config/routes/sylius_admin.yaml | 12 ++ .../config/routes/sylius_shop.yaml | 16 ++ tests/Application/config/services.yaml | 2 + tests/Application/config/services_test.yaml | 30 +++ .../src/Entity/Channel/Channel.php | 13 ++ .../src/Renderer/Block/ImageRenderer.php | 47 +++++ .../src/Renderer/Block/QuoteRenderer.php | 47 +++++ .../src/Renderer/Block/TextRenderer.php | 48 +++++ .../src/Renderer/Page/BlogPageRenderer.php | 46 +++++ .../Renderer/Page/FeaturedPagesRenderer.php | 49 +++++ .../shop/block/image.html.twig | 1 + .../shop/block/quote.html.twig | 1 + .../shop/block/text.html.twig | 1 + .../shop/page/blog.html.twig | 48 +++++ .../shop/page/featuredPages.html.twig | 16 ++ .../Homepage/_featuredPages.html.twig | 7 + .../blog_page_with_blocks_and_links.json | 183 ++++++++++++++++++ .../blog_page_with_properties.json | 62 ++++++ .../Behat/ApiResponseMock/featured_pages.json | 141 ++++++++++++++ tests/Behat/Context/Setup/SuluPageContext.php | 119 ++++++++++++ .../Context/Ui/SuluAdminPanelContext.php | 108 +++++++++++ tests/Behat/Context/Ui/SuluPageContext.php | 89 +++++++++ tests/Behat/Page/ChannelIndexPage.php | 21 ++ tests/Behat/Page/HomePage.php | 18 ++ tests/Behat/Page/SuluPage.php | 37 ++++ tests/Behat/Resources/services.xml | 63 ++++++ tests/Behat/Resources/suites.yml | 4 + .../Resources/suites/ui/cache_sulu_page.yml | 22 +++ .../Resources/suites/ui/render_sulu_page.yml | 19 ++ tests/Behat/Resources/suites/ui/sulu_page.yml | 19 ++ tests/Behat/Services/SuluApiClient.php | 100 ++++++++++ 46 files changed, 1585 insertions(+), 3 deletions(-) create mode 100644 features/caching_sulu_request.feature create mode 100644 features/render_sulu_page.feature create mode 100644 features/show_featured_pages_on_homepage.feature create mode 100644 tests/Application/config/doctrine/Channel.Channel.orm.xml create mode 100644 tests/Application/src/Entity/Channel/Channel.php create mode 100644 tests/Application/src/Renderer/Block/ImageRenderer.php create mode 100644 tests/Application/src/Renderer/Block/QuoteRenderer.php create mode 100644 tests/Application/src/Renderer/Block/TextRenderer.php create mode 100644 tests/Application/src/Renderer/Page/BlogPageRenderer.php create mode 100644 tests/Application/src/Renderer/Page/FeaturedPagesRenderer.php create mode 100644 tests/Application/templates/bundles/BitBagSyliusSuluPlugin/shop/block/image.html.twig create mode 100644 tests/Application/templates/bundles/BitBagSyliusSuluPlugin/shop/block/quote.html.twig create mode 100644 tests/Application/templates/bundles/BitBagSyliusSuluPlugin/shop/block/text.html.twig create mode 100644 tests/Application/templates/bundles/BitBagSyliusSuluPlugin/shop/page/blog.html.twig create mode 100644 tests/Application/templates/bundles/BitBagSyliusSuluPlugin/shop/page/featuredPages.html.twig create mode 100644 tests/Application/templates/bundles/SyliusShopBundle/Homepage/_featuredPages.html.twig create mode 100644 tests/Behat/ApiResponseMock/blog_page_with_blocks_and_links.json create mode 100644 tests/Behat/ApiResponseMock/blog_page_with_properties.json create mode 100644 tests/Behat/ApiResponseMock/featured_pages.json create mode 100644 tests/Behat/Context/Setup/SuluPageContext.php create mode 100644 tests/Behat/Context/Ui/SuluAdminPanelContext.php create mode 100644 tests/Behat/Context/Ui/SuluPageContext.php create mode 100644 tests/Behat/Page/ChannelIndexPage.php create mode 100644 tests/Behat/Page/HomePage.php create mode 100644 tests/Behat/Page/SuluPage.php create mode 100644 tests/Behat/Resources/suites.yml create mode 100644 tests/Behat/Resources/suites/ui/cache_sulu_page.yml create mode 100644 tests/Behat/Resources/suites/ui/render_sulu_page.yml create mode 100644 tests/Behat/Resources/suites/ui/sulu_page.yml create mode 100644 tests/Behat/Services/SuluApiClient.php diff --git a/composer.json b/composer.json index 7c83bb2..111e7a1 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ }, "require-dev": { "behat/behat": "^3.6.1", - "behat/mink-selenium2-driver": "^1.4", + "behat/mink-selenium2-driver": "~1.6.0", "bitbag/coding-standard": "^3.0", "dmore/behat-chrome-extension": "^1.3", "dmore/chrome-mink-driver": "^2.7", diff --git a/features/caching_sulu_request.feature b/features/caching_sulu_request.feature new file mode 100644 index 0000000..4cc118d --- /dev/null +++ b/features/caching_sulu_request.feature @@ -0,0 +1,50 @@ +@sulu_cache +Feature: Caching sulu page request + In order to see again some page + As a Visitor + I want to be able to see the page without sending request to sulu + + Background: + Given the store operates on a single channel in "United States" + And Sulu has defined page "blog_page_with_blocks_and_links" in locale "en_US" + And Cache for sulu not exists + + + @ui + Scenario: See the featured pages on homepage + When I visit this channel's homepage + And "United States" has enabled sulu localized requests + Then Sulu cache should not exists + And I visit a sulu page "blog_page_with_blocks_and_links" in locale en_US + And Sulu cache should exists for "United States" with locale en_US + + @ui + Scenario: Manually clear cache when localized urls are disabled + When I visit a sulu page "blog_page_with_blocks_and_links" in locale en_US + Then Sulu cache should exists + And I am logged in as an administrator + And I browse channels + And I should see button "Purge sulu cache" in "United States" + And I click "Clear Cache" button in "United States" + And I should see success "Successfully purged" flash message + And Sulu cache should exists for "United States" with locale en_US + + @ui + Scenario: Manually clear cache when localized urls are enabled + When "United States" has enabled sulu localized requests + And I visit a sulu page "blog_page_with_blocks_and_links" in locale en_US + Then Sulu cache should exists + And I am logged in as an administrator + And I browse channels + And I should see expanded button "Purge sulu cache" in "United States" + And I click expanded "en_US" button in "United States" + And I should see success "Successfully purged" flash message + And Sulu cache should exists for "United States" with locale en_US + + @ui + Scenario: Manually clear cache when localized urls are disabled and cache not exists + When I am logged in as an administrator + And I browse channels + Then I should see button "Purge sulu cache" in "United States" + And I click "Purge sulu cache" button in "United States" + And I should see error "Dir with sulu cache not found" flash message diff --git a/features/render_sulu_page.feature b/features/render_sulu_page.feature new file mode 100644 index 0000000..b617487 --- /dev/null +++ b/features/render_sulu_page.feature @@ -0,0 +1,27 @@ +@render_sulu_page +Feature: Render sulu page + In order to see a sulu page + As a Visitor + I want to be able to see rendered sulu page + + Background: + Given the store operates on a single channel in "United States" + And Sulu has defined page "blog_page_with_properties" in locale "en_US" + And Sulu has defined page "blog_page_with_blocks_and_links" in locale "en_US" + And Page "blog_page_with_blocks_and_links" has block "quote" + And Page "blog_page_with_blocks_and_links" has block "text" + And Page "blog_page_with_blocks_and_links" has block "image" + + @ui + Scenario: Rendering a sulu page with properties + When I visit a sulu page "blog_page_with_properties" in locale en_US + Then I should see a "title" with value "E-commerce trends" + And I should see a "content" with value "CONTENT" + + @ui + Scenario: Rendering a sulu page with properties and blocks + When I visit a sulu page "blog_page_with_blocks_and_links" in locale en_US + Then I should see a "title" with value "E-commerce trends" + And I should see a block "content" with value "2021 was followed by the time of the 2020 pandemic. During these two years, a lot has changed..." + And I should see a block image with url "https://en.wikipedia.org/wiki/Cat#/media/File:Sheba1.JPG" + And I should see a block "quote" with value "Lorem ipsum dolor sit amet, con" diff --git a/features/show_featured_pages_on_homepage.feature b/features/show_featured_pages_on_homepage.feature new file mode 100644 index 0000000..daa2964 --- /dev/null +++ b/features/show_featured_pages_on_homepage.feature @@ -0,0 +1,20 @@ +@sulu_page +Feature: Show featured pages on homepage + In order to see featured pages on homepage + As a Visitor + I want to be able to see featured pages on homepage + + Background: + Given the store operates on a single channel in "United States" + And Sulu has defined featured pages list featured_pages in locale en_US + And One of the featured page is page "Blog Page 1" + And One of the featured page is page "Blog Page 2" + And One of the featured page is page "Blog Page 3" + + @ui + Scenario: See the featured pages on homepage + When I visit this channel's homepage + Then I should see 3 featured pages. + And I should see featured page "Blog Page 1" + And I should see featured page "Blog Page 2" + And I should see featured page "Blog Page 3" diff --git a/src/Entity/SuluChannelConfigurationTrait.php b/src/Entity/SuluChannelConfigurationTrait.php index 2143228..583e729 100644 --- a/src/Entity/SuluChannelConfigurationTrait.php +++ b/src/Entity/SuluChannelConfigurationTrait.php @@ -4,8 +4,18 @@ namespace BitBag\SyliusSuluPlugin\Entity; +use Doctrine\ORM\Mapping as ORM; + +/** + * @ORM\Entity + * + * @ORM\Table(name="sylius_channel") + */ +#[ORM\Entity] +#[ORM\Table(name: 'sylius_channel')] trait SuluChannelConfigurationTrait { + /** @ORM\Column(type="boolean", nullable=false) */ protected bool $suluUseLocalizedUrls = false; public function isSuluUseLocalizedUrls(): bool diff --git a/src/Twig/Extension/SuluPageExtension.php b/src/Twig/Extension/SuluPageExtension.php index 064137c..6d927ee 100644 --- a/src/Twig/Extension/SuluPageExtension.php +++ b/src/Twig/Extension/SuluPageExtension.php @@ -23,6 +23,7 @@ public function getFunctions(): array new TwigFunction('bitbag_render_sulu_blocks', [$this->suluRuntime, 'renderSuluBlocks']), new TwigFunction('bitbag_render_sulu_blocks_with_type', [$this->suluRuntime, 'renderSuluBlocksWithType']), new TwigFunction('bitbag_render_sulu_block_with_type', [$this->suluRuntime, 'renderSuluBlockWithType']), + new TwigFunction('bitbag_page_has_sulu_block', [$this->suluRuntime, 'hasSuluBlock']), ]; } } diff --git a/src/Twig/Runtime/SuluRuntime.php b/src/Twig/Runtime/SuluRuntime.php index c8e7104..f31e6ea 100644 --- a/src/Twig/Runtime/SuluRuntime.php +++ b/src/Twig/Runtime/SuluRuntime.php @@ -59,10 +59,10 @@ public function renderSuluBlocksWithType(array $blocks, string $type, ?string $d public function renderSuluBlockWithType(array $blocks, string $type): string { $blocks = array_filter($blocks, fn (array $block) => $block['type'] === $type); - if (count($blocks) > 1) { $blocks = $blocks[0]; } + $content = ''; foreach ($blocks as $block) { @@ -71,4 +71,21 @@ public function renderSuluBlockWithType(array $blocks, string $type): string return $content; } + + public function hasSuluBlock(array $page, string $type): bool + { + if (!array_key_exists('blocks', $page)) { + return false; + } + + $blocks = $page['blocks']; + + if (count($blocks) === 0) { + return false; + } + + $blocks = array_filter($blocks, fn (array $block) => $block['type'] === $type); + + return count($blocks) !== 0; + } } diff --git a/src/Twig/Runtime/SuluRuntimeInterface.php b/src/Twig/Runtime/SuluRuntimeInterface.php index 643f1ec..f1c7753 100644 --- a/src/Twig/Runtime/SuluRuntimeInterface.php +++ b/src/Twig/Runtime/SuluRuntimeInterface.php @@ -17,4 +17,6 @@ public function renderSuluBlocks(array $blocks): string; public function renderSuluBlocksWithType(array $blocks, string $type): string; public function renderSuluBlockWithType(array $blocks, string $type): string; + + public function hasSuluBlock(array $page, string $type): bool; } diff --git a/templates/Admin/Grid/Action/invalidateSuluCache.html.twig b/templates/Admin/Grid/Action/invalidateSuluCache.html.twig index 2523db1..24be26c 100644 --- a/templates/Admin/Grid/Action/invalidateSuluCache.html.twig +++ b/templates/Admin/Grid/Action/invalidateSuluCache.html.twig @@ -1,5 +1,5 @@ {% if true == data.isSuluUseLocalizedUrls %} -