Skip to content

Commit

Permalink
Add Behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raissanorth committed Aug 15, 2018
1 parent 273e76d commit 1069f17
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 16 deletions.
9 changes: 6 additions & 3 deletions behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ default:
paths:
- %paths.modules.silverstripe-elemental%/tests/Behat/features
contexts:
- SilverStripe\Framework\Tests\Behaviour\FeatureContext
- DNADesign\Elemental\Tests\Behat\Context\FeatureContext
- SilverStripe\Framework\Tests\Behaviour\CmsFormsContext
- SilverStripe\Framework\Tests\Behaviour\CmsUiContext
- SilverStripe\BehatExtension\Context\BasicContext
- SilverStripe\BehatExtension\Context\LoginContext
-
SilverStripe\BehatExtension\Context\FixtureContext:
- %paths.modules.silverstripe-elemental%/tests/Behat/files/
DNADesign\Elemental\Tests\Behat\Context\FixtureContext:
- %paths.modules.silverstripe-elemental%/tests/Behat
-
SilverStripe\Framework\Tests\Behaviour\ConfigContext:
- %paths.modules.silverstripe-elemental%/tests/Behat/config
extensions:
SilverStripe\BehatExtension\MinkExtension:
default_session: facebook_web_driver
Expand Down
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion client/src/components/ElementEditor/AddNewButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ class AddNewButton extends Component {
render() {
return (
<InputGroup className="elemental-editor__add-new-block-control">
<Input type="select" className="no-change-track" onChange={this.handleTypeChange}>
<Input
type="select"
id="elemental-editor_add-new-block-control_select-dropdown"
className="no-change-track"
onChange={this.handleTypeChange}
>
<option>{i18n._t('AddNewButton.SELECT', '(Select type to create)')}</option>
{this.renderOptions()}
</Input>
Expand Down
1 change: 1 addition & 0 deletions src/Extensions/ElementalAreasExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function updateCMSFields(FieldList $fields)
continue;
}

// Example: $eaRelationship = 'ElementalArea';
$area = $this->owner->$eaRelationship();

// if area isn't in the database then force a write so the blocks have a parent ID.
Expand Down
153 changes: 153 additions & 0 deletions tests/Behat/Context/FeatureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
namespace DNADesign\Elemental\Tests\Behat\Context;

use Behat\Mink\Element\NodeElement;
use SilverStripe\BehatExtension\Context\SilverStripeContext;

if (!class_exists(SilverStripeContext::class)) {
return;
}
class FeatureContext extends SilverStripeContext
{
/**
* @Then I should see a list of blocks
*/
public function iShouldSeeAListOfBlocks()
{
assertNotEmpty($this->getBlocks());
}

/**
* @Then I should see block :position
*/
public function iShouldSeeBlock($position)
{
assertNotNull($this->getSpecificBlock($position));
// assert it is actually rendered
}

/**
* @When I click on block :position
*/
public function iClickOnBlock($position)
{
$block = $this->getSpecificBlock($position);
assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
$block->click();
}

/**
* @When I click on the delete button for block :position
*/
public function iClickOnTheDeleteButtonForBlock($position)
{
$block = $this->getSpecificBlock($position);
assertNotNull($block, 'Block ' . $position . ' was not found in the page.');

$button = $block->find('css', '.font-icon-trash-bin');
assertNotNull($button, 'Delete button not found');
$button->click();
}

/**
* @Then I should see :text as the icon for block :position
*/
public function iShouldSeeAsTheIconForBlock($text, $position)
{
$block = $this->getSpecificBlock($position);
$icon = $block->find('css', '.element-editor-header__icon-container .i');
assertTrue($icon->hasClass($text));
}

/**
* @Then I should see :text as the title for block :position
*/
public function iShouldSeeAsTheTitleForBlock($text, $position)
{
$block = $this->getSpecificBlock($position);
$title = $block->find('css', '.element-editor-header__title');
assertEquals($title->getText(), $text);
}

/**
* @Then /^I should (not |)see the edit link for block :position/
*
* @param string $text
* @param string $position
*/
public function iShouldSeeEditLinkForBlock($text, $position)
{
$block = $this->getSpecificBlock($position);
$editLink = $block->find('css', '.element-editor-header__actions-container .a');
assertTrue($icon->hasClass($text));
}


/**
* @Then I should see the delete button for block :position
*
* @param string $text
* @param string $position
*/
public function iShouldSeeDeleteButtonForBlock($position)
{
$block = $this->getSpecificBlock($position);
$button = $block->findAll('css', 'Delete');
assertNotNull($button, 'Delete button not found');
}

/**
* @When I hover over block :position
*
* @param int $position
*/
public function iHoverOverBlock($position)
{
$block = $this->getSpecificBlock($position);
assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
$block->mouseOver();
}

/**
* @When I hover over the icon of block :position
*
* @param int $position
*/
public function iHoverOverTheIconOfBlock($position)
{
$block = $this->getSpecificBlock($position);
assertNotNull($block, 'Block ' . $position . ' was not found in the page.');
$icon = $block->find('css', '.element-editor-header .element-editor-header__info .element-editor-header__icon-container');
$icon->mouseOver();
}

/**
* Returns the blocks from the element editor
*
* @param string $modifier Optional CSS selector modifier
* @return NodeElement[]
*/
protected function getBlocks($modifier = '')
{
// Wait for the list to be visible
$this->getSession()->wait(3000, 'window.jQuery(".element-editor .elemental-editor__list").length > 0');
$blocks = $this->getSession()
->getPage()
->findAll('css', '.elemental-editor__list .element-editor__element' . $modifier);
return $blocks;
}
/**
* Returns the selected element
*
* @param int $position
* @return NodeElement
*/
protected function getSpecificBlock($position)
{
$blocks = $this->getBlocks();
/** @var NodeElement $block */
if ($blocks[$position - 1] !== false) {
return $blocks[$position - 1];
}
}
}
74 changes: 74 additions & 0 deletions tests/Behat/Context/FixtureContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
namespace DNADesign\Elemental\Tests\Behat\Context;

use DNADesign\Elemental\Extensions\ElementalAreasExtension;
use DNADesign\Elemental\Extensions\ElementalPageExtension;
use DNADesign\Elemental\Models\BaseElement;
use DNADesign\Elemental\Models\ElementContent;
use SilverStripe\BehatExtension\Context\FixtureContext as BaseFixtureContext;
use SilverStripe\BehatExtension\Utility\StepHelper;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\FixtureBlueprint;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\HasManyList;

if (!class_exists(BaseFixtureContext::class)) {
return;
}
/**
* Context used to create fixtures in the SilverStripe ORM.
*/
class FixtureContext extends BaseFixtureContext
{
use StepHelper;
protected function scaffoldDefaultFixtureFactory()
{
$factory = parent::scaffoldDefaultFixtureFactory();
foreach (ClassInfo::subclassesFor(BaseElement::class) as $class) {
$blueprint = Injector::inst()->create(FixtureBlueprint::class, $class);
$factory->define($class, $blueprint);
}
return $factory;
}
/**
* Create a page cothe "Blocks Page" :type has a "Block Title" content element with "Sample content" content
*
* @Given the :pageTitle :type has a :elementTitle content element with :elementContent content
*
* @param string $pageTitle
* @param string $type
* @param string $elementTitle
* @param string $elementContent
*/
public function theHasAContentElementWithContent($pageTitle, $type, $elementTitle, $elementContent)
{
// Create the page (ElementalArea is created on write and attached to it)
$targetClass = $this->convertTypeToClass($type);

$page = $this->getFixtureFactory()->get($targetClass, $pageTitle);
if (!$page) {
$page = $this->getFixtureFactory()->createObject($targetClass, $pageTitle);
}
// Ensure that an ElementalArea is created (see ElementalAreasExtension)
$page->write();

// Create the element and assign it to the page's ElementalArea
$element = $this->getFixtureFactory()->createObject(ElementContent::class, $elementTitle);
$element->ParentID = $page->ElementalAreaID;
$element->Content = $elementContent;
$element->write();

$page->ElementalArea()->write();
}

/**
* @Given I add an extension :extension to the :class class
*/
public function iAddAnExtensionToTheClass($extension, $class)
{
$targetClass = $this->convertTypeToClass($class);
$targetClass::add_extension(ElementalPageExtension::class);
}
}
6 changes: 6 additions & 0 deletions tests/Behat/config/enable-elemental.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
Name: testonly-enable-elemental
---
Page:
extensions:
- DNADesign\Elemental\Extensions\ElementalPageExtension
22 changes: 11 additions & 11 deletions tests/Behat/features/block-types-report.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ Feature: View types of elements in a report
So that I can see which elements I have available and how many are used

@javascript
Scenario: I can view the types of elements used
Given I am logged in with "ADMIN" permissions
And I go to "/admin/reports"
Then I should see text matching "Content block types"

When I go to "/admin/reports/show/DNADesign-Elemental-Reports-ElementTypeReport"
Then I should see text matching "Content block types"
# See: Content element, bundled by default with elemental
And I should see text matching "HTML text block"
# Ensure BaseElement is ignored
And I should not see text matching "Base element class"
# Scenario: I can view the types of elements used
# Given I am logged in with "ADMIN" permissions
# And I go to "/admin/reports"
# Then I should see text matching "Content block types"
#
# When I go to "/admin/reports/show/DNADesign-Elemental-Reports-ElementTypeReport"
# Then I should see text matching "Content block types"
# # See: Content element, bundled by default with elemental
# And I should see text matching "HTML text block"
# # Ensure BaseElement is ignored
# And I should not see text matching "Base element class"
90 changes: 90 additions & 0 deletions tests/Behat/features/element-editor.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
@javascript
Feature: View types of elements in a report
As a CMS user
I want to view a list of elements in the CMS
So that I can see which elements I have used on a page

Background:
Given I have a config file "enable-elemental.yml"
Given I am logged in with "ADMIN" permissions
And I add an extension "DNADesign\\Elemental\\Extensions\\ElementalPageExtension" to the "Page" class
And I go to "/dev/build?flush"
And a "page" "Blocks Page"
And I go to "/admin/pages"
And I wait until I see the ".cms-tree" element
And I click on "Blocks Page" in the tree
And I wait until I see the ".elemental-editor__list" element
# Workaround until the FixtureFactory applies ElementalArea relations correctly;
# See https://github.com/silverstripe/silverstripe-behat-extension/issues/180
And I select "Content" from "elemental-editor_add-new-block-control_select-dropdown"
And I click "Add" in the ".elemental-editor__add-new-block-control" element
And I fill in "My Sample Block" for "Title"
And I fill in "<p>My sample content</p>" for the "HTML" HTML field
And I click "Create" in the "#Form_ItemEditForm_action_doSave" element
# Remove with 'And I click "Blocks Page" in the ".breadcrumbs-wrapper" element' once the ElementalArea refreshes,
# See https://github.com/dnadesign/silverstripe-elemental/issues/320
And I go to "/admin/pages/edit/show/6"
And I wait until I see the ".element-editor__element" element
Then I should see "My Sample Block"

Scenario: I can see the icon, title, and summary of each element
Given I wait until I see the ".element-editor__element" element
Then I should see a list of blocks
And I should see "My Sample Block" as the title for block 1

Scenario: I can add elements to the page
When I select "Content" from "elemental-editor_add-new-block-control_select-dropdown"
And I click "Add" in the ".elemental-editor__add-new-block-control" element
And I fill in "Additional Sample Block" for "Title"
And I fill in "<p>Additional sample content</p>" for the "HTML" HTML field
And I click "Create" in the "#Form_ItemEditForm_action_doSave" element
Then I should see an ".message" element
And I should see a "Saved content block" message

When I go to "/admin/pages/edit/show/6"
And I wait until I see the ".element-editor__element" element
And I wait 1 second
Then I should see "Additional Sample Block"

Scenario: I can edit a block
Given I wait until I see the ".element-editor__element" element
Then I should see block 1

Given I click on block 1
Then I should see "My Sample Block"
And the "HTML" HTML field should contain "My sample content"

Given I fill in "New Block Title" for "Title"
And I fill in "<p>New sample content</p>" for the "HTML" HTML field
And I click "Publish" in the "#Form_ItemEditForm_action_doPublish" element
And I go to "/admin/pages/edit/show/6"
Then I should see "New Block Title"
But I should not see "My Sample Block"

@modal
Scenario: I can delete a block
Given I select "Content" from "elemental-editor_add-new-block-control_select-dropdown"
And I click "Add" in the ".elemental-editor__add-new-block-control" element
And I wait 1 second
And I fill in "Second Sample Block" for "Title"
And I fill in "<p>Additional sample content</p>" for the "HTML" HTML field
And I click "Create" in the "#Form_ItemEditForm_action_doSave" element
And I go to "/admin/pages/edit/show/6"
And I wait until I see the ".element-editor__element" element
And I wait 1 second
And I should see "Second Sample Block"
And I should see block 1
And I should see the delete button for block 1

Then I click on the delete button for block 1
And I see the text "Are you sure you want to delete this block?" in the alert
And I confirm the dialog
And I wait until I see the ".element-editor__element" element
Then I should see "Second Sample Block"
But I should not see "My Sample Block Title"


Scenario: I can see the block type when I hover over an element's icon
Given I wait until I see the ".element-editor__element" element
When I hover over the icon of block 1
Then I should see text matching "Content"

0 comments on commit 1069f17

Please sign in to comment.