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 tool_brickfield\local\areas\core_question;
  18  
  19  /**
  20   * Tests for questionanswer.
  21   *
  22   * @package     tool_brickfield
  23   * @copyright   2020 onward: Brickfield Education Labs, https://www.brickfield.ie
  24   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class questionanswers_test extends \advanced_testcase {
  27  
  28      /**
  29       * Set up before class.
  30       */
  31      public static function setUpBeforeClass(): void {
  32          global $CFG;
  33          require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  34      }
  35  
  36      /**
  37       * Test find course areas.
  38       */
  39      public function test_find_course_areas() {
  40          $this->resetAfterTest();
  41          $this->setAdminUser();
  42  
  43          $category = $this->getDataGenerator()->create_category();
  44          $course = $this->getDataGenerator()->create_course(['category' => $category->id]);
  45          $coursecontext = \context_course::instance($course->id);
  46          $catcontext = \context_coursecat::instance($category->id);
  47          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  48          $cat1 = $generator->create_question_category(['contextid' => $coursecontext->id]);
  49          $question1 = $generator->create_question('multichoice', null, ['category' => $cat1->id]);
  50          $question2 = $generator->create_question('multichoice', null, ['category' => $cat1->id]);
  51          $questionanswers = new questionanswers();
  52          $rs = $questionanswers->find_course_areas($course->id);
  53          $this->assertNotNull($rs);
  54  
  55          // Each multichoice question generated has four answers. So there should be eight records.
  56          $count = 0;
  57          foreach ($rs as $rec) {
  58              $count++;
  59              $this->assertEquals($coursecontext->id, $rec->contextid);
  60              $this->assertEquals($course->id, $rec->courseid);
  61              if ($count <= 4) {
  62                  $this->assertEquals($question1->id, $rec->refid);
  63              } else {
  64                  $this->assertEquals($question2->id, $rec->refid);
  65              }
  66          }
  67          $rs->close();
  68          $this->assertEquals(8, $count);
  69  
  70          // Add a question to a quiz in the course.
  71          $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id, 'name' => 'Quiz1']);
  72          $quizmodule = get_coursemodule_from_instance('quiz', $quiz->id, $course->id);
  73          $quizcontext = \context_module::instance($quizmodule->id);
  74  
  75          // Add a question to the quiz context.
  76          $cat2 = $generator->create_question_category(['contextid' => $quizcontext->id]);
  77          $question3 = $generator->create_question('multichoice', null, ['category' => $cat2->id]);
  78          $rs2 = $questionanswers->find_course_areas($course->id);
  79          $this->assertNotNull($rs2);
  80  
  81          // Each multichoice question generated has four answers. So there should be twelve records now.
  82          $count = 0;
  83          foreach ($rs2 as $rec) {
  84              $count++;
  85              if ($count <= 4) {
  86                  $this->assertEquals($coursecontext->id, $rec->contextid);
  87                  $this->assertEquals($course->id, $rec->courseid);
  88                  $this->assertEquals($question1->id, $rec->refid);
  89              } else if ($count <= 8) {
  90                  $this->assertEquals($coursecontext->id, $rec->contextid);
  91                  $this->assertEquals($course->id, $rec->courseid);
  92                  $this->assertEquals($question2->id, $rec->refid);
  93              } else {
  94                  $this->assertEquals($quizcontext->id, $rec->contextid);
  95                  $this->assertEquals($course->id, $rec->courseid);
  96                  $this->assertEquals($question3->id, $rec->refid);
  97              }
  98          }
  99          $rs2->close();
 100          $this->assertEquals(12, $count);
 101  
 102          // Add a question to the category context.
 103          $cat3 = $generator->create_question_category(['contextid' => $catcontext->id]);
 104          $question4 = $generator->create_question('multichoice', null, ['category' => $cat3->id]);
 105          $rs3 = $questionanswers->find_course_areas($course->id);
 106          $this->assertNotNull($rs3);
 107  
 108          // The category level questions should not be found.
 109          $count = 0;
 110          foreach ($rs3 as $rec) {
 111              $count++;
 112              if ($count > 8) {
 113                  $this->assertEquals($quizcontext->id, $rec->contextid);
 114                  $this->assertEquals($course->id, $rec->courseid);
 115                  $this->assertEquals($question3->id, $rec->refid);
 116              }
 117          }
 118          $rs2->close();
 119          $this->assertEquals(12, $count);
 120      }
 121  
 122      /**
 123       * Test find relevant areas.
 124       */
 125      public function test_find_relevant_areas() {
 126          $this->resetAfterTest();
 127          $this->setAdminUser();
 128  
 129          $course = $this->getDataGenerator()->create_course();
 130          $coursecontext = \context_course::instance($course->id);
 131          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 132          $cat1 = $generator->create_question_category(['contextid' => $coursecontext->id]);
 133          $question1 = $generator->create_question('multichoice', null, ['category' => $cat1->id]);
 134          $question2 = $generator->create_question('multichoice', null, ['category' => $cat1->id]);
 135          $questionanswers = new questionanswers();
 136          $event = \core\event\question_updated::create_from_question_instance($question1,
 137              \context_course::instance($course->id));
 138          $rs = $questionanswers->find_relevant_areas($event);
 139          $this->assertNotNull($rs);
 140  
 141          // Each multichoice question generated has four answers.
 142          $count = 0;
 143          foreach ($rs as $rec) {
 144              $count++;
 145              $this->assertEquals($coursecontext->id, $rec->contextid);
 146              $this->assertEquals($course->id, $rec->courseid);
 147              $this->assertEquals($question1->id, $rec->refid);
 148          }
 149          $rs->close();
 150          $this->assertEquals(4, $count);
 151      }
 152  
 153      /**
 154       * Test find system areas.
 155       */
 156      public function test_find_system_areas() {
 157          $this->resetAfterTest();
 158          $this->setAdminUser();
 159  
 160          $category = $this->getDataGenerator()->create_category();
 161          $catcontext = \context_coursecat::instance($category->id);
 162          $systemcontext = \context_system::instance();
 163          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 164          $cat = $generator->create_question_category(['contextid' => $catcontext->id]);
 165          $cat2 = $generator->create_question_category(['contextid' => $systemcontext->id]);
 166          $question = $generator->create_question('multichoice', null, ['category' => $cat2->id]);
 167          $question2 = $generator->create_question('multichoice', null, ['category' => $cat->id]);
 168          $questionanswers = new questionanswers();
 169          $rs = $questionanswers->find_system_areas();
 170          $this->assertNotNull($rs);
 171  
 172          // Each multichoice question generated has four answers.
 173          $count = 0;
 174          foreach ($rs as $rec) {
 175              $count++;
 176              if ($count <= 4) {
 177                  $this->assertEquals($systemcontext->id, $rec->contextid);
 178                  $this->assertEquals(1, $rec->courseid);
 179                  $this->assertEquals(0, $rec->categoryid);
 180                  $this->assertEquals($question->id, $rec->refid);
 181              } else {
 182                  $this->assertEquals($catcontext->id, $rec->contextid);
 183                  $this->assertEquals(1, $rec->courseid);
 184                  $this->assertEquals($category->id, $rec->categoryid);
 185                  $this->assertEquals($question2->id, $rec->refid);
 186              }
 187          }
 188          $rs->close();
 189          $this->assertEquals(8, $count);
 190      }
 191  }