Skip to content

Commit

Permalink
OP-326: Behat single media
Browse files Browse the repository at this point in the history
  • Loading branch information
jkindly committed Jul 8, 2024
1 parent f74e4e1 commit 3079770
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 28 deletions.
14 changes: 14 additions & 0 deletions features/admin/adding_block.feature
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,24 @@ Feature: Adding blocks
When I go to the create block page
And I fill the code with "intro"
And I fill the name with "Intro"
And I click on Add button in Content elements section
And I select "Textarea" content element
And I add a textarea content element with "Welcome to our store" content
And I add it
Then I should be notified that the block has been created

@ui @javascript
Scenario: Adding block with single media content element
Given there is an existing media with "image_1" code and name "Image 1"
When I go to the create block page
And I fill the code with "intro"
And I fill the name with "Intro"
And I click on Add button in Content elements section
And I select "Single media" content element
And I add a single media content element with name "Image 1"
And I add it
Then I should be notified that the block has been created

@ui
Scenario: Trying to add block with existing code
Given there is an existing block with "homepage_image" code
Expand Down
4 changes: 4 additions & 0 deletions features/admin/adding_page.feature
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Feature: Adding new page
And I fill the code with "my_page"
And I fill the slug with "my_page"
And I fill the name with "My page"
And I click on Add button in Content elements section
And I select "Textarea" content element
And I add a textarea content element with "Welcome to our store" content
And I add it
Then I should be notified that the page has been created
Expand All @@ -84,6 +86,8 @@ Feature: Adding new page
And I fill the code with "my_page"
And I fill the slug with "my_page"
And I fill the name with "My page"
And I click on Add button in Content elements section
And I select "Single media" content element
And I add a single media content element with name "Image 1"
And I add it
Then I should be notified that the page has been created
40 changes: 32 additions & 8 deletions tests/Behat/Context/Ui/Admin/BlockContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ public function iFillTheNameIfItIsEmpty(string $name): void
$this->resolveCurrentPage()->fillNameIfItIsEmpty($name);
}

/**
* @When I click on Add button in Content elements section
*/
public function iClickOnAddButtonInContentElementsSection(): void
{
$this->resolveCurrentPage()->clickOnAddContentElementButton();
}

/**
* @When I select :option content element
*/
public function iSelectContentElement(string $option): void
{
$this->resolveCurrentPage()->selectContentElement($option);
}

/**
* @When I add a textarea content element with :content content
*/
public function iAddATextareaContentElementWithContent(string $content): void
{
$this->resolveCurrentPage()->addTextareaContentElementWithContent($content);
}

/**
* @When I change textarea content element value to :value
*/
Expand All @@ -139,6 +163,14 @@ public function iChangeTextareaContentElementValueTo(string $value): void
$this->resolveCurrentPage()->changeTextareaContentElementValue($value);
}

/**
* @When I add a single media content element with name :name
*/
public function iAddASingleMediaContentElementWithName(string $name): void
{
$this->resolveCurrentPage()->addSingleMediaContentElementWithName($name);
}

/**
* @Then I should see :content in the textarea content element
*/
Expand Down Expand Up @@ -179,14 +211,6 @@ public function iAddAndCollectionsToIt(string ...$collectionsNames): void
$this->resolveCurrentPage()->associateCollections($collectionsNames);
}

/**
* @When I add a textarea content element with :content content
*/
public function iAddATextareaContentElementWithContent(string $content): void
{
$this->resolveCurrentPage()->addTextareaContentElementWithContent($content);
}

/**
* @When I add it
* @When I try to add it
Expand Down
16 changes: 16 additions & 0 deletions tests/Behat/Context/Ui/Admin/PageContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ public function iAddAndCollectionsToIt(string ...$collectionsNames): void
$this->resolveCurrentPage()->associateCollections($collectionsNames);
}

/**
* @When I click on Add button in Content elements section
*/
public function iClickOnAddButtonInContentElementsSection(): void
{
$this->resolveCurrentPage()->clickOnAddContentElementButton();
}

/**
* @When I select :option content element
*/
public function iSelectContentElement(string $option): void
{
$this->resolveCurrentPage()->selectContentElement($option);
}

/**
* @When I add a textarea content element with :content content
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/Behat/Helpers/ContentElementHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace Tests\BitBag\SyliusCmsPlugin\Behat\Helpers;

final class ContentElementHelper
{
public static function getDefinedElementThatShouldAppearAfterSelectContentElement(string $contentElement): string
{
return match ($contentElement) {
'Textarea' => 'content_elements_textarea',
'Single media' => 'content_elements_single_media_dropdown',
default => throw new \InvalidArgumentException(sprintf('Content element with name "%s" does not exist.', $contentElement)),
};
}
}
55 changes: 52 additions & 3 deletions tests/Behat/Page/Admin/Block/CreatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use DMore\ChromeDriver\ChromeDriver;
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;
use Tests\BitBag\SyliusCmsPlugin\Behat\Behaviour\ContainsErrorTrait;
use Tests\BitBag\SyliusCmsPlugin\Behat\Helpers\ContentElementHelper;
use Webmozart\Assert\Assert;

class CreatePage extends BaseCreatePage implements CreatePageInterface
Expand Down Expand Up @@ -82,28 +83,76 @@ public function associateCollections(array $collectionsNames): void
/**
* @throws ElementNotFoundException
*/
public function addTextareaContentElementWithContent(string $content): void
public function clickOnAddContentElementButton(): void
{
Assert::isInstanceOf($this->getDriver(), ChromeDriver::class);

$addButton = $this->getElement('content_elements_add_button');
$addButton->click();

$addButton->waitFor(3, function (): bool {
return $this->hasElement('content_elements_textarea');
$addButton->waitFor(2, function (): bool {
return $this->hasElement('content_elements_select_type');
});
}

/**
* @throws ElementNotFoundException
*/
public function selectContentElement(string $contentElement): void
{
Assert::isInstanceOf($this->getDriver(), ChromeDriver::class);

$select = $this->getElement('content_elements_select_type');
$select->selectOption($contentElement);
$select->waitFor(3, function () use ($contentElement): bool {
return $this->hasElement(
ContentElementHelper::getDefinedElementThatShouldAppearAfterSelectContentElement($contentElement)
);
});
}

/**
* @throws ElementNotFoundException
*/
public function addTextareaContentElementWithContent(string $content): void
{
Assert::isInstanceOf($this->getDriver(), ChromeDriver::class);

$textarea = $this->getElement('content_elements_textarea');
$textarea->setValue($content);
}

/**
* @throws ElementNotFoundException
*/
public function addSingleMediaContentElementWithName(string $name): void
{
$dropdown = $this->getElement('content_elements_single_media_dropdown');
$dropdown->click();

$dropdown->waitFor(10, function () use ($name): bool {
return $this->hasElement('content_elements_single_media_dropdown_item', [
'%item%' => $name,
]);
});

$item = $this->getElement('content_elements_single_media_dropdown_item', [
'%item%' => $name,
]);

$item->click();
}

protected function getDefinedElements(): array
{
return array_merge(parent::getDefinedElements(), [
'association_dropdown_collection' => '.field > label:contains("Collections") ~ .sylius-autocomplete',
'association_dropdown_collection_item' => '.field > label:contains("Collections") ~ .sylius-autocomplete > div.menu > div.item:contains("%item%")',
'content_elements_add_button' => '#bitbag_sylius_cms_plugin_block_contentElements a[data-form-collection="add"]',
'content_elements_select_type' => '.field > label:contains("Type") ~ select',
'content_elements_textarea' => '.field > label:contains("Textarea") ~ textarea',
'content_elements_single_media_dropdown' => '.field > label:contains("Single media") ~ .bitbag-media-autocomplete',
'content_elements_single_media_dropdown_item' => '.field > label:contains("Single media") ~ .bitbag-media-autocomplete > div.menu > div.item:contains("%item%")',
]);
}
}
6 changes: 6 additions & 0 deletions tests/Behat/Page/Admin/Block/CreatePageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public function fillContent(string $content): void;

public function associateCollections(array $collectionsNames): void;

public function clickOnAddContentElementButton(): void;

public function selectContentElement(string $contentElement): void;

public function addTextareaContentElementWithContent(string $content): void;

public function addSingleMediaContentElementWithName(string $name): void;

public function disable(): void;
}
43 changes: 26 additions & 17 deletions tests/Behat/Page/Admin/Page/CreatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;
use Sylius\Behat\Service\SlugGenerationHelper;
use Tests\BitBag\SyliusCmsPlugin\Behat\Behaviour\ContainsErrorTrait;
use Tests\BitBag\SyliusCmsPlugin\Behat\Helpers\ContentElementHelper;
use Tests\BitBag\SyliusCmsPlugin\Behat\Service\FormHelper;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -91,42 +92,50 @@ public function associateCollections(array $collectionsNames): void
/**
* @throws ElementNotFoundException
*/
public function addTextareaContentElementWithContent(string $content): void
public function clickOnAddContentElementButton(): void
{
Assert::isInstanceOf($this->getDriver(), ChromeDriver::class);

$addButton = $this->getElement('content_elements_add_button');
$addButton->click();

$addButton->waitFor(3, function (): bool {
return $this->hasElement('content_elements_textarea');
$addButton->waitFor(2, function (): bool {
return $this->hasElement('content_elements_select_type');
});

$textarea = $this->getElement('content_elements_textarea');
$textarea->setValue($content);
}

/**
* @throws ElementNotFoundException
*/
public function addSingleMediaContentElementWithName(string $name): void
public function selectContentElement(string $contentElement): void
{
Assert::isInstanceOf($this->getDriver(), ChromeDriver::class);

$addButton = $this->getElement('content_elements_add_button');
$addButton->click();

$addButton->waitFor(3, function (): bool {
return $this->hasElement('content_elements_select_type');
$select = $this->getElement('content_elements_select_type');
$select->selectOption($contentElement);
$select->waitFor(3, function () use ($contentElement): bool {
return $this->hasElement(
ContentElementHelper::getDefinedElementThatShouldAppearAfterSelectContentElement($contentElement)
);
});
}

$select = $this->getElement('content_elements_select_type');
$select->selectOption('Single media');
/**
* @throws ElementNotFoundException
*/
public function addTextareaContentElementWithContent(string $content): void
{
Assert::isInstanceOf($this->getDriver(), ChromeDriver::class);

$select->waitFor(3, function (): bool {
return $this->hasElement('content_elements_single_media_dropdown');
});
$textarea = $this->getElement('content_elements_textarea');
$textarea->setValue($content);
}

/**
* @throws ElementNotFoundException
*/
public function addSingleMediaContentElementWithName(string $name): void
{
$dropdown = $this->getElement('content_elements_single_media_dropdown');
$dropdown->click();

Expand Down
4 changes: 4 additions & 0 deletions tests/Behat/Page/Admin/Page/CreatePageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function fillContent(string $content): void;

public function associateCollections(array $collectionsNames): void;

public function clickOnAddContentElementButton(): void;

public function selectContentElement(string $contentElement): void;

public function addTextareaContentElementWithContent(string $content): void;

public function addSingleMediaContentElementWithName(string $name): void;
Expand Down

0 comments on commit 3079770

Please sign in to comment.