Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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

   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 qbehaviour_missing;
  18  
  19  use qbehaviour_missing;
  20  use question_attempt;
  21  use question_attempt_pending_step;
  22  use question_attempt_step;
  23  use question_bank;
  24  use question_display_options;
  25  use question_state;
  26  use question_test_recordset;
  27  use question_usage_null_observer;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  require_once (__DIR__ . '/../../../engine/lib.php');
  33  require_once (__DIR__ . '/../../../engine/tests/helpers.php');
  34  require_once (__DIR__ . '/../behaviour.php');
  35  
  36  /**
  37   * Unit tests for the 'missing' behaviour.
  38   *
  39   * @package    qbehaviour_missing
  40   * @copyright  2009 The Open University
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class missingbehaviour_test extends \advanced_testcase {
  44  
  45      public function test_missing_cannot_start() {
  46          $qa = new question_attempt(\test_question_maker::make_question('truefalse', 'true'), 0);
  47          $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
  48          $this->expectException(\moodle_exception::class);
  49          $behaviour->init_first_step(new question_attempt_step(array()), 1);
  50      }
  51  
  52      public function test_missing_cannot_process() {
  53          $qa = new question_attempt(\test_question_maker::make_question('truefalse', 'true'), 0);
  54          $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
  55          $this->expectException(\moodle_exception::class);
  56          $behaviour->process_action(new question_attempt_pending_step(array()));
  57      }
  58  
  59      public function test_missing_cannot_get_min_fraction() {
  60          $qa = new question_attempt(\test_question_maker::make_question('truefalse', 'true'), 0);
  61          $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
  62          $this->expectException(\moodle_exception::class);
  63          $behaviour->get_min_fraction();
  64      }
  65  
  66      public function test_missing_cannot_get_max_fraction() {
  67          $qa = new question_attempt(\test_question_maker::make_question('truefalse', 'true'), 0);
  68          $behaviour = new qbehaviour_missing($qa, 'deferredfeedback');
  69          $this->expectException(\moodle_exception::class);
  70          $behaviour->get_max_fraction();
  71      }
  72  
  73      public function test_render_missing() {
  74          $records = new question_test_recordset(array(
  75              array('questionattemptid', 'contextid', 'questionusageid', 'slot',
  76                                     'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged',
  77                                              'questionsummary', 'rightanswer', 'responsesummary',
  78                      'timemodified', 'attemptstepid', 'sequencenumber', 'state', 'fraction',
  79                                                         'timecreated', 'userid', 'name', 'value'),
  80              array(1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '',
  81                      1256233790, 1, 0, 'todo',     null, 1256233700, 1,   '_order', '1,2,3'),
  82              array(1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '',
  83                      1256233790, 2, 1, 'complete', 0.50, 1256233705, 1,  '-submit',  '1'),
  84              array(1, 123, 1, 1, 'strangeunknown', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '',
  85                      1256233790, 2, 1, 'complete', 0.50, 1256233705, 1,  'choice0',  '1'),
  86          ));
  87  
  88          $question = \test_question_maker::make_question('truefalse', 'true');
  89          $question->id = -1;
  90  
  91          question_bank::start_unit_test();
  92          question_bank::load_test_question_data($question);
  93          $qa = question_attempt::load_from_records($records, 1,
  94                  new question_usage_null_observer(), 'deferredfeedback');
  95          question_bank::end_unit_test();
  96  
  97          $this->assertEquals(2, $qa->get_num_steps());
  98  
  99          $step = $qa->get_step(0);
 100          $this->assertEquals(question_state::$todo, $step->get_state());
 101          $this->assertNull($step->get_fraction());
 102          $this->assertEquals(1256233700, $step->get_timecreated());
 103          $this->assertEquals(1, $step->get_user_id());
 104          $this->assertEquals(array('_order' => '1,2,3'), $step->get_all_data());
 105  
 106          $step = $qa->get_step(1);
 107          $this->assertEquals(question_state::$complete, $step->get_state());
 108          $this->assertEquals(0.5, $step->get_fraction());
 109          $this->assertEquals(1256233705, $step->get_timecreated());
 110          $this->assertEquals(1, $step->get_user_id());
 111          $this->assertEquals(array('-submit' => '1', 'choice0' => '1'), $step->get_all_data());
 112  
 113          $output = $qa->render(new question_display_options(), '1');
 114          $this->assertMatchesRegularExpression('/' . preg_quote($qa->get_question(false)->questiontext, '/') . '/', $output);
 115          $this->assertMatchesRegularExpression('/' . preg_quote(
 116                  get_string('questionusedunknownmodel', 'qbehaviour_missing'), '/') . '/', $output);
 117          $this->assertTag(array('tag'=>'div', 'attributes'=>array('class'=>'warning')), $output);
 118      }
 119  }