Skip to content

Commit

Permalink
MDL-44619 behat: Converting MDLQA-8
Browse files Browse the repository at this point in the history
In a standard forum, students can start discussions
add replies and attach files to their posts.
  • Loading branch information
David Monllao committed Apr 9, 2014
1 parent 9bc60e0 commit af4830a
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 29 deletions.
4 changes: 1 addition & 3 deletions backup/util/ui/tests/behat/behat_backup.php
Expand Up @@ -349,9 +349,7 @@ protected function fill_backup_restore_form($options) {
foreach ($datahash as $locator => $value) {

try {
// Using $this->find* to enforce stability over speed.
$fieldnode = $this->find_field($locator);
$field = behat_field_manager::get_form_field($fieldnode, $this->getSession());
$field = behat_field_manager::get_form_field_from_label($locator, $this);
$field->set_value($value);

} catch (ElementNotFoundException $e) {
Expand Down
30 changes: 29 additions & 1 deletion lib/behat/behat_field_manager.php
Expand Up @@ -26,7 +26,9 @@
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.

use Behat\Mink\Session as Session,
Behat\Mink\Element\NodeElement as NodeElement;
Behat\Mink\Element\NodeElement as NodeElement,
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
Behat\MinkExtension\Context\RawMinkContext as RawMinkContext;

/**
* Helper to interact with form fields.
Expand All @@ -38,6 +40,30 @@
*/
class behat_field_manager {

/**
* Gets an instance of the form field from it's label
*
* @param string $label
* @param RawMinkContext $context
* @return behat_form_field
*/
public static function get_form_field_from_label($label, RawMinkContext $context) {

// There are moodle form elements that are not directly related with
// a basic HTML form field, we should also take care of them.
try {
// The DOM node.
$fieldnode = $context->find_field($label);
} catch (ElementNotFoundException $e) {

// Looking for labels that points to filemanagers.
$fieldnode = $context->find_filemanager($label);
}

// The behat field manager.
return self::get_form_field($fieldnode, $context->getSession());
}

/**
* Gets an instance of the form field.
*
Expand Down Expand Up @@ -217,6 +243,7 @@ protected static function get_field_node_type(NodeElement $fieldnode, Session $s
* @todo MDL-XXXXX This will be deleted in Moodle 2.8
* @see behat_field_manager::get_form_field()
* @param NodeElement $fieldnode
* @param string $locator
* @param Session $session The behat browser session
* @return behat_form_field
*/
Expand All @@ -237,6 +264,7 @@ public static function get_field(NodeElement $fieldnode, $locator, Session $sess
* @todo MDL-XXXXX This will be deleted in Moodle 2.8
* @see behat_field_manager::get_field_node_type()
* @param NodeElement $fieldnode The current node.
* @param string $locator
* @param Session $session The behat browser session
* @return mixed A NodeElement if we continue looking for the element type and String or false when we are done.
*/
Expand Down
6 changes: 3 additions & 3 deletions lib/behat/classes/behat_context_helper.php
Expand Up @@ -18,7 +18,7 @@
* Helper to initialise behat contexts from moodle code.
*
* @package core
* @category testing
* @category test
* @copyright 2014 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
Expand All @@ -32,7 +32,7 @@
* Helper to get behat contexts.
*
* @package core
* @category testing
* @category test
* @copyright 2014 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
Expand Down Expand Up @@ -71,7 +71,7 @@ public static function set_session(Session $session) {
* can not be executed like this.
*
* @throws coding_exception
* @param string Context identifier (the class name).
* @param string $classname Context identifier (the class name).
* @return behat_base
*/
public static function get($classname) {
Expand Down
4 changes: 4 additions & 0 deletions lib/behat/classes/behat_selectors.php
Expand Up @@ -64,6 +64,7 @@ class behat_selectors {
'checkbox' => 'checkbox',
'radio' => 'radio',
'file' => 'file',
'filemanager' => 'filemanager',
'optgroup' => 'optgroup',
'option' => 'option',
'table' => 'table',
Expand Down Expand Up @@ -98,6 +99,9 @@ class behat_selectors {
XPATH
, 'table_row' => <<<XPATH
.//tr[contains(normalize-space(.), %locator%)]
XPATH
, 'filemanager' => <<<XPATH
//div[contains(concat(' ', normalize-space(@class), ' '), ' ffilemanager ')]/descendant::input[@id = //label[contains(normalize-space(string(.)), %locator%)]/@for]
XPATH
);

Expand Down
65 changes: 65 additions & 0 deletions lib/behat/form_field/behat_form_field.php
Expand Up @@ -50,6 +50,12 @@ class behat_form_field {
*/
protected $field;

/**
* @var string The field's locator.
*/
protected $fieldlocator = false;


/**
* General constructor with the node and the session to interact with.
*
Expand Down Expand Up @@ -171,4 +177,63 @@ protected function text_matches($expectedvalue) {
}
return true;
}

/**
* Gets the field locator.
*
* Defaults to the field label but you can
* specify other locators if you are interested.
*
* Public visibility as in most cases will be hard to
* use this method in a generic way, as fields can
* be selected using multiple ways (label, id, name...).
*
* @throws coding_exception
* @param string $locatortype
* @return string
*/
protected function get_field_locator($locatortype = false) {

if (!empty($this->fieldlocator)) {
return $this->fieldlocator;
}

$fieldid = $this->field->getAttribute('id');

// Defaults to label.
if ($locatortype == 'label' || $locatortype == false) {

$labelnode = $this->session->getPage()->find('xpath', '//label[@for="' . $fieldid . '"]');

// Exception only if $locatortype was specified.
if (!$labelnode && $locatortype == 'label') {
throw new coding_exception('Field with "' . $fieldid . '" id does not have a label.');
}

$this->fieldlocator = $labelnode->getText();
}

// Let's look for the name as a second option (more popular than
// id's when pointing to fields).
if (($locatortype == 'name' || $locatortype == false) &&
empty($this->fieldlocator)) {

$name = $this->field->getAttribute('name');

// Exception only if $locatortype was specified.
if (!$name && $locatortype == 'name') {
throw new coding_exception('Field with "' . $fieldid . '" id does not have a name attribute.');
}

$this->fieldlocator = $name;
}

// Otherwise returns the id if no specific locator type was provided.
if (empty($this->fieldlocator)) {
$this->fieldlocator = $fieldid;
}

return $this->fieldlocator;
}

}
2 changes: 1 addition & 1 deletion lib/behat/form_field/behat_form_filemanager.php
Expand Up @@ -41,7 +41,7 @@
*
* This field manager allows you to:
* - Get: A comma-separated list of the root directory
file names, including folders.
* file names, including folders.
* - Set: Add a file, in case you want to add more than
* one file you can always set two table rows using
* the same locator.
Expand Down
12 changes: 3 additions & 9 deletions lib/tests/behat/behat_forms.php
Expand Up @@ -166,10 +166,8 @@ public function i_set_the_field_to($field, $value) {
*/
public function the_field_matches_value($field, $value) {

$fieldnode = $this->find_field($field);

// Get the field.
$formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession());
$formfield = behat_field_manager::get_form_field_from_label($field, $this);

// Checks if the provided value matches the current field value.
if (!$formfield->matches($value)) {
Expand All @@ -193,10 +191,8 @@ public function the_field_matches_value($field, $value) {
*/
public function the_field_does_not_match_value($field, $value) {

$fieldnode = $this->find_field($field);

// Get the field.
$formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession());
$formfield = behat_field_manager::get_form_field_from_label($field, $this);

// Checks if the provided value matches the current field value.
if ($formfield->matches($value)) {
Expand Down Expand Up @@ -348,11 +344,9 @@ public function the_select_box_should_not_contain($select, $option) {
*/
protected function set_field_value($fieldlocator, $value) {

$node = $this->find_field($fieldlocator);

// We delegate to behat_form_field class, it will
// guess the type properly as it is a select tag.
$field = behat_field_manager::get_form_field($node, $this->getSession());
$field = behat_field_manager::get_form_field_from_label($fieldlocator, $this);
$field->set_value($value);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/tests/behat/behat_hooks.php
Expand Up @@ -110,6 +110,7 @@ public static function before_suite($event) {
// Now that we are MOODLE_INTERNAL.
require_once(__DIR__ . '/../../behat/classes/behat_command.php');
require_once(__DIR__ . '/../../behat/classes/behat_selectors.php');
require_once(__DIR__ . '/../../behat/classes/behat_context_helper.php');
require_once(__DIR__ . '/../../behat/classes/util.php');
require_once(__DIR__ . '/../../testing/classes/test_lock.php');
require_once(__DIR__ . '/../../testing/classes/nasty_strings.php');
Expand Down Expand Up @@ -184,6 +185,7 @@ public function before_scenario($event) {
// We need the Mink session to do it and we do it only before the first scenario.
if (self::is_first_scenario()) {
behat_selectors::register_moodle_selectors($session);
behat_context_helper::set_session($session);
}

// Reset $SESSION.
Expand Down
27 changes: 23 additions & 4 deletions mod/forum/tests/behat/add_forum.feature
@@ -1,28 +1,47 @@
@mod @mod_forum
@mod @mod_forum @_file_upload
Feature: Add forum activities and discussions
In order to discuss topics with other users
As a teacher
I need to add forum activities to moodle courses

@javascript
Scenario: Add a forum and a discussion
Scenario: Add a forum and a discussion attaching files
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@asd.com |
| student1 | Student | 1 | student1@asd.com |
And the following "courses" exist:
| fullname | shortname | category |
| Course 1 | C1 | 0 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And I log in as "teacher1"
And I follow "Course 1"
And I turn editing mode on
And I add a "Forum" to section "1" and I fill the form with:
| Forum name | Test forum name |
| Forum type | Standard forum for general use |
| Description | Test forum description |
When I add a new discussion to "Test forum name" forum with:
And I add a new discussion to "Test forum name" forum with:
| Subject | Forum post 1 |
| Message | This is the body |
Then I should see "Test forum name"
And I log out
And I log in as "student1"
And I follow "Course 1"
When I add a new discussion to "Test forum name" forum with:
| Subject | Post with attachment |
| Message | This is the body |
| Attachment | lib/tests/fixtures/empty.txt |
And I reply "Forum post 1" post from "Test forum name" forum with:
| Subject | Reply with attachment |
| Message | This is the body |
| Attachment | lib/tests/fixtures/upload_users.csv |
Then I should see "Reply with attachment"
And I should see "upload_users.csv"
And I follow "Test forum name"
And I follow "Post with attachment"
And I should see "empty.txt"
And I follow "Edit"
And the field "Attachment" matches value "empty.txt"
5 changes: 1 addition & 4 deletions repository/tests/behat/behat_filepicker.php
Expand Up @@ -281,11 +281,8 @@ protected function add_file_from_repository_to_filemanager($filepath, $repositor

// The action depends on the field type.
foreach ($datahash as $locator => $value) {
// Getting the node element pointed by the label.
$fieldnode = $this->find_field($locator);

// Gets the field type from a parent node.
$field = behat_field_manager::get_form_field($fieldnode, $this->getSession());
$field = behat_field_manager::get_form_field_from_label($locator, $this);

// Delegates to the field class.
$field->set_value($value);
Expand Down
5 changes: 1 addition & 4 deletions repository/upload/tests/behat/behat_repository_upload.php
Expand Up @@ -143,11 +143,8 @@ protected function upload_file_to_filemanager($filepath, $filemanagerelement, Ta

// The action depends on the field type.
foreach ($datahash as $locator => $value) {
// Getting the node element pointed by the label.
$fieldnode = $this->find_field($locator);

// Gets the field type from a parent node.
$field = behat_field_manager::get_form_field($fieldnode, $this->getSession());
$field = behat_field_manager::get_form_field_from_label($locator, $this);

// Delegates to the field class.
$field->set_value($value);
Expand Down

0 comments on commit af4830a

Please sign in to comment.