MDL-79863 qtype_ordering: Ordering qtype: write some Behat tests
[moodle.git] / question / type / ordering / tests / helper.php
blobd24f65b40766a9c17946042900b7702b71babc03
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.
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 * Test helper for the ordering question type.
20 * @package qtype_ordering
21 * @copyright 2018 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/question/type/ordering/question.php');
31 /**
32 * Test helper for the ordering question type.
34 * The class has code to generate question data structures for sample ordering questions.
36 * @copyright 2018 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class qtype_ordering_test_helper extends question_test_helper {
40 public function get_test_questions() {
41 return array('moodle');
44 /**
45 * Makes an ordering question to sort the words Modular Object Oriented Dynamic Learning Environment.
47 * @return qtype_ordering_question the question instance.
49 public function make_ordering_question_moodle() {
50 question_bank::load_question_definition_classes('ordering');
51 $q = new qtype_ordering_question();
52 $q->questionid = $q->id;
53 test_question_maker::initialise_a_question($q);
54 $q->qtype = question_bank::get_qtype('ordering');
55 $q->name = 'Moodle';
56 $q->questiontext = 'Put these words in order';
57 $q->generalfeedback = 'The correct answer is "Modular Object Oriented Dynamic Learning Environment".';
58 test_question_maker::set_standard_combined_feedback_fields($q);
59 $q->answers = [
60 13 => $this->make_answer(13, 'Modular', FORMAT_HTML, 1, true),
61 14 => $this->make_answer(14, 'Object', FORMAT_HTML, 2, true),
62 15 => $this->make_answer(15, 'Oriented', FORMAT_HTML, 3, true),
63 16 => $this->make_answer(16, 'Dynamic', FORMAT_HTML, 4, true),
64 17 => $this->make_answer(17, 'Learning', FORMAT_HTML, 5, true),
65 18 => $this->make_answer(18, 'Environment', FORMAT_HTML, 6, true),
67 $q->options = new stdClass();
68 $q->options->layouttype = qtype_ordering_question::LAYOUT_HORIZONTAL;
69 $q->options->selecttype = qtype_ordering_question::SELECT_ALL;
70 $q->options->selectcount = 0;
71 $q->options->gradingtype = qtype_ordering_question::GRADING_RELATIVE_ALL_PREVIOUS_AND_NEXT;
72 $q->options->showgrading = true;
74 return $q;
77 /**
78 * Create an answer record to use in a test question.
80 * @param int $id the id to set.
81 * @param string $text
82 * @param int $textformat one of the FORMAT_... constanst.
83 * @param int $order the position in order, numbered from 1.
84 * @param bool $addmd5 whether to add the md5key property.
85 * @return stdClass the answer.
87 protected function make_answer($id, $text, $textformat, $order, $addmd5 = false) {
88 global $CFG;
90 $answer = new stdClass();
91 $answer->id = $id;
92 $answer->question = 0;
93 $answer->answer = $text;
94 $answer->answerformat = $textformat;
95 $answer->fraction = $order;
96 $answer->feedback = '';
97 $answer->feedbackformat = FORMAT_MOODLE;
99 if ($addmd5) {
100 if (isset($CFG->passwordsaltmain)) {
101 $salt = $CFG->passwordsaltmain;
102 } else {
103 $salt = '';
105 $answer->md5key = 'ordering_item_' . md5($salt . $answer->answer);
108 return $answer;
112 * Get the form data that corresponds an ordering question.
114 * The question is to sort the words Modular Object Oriented Dynamic Learning Environment.
116 * @return stdClass simulated question form data.
118 public function get_ordering_question_form_data_moodle() {
119 $form = new stdClass();
120 $form->name = 'Moodle';
121 $form->questiontext = ['text' => 'Put these words in order.', 'format' => FORMAT_HTML];
122 $form->defaultmark = 1;
123 $form->generalfeedback = [
124 'text' => 'The correct answer is "Modular Object Oriented Dynamic Learning Environment".',
125 'format' => FORMAT_HTML
128 $form->layouttype = qtype_ordering_question::LAYOUT_HORIZONTAL;
129 $form->selecttype = qtype_ordering_question::SELECT_ALL;
130 $form->selectcount = 0;
131 $form->gradingtype = qtype_ordering_question::GRADING_RELATIVE_ALL_PREVIOUS_AND_NEXT;
132 $form->showgrading = true;
134 $form->countanswers = 6;
135 $form->answer = [
136 ['text' => 'Modular', 'format' => FORMAT_HTML],
137 ['text' => 'Object', 'format' => FORMAT_HTML],
138 ['text' => 'Oriented', 'format' => FORMAT_HTML],
139 ['text' => 'Dynamic', 'format' => FORMAT_HTML],
140 ['text' => 'Learning', 'format' => FORMAT_HTML],
141 ['text' => 'Environment', 'format' => FORMAT_HTML],
144 test_question_maker::set_standard_combined_feedback_form_data($form);
146 $form->penalty = '0.3333333';
147 $form->numhints = 0;
148 $form->hint = [];
150 $form->qtype = 'ordering';
151 return $form;
155 * Get the raw data that corresponds an ordering question.
157 * The question is to sort the words Modular Object Oriented Dynamic Learning Environment.
159 * @return stdClass simulated question form data.
161 public function get_ordering_question_data_moodle() {
162 $questiondata = new stdClass();
163 test_question_maker::initialise_question_data($questiondata);
164 $questiondata->qtype = 'ordering';
165 $questiondata->name = 'Moodle';
166 $questiondata->questiontext = 'Put these words in order';
167 $questiondata->generalfeedback = 'The correct answer is "Modular Object Oriented Dynamic Learning Environment".';
169 $questiondata->options = new stdClass();
170 test_question_maker::set_standard_combined_feedback_fields($questiondata->options);
171 unset($questiondata->options->shownumcorrect);
172 $questiondata->options->layouttype = qtype_ordering_question::LAYOUT_HORIZONTAL;
173 $questiondata->options->selecttype = qtype_ordering_question::SELECT_ALL;
174 $questiondata->options->selectcount = 0;
175 $questiondata->options->gradingtype = qtype_ordering_question::GRADING_RELATIVE_ALL_PREVIOUS_AND_NEXT;
176 $questiondata->options->showgrading = true;
178 $questiondata->options->answers = [
179 13 => $this->make_answer(13, 'Modular', FORMAT_HTML, 1),
180 14 => $this->make_answer(14, 'Object', FORMAT_HTML, 2),
181 15 => $this->make_answer(15, 'Oriented', FORMAT_HTML, 3),
182 16 => $this->make_answer(16, 'Dynamic', FORMAT_HTML, 4),
183 17 => $this->make_answer(17, 'Learning', FORMAT_HTML, 5),
184 18 => $this->make_answer(18, 'Environment', FORMAT_HTML, 6),
187 return $questiondata;