Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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