Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/op 237 tests #10

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
50 changes: 50 additions & 0 deletions features/caching_sulu_request.feature
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions features/render_sulu_page.feature
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 20 additions & 0 deletions features/show_featured_pages_on_homepage.feature
Original file line number Diff line number Diff line change
@@ -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"
68 changes: 68 additions & 0 deletions spec/ApiClient/SuluApiClientSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace spec\BitBag\SyliusSuluPlugin\ApiClient;

use BitBag\SyliusSuluPlugin\ApiClient\SuluApiClient;
use BitBag\SyliusSuluPlugin\ApiClient\SuluApiClientInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Context\ShopperContextInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class SuluApiClientSpec extends ObjectBehavior
{
function let(
HttpClientInterface $client,
ShopperContextInterface $shopperContext,
) {
$suluBaseUri = 'http://example.com';
$cacheDir = '/var/cache/sulu';

$this->beConstructedWith($client, $shopperContext, $suluBaseUri, $cacheDir);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(SuluApiClient::class);
}

public function it_implements_sulu_api_client_interface_interface(): void
{
$this->shouldHaveType(SuluApiClientInterface::class);
}

// function it_fetches_cms_content(
// HttpClientInterface $client,
// ShopperContextInterface $shopperContext,
// ChannelInterface $channel,
// ) {
// $url = '/cms/content';
// $locale = 'en_US';
//
// $shopperContext->getChannel()->willReturn($channel);
// $shopperContext->getLocaleCode()->willReturn($locale);
// $channel->isSuluUseLocalizedUrls()->willReturn(true);
//
// $client->request('GET', 'http://example.com/en_US/cms/content.json', ['headers' => ['Accept' => 'application/json']])->shouldBeCalled();
//
// $this->fetchCmsContent($url);
// }
//
// function it_fetches_cms_content_with_global_channel(
// HttpClientInterface $client,
// ShopperContextInterface $shopperContext,
// ChannelInterface $channel,
// ) {
// $url = '/cms/content';
//
// $shopperContext->getChannel()->willReturn($channel);
// $shopperContext->getLocaleCode()->willReturn(null);
// $channel->isSuluUseLocalizedUrls()->willReturn(false);
//
//
// $client->request('GET', 'http://example.com/cms/content.json', ['headers' => ['Accept' => 'application/json']])->shouldBeCalled();
//
// $this->fetchCmsContent($url);
// }
}
62 changes: 62 additions & 0 deletions spec/Controller/Action/PurgeSuluCacheActionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace spec\BitBag\SyliusSuluPlugin\Controller\Action;

use BitBag\SyliusSuluPlugin\Controller\Action\PurgeSuluCacheAction;
use BitBag\SyliusSuluPlugin\Entity\ChannelInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\ServerBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Contracts\Translation\TranslatorInterface;

final class PurgeSuluCacheActionSpec extends ObjectBehavior
{
function let(
ChannelRepositoryInterface $channelRepository,
RequestStack $requestStack,
TranslatorInterface $translator,
) {
$cacheDir = '/path/to/cache';

$this->beConstructedWith($channelRepository, $requestStack, $translator, $cacheDir);
}

function it_is_initializable()
{
$this->shouldHaveType(PurgeSuluCacheAction::class);
}

function it_returns_response(
ChannelRepositoryInterface $channelRepository,
ChannelInterface $channel,
RequestStack $requestStack,
Request $request,
ServerBag $serverBag,
Session $session,
FlashBagInterface $flashbag,
) {
$channelId = 1;
$localeCode = 'en_US';
$referer = 'http://example.com';

$request->get('id')->willReturn($channelId);
$request->get('locale')->willReturn($localeCode);
$request->server = $serverBag;
$serverBag->get('HTTP_REFERER')->willReturn($referer);
$session->getFlashBag()->willReturn($flashbag);

$channel->getCode()->willReturn('TEST');
$channelRepository->find($channelId)->willReturn($channel);

$requestStack->getSession()->willReturn($session);

$this->__invoke($request)->shouldBeAnInstanceOf(RedirectResponse::class);
}
}
84 changes: 84 additions & 0 deletions spec/Controller/Action/RenderPageActionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace spec\BitBag\SyliusSuluPlugin\Controller\Action;

use BitBag\SyliusSuluPlugin\ApiClient\SuluApiClientInterface;
use BitBag\SyliusSuluPlugin\Controller\Action\RenderPageAction;
use BitBag\SyliusSuluPlugin\Renderer\Page\SuluPageRendererStrategyInterface;
use PhpSpec\ObjectBehavior;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class RenderPageActionSpec extends ObjectBehavior
{
function let(
SuluApiClientInterface $suluApiClient,
SuluPageRendererStrategyInterface $pageRendererStrategy,
Request $request,
) {
$this->beConstructedWith($suluApiClient, $pageRendererStrategy);
}

function it_is_initializable()
{
$this->shouldHaveType(RenderPageAction::class);
}

function it_returns_response_for_valid_url(
SuluApiClientInterface $suluApiClient,
SuluPageRendererStrategyInterface $pageRendererStrategy,
Request $request,
) {
$url = 'example-page';
$pageContent = '<html><body><h1>Example Page</h1></body></html>';

$request->get('slug')->willReturn($url);
$request->get('second_slug')->willReturn(null);

$suluApiClient->fetchCmsContent($url)->willReturn(['content' => $pageContent]);
$pageRendererStrategy->renderPage(['content' => $pageContent])->willReturn($pageContent);

$this->__invoke($request)->shouldBeAnInstanceOf(Response::class);
$this->__invoke($request)->getContent()->shouldReturn($pageContent);
}

function it_returns_response_for_valid_url_with_second_slug(
SuluApiClientInterface $suluApiClient,
SuluPageRendererStrategyInterface $pageRendererStrategy,
Request $request,
) {
$url = 'example-page';
$secondSlug = 'second-slug';
$fullUrl = "{$url}/{$secondSlug}";
$pageContent = '<html><body><h1>Example Page</h1></body></html>';

$request->get('slug')->willReturn($url);
$request->get('second_slug')->willReturn($secondSlug);

$suluApiClient->fetchCmsContent($fullUrl)->willReturn(['content' => $pageContent]);
$pageRendererStrategy->renderPage(['content' => $pageContent])->willReturn($pageContent);

$this->__invoke($request)->shouldBeAnInstanceOf(Response::class);
$this->__invoke($request)->getContent()->shouldReturn($pageContent);
}

function it_returns_404_response_for_unknown_url(
SuluApiClientInterface $suluApiClient,
Request $request,
SuluPageRendererStrategyInterface $pageRendererStrategy,
) {
$url = 'non-existing-page';

$request->get('slug')->willReturn($url);
$request->get('second_slug')->willReturn(null);

$suluApiClient->fetchCmsContent($url)->willReturn([]);
$pageRendererStrategy->renderPage([])->willReturn('');

$response = $this->__invoke($request);
$response->shouldBeAnInstanceOf(Response::class);
$response->getStatusCode()->shouldReturn(404);
}
}
63 changes: 63 additions & 0 deletions spec/Renderer/Block/SuluBlockRendererStrategySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace spec\BitBag\SyliusSuluPlugin\Renderer\Block;

use BitBag\SyliusSuluPlugin\Renderer\Block\SuluBlockRendererStrategy;
use BitBag\SyliusSuluPlugin\Renderer\Block\SuluBlockRenderStrategyInterface;
use PhpSpec\ObjectBehavior;
use Twig\Error\Error;
use Twig\Error\RuntimeError;

class SuluBlockRendererStrategySpec extends ObjectBehavior
{
function let(SuluBlockRenderStrategyInterface $blockRenderer1, SuluBlockRenderStrategyInterface $blockRenderer2)
{
$this->beConstructedWith([$blockRenderer1, $blockRenderer2]);
}

function it_is_initializable()
{
$this->shouldHaveType(SuluBlockRendererStrategy::class);
}

function it_throws_runtime_error_if_no_renderer_is_found(
SuluBlockRenderStrategyInterface $blockRenderer1,
SuluBlockRenderStrategyInterface $blockRenderer2,
) {
$blockData = ['type' => 'non-existing-type'];

$blockRenderer1->support($blockData)->willReturn(false);
$blockRenderer2->support($blockData)->willReturn(false);

$this->shouldThrow(RuntimeError::class)->during('renderBlock', [$blockData]);
}

function it_returns_rendered_block_content(
SuluBlockRenderStrategyInterface $blockRenderer1,
SuluBlockRenderStrategyInterface $blockRenderer2,
) {
$blockData = ['type' => 'existing-type', 'data' => []];
$renderedContent = '<div>Rendered content</div>';

$blockRenderer1->support($blockData)->willReturn(false);
$blockRenderer2->support($blockData)->willReturn(true);
$blockRenderer2->render($blockData)->willReturn($renderedContent);

$this->renderBlock($blockData)->shouldReturn($renderedContent);
}

function it_returns_empty_string_on_render_error(
SuluBlockRenderStrategyInterface $blockRenderer1,
SuluBlockRenderStrategyInterface $blockRenderer2,
) {
$blockData = ['type' => 'existing-type', 'data' => []];

$blockRenderer1->support($blockData)->willReturn(false);
$blockRenderer2->support($blockData)->willReturn(true);
$blockRenderer2->render($blockData)->willThrow(Error::class);

$this->renderBlock($blockData)->shouldReturn('');
}
}
Loading
Loading