Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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