2012-11-09 19:16:16 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Framework\Test\Behaviour;
|
|
|
|
|
|
|
|
use Behat\Behat\Context\ClosuredContextInterface,
|
2012-12-08 12:20:20 +01:00
|
|
|
Behat\Behat\Context\TranslatedContextInterface,
|
|
|
|
Behat\Behat\Context\BehatContext,
|
|
|
|
Behat\Behat\Context\Step,
|
2013-06-07 15:56:51 +02:00
|
|
|
Behat\Behat\Exception\PendingException,
|
|
|
|
Behat\Mink\Exception\ElementHtmlException,
|
|
|
|
Behat\Gherkin\Node\PyStringNode,
|
2012-12-08 12:20:20 +01:00
|
|
|
Behat\Gherkin\Node\TableNode;
|
2012-11-09 19:16:16 +01:00
|
|
|
|
2013-09-15 01:50:10 +02:00
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
|
2012-11-09 19:16:16 +01:00
|
|
|
// PHPUnit
|
|
|
|
require_once 'PHPUnit/Autoload.php';
|
|
|
|
require_once 'PHPUnit/Framework/Assert/Functions.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CmsFormsContext
|
|
|
|
*
|
|
|
|
* Context used to define steps related to forms inside CMS.
|
|
|
|
*/
|
2013-11-15 12:03:43 +01:00
|
|
|
class CmsFormsContext extends BehatContext {
|
2012-12-08 12:20:20 +01:00
|
|
|
protected $context;
|
2012-11-09 19:16:16 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
|
|
|
* Initializes context.
|
|
|
|
* Every scenario gets it's own context object.
|
|
|
|
*
|
|
|
|
* @param array $parameters context parameters (set them up through behat.yml)
|
|
|
|
*/
|
2013-11-15 12:03:43 +01:00
|
|
|
public function __construct(array $parameters) {
|
2012-12-08 12:20:20 +01:00
|
|
|
// Initialize your context here
|
|
|
|
$this->context = $parameters;
|
|
|
|
}
|
2012-11-09 19:16:16 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
|
|
|
* Get Mink session from MinkContext
|
|
|
|
*/
|
2013-11-15 12:03:43 +01:00
|
|
|
public function getSession($name = null) {
|
2012-12-08 12:20:20 +01:00
|
|
|
return $this->getMainContext()->getSession($name);
|
|
|
|
}
|
2012-11-09 19:16:16 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
2013-12-02 18:35:21 +01:00
|
|
|
* @Then /^I should( not? |\s*)see an edit page form$/
|
2012-12-08 12:20:20 +01:00
|
|
|
*/
|
2013-12-02 18:35:21 +01:00
|
|
|
public function stepIShouldSeeAnEditPageForm($negative) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$page = $this->getSession()->getPage();
|
2012-11-09 19:16:16 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
$form = $page->find('css', '#Form_EditForm');
|
2013-12-02 18:35:21 +01:00
|
|
|
if(trim($negative)) {
|
|
|
|
assertNull($form, 'I should not see an edit page form');
|
|
|
|
} else {
|
|
|
|
assertNotNull($form, 'I should see an edit page form');
|
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2012-11-09 19:16:16 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
|
|
|
* @When /^I fill in the "(?P<field>([^"]*))" HTML field with "(?P<value>([^"]*))"$/
|
|
|
|
* @When /^I fill in "(?P<value>([^"]*))" for the "(?P<field>([^"]*))" HTML field$/
|
|
|
|
*/
|
2013-11-15 12:03:43 +01:00
|
|
|
public function stepIFillInTheHtmlFieldWith($field, $value) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$page = $this->getSession()->getPage();
|
|
|
|
$inputField = $page->findField($field);
|
|
|
|
assertNotNull($inputField, sprintf('HTML field "%s" not found', $field));
|
2012-12-10 15:37:00 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->getSession()->evaluateScript(sprintf(
|
|
|
|
"jQuery('#%s').entwine('ss').getEditor().setContent('%s')",
|
|
|
|
$inputField->getAttribute('id'),
|
|
|
|
addcslashes($value, "'")
|
|
|
|
));
|
|
|
|
}
|
2012-11-09 19:16:16 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
|
|
|
* @When /^I append "(?P<value>([^"]*))" to the "(?P<field>([^"]*))" HTML field$/
|
|
|
|
*/
|
2013-11-15 12:03:43 +01:00
|
|
|
public function stepIAppendTotheHtmlField($field, $value) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$page = $this->getSession()->getPage();
|
|
|
|
$inputField = $page->findField($field);
|
|
|
|
assertNotNull($inputField, sprintf('HTML field "%s" not found', $field));
|
2012-12-10 15:37:00 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
$this->getSession()->evaluateScript(sprintf(
|
|
|
|
"jQuery('#%s').entwine('ss').getEditor().insertContent('%s')",
|
|
|
|
$inputField->getAttribute('id'),
|
|
|
|
addcslashes($value, "'")
|
|
|
|
));
|
|
|
|
}
|
2012-12-10 15:37:00 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
2014-01-06 05:22:40 +01:00
|
|
|
* @Then /^the "(?P<locator>([^"]*))" HTML field should(?P<negative> not? |\s*)contain "(?P<html>.*)"$/
|
2012-12-08 12:20:20 +01:00
|
|
|
*/
|
2014-01-06 05:22:40 +01:00
|
|
|
public function theHtmlFieldShouldContain($locator, $negative, $html) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$page = $this->getSession()->getPage();
|
2013-06-07 15:56:51 +02:00
|
|
|
$element = $page->findField($locator);
|
|
|
|
assertNotNull($element, sprintf('HTML field "%s" not found', $locator));
|
|
|
|
|
2013-11-30 13:27:20 +01:00
|
|
|
$actual = $element->getValue();
|
2013-06-07 15:56:51 +02:00
|
|
|
$regex = '/'.preg_quote($html, '/').'/ui';
|
2014-01-06 05:22:40 +01:00
|
|
|
$failed = false;
|
|
|
|
|
|
|
|
if(trim($negative)) {
|
|
|
|
if (preg_match($regex, $actual)) {
|
|
|
|
$failed = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!preg_match($regex, $actual)) {
|
|
|
|
$failed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($failed) {
|
2013-06-07 15:56:51 +02:00
|
|
|
$message = sprintf(
|
2014-01-06 05:22:40 +01:00
|
|
|
'The string "%s" should%sbe found in the HTML of the element matching name "%s". Actual content: "%s"',
|
|
|
|
$html,
|
|
|
|
$negative,
|
2013-09-14 18:28:02 +02:00
|
|
|
$locator,
|
|
|
|
$actual
|
2013-06-07 15:56:51 +02:00
|
|
|
);
|
|
|
|
throw new ElementHtmlException($message, $this->getSession(), $element);
|
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2013-04-09 09:39:06 +02:00
|
|
|
|
2013-09-15 01:50:10 +02:00
|
|
|
/**
|
|
|
|
* 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'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-14 18:32:29 +02:00
|
|
|
/**
|
|
|
|
* Selects the first textual match in the HTML editor. Does not support
|
|
|
|
* selection across DOM node boundaries.
|
|
|
|
*
|
|
|
|
* @When /^I select "(?P<text>([^"]*))" in the "(?P<field>([^"]*))" HTML field$/
|
|
|
|
*/
|
2013-11-15 12:03:43 +01:00
|
|
|
public function stepIHighlightTextInHtmlField($text, $field) {
|
2013-09-14 18:32:29 +02:00
|
|
|
$page = $this->getSession()->getPage();
|
|
|
|
$inputField = $page->findField($field);
|
|
|
|
assertNotNull($inputField, sprintf('HTML field "%s" not found', $field));
|
|
|
|
$inputFieldId = $inputField->getAttribute('id');
|
|
|
|
$text = addcslashes($text, "'");
|
|
|
|
|
|
|
|
$js = <<<JS
|
|
|
|
// TODO <IE9 support
|
|
|
|
// TODO Allow text matches across nodes
|
|
|
|
var editor = jQuery('#$inputFieldId').entwine('ss').getEditor(),
|
|
|
|
doc = editor.getDOM().doc,
|
|
|
|
sel = editor.getInstance().selection,
|
|
|
|
rng = document.createRange(),
|
|
|
|
matched = false;
|
2014-02-13 05:36:46 +01:00
|
|
|
|
2013-09-14 18:32:29 +02:00
|
|
|
jQuery(doc).find('body *').each(function() {
|
2014-02-13 05:36:46 +01:00
|
|
|
if(!matched) {
|
|
|
|
for(var i=0;i<this.childNodes.length;i++) {
|
|
|
|
if(!matched && this.childNodes[i].nodeValue && this.childNodes[i].nodeValue.match('$text')) {
|
|
|
|
rng.setStart(this.childNodes[i], this.childNodes[i].nodeValue.indexOf('$text'));
|
|
|
|
rng.setEnd(this.childNodes[i], this.childNodes[i].nodeValue.indexOf('$text') + '$text'.length);
|
|
|
|
sel.setRng(rng);
|
|
|
|
editor.getInstance().nodeChanged();
|
|
|
|
matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-14 18:32:29 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
JS;
|
2014-02-13 05:36:46 +01:00
|
|
|
|
|
|
|
$this->getSession()->executeScript($js);
|
2013-09-14 18:32:29 +02:00
|
|
|
}
|
|
|
|
|
2013-04-09 09:39:06 +02:00
|
|
|
/**
|
2013-12-02 18:35:21 +01:00
|
|
|
* Example: I should see a "Submit" button
|
|
|
|
* Example: I should not see a "Delete" button
|
|
|
|
*
|
|
|
|
* @Given /^I should( not? |\s*)see a "([^"]*)" button$/
|
2013-04-09 09:39:06 +02:00
|
|
|
*/
|
2013-12-02 18:35:21 +01:00
|
|
|
public function iShouldSeeAButton($negative, $text) {
|
2013-04-09 09:39:06 +02:00
|
|
|
$page = $this->getSession()->getPage();
|
2013-06-07 15:45:15 +02:00
|
|
|
$els = $page->findAll('named', array('link_or_button', "'$text'"));
|
|
|
|
$matchedEl = null;
|
|
|
|
foreach($els as $el) {
|
|
|
|
if($el->isVisible()) $matchedEl = $el;
|
|
|
|
}
|
2013-12-02 18:35:21 +01:00
|
|
|
|
|
|
|
if(trim($negative)) {
|
|
|
|
assertNull($matchedEl, sprintf('%s button found', $text));
|
|
|
|
} else {
|
|
|
|
assertNotNull($matchedEl, sprintf('%s button not found', $text));
|
|
|
|
}
|
2013-04-09 09:39:06 +02:00
|
|
|
}
|
2013-06-07 05:01:57 +02:00
|
|
|
|
|
|
|
/**
|
2013-12-02 18:35:21 +01:00
|
|
|
* @Given /^I should( not? |\s*)see a "([^"]*)" field$/
|
2013-06-07 05:01:57 +02:00
|
|
|
*/
|
2013-12-02 18:35:21 +01:00
|
|
|
public function iShouldSeeAField($negative, $text) {
|
2013-06-07 05:01:57 +02:00
|
|
|
$page = $this->getSession()->getPage();
|
2013-12-02 18:35:21 +01:00
|
|
|
$els = $page->findAll('named', array('field', "'$text'"));
|
2013-06-07 15:45:15 +02:00
|
|
|
$matchedEl = null;
|
|
|
|
foreach($els as $el) {
|
|
|
|
if($el->isVisible()) $matchedEl = $el;
|
|
|
|
}
|
2013-12-02 18:35:21 +01:00
|
|
|
|
|
|
|
if(trim($negative)) {
|
|
|
|
assertNull($matchedEl);
|
|
|
|
} else {
|
|
|
|
assertNotNull($matchedEl);
|
|
|
|
}
|
2013-06-07 05:01:57 +02:00
|
|
|
}
|
|
|
|
|
2012-11-09 19:16:16 +01:00
|
|
|
}
|