diff --git a/lib/behat/form_field/behat_form_field.php b/lib/behat/form_field/behat_form_field.php index 938bde631e8..8ba838a3a44 100644 --- a/lib/behat/form_field/behat_form_field.php +++ b/lib/behat/form_field/behat_form_field.php @@ -69,7 +69,14 @@ class behat_form_field { * @return void */ public function set_value($value) { - $this->field->setValue($value); + + // If we are not dealing with a text-based tag try to find the most appropiate + // behat_form_* class to deal with it. + if ($instance = $this->guess_type()) { + $instance->set_value($value); + } else { + $this->field->setValue($value); + } } /** @@ -78,7 +85,62 @@ class behat_form_field { * @return string */ public function get_value() { - return $this->field->getValue(); + + // If we are not dealing with a text-based tag try to find the most appropiate + // behat_form_* class to deal with it. + if ($instance = $this->guess_type()) { + return $instance->get_value(); + } else { + return $this->field->getValue(); + } + } + + /** + * Guesses the element type we are dealing with in case is not a text-based element. + * + * This class is the generic field type, behat_field_manager::get_field() + * should be able to find the appropiate class for the field type, but + * in cases like moodle form group elements we can not find the type of + * the field through the DOM so we also need to take care of the + * different field types from here. If we need to deal with more complex + * moodle form elements we will need to refactor this simple HTML elements + * guess method. + * + * @return mixed False if no need for an special behat_form_*, otherwise the behat_form_* + */ + private function guess_type() { + global $CFG; + + // Textareas are considered text based elements. + $tagname = $this->field->getTagName(); + if ($tagname == 'textarea') { + return false; + } + + if ($tagname == 'input') { + $type = $this->field->getAttribute('type'); + switch ($type) { + case 'text': + return false; + case 'checkbox': + $classname = 'behat_form_checkbox'; + break; + case 'radio': + // Behaves the same way. + $classname = 'behat_form_checkbox'; + break; + default: + return false; + } + } + + // Select tag. + if ($tagname == 'select') { + $classname = 'behat_form_select'; + } + + $classpath = $CFG->dirroot . '/lib/behat/form_field/' . $classname . '.php'; + return new $classname($this->session, $this->field); } } diff --git a/mod/forum/tests/behat/completion_condition_number_discussions.feature b/mod/forum/tests/behat/completion_condition_number_discussions.feature new file mode 100644 index 00000000000..eb0c914b848 --- /dev/null +++ b/mod/forum/tests/behat/completion_condition_number_discussions.feature @@ -0,0 +1,54 @@ +@mod_forum +Feature: Set a certain number of discussions as a completion condition for a forum + In order to ensure students are participating on forums + As a moodle teacher + I need to set a minimum number of discussions to mark the forum activity as completed + + @javascript + Scenario: Set X number of discussions as a condition + Given the following "users" exists: + | username | firstname | lastname | email | + | student1 | Student | 1 | student1@asd.com | + | teacher1 | Teacher | 1 | teacher1@asd.com | + And the following "courses" exists: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exists: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And I log in as "admin" + And I set the following administration settings values: + | Enable completion tracking | 1 | + | Enable conditional access | 1 | + And I log out + And I log in as "teacher1" + And I follow "Course 1" + And I turn editing mode on + And I follow "Edit settings" + And I fill the moodle form with: + | Completion tracking | Enabled, control via completion and activity settings | + | Completion tracking begins on enrolment | 1 | + And I press "Save changes" + When I add a "Forum" to section "1" and I fill the form with: + | Forum name | Test forum name | + | Description | Test forum description | + | Completion tracking | Show activity as complete when conditions are met | + | completiondiscussionsenabled | 1 | + | completiondiscussions | 2 | + And I log out + And I log in as "student1" + And I follow "Course 1" + Then I hover "//li[contains(concat(' ', @class, ' '), ' modtype_forum ')]/descendant::img[@alt='Not completed: Test forum name']" "xpath_element" + And I add a new discussion to "Test forum name" forum with: + | Subject | Post 1 subject | + | Message | Body 1 content | + And I add a new discussion to "Test forum name" forum with: + | Subject | Post 2 subject | + | Message | Body 2 content | + And I follow "Course 1" + And I hover "//li[contains(concat(' ', @class, ' '), ' modtype_forum ')]/descendant::img[contains(@alt, 'Completed: Test forum name')]" "xpath_element" + And I log out + And I log in as "teacher1" + And I follow "Course 1" + And "Student 1" user has completed "Test forum name" activity