[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/match/tests/ -> walkthrough_test.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1