Search moodle.org's
Developer Documentation

See Release Notes

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

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

   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 core_question;
  18  
  19  use question_bank;
  20  use question_state;
  21  use question_test_recordset;
  22  use question_usage_by_activity;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once (__DIR__ . '/../lib.php');
  28  require_once (__DIR__ . '/helpers.php');
  29  
  30  /**
  31   * Unit tests for loading data into the {@link question_usage_by_activity} class.
  32   *
  33   * @package   core_question
  34   * @copyright 2012 The Open University
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class questionusagebyactivity_data_test extends \data_loading_method_test_base {
  38      public function test_load() {
  39          $scid = \context_system::instance()->id;
  40          $records = new question_test_recordset(array(
  41          array('qubaid', 'contextid', 'component', 'preferredbehaviour',
  42                                                 'questionattemptid', 'questionusageid', 'slot',
  43                                                                'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged',
  44                                                                                                               'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
  45                                                                                                                                       'attemptstepid', 'sequencenumber', 'state', 'fraction',
  46                                                                                                                                                                       'timecreated', 'userid', 'name', 'value'),
  47          array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '', 1256233790, 1, 0, 'todo',             null, 1256233700, 1,       null, null),
  48          array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '', 1256233790, 2, 1, 'todo',             null, 1256233705, 1,   'answer',  '1'),
  49          array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '', 1256233790, 5, 2, 'gradedright', 1.0000000, 1256233720, 1,  '-finish',  '1'),
  50          ));
  51  
  52          $question = \test_question_maker::make_question('truefalse', 'true');
  53          $question->id = -1;
  54  
  55          question_bank::start_unit_test();
  56          question_bank::load_test_question_data($question);
  57          $quba = question_usage_by_activity::load_from_records($records, 1);
  58          question_bank::end_unit_test();
  59  
  60          $this->assertEquals('unit_test', $quba->get_owning_component());
  61          $this->assertEquals(1, $quba->get_id());
  62          $this->assertInstanceOf('question_engine_unit_of_work', $quba->get_observer());
  63          $this->assertEquals('interactive', $quba->get_preferred_behaviour());
  64  
  65          $qa = $quba->get_question_attempt(1);
  66  
  67          $this->assertEquals($question->questiontext, $qa->get_question(false)->questiontext);
  68  
  69          $this->assertEquals(3, $qa->get_num_steps());
  70  
  71          $step = $qa->get_step(0);
  72          $this->assertEquals(question_state::$todo, $step->get_state());
  73          $this->assertNull($step->get_fraction());
  74          $this->assertEquals(1256233700, $step->get_timecreated());
  75          $this->assertEquals(1, $step->get_user_id());
  76          $this->assertEquals(array(), $step->get_all_data());
  77  
  78          $step = $qa->get_step(1);
  79          $this->assertEquals(question_state::$todo, $step->get_state());
  80          $this->assertNull($step->get_fraction());
  81          $this->assertEquals(1256233705, $step->get_timecreated());
  82          $this->assertEquals(1, $step->get_user_id());
  83          $this->assertEquals(array('answer' => '1'), $step->get_all_data());
  84  
  85          $step = $qa->get_step(2);
  86          $this->assertEquals(question_state::$gradedright, $step->get_state());
  87          $this->assertEquals(1, $step->get_fraction());
  88          $this->assertEquals(1256233720, $step->get_timecreated());
  89          $this->assertEquals(1, $step->get_user_id());
  90          $this->assertEquals(array('-finish' => '1'), $step->get_all_data());
  91      }
  92  
  93      public function test_load_data_no_steps() {
  94          // The code had a bug where if one question_attempt had no steps,
  95          // load_from_records got stuck in an infinite loop. This test is to
  96          // verify that no longer happens.
  97          $scid = \context_system::instance()->id;
  98          $records = new question_test_recordset(array(
  99          array('qubaid', 'contextid', 'component', 'preferredbehaviour',
 100                                                     'questionattemptid', 'questionusageid', 'slot',
 101                                                               'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged',
 102                                                                                                              'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
 103                                                                                                                                                                                 'attemptstepid', 'sequencenumber', 'state', 'fraction',
 104                                                                                                                                                                                                           'timecreated', 'userid', 'name', 'value'),
 105          array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', 0, 1, 1.0000000, 0.0000000, 1.0000000, 0, 'This question is missing. Unable to display anything.', '', '', 0, null, null, null, null, null, null, null, null),
 106          array(1, $scid, 'unit_test', 'interactive', 2, 1, 2, 'interactive', 0, 1, 1.0000000, 0.0000000, 1.0000000, 0, 'This question is missing. Unable to display anything.', '', '', 0, null, null, null, null, null, null, null, null),
 107          array(1, $scid, 'unit_test', 'interactive', 3, 1, 3, 'interactive', 0, 1, 1.0000000, 0.0000000, 1.0000000, 0, 'This question is missing. Unable to display anything.', '', '', 0, null, null, null, null, null, null, null, null),
 108          ));
 109  
 110          question_bank::start_unit_test();
 111          $quba = question_usage_by_activity::load_from_records($records, 1);
 112          question_bank::end_unit_test();
 113  
 114          $this->assertEquals('unit_test', $quba->get_owning_component());
 115          $this->assertEquals(1, $quba->get_id());
 116          $this->assertInstanceOf('question_engine_unit_of_work', $quba->get_observer());
 117          $this->assertEquals('interactive', $quba->get_preferred_behaviour());
 118  
 119          $this->assertEquals(array(1, 2, 3), $quba->get_slots());
 120  
 121          $qa = $quba->get_question_attempt(1);
 122          $this->assertEquals(0, $qa->get_num_steps());
 123      }
 124  
 125      public function test_load_data_no_qas() {
 126          // The code had a bug where if a question_usage had no question_attempts,
 127          // load_from_records got stuck in an infinite loop. This test is to
 128          // verify that no longer happens.
 129          $scid = \context_system::instance()->id;
 130          $records = new question_test_recordset(array(
 131          array('qubaid', 'contextid', 'component', 'preferredbehaviour',
 132                                                     'questionattemptid', 'questionusageid', 'slot',
 133                                                                          'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged',
 134                                                                                                                 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
 135                                                                                                                                           'attemptstepid', 'sequencenumber', 'state', 'fraction',
 136                                                                                                                                                                     'timecreated', 'userid', 'name', 'value'),
 137          array(1, $scid, 'unit_test', 'interactive', null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null),
 138          ));
 139  
 140          question_bank::start_unit_test();
 141          $quba = question_usage_by_activity::load_from_records($records, 1);
 142          question_bank::end_unit_test();
 143  
 144          $this->assertEquals('unit_test', $quba->get_owning_component());
 145          $this->assertEquals(1, $quba->get_id());
 146          $this->assertInstanceOf('question_engine_unit_of_work', $quba->get_observer());
 147          $this->assertEquals('interactive', $quba->get_preferred_behaviour());
 148  
 149          $this->assertEquals(array(), $quba->get_slots());
 150      }
 151  }