Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 311 and 401]

   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   * @coversDefaultClass \tool_brickfield\local\areas\core_question\base
  32   */
  33  class questiontext_test extends area_test_base {
  34      /**
  35       * Set up before class.
  36       */
  37      public static function setUpBeforeClass(): void {
  38          global $CFG;
  39          require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  40      }
  41  
  42      /**
  43       * Test find course areas.
  44       */
  45      public function test_find_course_areas() {
  46          $this->resetAfterTest();
  47          $this->setAdminUser();
  48  
  49          $category = $this->getDataGenerator()->create_category();
  50          $course = $this->getDataGenerator()->create_course(['category' => $category->id]);
  51          $coursecontext = \context_course::instance($course->id);
  52          $catcontext = \context_coursecat::instance($category->id);
  53          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  54          $cat1 = $generator->create_question_category(['contextid' => $coursecontext->id]);
  55          $question1 = $generator->create_question('multichoice', null, ['category' => $cat1->id]);
  56          $question2 = $generator->create_question('multichoice', null, ['category' => $cat1->id]);
  57          $questiontext = new questiontext();
  58          $rs = $questiontext->find_course_areas($course->id);
  59          $this->assertNotNull($rs);
  60  
  61          $count = 0;
  62          foreach ($rs as $rec) {
  63              $count++;
  64              $this->assertEquals($coursecontext->id, $rec->contextid);
  65              $this->assertEquals($course->id, $rec->courseid);
  66              if ($count <= 1) {
  67                  $this->assertEquals($question1->id, $rec->itemid);
  68              } else {
  69                  $this->assertEquals($question2->id, $rec->itemid);
  70              }
  71          }
  72          $rs->close();
  73          $this->assertEquals(2, $count);
  74  
  75          // Add a question to a quiz in the course.
  76          $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id, 'name' => 'Quiz1']);
  77          $quizmodule = get_coursemodule_from_instance('quiz', $quiz->id, $course->id);
  78          $quizcontext = \context_module::instance($quizmodule->id);
  79  
  80          // Add a question to the quiz context.
  81          $cat2 = $generator->create_question_category(['contextid' => $quizcontext->id]);
  82          $question3 = $generator->create_question('multichoice', null, ['category' => $cat2->id]);
  83          $rs2 = $questiontext->find_course_areas($course->id);
  84          $this->assertNotNull($rs2);
  85  
  86          $count = 0;
  87          foreach ($rs2 as $rec) {
  88              $count++;
  89              if ($count <= 1) {
  90                  $this->assertEquals($coursecontext->id, $rec->contextid);
  91                  $this->assertEquals($course->id, $rec->courseid);
  92                  $this->assertEquals($question1->id, $rec->itemid);
  93              } else if ($count <= 2) {
  94                  $this->assertEquals($coursecontext->id, $rec->contextid);
  95                  $this->assertEquals($course->id, $rec->courseid);
  96                  $this->assertEquals($question2->id, $rec->itemid);
  97              } else {
  98                  $this->assertEquals($quizcontext->id, $rec->contextid);
  99                  $this->assertEquals($course->id, $rec->courseid);
 100                  $this->assertEquals($question3->id, $rec->itemid);
 101              }
 102          }
 103          $rs2->close();
 104          $this->assertEquals(3, $count);
 105  
 106          // Add a question to the category context.
 107          $cat3 = $generator->create_question_category(['contextid' => $catcontext->id]);
 108          $question4 = $generator->create_question('multichoice', null, ['category' => $cat3->id]);
 109          $rs3 = $questiontext->find_course_areas($course->id);
 110          $this->assertNotNull($rs3);
 111  
 112          // The category level questions should not be found.
 113          $count = 0;
 114          foreach ($rs3 as $rec) {
 115              $count++;
 116              if ($count > 2) {
 117                  $this->assertEquals($quizcontext->id, $rec->contextid);
 118                  $this->assertEquals($course->id, $rec->courseid);
 119                  $this->assertEquals($question3->id, $rec->itemid);
 120              }
 121          }
 122          $rs2->close();
 123          $this->assertEquals(3, $count);
 124      }
 125  
 126      /**
 127       * Test find relevant areas.
 128       */
 129      public function test_find_relevant_areas() {
 130          $this->resetAfterTest();
 131          $this->setAdminUser();
 132  
 133          $course = $this->getDataGenerator()->create_course();
 134          $coursecontext = \context_course::instance($course->id);
 135          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 136          $cat1 = $generator->create_question_category(['contextid' => $coursecontext->id]);
 137          $question1 = $generator->create_question('multichoice', null, ['category' => $cat1->id]);
 138          $question2 = $generator->create_question('multichoice', null, ['category' => $cat1->id]);
 139          $questiontext = new questiontext();
 140          $event = \core\event\question_updated::create_from_question_instance($question1,
 141              \context_course::instance($course->id));
 142          $rs = $questiontext->find_relevant_areas($event);
 143          $this->assertNotNull($rs);
 144  
 145          $count = 0;
 146          foreach ($rs as $rec) {
 147              $count++;
 148              $this->assertEquals($coursecontext->id, $rec->contextid);
 149              $this->assertEquals($course->id, $rec->courseid);
 150              $this->assertEquals($question1->id, $rec->itemid);
 151          }
 152          $rs->close();
 153          $this->assertEquals(1, $count);
 154      }
 155  
 156      /**
 157       * Test find system areas.
 158       */
 159      public function test_find_system_areas() {
 160          $this->resetAfterTest();
 161          $this->setAdminUser();
 162  
 163          $category = $this->getDataGenerator()->create_category();
 164          $catcontext = \context_coursecat::instance($category->id);
 165          $systemcontext = \context_system::instance();
 166          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 167          $component = 'core_question';
 168  
 169          $cat = $generator->create_question_category(['contextid' => $catcontext->id]);
 170          $cat2 = $generator->create_question_category(['contextid' => $systemcontext->id]);
 171          $question = $generator->create_question('multichoice', null, ['category' => $cat2->id]);
 172          $question2 = $generator->create_question('multichoice', null, ['category' => $cat->id]);
 173          $questiontext = new questiontext();
 174          $areas = $this->array_from_recordset($questiontext->find_system_areas());
 175  
 176          // Assert the core_question area exists for the individual question's context, courseid and categoryid.
 177          $this->assert_area_in_array(
 178              $areas,
 179              $component,
 180              $systemcontext->id,
 181              $question->id,
 182              SITEID,
 183              null
 184          );
 185          $this->assert_area_in_array(
 186              $areas,
 187              $component,
 188              $catcontext->id,
 189              $question2->id,
 190              SITEID,
 191              $category->id
 192          );
 193      }
 194  
 195      /**
 196       * Test get course and category.
 197       *
 198       * @covers ::get_course_and_category
 199       */
 200      public function test_get_course_and_category() {
 201          $this->resetAfterTest();
 202          $this->setAdminUser();
 203  
 204          $course = $this->getDataGenerator()->create_course();
 205          $coursecontext = \context_course::instance($course->id);
 206          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 207          $cat1 = $generator->create_question_category(['contextid' => $coursecontext->id]);
 208          $question1 = $generator->create_question('multichoice', null, ['category' => $cat1->id]);
 209          $event = \core\event\question_updated::create_from_question_instance($question1,
 210              \context_course::instance($course->id));
 211          $rs = base::get_course_and_category(CONTEXT_COURSE, $event->objectid);
 212          $this->assertNotNull($rs);
 213          $this->assertEquals(CONTEXT_COURSE, $rs->contextlevel);
 214          $this->assertNotEquals(CONTEXT_MODULE, $rs->contextlevel);
 215          // Invalid objectid.
 216          $rs = base::get_course_and_category(CONTEXT_COURSE, 0);
 217          $this->assertFalse($rs);
 218          // Incorrect objectid.
 219          $rs = base::get_course_and_category(CONTEXT_COURSE, 100);
 220          $this->assertFalse($rs);
 221      }
 222  }