Skip to content

Commit

Permalink
NEW Adding behat tests for various saving actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ScopeyNZ committed Oct 16, 2018
1 parent 7d1b539 commit a927b8e
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 6 deletions.
105 changes: 105 additions & 0 deletions tests/Behat/Context/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
<?php
namespace DNADesign\Elemental\Tests\Behat\Context;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Mink\Element\NodeElement;
use SilverStripe\BehatExtension\Context\BasicContext;
use SilverStripe\BehatExtension\Context\SilverStripeContext;
use SilverStripe\Framework\Tests\Behaviour\CmsFormsContext;

if (!class_exists(SilverStripeContext::class)) {
return;
}

class FeatureContext extends SilverStripeContext
{
/**
* @var CmsFormsContext
*/
protected $cmsContext;

/**
* @var BasicContext
*/
protected $basicContext;


/** @BeforeScenario */
public function gatherContexts(BeforeScenarioScope $scope)
{
$this->cmsContext = $scope->getEnvironment()->getContext(CmsFormsContext::class);
$this->basicContext = $scope->getEnvironment()->getContext(BasicContext::class);
}

/**
* @Then /^I should( not |\s+)see the edit form for block (\d+)$/i
*/
Expand Down Expand Up @@ -187,6 +208,66 @@ public function stepIPressTheButtonInTheAddBlockPopover($text)
$button->click();
}

/**
* @Given /^I press the "([^"]*)" button in the actions? menu for block (\d+)$/
*/
public function stepIPressTheButtonInTheActionMenuForBlock($buttonName, $blockNumber)
{
$block = $this->getSpecificBlock($blockNumber);

// Check if the popover is open for the block
$popover = $block->find('css', '.action-menu__dropdown');
if (!$popover->isVisible()) {
$block->find('css', '.element-editor-header__actions-toggle')->click();
}

$button = $popover->find('xpath', sprintf('/button[contains(text(), \'%s\')]', $buttonName));

assertNotNull($button, sprintf('Could not find button labelled "%s"', $buttonName));

$button->click();
}

/**
* @Given /^I fill in "([^"]*)" for "([^"]*)" for block (\d+)$/
*/
public function stepIFillInForForBlock($value, $name, $blockNumber)
{
$block = $this->getSpecificBlock($blockNumber);
$field = $this->findFieldInBlock($block, $name);
$fieldName = $field->getAttribute('name');

$isTinyMCE = $field->getAttribute('data-editor') === 'tinyMCE';

if ($isTinyMCE) {
$this->cmsContext->stepIFillInTheHtmlFieldWith($fieldName, $value);
} else {
$this->basicContext->iFillinTheRegion($fieldName, $value, 'html');
}
}

/**
* @Given /^the "([^"]*)" field for block (\d+) should (not\s*)?contain "([^"]*)"$/
*/
public function theFieldForBlockShouldContain($field, $blockNumber, $negate, $content)
{
$block = $this->getSpecificBlock($blockNumber);
$field = $this->findFieldInBlock($block, $field);
$isTinyMCE = $field->getAttribute('data-editor') === 'tinyMCE';

if ($isTinyMCE) {
$this->cmsContext->theHtmlFieldShouldContain(
$field->getAttribute('name'),
$negate,
$content
);
} elseif ($negate) {
$this->assertFieldNotContains($field, $content);
} else {
$this->assertFieldContains($field, $content);
}
}

/**
* Returns the blocks from the element editor
*
Expand Down Expand Up @@ -282,4 +363,28 @@ protected function getCaretButton($block)

return $button;
}

/**
* @param $block
* @param $name
* @return mixed
*/
protected function findFieldInBlock($block, $name)
{
$label = $block->findAll('xpath', sprintf('//label[contains(text(), \'%s\')]', $name));
assertNotNull($label, sprintf('Could not find a label for a field with the content "%s"', $name));
assertCount(
1, $label, sprintf(
'Found more than one label containing the phrase "%s".',
$name
)
);

$label = array_shift($label);

$fieldId = $label->getAttribute('for');
$field = $block->find('css', '#' . $fieldId);
return $field;
}
}
50 changes: 44 additions & 6 deletions tests/Behat/features/edit-block-element.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,49 @@ Feature: Edit elements in the CMS
Then I should see "Eve's Block"
But I should not see "Alice's Block"

Scenario: I can get to the edit form of an inline-editable block
Given I go to "/admin/pages/edit/show/6"
When I see a list of blocks
Then I should see block 2
Scenario: I can preview a block and hide the form again
Given I see a list of blocks
Then I should see block 1
# The entire block should be clickable to reveal the form
When I click on block 1
Then I should see the edit form for block 1
And I should see "Title (displayed if checked)"
And the "Content" field should contain "Some content"
And I fill in "<p>New sample content</p>" for the "Content" HTML field
When I click on the caret button for block 1
Then I should not see the edit form for block 1
# Re-opening the closed form should contain the updated content
When I click on the caret button for block 1
Then I should see the edit form for block 1
And the "Content" field should contain "<p>New sample content</p>"

Scenario: I can edit an inline-editable block and save the individual block
Given I see a list of blocks
Given I click on block 2
Then I should see "Bob's Block"
And the "Content" field should contain "Some content II"
Then I fill in "<p>New sample content</p>" for "Content" for block 2
And I fill in "Charlie's Block" for "Title" for block 2
When I press the "Save" button in the actions menu for block 2
And I wait 1 second
Then I should see a "Saved 'Charlie's Block' successfully" notice
When I click on the caret button for block 2
Then I should see "New sample content"

Scenario: I can edit inline-editable blocks and save the page as a whole
Given I see a list of blocks
Given I click on block 1
Then I fill in "<p>New content for block 1</p>" for "Content" for block 1
And I fill in "Alice's Much Improved Block" for "Title" for block 1
Given I click on block 2
Then I fill in "<p>Alternate HTML within element 2</p>" for "Content" for block 2
And I fill in "Bob's Radically Redesigned Revolutionary Element" for "Title" for block 2
When I press the "Save" button
Then I should see a "Saved 'Blocks Page' successfully" notice
And I should see "Alice's Much Improved Block"
And I should see "New content for block 1"
And I should see "Bob's Radically Redesigned Revolutionary Element"
And I should see "Alternate HTML within element 2"
When I wait 1 second
And I click on block 2
Then the "Content" field for block 2 should contain "<p>Alternate HTML within element 2</p>"


0 comments on commit a927b8e

Please sign in to comment.