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
|
|
|
|
|
|
|
// PHPUnit
|
|
|
|
require_once 'PHPUnit/Autoload.php';
|
|
|
|
require_once 'PHPUnit/Framework/Assert/Functions.php';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CmsFormsContext
|
|
|
|
*
|
|
|
|
* Context used to define steps related to forms inside CMS.
|
|
|
|
*/
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
public function __construct(array $parameters)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
public function getSession($name = null)
|
|
|
|
{
|
|
|
|
return $this->getMainContext()->getSession($name);
|
|
|
|
}
|
2012-11-09 19:16:16 +01:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
/**
|
|
|
|
* @Then /^I should see an edit page form$/
|
|
|
|
*/
|
|
|
|
public function stepIShouldSeeAnEditPageForm()
|
|
|
|
{
|
|
|
|
$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');
|
|
|
|
assertNotNull($form, 'I should see an edit page form');
|
|
|
|
}
|
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$/
|
|
|
|
*/
|
|
|
|
public function stepIFillInTheHtmlFieldWith($field, $value)
|
|
|
|
{
|
|
|
|
$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$/
|
|
|
|
*/
|
|
|
|
public function stepIAppendTotheHtmlField($field, $value)
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
2013-06-07 15:56:51 +02:00
|
|
|
* @Then /^the "(?P<locator>([^"]*))" HTML field should contain "(?P<html>([^"]*))"$/
|
2012-12-08 12:20:20 +01:00
|
|
|
*/
|
2013-06-07 15:56:51 +02:00
|
|
|
public function theHtmlFieldShouldContain($locator, $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));
|
|
|
|
|
|
|
|
$actual = $element->getAttribute('value');
|
|
|
|
$regex = '/'.preg_quote($html, '/').'/ui';
|
|
|
|
if (!preg_match($regex, $actual)) {
|
|
|
|
$message = sprintf(
|
|
|
|
'The string "%s" was not found in the HTML of the element matching %s "%s".',
|
|
|
|
$html,
|
|
|
|
'named',
|
|
|
|
$locator
|
|
|
|
);
|
|
|
|
throw new ElementHtmlException($message, $this->getSession(), $element);
|
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|
2013-04-09 09:39:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^I should see a "([^"]*)" button$/
|
|
|
|
*/
|
|
|
|
public function iShouldSeeAButton($text)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
assertNotNull($matchedEl, sprintf('%s button not found', $text));
|
2013-04-09 09:39:06 +02:00
|
|
|
}
|
2013-06-07 05:01:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^I should not see a "([^"]*)" button$/
|
|
|
|
*/
|
|
|
|
public function iShouldNotSeeAButton($text)
|
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
assertNull($matchedEl, sprintf('%s button found', $text));
|
2013-06-07 05:01:57 +02:00
|
|
|
}
|
|
|
|
|
2012-11-09 19:16:16 +01:00
|
|
|
}
|