286763183941090294ccf7e476b7dd9817e6f9b5
[moodle.git] / question / type / gapselect / tests / walkthrough_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * This file contains tests that simulate a user attempt a gapselect question.
19  *
20  * @package   qtype_gapselect
21  * @copyright 2012 The Open University
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
26 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
30 require_once($CFG->dirroot . '/question/type/gapselect/tests/helper.php');
33 /**
34  * Unit tests for the gap-select question type.
35  *
36  * @copyright 2012 The Open University
37  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class qtype_gapselect_walkthrough_test extends qbehaviour_walkthrough_test_base {
40     public function test_interactive_behaviour() {
42         // Create a gapselect question.
43         $q = qtype_gapselect_test_helper::make_a_gapselect_question();
44         $q->hints = array(
45             new question_hint_with_parts(1, 'This is the first hint.', FORMAT_HTML, false, false),
46             new question_hint_with_parts(2, 'This is the second hint.', FORMAT_HTML, true, true),
47         );
48         $q->shufflechoices = false;
49         $this->start_attempt_at_question($q, 'interactive', 3);
51         // Check the initial state.
52         $this->check_current_state(question_state::$todo);
53         $this->check_current_mark(null);
54         $this->check_current_output(
55                 $this->get_contains_select_expectation('p1',
56                                 array('' => get_string('choosedots'), '1' => 'quick', '2' => 'slow'), null, true),
57                 $this->get_contains_select_expectation('p2',
58                                 array('' => get_string('choosedots'), '1' => 'fox', '2' => 'dog'), null, true),
59                 $this->get_contains_select_expectation('p3',
60                                 array('' => get_string('choosedots'), '1' => 'lazy', '2' => 'assiduous'), null, true),
61                 $this->get_contains_submit_button_expectation(true),
62                 $this->get_does_not_contain_feedback_expectation(),
63                 $this->get_tries_remaining_expectation(3),
64                 $this->get_no_hint_visible_expectation());
66         // Save the wrong answer.
67         $this->process_submission(array('p1' => '2', 'p2' => '2', 'p3' => '2'));
69         // Verify.
70         $this->check_current_state(question_state::$todo);
71         $this->check_current_mark(null);
72         $this->check_current_output(
73                 $this->get_contains_select_expectation('p1',
74                                 array('' => get_string('choosedots'), '1' => 'quick', '2' => 'slow'), 2, true),
75                 $this->get_contains_select_expectation('p2',
76                                 array('' => get_string('choosedots'), '1' => 'fox', '2' => 'dog'), 2, true),
77                 $this->get_contains_select_expectation('p3',
78                                 array('' => get_string('choosedots'), '1' => 'lazy', '2' => 'assiduous'), 2, true),
79                 $this->get_contains_submit_button_expectation(true),
80                 $this->get_does_not_contain_correctness_expectation(),
81                 $this->get_does_not_contain_feedback_expectation(),
82                 $this->get_tries_remaining_expectation(3),
83                 $this->get_no_hint_visible_expectation());
85         // Submit the wrong answer.
86         $this->process_submission(array('p1' => '2', 'p2' => '2', 'p3' => '2', '-submit' => 1));
88         // Verify.
89         $this->check_current_state(question_state::$todo);
90         $this->check_current_mark(null);
91         $this->check_current_output(
92                 $this->get_contains_select_expectation('p1',
93                                 array('' => get_string('choosedots'), '1' => 'quick', '2' => 'slow'), 2, false),
94                 $this->get_contains_select_expectation('p2',
95                                 array('' => get_string('choosedots'), '1' => 'fox', '2' => 'dog'), 2, false),
96                 $this->get_contains_select_expectation('p3',
97                                 array('' => get_string('choosedots'), '1' => 'lazy', '2' => 'assiduous'), 2, false),
98                         $this->get_contains_submit_button_expectation(false),
99                 $this->get_contains_try_again_button_expectation(true),
100                 $this->get_does_not_contain_correctness_expectation(),
101                 new question_pattern_expectation('/' . preg_quote(
102                         get_string('notcomplete', 'qbehaviour_interactive'), '/') . '/'),
103                 $this->get_contains_hint_expectation('This is the first hint'));
105         // Do try again.
106         $this->process_submission(array('-tryagain' => 1));
108         // Verify.
109         $this->check_current_state(question_state::$todo);
110         $this->check_current_mark(null);
111         $this->check_current_output(
112                 $this->get_contains_select_expectation('p1',
113                                 array('' => get_string('choosedots'), '1' => 'quick', '2' => 'slow'), 2, true),
114                 $this->get_contains_select_expectation('p2',
115                                 array('' => get_string('choosedots'), '1' => 'fox', '2' => 'dog'), 2, true),
116                 $this->get_contains_select_expectation('p3',
117                                 array('' => get_string('choosedots'), '1' => 'lazy', '2' => 'assiduous'), 2, true),
118                 $this->get_contains_submit_button_expectation(true),
119                 $this->get_does_not_contain_correctness_expectation(),
120                 $this->get_does_not_contain_feedback_expectation(),
121                 $this->get_tries_remaining_expectation(2),
122                 $this->get_no_hint_visible_expectation());
124         // Submit the right answer.
125         $this->process_submission(array('p1' => '1', 'p2' => '1', 'p3' => '1', '-submit' => 1));
127         // Verify.
128         $this->check_current_state(question_state::$gradedright);
129         $this->check_current_mark(2);
130         $this->check_current_output(
131                 $this->get_contains_select_expectation('p1',
132                                 array('' => get_string('choosedots'), '1' => 'quick', '2' => 'slow'), 1, false),
133                 $this->get_contains_select_expectation('p2',
134                                 array('' => get_string('choosedots'), '1' => 'fox', '2' => 'dog'), 1, false),
135                 $this->get_contains_select_expectation('p3',
136                                 array('' => get_string('choosedots'), '1' => 'lazy', '2' => 'assiduous'), 1, false),
137                 $this->get_contains_submit_button_expectation(false),
138                 $this->get_contains_correct_expectation(),
139                 $this->get_no_hint_visible_expectation());
141         // Check regrading does not mess anything up.
142         $this->quba->regrade_all_questions();
144         // Verify.
145         $this->check_current_state(question_state::$gradedright);
146         $this->check_current_mark(2);
147     }