Merge branch 'MDL-38025_master' of git://github.com/dmonllao/moodle

This commit is contained in:
Aparup Banerjee
2013-03-05 14:43:46 +08:00
14 changed files with 215 additions and 68 deletions
+2 -10
View File
@@ -44,7 +44,7 @@ class tool_behat {
* @param string $type
* @param string $component
* @param string $filter
* @return string
* @return array System steps or empty array if case there are no steps
*/
public static function stepsdefinitions($type, $component, $filter) {
@@ -70,15 +70,7 @@ class tool_behat {
$options = ' --config="'.behat_config_manager::get_steps_list_config_filepath(). '" '.$filteroption;
list($steps, $code) = behat_command::run($options);
if ($steps) {
$stepshtml = implode('', $steps);
}
if (empty($stepshtml)) {
$stepshtml = get_string('nostepsdefinitions', 'tool_behat');
}
return $stepshtml;
return $steps;
}
}
+26 -2
View File
@@ -37,9 +37,9 @@ require_once($CFG->libdir . '/behat/classes/behat_command.php');
class tool_behat_renderer extends plugin_renderer_base {
/**
* Renders the list of available steps according to the submitted filters
* Renders the list of available steps according to the submitted filters.
*
* @param string $stepsdefinitions HTML from behat with the available steps
* @param mixed $stepsdefinitions Available steps array.
* @param moodleform $form
* @return string HTML code
*/
@@ -82,6 +82,30 @@ class tool_behat_renderer extends plugin_renderer_base {
$html .= ob_get_contents();
ob_end_clean();
if (empty($stepsdefinitions)) {
$stepsdefinitions = get_string('nostepsdefinitions', 'tool_behat');
} else {
$stepsdefinitions = implode('', $stepsdefinitions);
// Replace text selector type arguments with a user-friendly select.
$stepsdefinitions = preg_replace_callback('/(TEXT_SELECTOR_STRING)/',
function ($matches) {
return html_writer::select(behat_command::$allowedtextselectors, uniqid());
},
$stepsdefinitions
);
// Replace selector type arguments with a user-friendly select.
$stepsdefinitions = preg_replace_callback('/(SELECTOR_STRING)/',
function ($matches) {
return html_writer::select(behat_command::$allowedselectors, uniqid());
},
$stepsdefinitions
);
}
// Steps definitions.
$html .= html_writer::tag('div', $stepsdefinitions, array('class' => 'steps-definitions'));
@@ -18,12 +18,12 @@ Feature: Page contents assertions
When I follow "Overview"
And I wait until the page is ready
And I wait "2" seconds
And I hover ".region-content .generaltable td span"
And I hover ".region-content .generaltable td span" "css_element"
Then I should see "I'm the description"
And I should see "Filter groups by"
And I should not see "Filter groupssss by"
And I should see "Group members" in the ".region-content table th.c1" element
And I should not see "Group membersssss" in the ".region-content table th.c1" element
And I should see "Group members" in the ".region-content table th.c1" "css_element"
And I should not see "Group membersssss" in the ".region-content table th.c1" "css_element"
And I follow "Groups"
And the element "#groupeditform #showcreateorphangroupform" should be enabled
And the element "#groupeditform #showeditgroupsettingsform" should be disabled
And the "#groupeditform #showcreateorphangroupform" "css_element" should be enabled
And the "#groupeditform #showeditgroupsettingsform" "css_element" should be disabled
@@ -52,7 +52,6 @@ class testable_behat_config_manager extends behat_config_manager {
/**
* Allow access to protected method
* @see parent::get_config_file_contents()
* @param string $prefix
* @param array $features
* @param array $stepsdefinitions
* @return string
+59 -7
View File
@@ -59,18 +59,18 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
* @param string $path
* @return string
*/
protected function locatePath($path) {
$startUrl = rtrim($this->getMinkParameter('base_url'), '/') . '/';
return 0 !== strpos($path, 'http') ? $startUrl . ltrim($path, '/') : $path;
protected function locate_path($path) {
$starturl = rtrim($this->getMinkParameter('base_url'), '/') . '/';
return 0 !== strpos($path, 'http') ? $starturl . ltrim($path, '/') : $path;
}
/**
* Adapter to Behat\Mink\Element\Element::find() using the spin() method.
*
* @link http://mink.behat.org/#traverse-the-page-selectors
* @param Exception $exception Otherwise we throw expcetion with generic info
* @param string $selector The selector type (css, xpath, named...)
* @param mixed $locator It depends on the $selector, can be the xpath, a name, a css locator...
* @param Exception $exception Otherwise we throw exception with generic info
* @return NodeElement
*/
protected function find($selector, $locator, $exception = false) {
@@ -98,7 +98,7 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
array($selector, $locator),
self::TIMEOUT,
$exception
);
);
}
/**
@@ -119,7 +119,7 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
*
* @link http://mink.behat.org/#named-selectors
* @throws coding_exception
* @param string $method The name of the called method
* @param string $name The name of the called method
* @param mixed $arguments
* @return NodeElement
*/
@@ -193,7 +193,7 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
if ($return = $lambda($this, $args)) {
return $return;
}
} catch(Exception $e) {
} catch (Exception $e) {
// We would use the first closure exception if no exception has been provided.
if (!$exception) {
@@ -216,4 +216,56 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
throw $exception;
}
/**
* Gets a NodeElement based on the locator and selector type received as argument from steps definitions.
*
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $selectortype
* @param string $element
* @return NodeElement
*/
protected function get_selected_node($selectortype, $element) {
// Getting Mink selector and locator.
list($selector, $locator) = $this->transform_selector($selectortype, $element);
// Returns the NodeElement.
return $this->find($selector, $locator);
}
/**
* Transforms from step definition's argument style to Mink format.
*
* Mink has 3 different selectors css, xpath and named, where named
* selectors includes link, button, field... to simplify and group multiple
* steps in one we use the same interface, considering all link, buttons...
* at the same level as css selectors and xpath; this method makes the
* conversion from the arguments received by the steps to the selectors and locators
* required to interact with Mink.
*
* @throws ExpectationException
* @param string $selectortype It can be css, xpath or any of the named selectors.
* @param string $element The locator (or string) we are looking for.
* @return array Contains the selector and the locator expected by Mink.
*/
protected function transform_selector($selectortype, $element) {
// Here we don't know if a $allowedtextselector is used.
if (!isset(behat_command::$allowedselectors[$selectortype])) {
throw new ExpectationException('The "' . $selectortype . '" selector type does not exist', $this->getSession());
}
// CSS and XPath selectors locator is one single argument.
if ($selectortype == 'css_element' || $selectortype == 'xpath_element') {
$selector = str_replace('_element', '', $selectortype);
$locator = $element;
} else {
// Named selectors uses arrays as locators including the type of named selector.
$locator = array($selectortype, $this->getSession()->getSelectorsHandler()->xpathLiteral($element));
$selector = 'named';
}
return array($selector, $locator);
}
}
+28
View File
@@ -42,6 +42,34 @@ class behat_command {
*/
const DOCS_URL = 'http://docs.moodle.org/dev/Acceptance_testing';
/**
* @var Allowed types when using text selectors arguments.
*/
public static $allowedtextselectors = array(
'css_element' => 'css_element',
'xpath_element' => 'xpath_element'
);
/**
* @var Allowed types when using selector arguments.
*/
public static $allowedselectors = array(
'link' => 'link',
'button' => 'button',
'link_or_button' => 'link_or_button',
'select' => 'select',
'checkbox' => 'checkbox',
'radio' => 'radio',
'file' => 'file',
'optgroup' => 'optgroup',
'option' => 'option',
'table' => 'table',
'field' => 'field',
'fieldset' => 'fieldset',
'css_element' => 'css_element',
'xpath_element' => 'xpath_element'
);
/**
* Ensures the behat dir exists in moodledata
* @return string Full path
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
+7 -1
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -41,7 +40,14 @@ use Behat\Mink\Session as Session,
*/
class behat_form_field {
/**
* @var Session Behat session.
*/
protected $session;
/**
* @var NodeElement The field DOM node to interact with.
*/
protected $fieldnode;
/**
+5 -3
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -51,6 +50,9 @@ use Behat\Behat\Exception\PendingException as PendingException;
*/
class behat_data_generators extends behat_base {
/**
* @var testing_data_generator
*/
protected $datagenerator;
/**
@@ -215,7 +217,7 @@ class behat_data_generators extends behat_base {
/**
* Gets the user id from it's username.
* @throws Exception
* @param string $idnumber
* @param string $username
* @return int
*/
protected function get_user_id($username) {
@@ -230,7 +232,7 @@ class behat_data_generators extends behat_base {
/**
* Gets the role id from it's shortname.
* @throws Exception
* @param string $idnumber
* @param string $roleshortname
* @return int
*/
protected function get_role_id($roleshortname) {
+11 -2
View File
@@ -50,6 +50,7 @@ class behat_forms extends behat_base {
*
* @When /^I press "(?P<button_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $button
*/
public function press_button($button) {
@@ -90,6 +91,8 @@ class behat_forms extends behat_base {
*
* @When /^I fill in "(?P<field_string>(?:[^"]|\\")*)" with "(?P<value_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $field
* @param string $value
*/
public function fill_field($field, $value) {
@@ -102,6 +105,8 @@ class behat_forms extends behat_base {
*
* @When /^I select "(?P<option_string>(?:[^"]|\\")*)" from "(?P<select_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $option
* @param string $select
*/
public function select_option($option, $select) {
@@ -117,6 +122,7 @@ class behat_forms extends behat_base {
*
* @When /^I check "(?P<option_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $option
*/
public function check_option($option) {
@@ -129,6 +135,7 @@ class behat_forms extends behat_base {
*
* @When /^I uncheck "(?P<option_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $option
*/
public function uncheck_option($option) {
@@ -142,8 +149,8 @@ class behat_forms extends behat_base {
* @Then /^the "(?P<field_string>(?:[^"]|\\")*)" field should match "(?P<value_string>(?:[^"]|\\")*)" value$/
* @throws ExpectationException
* @throws ElementNotFoundException Thrown by behat_base::find
* @param mixed $locator
* @param mixed $value
* @param string $locator
* @param string $value
*/
public function the_field_should_match_value($locator, $value) {
@@ -166,6 +173,7 @@ class behat_forms extends behat_base {
*
* @Then /^the "(?P<checkbox_string>(?:[^"]|\\")*)" checkbox should be checked$/
* @see Behat\MinkExtension\Context\MinkContext
* @param string $checkbox
*/
public function assert_checkbox_checked($checkbox) {
$this->assertSession()->checkboxChecked($checkbox);
@@ -176,6 +184,7 @@ class behat_forms extends behat_base {
*
* @Then /^the "(?P<checkbox_string>(?:[^"]|\\")*)" checkbox should not be checked$/
* @see Behat\MinkExtension\Context\MinkContext
* @param string $checkbox
*/
public function assert_checkbox_not_checked($checkbox) {
$this->assertSession()->checkboxNotChecked($checkbox);
+69 -31
View File
@@ -50,7 +50,7 @@ class behat_general extends behat_base {
* @Given /^I am on homepage$/
*/
public function i_am_on_homepage() {
$this->getSession()->visit($this->locatePath('/'));
$this->getSession()->visit($this->locate_path('/'));
}
/**
@@ -58,6 +58,7 @@ class behat_general extends behat_base {
*
* @When /^I follow "(?P<link_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $link
*/
public function click_link($link) {
@@ -85,27 +86,39 @@ class behat_general extends behat_base {
}
/**
* Mouse over a CSS element.
* Generic mouse over action. Mouse over a element of the specified type.
*
* @When /^I hover "(?P<element_string>(?:[^"]|\\")*)"$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $element
* @When /^I hover "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
* @param string $element Element we look for
* @param string $selectortype The type of what we look for
*/
public function i_hover($element) {
public function i_hover($element, $selectortype) {
$exception = new ExpectationException(
'The hovered element "' . $element . '" was not found anywhere in the page', $this->getSession()
);
$node = $this->find('css', $element, $exception);
// Gets the node based on the requested selector type and locator.
$node = $this->get_selected_node($selectortype, $element);
$node->mouseOver();
}
/**
* Generic click action. Click on the element of the specified type.
*
* @When /^I click on "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)"$/
* @param string $element Element we look for
* @param string $selectortype The type of what we look for
*/
public function i_click_on($element, $selectortype) {
// Gets the node based on the requested selector type and locator.
$node = $this->get_selected_node($selectortype, $element);
$node->click();
}
/**
* Checks, that page contains specified text.
*
* @see Behat\MinkExtension\Context\MinkContext
* @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
* @param string $text
*/
public function assert_page_contains_text($text) {
$this->assertSession()->pageTextContains($text);
@@ -116,40 +129,64 @@ class behat_general extends behat_base {
*
* @see Behat\MinkExtension\Context\MinkContext
* @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)"$/
* @param string $text
*/
public function assert_page_not_contains_text($text) {
$this->assertSession()->pageTextNotContains($text);
}
/**
* Checks, that element with specified CSS contains specified text.
* Checks, that element with specified CSS selector or XPath contains specified text.
*
* @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" element$/
* @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
* @param string $text
* @param string $element Element we look in.
* @param string $selectortype The type of element where we are looking in.
*/
public function assert_element_contains_text($text, $element) {
$this->assertSession()->elementTextContains('css', $element, $text);
public function assert_element_contains_text($text, $element, $selectortype) {
// Transforming from steps definitions selector/locator format to Mink format.
list($selector, $locator) = $this->transform_selector($selectortype, $element);
if ($selector != 'css' && $selector != 'xpath') {
throw new ExpectationException('The "' . $selectortype . '" selector can not be used to select text nodes', $this->getSession());
}
$this->assertSession()->elementTextContains($selector, $locator, $text);
}
/**
* Checks, that element with specified CSS doesn't contain specified text.
* Checks, that element with specified CSS selector or XPath doesn't contain specified text.
*
* @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" element$/
* @Then /^I should not see "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
* @param string $text
* @param string $element Element we look in.
* @param string $selectortype The type of element where we are looking in.
*/
public function assert_element_not_contains_text($text, $element) {
$this->assertSession()->elementTextNotContains('css', $element, $text);
public function assert_element_not_contains_text($text, $element, $selectortype) {
// Transforming from steps definitions selector/locator format to mink format.
list($selector, $locator) = $this->transform_selector($selectortype, $element);
if ($selector != 'css' && $selector != 'xpath') {
throw new ExpectationException('The "' . $selectortype . '" selector can not be used to select text nodes', $this->getSession());
}
$this->assertSession()->elementTextNotContains($selector, $locator, $text);
}
/**
* Checks, that element with given CSS is disabled.
* Checks, that element of specified type is disabled.
*
* @Then /^the element "(?P<element_string>(?:[^"]|\\")*)" should be disabled$/
* @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be disabled$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $element
* @param string $element Element we look in
* @param string $selectortype The type of element where we are looking in.
*/
public function the_element_should_be_disabled($element) {
public function the_element_should_be_disabled($element, $selectortype) {
$exception = new ExpectationException('There is no "' . $element . '" element', $this->getSession());
$node = $this->find('css', $element, $exception);
// Transforming from steps definitions selector/locator format to Mink format and getting the NodeElement.
$node = $this->get_selected_node($selectortype, $element);
if (!$node->hasAttribute('disabled')) {
throw new ExpectationException('The element "' . $element . '" is not disabled', $this->getSession());
@@ -157,16 +194,17 @@ class behat_general extends behat_base {
}
/**
* Checks, that element with given CSS is enabled.
* Checks, that element of specified type is enabled.
*
* @Then /^the element "(?P<element_string>(?:[^"]|\\")*)" should be enabled$/
* @Then /^the "(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should be enabled$/
* @throws ExpectationException Thrown by behat_base::find
* @param string $element
* @param string $element Element we look on
* @param string $selectortype The type of where we look
*/
public function the_element_should_be_enabled($element) {
public function the_element_should_be_enabled($element, $selectortype) {
$exception = new ExpectationException('There is no "' . $element . '" element', $this->getSession());
$node = $this->find('css', $element, $exception);
// Transforming from steps definitions selector/locator format to mink format and getting the NodeElement.
$node = $this->get_selected_node($selectortype, $element);
if ($node->hasAttribute('disabled')) {
throw new ExpectationException('The element "' . $element . '" is not enabled', $this->getSession());
+2 -3
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -113,8 +112,8 @@ class behat_hooks extends behat_base {
php_sapi_name() != 'cli' ||
!behat_util::is_test_mode_enabled() ||
!behat_util::is_test_site() ||
!isset($CFG->originaldataroot)) {
throw new coding_exception('Behat only can modify the test database and the test dataroot!');
!isset($CFG->originaldataroot)) {
throw new coding_exception('Behat only can modify the test database and the test dataroot!');
}
behat_util::reset_database();
-1
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
+1 -1
View File
@@ -77,7 +77,7 @@ class behat_transformations extends behat_base {
* adding them in a different method for Behat API restrictions.
*
* @Transform /^table:(.*)/
* @param TableNode $table
* @param TableNode $tablenode
* @return TableNode The transformed table
*/
public function tablenode_transformations(TableNode $tablenode) {