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.
   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 advanced_testcase;
  20  use context_system;
  21  use core_question\local\bank\question_version_status;
  22  use core_question_generator;
  23  
  24  /**
  25   * Unit tests for the {@see question_reference_manager} class.
  26   *
  27   * @package   core_question
  28   * @category  test
  29   * @copyright 2011 The Open University
  30   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   * @covers \core_question\question_reference_manager
  32   */
  33  class question_reference_manager_test extends advanced_testcase {
  34  
  35      public function test_questions_with_references() {
  36          global $DB;
  37          $this->resetAfterTest();
  38  
  39          /** @var core_question_generator $questiongenerator */
  40          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
  41          $systemcontext = context_system::instance();
  42  
  43          // Create three questions, each with three versions.
  44          // In each case, the third version is draft.
  45          $cat = $questiongenerator->create_question_category();
  46          $q1v1 = $questiongenerator->create_question('truefalse', null, ['name' => 'Q1V1', 'category' => $cat->id]);
  47          $q1v2 = $questiongenerator->update_question($q1v1, null, ['name' => 'Q1V2']);
  48          $q1v3 = $questiongenerator->update_question($q1v2, null,
  49                  ['name' => 'Q1V3', 'status' => question_version_status::QUESTION_STATUS_DRAFT]);
  50          $q2v1 = $questiongenerator->create_question('truefalse', null, ['name' => 'Q2V1', 'category' => $cat->id]);
  51          $q2v2 = $questiongenerator->update_question($q2v1, null, ['name' => 'Q2V2']);
  52          $q2v3 = $questiongenerator->update_question($q2v2, null,
  53                  ['name' => 'Q2V3', 'status' => question_version_status::QUESTION_STATUS_DRAFT]);
  54          $q3v1 = $questiongenerator->create_question('truefalse', null, ['name' => 'Q3V1', 'category' => $cat->id]);
  55          $q3v2 = $questiongenerator->update_question($q3v1, null, ['name' => 'Q3V2']);
  56          $q3v3 = $questiongenerator->update_question($q3v2, null,
  57                  ['name' => 'Q3V3', 'status' => question_version_status::QUESTION_STATUS_DRAFT]);
  58  
  59          // Create specific references to Q2V1 and Q2V3.
  60          $DB->insert_record('question_references', ['usingcontextid' => $systemcontext->id,
  61                  'component' => 'core_question', 'questionarea' => 'test', 'itemid' => 0,
  62                  'questionbankentryid' => $q2v1->questionbankentryid, 'version' => 1]);
  63          $DB->insert_record('question_references', ['usingcontextid' => $systemcontext->id,
  64                  'component' => 'core_question', 'questionarea' => 'test', 'itemid' => 1,
  65                  'questionbankentryid' => $q2v1->questionbankentryid, 'version' => 3]);
  66  
  67          // Create an always-latest reference to Q3.
  68          $DB->insert_record('question_references', ['usingcontextid' => $systemcontext->id,
  69                  'component' => 'core_question', 'questionarea' => 'test', 'itemid' => 2,
  70                  'questionbankentryid' => $q3v1->questionbankentryid, 'version' => null]);
  71  
  72          // Verify which versions of Q1 are used.
  73          $this->assertEqualsCanonicalizing([],
  74                  question_reference_manager::questions_with_references([$q1v1->id]));
  75          $this->assertEqualsCanonicalizing([],
  76                  question_reference_manager::questions_with_references([$q1v2->id]));
  77          $this->assertEqualsCanonicalizing([],
  78                  question_reference_manager::questions_with_references([$q1v3->id]));
  79          $this->assertEqualsCanonicalizing([],
  80                  question_reference_manager::questions_with_references([$q1v1->id, $q1v2->id, $q1v3->id]));
  81  
  82          // Verify which versions of Q2 are used.
  83          $this->assertEqualsCanonicalizing([$q2v1->id],
  84                  question_reference_manager::questions_with_references([$q2v1->id]));
  85          $this->assertEqualsCanonicalizing([],
  86                  question_reference_manager::questions_with_references([$q2v2->id]));
  87          $this->assertEqualsCanonicalizing([$q2v3->id],
  88                  question_reference_manager::questions_with_references([$q2v3->id]));
  89          $this->assertEqualsCanonicalizing([$q2v1->id, $q2v3->id],
  90                  question_reference_manager::questions_with_references([$q2v1->id, $q2v2->id, $q2v3->id]));
  91  
  92          // Verify which versions of Q1 are used.
  93          $this->assertEqualsCanonicalizing([],
  94                  question_reference_manager::questions_with_references([$q3v1->id]));
  95          $this->assertEqualsCanonicalizing([$q3v2->id],
  96                  question_reference_manager::questions_with_references([$q3v2->id]));
  97          $this->assertEqualsCanonicalizing([],
  98                  question_reference_manager::questions_with_references([$q3v3->id]));
  99          $this->assertEqualsCanonicalizing([$q3v2->id],
 100                  question_reference_manager::questions_with_references([$q3v1->id, $q3v2->id, $q3v3->id]));
 101  
 102          // Do some combined queries.
 103          $this->assertEqualsCanonicalizing([$q2v1->id, $q2v3->id, $q3v2->id],
 104                  question_reference_manager::questions_with_references([
 105                          $q1v1->id, $q1v2->id, $q1v3->id,
 106                          $q2v1->id, $q2v2->id, $q2v3->id,
 107                          $q3v1->id, $q3v2->id, $q3v3->id]));
 108          $this->assertEqualsCanonicalizing([$q2v1->id, $q2v3->id, $q3v2->id],
 109                  question_reference_manager::questions_with_references([$q2v1->id, $q2v3->id, $q3v2->id]));
 110          $this->assertEqualsCanonicalizing([],
 111                  question_reference_manager::questions_with_references([
 112                          $q1v1->id, $q1v2->id, $q1v3->id,
 113                          $q2v2->id,
 114                          $q3v1->id, $q3v3->id]));
 115  
 116          // Test some edge cases.
 117          $this->assertEqualsCanonicalizing([],
 118                  question_reference_manager::questions_with_references([]));
 119          $this->assertEqualsCanonicalizing([],
 120                  question_reference_manager::questions_with_references([-1]));
 121  
 122      }
 123  }