Behat: "apply formatting" steps

This commit is contained in:
Ingo Schommer 2013-09-15 01:50:10 +02:00
parent 849f2c1cb1
commit a87000170a
2 changed files with 58 additions and 20 deletions

View File

@ -1,33 +1,28 @@
@todo
Feature: Apply rich formatting to content
As a cms author
I want to work with content in the way I'm used to from word processing software
So that I make it more appealing by creating structure and highlights
Background:
Given a "page" "About Us"
Given I am logged in with "ADMIN" permissions
Given "About Us" has the Content
"""<h1>My awesome headline</1>
<p>Some amazing content</p>"""
Given a "page" "About Us" has the "Content" "<h1>My awesome headline</h1><p>Some amazing content</p>"
And I am logged in with "ADMIN" permissions
And I go to "/admin/pages"
Then I click on "About Us" in the tree
And I focus the "Content" field
Scenario: I can control alignment of selected content
Given I highlight the content "My awesome headline"
When I press the "Align Right" button in the HTML editor
Then the content "My awesome headline" should be right aligned
But the content "Some amazing content" should be left aligned
Then I press the "Save draft" button in the HTML editor
Then the content "My awesome headline" should still be right aligned
Given I select "My awesome headline" in the "Content" HTML field
When I press the "Align Right" button
Then "My awesome headline" in the "Content" HTML field should be right aligned
But "Some amazing content" in the "Content" HTML field should be left aligned
Then I press the "Save draft" button
Then "My awesome headline" in the "Content" HTML field should be right aligned
Scenario: I can bold selected content
Given I highlight the content "awesome"
When I press the "Bold" button in the HTML editor
Then the content "awesome" should be bold
But the content "My" should not be bold
When I press the "Save draft" button in the HTML editor
Then the content "awesome" should still be bold
But the content "My" should still not be bold
Given I select "awesome" in the "Content" HTML field
When I press the "Bold (Ctrl+B)" button
Then "awesome" in the "Content" HTML field should be bold
But "My" in the "Content" HTML field should not be bold
When I press the "Save draft" button
Then "awesome" in the "Content" HTML field should be bold
But "My" in the "Content" HTML field should not be bold

View File

@ -11,6 +11,8 @@ use Behat\Behat\Context\ClosuredContextInterface,
Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
use Symfony\Component\DomCrawler\Crawler;
// PHPUnit
require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';
@ -111,6 +113,47 @@ class CmsFormsContext extends BehatContext
}
}
/**
* Checks formatting in the HTML field, by analyzing the HTML node surrounding
* the text for certain properties.
*
* Example: Given "my text" in the "Content" HTML field should be right aligned
* Example: Given "my text" in the "Content" HTML field should not be bold
*
* @todo Use an actual DOM parser for more accurate assertions
*
* @Given /^"(?P<text>([^"]*))" in the "(?P<field>([^"]*))" HTML field should(?P<negate>(?: not)?) be (?P<formatting>(.*))$/
*/
public function stepContentInHtmlFieldShouldHaveFormatting($text, $field, $negate, $formatting) {
$page = $this->getSession()->getPage();
$inputField = $page->findField($field);
assertNotNull($inputField, sprintf('HTML field "%s" not found', $field));
$crawler = new Crawler($inputField->getValue());
$matchedNode = null;
foreach($crawler->filterXPath('//*') as $node) {
if(
$node->firstChild
&& $node->firstChild->nodeType == XML_TEXT_NODE
&& stripos($node->firstChild->nodeValue, $text) !== FALSE
) {
$matchedNode = $node;
}
}
assertNotNull($matchedNode);
$assertFn = $negate ? 'assertNotEquals' : 'assertEquals';
if($formatting == 'bold') {
call_user_func($assertFn, 'strong', $matchedNode->nodeName);
} else if($formatting == 'left aligned') {
if($matchedNode->getAttribute('style')) {
call_user_func($assertFn, 'text-align: left;', $matchedNode->getAttribute('style'));
}
} else if($formatting == 'right aligned') {
call_user_func($assertFn, 'text-align: right;', $matchedNode->getAttribute('style'));
}
}
/**
* Selects the first textual match in the HTML editor. Does not support
* selection across DOM node boundaries.