Skip to content

Commit

Permalink
Add basic ManyANy behat test
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Rainville committed Aug 10, 2023
1 parent 3b22753 commit 18ddca0
Show file tree
Hide file tree
Showing 5 changed files with 410 additions and 169 deletions.
28 changes: 15 additions & 13 deletions tests/behat/features/manage-link-list.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Feature: Manage a list of items

Background:
Given a "page" "About Us" has the "Content" "<p>My content</p>"
Given a "page" "Contact us" has the "Content" "<p>Contact details</p>"
Given a "page" "Contact us" has the "Content" "<p>Contact details</p><a name='test-anchor'></a>"
And a "image" "assets/file2.jpg"
And the "group" "EDITOR" has permissions "Access to 'Pages' section" and "SITETREE_GRANT_ACCESS" and "SITETREE_REORGANISE"
And I am logged in as a member of "EDITOR" group
Expand All @@ -15,22 +15,24 @@ Feature: Manage a list of items
And I should see an edit page form
And I click the "Link test" CMS tab

Scenario: I can fill an empty AnyField with a link
And I should see an empty "My test link" AnyField
Then I edit the "My test link" AnyField
And I should see an option to add a "Site Tree Link" item to the "My test link" AnyField
And I should see an option to add a "External Link" item to the "My test link" AnyField
And I should see an option to add a "File Link" item to the "My test link" AnyField
And I should see an option to add a "Email Link" item to the "My test link" AnyField
And I should see an option to add a "Phone Link" item to the "My test link" AnyField
Then I add a "Site Tree Link" item to the "My test link" AnyField
And I should see a "Site Tree Link" AnyField modal
@onlyme
Scenario: I can add items to an empty ManyAnyField
And I should see an empty "My test links" ManyAnyField
Then I edit the "My test links" ManyAnyField
And I should see an option to add a "Site Tree Link" item to the "My test links" ManyAnyField
And I should see an option to add a "External Link" item to the "My test links" ManyAnyField
And I should see an option to add a "File Link" item to the "My test links" ManyAnyField
And I should see an option to add a "Email Link" item to the "My test links" ManyAnyField
And I should see an option to add a "Phone Link" item to the "My test links" ManyAnyField
Then I add a "Site Tree Link" item to the "My test links" ManyAnyField
And I should see a "Site Tree Link" ManyAnyField modal
Then I select "Contact us" in the "#Form_ModalsAnyFieldForm_PageID_Holder" tree dropdown
And I fill in "Title" with "Test link site tree link"
And I select "test-anchor" in the "#Form_ModalsAnyFieldForm_Anchor_Holder" anchor dropdown
And I press the "Insert link" button
And I should see a "My test link" AnyField filled with "Test link site tree link" and a description of "Site Tree Link: contact-us"
And I should see a "My test links" ManyAnyField filled with "Test link site tree link" and a description of "Site Tree Link: contact-us" on position 1
Then I press the "Save" button
And I should see a "My test link" AnyField filled with "Test link site tree link" and a description of "Site Tree Link: contact-us"
And I should see a "My test links" ManyAnyField filled with "Test link site tree link" and a description of "Site Tree Link: contact-us" on position 1

Scenario: I can clear a AnyField
Then I edit the "My test link" AnyField
Expand Down
1 change: 1 addition & 0 deletions tests/behat/features/manage-single-link.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Feature: Manage Single item
And I should see an edit page form
And I click the "Link test" CMS tab


Scenario: I can fill an empty AnyField with a link
And I should see an empty "My test link" AnyField
Then I edit the "My test link" AnyField
Expand Down
180 changes: 180 additions & 0 deletions tests/behat/src/AnyFieldContextTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

namespace SilverStripe\AnyField\Tests\Behat\Context;

use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Element\NodeElement;
use PHPUnit\Framework\Assert;
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
use SilverStripe\BehatExtension\Utility\StepHelper;
use SilverStripe\MinkFacebookWebDriver\FacebookWebDriver;
use SilverStripe\Versioned\ChangeSet;

trait AnyFieldContextTrait
{

public function iShouldSeeAAnyField(string $label)
{
$field = $this->getAnyField($label);
Assert::assertNotNull($field, sprintf('HTML field "%s" not found', $label));
return $field;
}

/**
*
* @Then /^I should see an empty "(.+?)" AnyField/
* @param string $not
* @param string $tabLabel
*/
public function AnyFieldShouldBeEmpty(string $label)
{
$field = $this->iShouldSeeAAnyField($label);
$toggle = $field->find('css', '.any-picker-menu__toggle');

Assert::assertSame('Add Link', $toggle->getText(), "AnyField field $label is not empty");
}

/**
*
* @Then /^I should see a "(.+?)" AnyField filled with "(.+?)" and a description of "(.+?)"/
* @param string $not
* @param string $tabLabel
*/
public function AnyFieldShouldBeContain(string $label, string $title, string $description)
{
$field = $this->iShouldSeeAAnyField($label);
$titleNode = $field->find('css', '.any-picker-title__title');
/** @var NodeElement $description */
$descriptionNode = $field->find('css', '.any-picker-title__type');

Assert::assertSame($title, $titleNode->getText(), "$label should contain $title");
Assert::assertSame($description, $descriptionNode->getText(), "$label should contain $description");
}

/**
*
* @Then /^I edit the "(.+?)" AnyField/
* @param string $not
* @param string $tabLabel
*/
public function EditAnyField(string $label)
{
$field = $this->iShouldSeeAAnyField($label);
$toggle = $field->find('css', 'button.any-picker-menu__toggle, button.any-picker-title');

Assert::assertNotNull($toggle);
$toggle->click();
}

/**
*
* @Then /^I should see an option to add a "(.+?)" item to the "(.+?)" AnyField/
* @param string $not
* @param string $tabLabel
*/
public function iShouldSeeAnOptionToAddItem(string $type, string $label)
{
$option = $this->getAnyFieldOption($label, $type);
Assert::assertNotNull($option, "AnyField $type is not there");
}

/**
*
* @Then /^I add a "(.+?)" item to the "(.+?)" AnyField/
* @param string $not
* @param string $tabLabel
*/
public function iAddItemToAnyField(string $type, string $label)
{
$option = $this->getAnyFieldOption($label, $type);
$option->click();
}

/**
* @Then /^I should see a clear button in the "(.+?)" AnyField/
* @param string $title
*/
public function iShouldSeeClearButton(string $title): void
{
$this->getClearButton($title);
}

/**
* @Then /^I clear the "(.+?)" AnyField/
* @param string $title
*/
public function iClearAnyField(string $title): void
{
$this->getClearButton($title)->click();
}

/**
* Locate an HTML editor field
*
* @param string $locator Raw html field identifier as passed from
*/
protected function getAnyField(string $locator): ?NodeElement
{
$locator = str_replace('\\"', '"', $locator ?? '');
$page = $this->getMainContext()->getSession()->getPage();
$input = $page->find('css', 'input[name=\'' . $locator . '\']');
$fieldId = null;

if ($input) {
// First lets try to find the hidden input
$fieldId = $input->getAttribute('id');
} else {
// Then let's try to find the label
$label = $page->findAll('xpath', sprintf('//label[normalize-space()=\'%s\']', $locator));
if (!empty($label)) {
Assert::assertCount(1, $label, "Found more than one element containing the phrase \"$locator\"");
$label = array_shift($label);
$fieldId = $label->getAttribute('for');
}
}

if (empty($fieldId)) {
return null;
}

$element = $page->find('css', '[data-anyfield-id=\'' . $fieldId . '\']');
return $element;
}

protected function getAnyFieldOption(string $locator, string $option): ?NodeElement
{
$field = $this->getAnyField($locator);
Assert::assertNotNull($option, "AnyField field $$locator does not exist");

$buttons = $field->findAll('css', '.dropdown-item');
foreach ($buttons as $button) {
if ($button->getText() === $option) {
return $button;
}
}

return null;
}

protected function getClearButton(string $locator): NodeElement
{
$field = $this->getAnyField($locator);
Assert::assertNotNull($field, "AnyField $$locator does not exist");

$button = $field->find('css', '.any-picker-title__clear');
Assert::assertNotNull($button, "Could not find clear button in $locator AnyField");

return $button;
}

/**
* @Then I should see a modal titled :title
* @param string $title
*/
protected function getModal(): ?NodeElement
{
$page = $this->getMainContext()->getSession()->getPage();
return $page->find('css', '[role=dialog]');
}

}
Loading

0 comments on commit 18ddca0

Please sign in to comment.