Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   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/>.
  16  
  17  namespace qtype_randomsamatch;
  18  
  19  use question_hint_with_parts;
  20  use question_state;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  26  
  27  
  28  /**
  29   * Unit tests for the randomsamatch question type.
  30   *
  31   * @package   qtype_randomsamatch
  32   * @copyright 2013 Jean-Michel Vedrine
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class walkthrough_test extends \qbehaviour_walkthrough_test_base {
  36  
  37      public function test_deferred_feedback_unanswered() {
  38  
  39          // Create a randomsamatch question.
  40          $m = \test_question_maker::make_question('randomsamatch');
  41          $this->start_attempt_at_question($m, 'deferredfeedback', 4);
  42  
  43          $choiceorder = $m->get_choice_order();
  44          $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
  45          $choices = [];
  46          foreach ($choiceorder as $key => $choice) {
  47              $choices[$key] = $m->choices[$choice];
  48          }
  49  
  50          // Check the initial state.
  51          $this->check_current_state(question_state::$todo);
  52          $this->check_current_mark(null);
  53          $this->check_current_output(
  54                  $this->get_contains_question_text_expectation($m),
  55                  $this->get_does_not_contain_feedback_expectation());
  56          $this->check_output_contains_selectoptions(
  57                  $this->get_contains_select_expectation('sub0', $choices, null, true),
  58                  $this->get_contains_select_expectation('sub1', $choices, null, true),
  59                  $this->get_contains_select_expectation('sub2', $choices, null, true),
  60                  $this->get_contains_select_expectation('sub3', $choices, null, true));
  61          $this->check_step_count(1);
  62  
  63          // Save a blank response.
  64          $this->process_submission(array('sub0' => '0', 'sub1' => '0',
  65                  'sub2' => '0', 'sub3' => '0'));
  66  
  67          // Verify.
  68          $this->check_current_state(question_state::$todo);
  69          $this->check_current_mark(null);
  70          $this->check_current_output(
  71                  $this->get_contains_question_text_expectation($m),
  72                  $this->get_does_not_contain_feedback_expectation());
  73          $this->check_output_contains_selectoptions(
  74                  $this->get_contains_select_expectation('sub0', $choices, null, true),
  75                  $this->get_contains_select_expectation('sub1', $choices, null, true),
  76                  $this->get_contains_select_expectation('sub2', $choices, null, true),
  77                  $this->get_contains_select_expectation('sub3', $choices, null, true));
  78          $this->check_step_count(1);
  79  
  80          // Finish the attempt.
  81          $this->quba->finish_all_questions();
  82  
  83          // Verify.
  84          $this->check_current_state(question_state::$gaveup);
  85          $this->check_current_mark(null);
  86          $this->check_output_contains_selectoptions(
  87                  $this->get_contains_select_expectation('sub0', $choices, null, false),
  88                  $this->get_contains_select_expectation('sub1', $choices, null, false),
  89                  $this->get_contains_select_expectation('sub2', $choices, null, false),
  90                  $this->get_contains_select_expectation('sub3', $choices, null, false));
  91      }
  92  
  93      public function test_deferred_feedback_partial_answer() {
  94  
  95          // Create a randomsamatching question.
  96          $m = \test_question_maker::make_question('randomsamatch');
  97          $m->shufflestems = false;
  98          $this->start_attempt_at_question($m, 'deferredfeedback', 4);
  99  
 100          $choiceorder = $m->get_choice_order();
 101          $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
 102          $choices = [];
 103          foreach ($choiceorder as $key => $choice) {
 104              $choices[$key] = $m->choices[$choice];
 105          }
 106  
 107          // Check the initial state.
 108          $this->check_current_state(question_state::$todo);
 109          $this->check_current_mark(null);
 110          $this->check_current_output(
 111                  $this->get_contains_question_text_expectation($m),
 112                  $this->get_does_not_contain_feedback_expectation());
 113          $this->check_output_contains_selectoptions(
 114                  $this->get_contains_select_expectation('sub0', $choices, null, true),
 115                  $this->get_contains_select_expectation('sub1', $choices, null, true),
 116                  $this->get_contains_select_expectation('sub2', $choices, null, true),
 117                  $this->get_contains_select_expectation('sub3', $choices, null, true));
 118  
 119          // Save a partial response.
 120          $this->process_submission(array('sub0' => $orderforchoice[13],
 121                  'sub1' => $orderforchoice[16], 'sub2' => '0', 'sub3' => '0'));
 122  
 123          // Verify.
 124          $this->check_current_state(question_state::$invalid);
 125          $this->check_current_mark(null);
 126          $this->check_current_output(
 127                  $this->get_contains_question_text_expectation($m),
 128                  $this->get_does_not_contain_feedback_expectation());
 129          $this->check_output_contains_selectoptions(
 130                  $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[13], true),
 131                  $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[16], true),
 132                  $this->get_contains_select_expectation('sub2', $choices, null, true),
 133                  $this->get_contains_select_expectation('sub3', $choices, null, true));
 134  
 135          // Finish the attempt.
 136          $this->quba->finish_all_questions();
 137  
 138          // Verify.
 139          $this->check_current_state(question_state::$gradedpartial);
 140          $this->check_current_mark(2);
 141          $this->check_current_output(
 142                  $this->get_contains_partcorrect_expectation());
 143          $this->check_output_contains_selectoptions(
 144                  $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[13], false),
 145                  $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[16], false),
 146                  $this->get_contains_select_expectation('sub2', $choices, null, false),
 147                  $this->get_contains_select_expectation('sub3', $choices, null, false));
 148      }
 149  
 150      public function test_interactive_correct_no_submit() {
 151  
 152          // Create a randomsamatching question.
 153          $m = \test_question_maker::make_question('randomsamatch');
 154          $m->hints = array(
 155              new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, false, false),
 156              new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
 157          );
 158          $m->shufflestems = false;
 159          $this->start_attempt_at_question($m, 'interactive', 4);
 160  
 161          $choiceorder = $m->get_choice_order();
 162          $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
 163          $choices = [];
 164          foreach ($choiceorder as $key => $choice) {
 165              $choices[$key] = $m->choices[$choice];
 166          }
 167  
 168          // Check the initial state.
 169          $this->check_current_state(question_state::$todo);
 170          $this->check_current_mark(null);
 171          $this->check_current_output(
 172                  $this->get_contains_submit_button_expectation(true),
 173                  $this->get_does_not_contain_feedback_expectation(),
 174                  $this->get_tries_remaining_expectation(3),
 175                  $this->get_no_hint_visible_expectation());
 176          $this->check_output_contains_selectoptions(
 177                  $this->get_contains_select_expectation('sub0', $choices, null, true),
 178                  $this->get_contains_select_expectation('sub1', $choices, null, true),
 179                  $this->get_contains_select_expectation('sub2', $choices, null, true),
 180                  $this->get_contains_select_expectation('sub3', $choices, null, true));
 181  
 182          // Save the right answer.
 183          $this->process_submission(array('sub0' => $orderforchoice[13],
 184                  'sub1' => $orderforchoice[16], 'sub2' => $orderforchoice[16],
 185                  'sub3' => $orderforchoice[13]));
 186  
 187          // Finish the attempt without clicking check.
 188          $this->quba->finish_all_questions();
 189  
 190          // Verify.
 191          $this->check_current_state(question_state::$gradedright);
 192          $this->check_current_mark(4);
 193          $this->check_current_output(
 194                  $this->get_does_not_contain_submit_button_expectation(),
 195                  $this->get_contains_correct_expectation(),
 196                  $this->get_no_hint_visible_expectation());
 197          $this->check_output_contains_selectoptions(
 198                  $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[13], false),
 199                  $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[16], false),
 200                  $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[16], false),
 201                  $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[13], false));
 202      }
 203  
 204      public function test_interactive_partial_no_submit() {
 205  
 206          // Create a randomsamatching question.
 207          $m = \test_question_maker::make_question('randomsamatch');
 208          $m->hints = array(
 209              new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, false, false),
 210              new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
 211          );
 212          $m->shufflestems = false;
 213          $this->start_attempt_at_question($m, 'interactive', 4);
 214  
 215          $choiceorder = $m->get_choice_order();
 216          $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
 217          $choices = [];
 218          foreach ($choiceorder as $key => $choice) {
 219              $choices[$key] = $m->choices[$choice];
 220          }
 221  
 222          // Check the initial state.
 223          $this->check_current_state(question_state::$todo);
 224          $this->check_current_mark(null);
 225          $this->check_current_output(
 226                  $this->get_contains_submit_button_expectation(true),
 227                  $this->get_does_not_contain_feedback_expectation(),
 228                  $this->get_tries_remaining_expectation(3),
 229                  $this->get_no_hint_visible_expectation());
 230          $this->check_output_contains_selectoptions(
 231                  $this->get_contains_select_expectation('sub0', $choices, null, true),
 232                  $this->get_contains_select_expectation('sub1', $choices, null, true),
 233                  $this->get_contains_select_expectation('sub2', $choices, null, true),
 234                  $this->get_contains_select_expectation('sub3', $choices, null, true));
 235  
 236          // Save the right answer.
 237          $this->process_submission(array('sub0' => $orderforchoice[13],
 238                  'sub1' => $orderforchoice[16], 'sub2' => $orderforchoice[13],
 239                  'sub3' => '0'));
 240  
 241          // Finish the attempt without clicking check.
 242          $this->quba->finish_all_questions();
 243  
 244          // Verify.
 245          $this->check_current_state(question_state::$gradedpartial);
 246          $this->check_current_mark(2);
 247          $this->check_current_output(
 248                  $this->get_does_not_contain_submit_button_expectation(),
 249                  $this->get_contains_partcorrect_expectation(),
 250                  $this->get_no_hint_visible_expectation());
 251          $this->check_output_contains_selectoptions(
 252                  $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[13], false),
 253                  $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[16], false),
 254                  $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[13], false),
 255                  $this->get_contains_select_expectation('sub3', $choices, null, false));
 256      }
 257  
 258      public function test_interactive_with_invalid() {
 259  
 260          // Create a randomsamatching question.
 261          $m = \test_question_maker::make_question('randomsamatch');
 262          $m->hints = array(
 263              new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, false, false),
 264              new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
 265          );
 266          $m->shufflestems = false;
 267          $this->start_attempt_at_question($m, 'interactive', 4);
 268  
 269          $choiceorder = $m->get_choice_order();
 270          $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
 271          $choices = [];
 272          foreach ($choiceorder as $key => $choice) {
 273              $choices[$key] = $m->choices[$choice];
 274          }
 275  
 276          // Check the initial state.
 277          $this->check_current_state(question_state::$todo);
 278          $this->check_current_mark(null);
 279          $this->check_current_output(
 280                  $this->get_contains_submit_button_expectation(true),
 281                  $this->get_does_not_contain_feedback_expectation(),
 282                  $this->get_tries_remaining_expectation(3),
 283                  $this->get_no_hint_visible_expectation());
 284          $this->check_output_contains_selectoptions(
 285                  $this->get_contains_select_expectation('sub0', $choices, null, true),
 286                  $this->get_contains_select_expectation('sub1', $choices, null, true),
 287                  $this->get_contains_select_expectation('sub2', $choices, null, true),
 288                  $this->get_contains_select_expectation('sub3', $choices, null, true));
 289  
 290          // Try to submit an invalid answer.
 291          $this->process_submission(array('sub0' => '0',
 292                  'sub1' => '0', 'sub2' => '0',
 293                  'sub3' => '0', '-submit' => '1'));
 294  
 295          // Verify.
 296          $this->check_current_state(question_state::$invalid);
 297          $this->check_current_mark(null);
 298          $this->check_current_output(
 299                  $this->get_contains_submit_button_expectation(true),
 300                  $this->get_does_not_contain_feedback_expectation(),
 301                  $this->get_invalid_answer_expectation(),
 302                  $this->get_no_hint_visible_expectation());
 303          $this->check_output_contains_selectoptions(
 304                  $this->get_contains_select_expectation('sub0', $choices, null, true),
 305                  $this->get_contains_select_expectation('sub1', $choices, null, true),
 306                  $this->get_contains_select_expectation('sub2', $choices, null, true),
 307                  $this->get_contains_select_expectation('sub3', $choices, null, true));
 308  
 309          // Now submit the right answer.
 310          $this->process_submission(array('sub0' => $orderforchoice[13],
 311                  'sub1' => $orderforchoice[16], 'sub2' => $orderforchoice[16],
 312                  'sub3' => $orderforchoice[13], '-submit' => '1'));
 313  
 314          // Verify.
 315          $this->check_current_state(question_state::$gradedright);
 316          $this->check_current_mark(4);
 317          $this->check_current_output(
 318                  $this->get_does_not_contain_submit_button_expectation(),
 319                  $this->get_contains_correct_expectation(),
 320                  $this->get_no_hint_visible_expectation());
 321          $this->check_output_contains_selectoptions(
 322                  $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[13], false),
 323                  $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[16], false),
 324                  $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[16], false),
 325                  $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[13], false));
 326      }
 327  
 328      public function test_randomsamatch_clear_wrong() {
 329  
 330          // Create a randomsamatching question.
 331          $m = \test_question_maker::make_question('randomsamatch');
 332          $m->hints = array(
 333              new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, false, true),
 334              new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
 335          );
 336          $m->shufflestems = false;
 337          $this->start_attempt_at_question($m, 'interactive', 4);
 338  
 339          $choiceorder = $m->get_choice_order();
 340          $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
 341          $choices = [];
 342          foreach ($choiceorder as $key => $choice) {
 343              $choices[$key] = $m->choices[$choice];
 344          }
 345  
 346          // Check the initial state.
 347          $this->check_current_state(question_state::$todo);
 348          $this->check_current_mark(null);
 349          $this->check_current_output(
 350                  $this->get_contains_submit_button_expectation(true),
 351                  $this->get_does_not_contain_feedback_expectation(),
 352                  $this->get_tries_remaining_expectation(3),
 353                  $this->get_no_hint_visible_expectation());
 354          $this->check_output_contains_selectoptions(
 355                  $this->get_contains_select_expectation('sub0', $choices, null, true),
 356                  $this->get_contains_select_expectation('sub1', $choices, null, true),
 357                  $this->get_contains_select_expectation('sub2', $choices, null, true),
 358                  $this->get_contains_select_expectation('sub3', $choices, null, true));
 359  
 360          // Submit a completely wrong response.
 361          $this->process_submission(array('sub0' => $orderforchoice[16],
 362                  'sub1' => $orderforchoice[13], 'sub2' => $orderforchoice[13],
 363                  'sub3' => $orderforchoice[16], '-submit' => 1));
 364  
 365          // Verify.
 366          $this->check_current_state(question_state::$todo);
 367          $this->check_current_mark(null);
 368          $this->check_current_output(
 369                  $this->get_contains_hidden_expectation(
 370                          $this->quba->get_field_prefix($this->slot) . 'sub0', '0'),
 371                  $this->get_contains_hidden_expectation(
 372                          $this->quba->get_field_prefix($this->slot) . 'sub1', '0'),
 373                  $this->get_contains_hidden_expectation(
 374                          $this->quba->get_field_prefix($this->slot) . 'sub2', '0'),
 375                  $this->get_contains_hidden_expectation(
 376                          $this->quba->get_field_prefix($this->slot) . 'sub3', '0'),
 377                  $this->get_does_not_contain_submit_button_expectation(),
 378                  $this->get_contains_hint_expectation('This is the first hint.'));
 379          $this->check_output_contains_selectoptions(
 380                  $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[16], false),
 381                  $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[13], false),
 382                  $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[13], false),
 383                  $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[16], false));
 384  
 385          // Try again.
 386          $this->process_submission(array('sub0' => 0,
 387                  'sub1' => 0, 'sub2' => 0,
 388                  'sub3' => 0, '-tryagain' => 1));
 389  
 390          // Verify.
 391          $this->check_current_state(question_state::$todo);
 392          $this->check_current_mark(null);
 393          $this->check_current_output(
 394                  $this->get_contains_submit_button_expectation(true),
 395                  $this->get_does_not_contain_feedback_expectation(),
 396                  $this->get_tries_remaining_expectation(2),
 397                  $this->get_no_hint_visible_expectation());
 398          $this->check_output_contains_selectoptions(
 399                  $this->get_contains_select_expectation('sub0', $choices, null, true),
 400                  $this->get_contains_select_expectation('sub1', $choices, null, true),
 401                  $this->get_contains_select_expectation('sub2', $choices, null, true),
 402                  $this->get_contains_select_expectation('sub3', $choices, null, true));
 403  
 404          // Submit a partially wrong response.
 405          $this->process_submission(array('sub0' => $orderforchoice[16],
 406                  'sub1' => $orderforchoice[13], 'sub2' => $orderforchoice[16],
 407                  'sub3' => $orderforchoice[13], '-submit' => 1));
 408  
 409          // Verify.
 410          $this->check_current_state(question_state::$todo);
 411          $this->check_current_mark(null);
 412          $this->check_current_output(
 413                  $this->get_contains_hidden_expectation(
 414                          $this->quba->get_field_prefix($this->slot) . 'sub0', '0'),
 415                  $this->get_contains_hidden_expectation(
 416                          $this->quba->get_field_prefix($this->slot) . 'sub1', '0'),
 417                  $this->get_contains_hidden_expectation(
 418                          $this->quba->get_field_prefix($this->slot) . 'sub2', $orderforchoice[16]),
 419                  $this->get_contains_hidden_expectation(
 420                          $this->quba->get_field_prefix($this->slot) . 'sub3', $orderforchoice[13]),
 421                  $this->get_does_not_contain_submit_button_expectation(),
 422                  $this->get_contains_hint_expectation('This is the second hint.'));
 423          $this->check_output_contains_selectoptions(
 424                  $this->get_contains_select_expectation('sub0', $choices, $orderforchoice[16], false),
 425                  $this->get_contains_select_expectation('sub1', $choices, $orderforchoice[13], false),
 426                  $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[16], false),
 427                  $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[13], false));
 428  
 429          // Try again.
 430          $this->process_submission(array('sub0' => 0,
 431                  'sub1' => 0, 'sub2' => $orderforchoice[16],
 432                  'sub3' => $orderforchoice[13], '-tryagain' => 1));
 433  
 434          // Verify.
 435          $this->check_current_state(question_state::$todo);
 436          $this->check_current_mark(null);
 437          $this->check_current_output(
 438                  $this->get_contains_submit_button_expectation(true),
 439                  $this->get_does_not_contain_feedback_expectation(),
 440                  $this->get_tries_remaining_expectation(1),
 441                  $this->get_no_hint_visible_expectation());
 442          $this->check_output_contains_selectoptions(
 443                  $this->get_contains_select_expectation('sub0', $choices, null, true),
 444                  $this->get_contains_select_expectation('sub1', $choices, null, true),
 445                  $this->get_contains_select_expectation('sub2', $choices, $orderforchoice[16], true),
 446                  $this->get_contains_select_expectation('sub3', $choices, $orderforchoice[13], true));
 447      }
 448  }