From e200e47fe66d74a23c9c6899d908fdbeda772447 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Fri, 13 Feb 2015 17:34:07 +0000 Subject: [PATCH] MDL-47494 ddwtos: Improve the drag-drop question Behat tests. * Added tests for previewing the question, to make sure it works when attempted. * Added Moodle XML export tests. * Reorganised the tests to follow the 'Test one thing per scenario' best practice. This commit requires MDL-49154 to work. --- question/type/ddwtos/tests/behat/add.feature | 34 ++++++++ .../tests/behat/backup_and_restore.feature | 54 ++++++++++++ .../ddwtos/tests/behat/basic_test.feature | 80 ------------------ .../ddwtos/tests/behat/behat_qtype_ddwtos.php | 82 +++++++++++++++++++ question/type/ddwtos/tests/behat/edit.feature | 33 ++++++++ .../type/ddwtos/tests/behat/export.feature | 37 +++++++++ .../{import_test.feature => import.feature} | 16 ++-- .../type/ddwtos/tests/behat/preview.feature | 61 ++++++++++++++ question/type/ddwtos/tests/helper.php | 25 ++++++ 9 files changed, 334 insertions(+), 88 deletions(-) create mode 100644 question/type/ddwtos/tests/behat/add.feature create mode 100644 question/type/ddwtos/tests/behat/backup_and_restore.feature delete mode 100644 question/type/ddwtos/tests/behat/basic_test.feature create mode 100644 question/type/ddwtos/tests/behat/behat_qtype_ddwtos.php create mode 100644 question/type/ddwtos/tests/behat/edit.feature create mode 100644 question/type/ddwtos/tests/behat/export.feature rename question/type/ddwtos/tests/behat/{import_test.feature => import.feature} (73%) create mode 100644 question/type/ddwtos/tests/behat/preview.feature diff --git a/question/type/ddwtos/tests/behat/add.feature b/question/type/ddwtos/tests/behat/add.feature new file mode 100644 index 00000000000..320c5c1d1e0 --- /dev/null +++ b/question/type/ddwtos/tests/behat/add.feature @@ -0,0 +1,34 @@ +@ou @ou_vle @qtype @qtype_ddwtos +Feature: Test creating a drag and drop into text question + As a teacher + In order to test my students + I need to be able to create drag and drop into text questions + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | T1 | Teacher1 | teacher1@moodle.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 | + And I log in as "teacher1" + And I follow "Course 1" + And I navigate to "Question bank" node in "Course administration" + + @javascript + Scenario: Create a drag and drop into text question + When I add a "Drag and drop into text" question filling the form with: + | Question name | Drag and drop into text 001 | + | Question text | The [[1]] [[2]] on the [[3]]. | + | General feedback | The cat sat on the mat. | + | id_choices_0_answer | cat | + | id_choices_1_answer | sat | + | id_choices_2_answer | mat | + | id_choices_3_answer | dog | + | id_choices_4_answer | table | + | Hint 1 | First hint | + | Hint 2 | Second hint | + Then I should see "Drag and drop into text 001" diff --git a/question/type/ddwtos/tests/behat/backup_and_restore.feature b/question/type/ddwtos/tests/behat/backup_and_restore.feature new file mode 100644 index 00000000000..f33c5dd5017 --- /dev/null +++ b/question/type/ddwtos/tests/behat/backup_and_restore.feature @@ -0,0 +1,54 @@ +@ou @ou_vle @qtype @qtype_ddwtos +Feature: Test duplicating a quiz containing a drag and drop into text question + As a teacher + In order re-use my courses containing drag and drop into text questions + I need to be able to backup and restore them + + Background: + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "question categories" exist: + | contextlevel | reference | name | + | Course | C1 | Test questions | + And the following "questions" exist: + | questioncategory | qtype | name | template | + | Test questions | ddwtos | Drag to text | fox | + And the following "activities" exist: + | activity | name | course | idnumber | + | quiz | Test quiz | C1 | quiz1 | + And quiz "Test quiz" contains the following questions: + | Drag to text | 1 | + And I log in as "admin" + And I follow "Course 1" + + @javascript + Scenario: Backup and restore a course containing a drag and drop into text question + When I backup "Course 1" course using this options: + | Confirmation | Filename | test_backup.mbz | + And I restore "test_backup.mbz" backup into a new course using this options: + | Schema | Course name | Course 2 | + And I navigate to "Question bank" node in "Course administration" + And I click on "Edit" "link" in the "Drag to text" "table_row" + Then the following fields match these values: + | Question name | Drag to text | + | Question text | The [[1]] brown [[2]] jumped over the [[3]] dog. | + | General feedback | This sentence uses each letter of the alphabet. | + | Default mark | 1 | + | Shuffle | 0 | + | id_choices_0_answer | quick | + | id_choices_0_choicegroup | 1 | + | id_choices_1_answer | fox | + | id_choices_1_choicegroup | 2 | + | id_choices_2_answer | lazy | + | id_choices_2_choicegroup | 3 | + | id_choices_3_answer | slow | + | id_choices_3_choicegroup | 1 | + | id_choices_4_answer | dog | + | id_choices_4_choicegroup | 2 | + | id_choices_5_answer | assiduous | + | id_choices_5_choicegroup | 3 | + | For any correct response | Well done! | + | For any partially correct response | Parts, but only parts, of your response are correct. | + | id_shownumcorrect | 0 | + | For any incorrect response | That is not right at all. | diff --git a/question/type/ddwtos/tests/behat/basic_test.feature b/question/type/ddwtos/tests/behat/basic_test.feature deleted file mode 100644 index 9f955b00101..00000000000 --- a/question/type/ddwtos/tests/behat/basic_test.feature +++ /dev/null @@ -1,80 +0,0 @@ -@ou @ou_vle @qtype @qtype_ddwtos -Feature: Test all the basic functionality of this question type - In order to evaluate students responses, As a teacher I need to - create and preview ddwtos (Drag and drop into text) questions. - - # Due to complexity and since the JavaScript code needs to be converted at some stage, - # we are not going to test attempting this qtype. However, we will do all other - # functionality, such as creating the question preview it and seeing the - # correct information on the preview string as well as backing-up and restoring - # the course containing this qtype. - - # Another way to test attempting this qtype while previewing it, is to write a - # customised step for tabbing through place-holders and another customised - # step for making use of arrow keys in order to go through the list of choices. - - Background: - Given the following "users" exist: - | username | firstname | lastname | email | - | teacher1 | T1 | Teacher1 | teacher1@moodle.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 | - - @javascript - Scenario: Create, edit then preview a gapselect question. - Given I log in as "teacher1" - And I follow "Course 1" - And I navigate to "Question bank" node in "Course administration" - - # Create a new question. - And I add a "Drag and drop into text" question filling the form with: - | Question name | Drag and drop into text 001 | - | Question text | The [[1]] [[2]] on the [[3]]. | - | General feedback | The cat sat on the mat. | - | id_choices_0_answer | cat | - | id_choices_1_answer | sat | - | id_choices_2_answer | mat | - | id_choices_3_answer | dog | - | id_choices_4_answer | table | - | Hint 1 | First hint | - | Hint 2 | Second hint | - Then I should see "Drag and drop into text 001" - - # Preview it. - When I click on "Preview" "link" in the "Drag and drop into text 001" "table_row" - And I switch to "questionpreview" window - Then I should see "Preview question: Drag and drop into text 001" - And I switch to the main window - - # Backup the course and restore it. - When I log out - And I log in as "admin" - When I backup "Course 1" course using this options: - | Confirmation | Filename | test_backup.mbz | - When I restore "test_backup.mbz" backup into a new course using this options: - | Schema | Course name | Course 2 | - Then I should see "Course 2" - When I navigate to "Question bank" node in "Course administration" - Then I should see "Drag and drop into text 001" - - # Edit the copy and verify the form field contents. - When I click on "Edit" "link" in the "Drag and drop into text 001" "table_row" - Then the following fields match these values: - | Question name | Drag and drop into text 001 | - | Question text | The [[1]] [[2]] on the [[3]]. | - | General feedback | The cat sat on the mat. | - | id_choices_0_answer | cat | - | id_choices_1_answer | sat | - | id_choices_2_answer | mat | - | id_choices_3_answer | dog | - | id_choices_4_answer | table | - | Hint 1 | First hint | - | Hint 2 | Second hint | - And I set the following fields to these values: - | Question name | Edited question name | - And I press "id_submitbutton" - Then I should see "Edited question name" diff --git a/question/type/ddwtos/tests/behat/behat_qtype_ddwtos.php b/question/type/ddwtos/tests/behat/behat_qtype_ddwtos.php new file mode 100644 index 00000000000..4265ceb2bd5 --- /dev/null +++ b/question/type/ddwtos/tests/behat/behat_qtype_ddwtos.php @@ -0,0 +1,82 @@ +. + +/** + * Behat steps definitions for drag and drop into text. + * + * @package qtype_ddwtos + * @category test + * @copyright 2015 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +require_once(__DIR__ . '/../../../../../lib/behat/behat_base.php'); + +/** + * Steps definitions related with the drag and drop into text question type. + * + * @copyright 2015 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_qtype_ddwtos extends behat_base { + + /** + * Get the xpath for a given drag item. + * @param string $dragitem the text of the item to drag. + * @return string the xpath expression. + */ + protected function drag_xpath($dragitem) { + return '//span[contains(@class, " drag ") and contains(., "' . $this->escape($dragitem) . '")]'; + } + + /** + * Get the xpath for a given drop box. + * @param string $dragitem the number of the drop box. + * @return string the xpath expression. + */ + protected function drop_xpath($spacenumber) { + return '//span[contains(@class, " drop ") and contains(@class, "place' . $spacenumber . ' ")]'; + } + + /** + * Drag the drag item with the given text to the given space. + * + * @param string $dragitem the text of the item to drag. + * @param int $spacenumber the number of the gap to drop into. + * + * @Given /^I drag "(?P[^"]*)" to space "(?P\d+)" in the drag and drop into text question$/ + */ + public function i_drag_to_space_in_the_drag_and_drop_into_text_question($dragitem, $spacenumber) { + $generalcontext = behat_context_helper::get('behat_general'); + $generalcontext->i_drag_and_i_drop_it_in($this->drag_xpath($dragitem), + 'xpath_element', $this->drop_xpath($spacenumber), 'xpath_element'); + } + + /** + * Type some characters while focussed on a given space. + * + * @param string $keys the characters to type. + * @param int $spacenumber the number of the space to type into. + * + * @Given /^I type "(?P[^"]*)" into space "(?P\d+)" in the drag and drop onto image question$/ + */ + public function i_type_into_space_in_the_drag_and_drop_into_text_question($keys, $spacenumber) { + $generalcontext = behat_context_helper::get('behat_general'); + $generalcontext->i_type_into_the($keys, $this->drop_xpath($spacenumber), 'xpath_element'); + } +} diff --git a/question/type/ddwtos/tests/behat/edit.feature b/question/type/ddwtos/tests/behat/edit.feature new file mode 100644 index 00000000000..a41a5731623 --- /dev/null +++ b/question/type/ddwtos/tests/behat/edit.feature @@ -0,0 +1,33 @@ +@ou @ou_vle @qtype @qtype_ddwtos +Feature: Test editing a drag and drop into text questions + As a teacher + In order to be able to update my drag and drop into text questions + I need to edit them + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | T1 | Teacher1 | teacher1@example.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 | + And the following "question categories" exist: + | contextlevel | reference | name | + | Course | C1 | Test questions | + And the following "questions" exist: + | questioncategory | qtype | name | template | + | Test questions | ddwtos | Drag to text | fox | + And I log in as "teacher1" + And I follow "Course 1" + And I navigate to "Question bank" node in "Course administration" + + @javascript + Scenario: Edit a drag and drop into text question + When I click on "Edit" "link" in the "Drag to text" "table_row" + And I set the following fields to these values: + | Question name | Edited question name | + And I press "id_submitbutton" + Then I should see "Edited question name" diff --git a/question/type/ddwtos/tests/behat/export.feature b/question/type/ddwtos/tests/behat/export.feature new file mode 100644 index 00000000000..4da762e24b9 --- /dev/null +++ b/question/type/ddwtos/tests/behat/export.feature @@ -0,0 +1,37 @@ +@ou @ou_vle @qtype @qtype_ddwtos +Feature: Test exporting drag and drop into text questions + As a teacher + In order to be able to reuse my drag and drop into text questions + I need to export them + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | T1 | Teacher1 | teacher1@example.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 | + And the following "question categories" exist: + | contextlevel | reference | name | + | Course | C1 | Test questions | + And the following "questions" exist: + | questioncategory | qtype | name | template | + | Test questions | ddwtos | Drag to text | fox | + And I log in as "teacher1" + And I follow "Course 1" + + @javascript + Scenario: Export a drag and drop into text question + # Import sample file. + When I navigate to "Export" node in "Course administration > Question bank" + And I set the field "id_format_xml" to "1" + And I press "Export questions to file" + And following "click here" should download between "1450" and "1550" bytes + # If the download step is the last in the scenario then we can sometimes run + # into the situation where the download page causes a http redirect but behat + # has already conducted its reset (generating an error). By putting a logout + # step we avoid behat doing the reset until we are off that page. + And I log out diff --git a/question/type/ddwtos/tests/behat/import_test.feature b/question/type/ddwtos/tests/behat/import.feature similarity index 73% rename from question/type/ddwtos/tests/behat/import_test.feature rename to question/type/ddwtos/tests/behat/import.feature index d67405154d8..f6d3cd11aa9 100644 --- a/question/type/ddwtos/tests/behat/import_test.feature +++ b/question/type/ddwtos/tests/behat/import.feature @@ -1,12 +1,13 @@ @ou @ou_vle @qtype @qtype_ddwtos -Feature: Test importing questions of this type - As a manager/teacher I should be able to import questions from other courses - to this course +Feature: Test importing drag and drop into text questions + As a teacher + In order to reuse drag and drop into text questions + I need to import them Background: - Given the following "users" exist: + Given the following "users" exist: | username | firstname | lastname | email | - | teacher1 | T1 | Teacher1 | teacher1@moodle.com | + | teacher1 | T1 | Teacher1 | teacher1@example.com | And the following "courses" exist: | fullname | shortname | category | | Course 1 | C1 | 0 | @@ -16,9 +17,8 @@ Feature: Test importing questions of this type And I log in as "teacher1" And I follow "Course 1" - @javascript - Scenario: import a variable numeric sets question. - # Import sample file. + @javascript @_file_upload + Scenario: import drag and drop into text question. When I navigate to "Import" node in "Course administration > Question bank" And I set the field "id_format_xml" to "1" And I upload "question/type/ddwtos/tests/fixtures/testquestion.moodle.xml" file to "Import" filemanager diff --git a/question/type/ddwtos/tests/behat/preview.feature b/question/type/ddwtos/tests/behat/preview.feature new file mode 100644 index 00000000000..240f07f0e00 --- /dev/null +++ b/question/type/ddwtos/tests/behat/preview.feature @@ -0,0 +1,61 @@ +@ou @ou_vle @qtype @qtype_ddwtos +Feature: Preview a drag-drop into text question + As a teacher + In order to check my drag-drop into text questions will work for students + I need to preview them + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | T1 | Teacher1 | teacher1@moodle.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 | + And the following "question categories" exist: + | contextlevel | reference | name | + | Course | C1 | Test questions | + And the following "questions" exist: + | questioncategory | qtype | name | template | + | Test questions | ddwtos | Drag to text | fox | + Given I log in as "teacher1" + And I follow "Course 1" + And I navigate to "Question bank" node in "Course administration" + + @javascript + Scenario: Preview a question using the mouse. + When I click on "Preview" "link" in the "Drag to text" "table_row" + And I switch to "questionpreview" window + And I wait "1" seconds + And I drag "quick" to space "1" in the drag and drop into text question + And I drag "fox" to space "2" in the drag and drop into text question + And I drag "assiduous" to space "3" in the drag and drop into text question + And I press "Submit and finish" + Then the state of "The" question is shown as "Partially correct" + And I should see "Mark 0.67 out of 1.00" + + @javascript + Scenario: Preview a question using the keyboard & submit incomplete. + When I click on "Preview" "link" in the "Drag to text" "table_row" + And I switch to "questionpreview" window + And I wait "1" seconds + And I type " " into space "1" in the drag and drop onto image question + And I type " " into space "2" in the drag and drop onto image question + And I type " " into space "3" in the drag and drop onto image question + And I press "Save" + Then the state of "The" question is shown as "Incomplete answer" + And I should see "Please put an answer in each box." + + @javascript + Scenario: Preview a question using the keyboard. + When I click on "Preview" "link" in the "Drag to text" "table_row" + And I switch to "questionpreview" window + And I wait "1" seconds + And I type " " into space "1" in the drag and drop onto image question + And I type " " into space "2" in the drag and drop onto image question + And I type " " into space "3" in the drag and drop onto image question + And I press "Submit and finish" + Then the state of "The" question is shown as "Incorrect" + And I should see "Mark 0.00 out of 1.00" diff --git a/question/type/ddwtos/tests/helper.php b/question/type/ddwtos/tests/helper.php index 50e244f4157..0878b6ab0d8 100644 --- a/question/type/ddwtos/tests/helper.php +++ b/question/type/ddwtos/tests/helper.php @@ -75,6 +75,31 @@ class qtype_ddwtos_test_helper extends question_test_helper { return $dd; } + /** + * @return stdClass date to create a ddwtos question. + */ + public function get_ddwtos_question_form_data_fox() { + $fromform = new stdClass(); + + $fromform->name = 'Drag-and-drop words into sentences question'; + $fromform->questiontext = array('text' => 'The [[1]] brown [[2]] jumped over the [[3]] dog.', 'format' => FORMAT_HTML); + $fromform->defaultmark = 1.0; + $fromform->generalfeedback = array('text' => 'This sentence uses each letter of the alphabet.', 'format' => FORMAT_HTML); + $fromform->choices = array( + array('answer' => 'quick', 'choicegroup' => '1'), + array('answer' => 'fox', 'choicegroup' => '2'), + array('answer' => 'lazy', 'choicegroup' => '3'), + array('answer' => 'slow', 'choicegroup' => '1'), + array('answer' => 'dog', 'choicegroup' => '2'), + array('answer' => 'assiduous', 'choicegroup' => '3'), + ); + test_question_maker::set_standard_combined_feedback_form_data($fromform); + $fromform->shownumcorrect = 0; + $fromform->penalty = 0.3333333; + + return $fromform; + } + /** * @return qtype_ddwtos_question */