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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 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  
  17  namespace core_question;
  18  
  19  use core_question\local\bank\question_edit_contexts;
  20  use core_question\local\bank\view;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/question/editlib.php');
  26  
  27  /**
  28   * Unit tests for the question bank view class.
  29   *
  30   * @package    core_question
  31   * @category   test
  32   * @copyright  2018 the Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class bank_view_test extends \advanced_testcase {
  36  
  37      public function test_viewing_question_bank_should_not_load_individual_questions() {
  38          $this->resetAfterTest();
  39          $this->setAdminUser();
  40          $generator = $this->getDataGenerator();
  41          /** @var core_question_generator $questiongenerator */
  42          $questiongenerator = $generator->get_plugin_generator('core_question');
  43  
  44          // Create a course.
  45          $course = $generator->create_course();
  46          $context = \context_course::instance($course->id);
  47  
  48          // Create a question in the default category.
  49          $contexts = new question_edit_contexts($context);
  50          $cat = question_make_default_categories($contexts->all());
  51          $questiondata = $questiongenerator->create_question('numerical', null,
  52                  ['name' => 'Example question', 'category' => $cat->id]);
  53  
  54          // Ensure the question is not in the cache.
  55          $cache = \cache::make('core', 'questiondata');
  56          $cache->delete($questiondata->id);
  57  
  58          // Generate the view.
  59          $view = new view($contexts, new \moodle_url('/'), $course);
  60          ob_start();
  61          $pagevars = [
  62              'qpage' => 0,
  63              'qperpage' => DEFAULT_QUESTIONS_PER_PAGE,
  64              'cat' => $cat->id . ',' . $context->id,
  65              'recurse' => false,
  66              'showhidden' => false,
  67              'qbshowtext' => false
  68  
  69          ];
  70          $view->display($pagevars, 'editq');
  71          $html = ob_get_clean();
  72  
  73          // Verify the output includes the expected question.
  74          $this->assertStringContainsString('Example question', $html);
  75  
  76          // Verify the question has not been loaded into the cache.
  77          $this->assertFalse($cache->has($questiondata->id));
  78      }
  79  
  80      public function test_unknown_qtype_does_not_break_view() {
  81          global $DB;
  82          $this->resetAfterTest();
  83          $this->setAdminUser();
  84          $generator = $this->getDataGenerator();
  85          /** @var core_question_generator $questiongenerator */
  86          $questiongenerator = $generator->get_plugin_generator('core_question');
  87  
  88          // Create a course.
  89          $course = $generator->create_course();
  90          $context = \context_course::instance($course->id);
  91  
  92          // Create a question in the default category.
  93          $contexts = new question_edit_contexts($context);
  94          $cat = question_make_default_categories($contexts->all());
  95          $questiondata = $questiongenerator->create_question('numerical', null,
  96                  ['name' => 'Example question', 'category' => $cat->id]);
  97          $DB->set_field('question', 'qtype', 'unknownqtype', ['id' => $questiondata->id]);
  98  
  99          // Generate the view.
 100          $view = new view($contexts, new \moodle_url('/'), $course);
 101          ob_start();
 102          $pagevars = [
 103              'qpage' => 0,
 104              'qperpage' => DEFAULT_QUESTIONS_PER_PAGE,
 105              'cat' => $cat->id . ',' . $context->id,
 106              'recurse' => false,
 107              'showhidden' => false,
 108              'qbshowtext' => false
 109  
 110          ];
 111          $view->display($pagevars, 'editq');
 112          $html = ob_get_clean();
 113  
 114          // Mainly we are verifying that there was no fatal error.
 115  
 116          // Verify the output includes the expected question.
 117          $this->assertStringContainsString('Example question', $html);
 118      }
 119  }