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 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 39 and 311]

   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\bank\view;
  20  use question_edit_contexts;
  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          $view->display('editq', 0, 20, $cat->id . ',' . $context->id, false, false, false);
  62          $html = ob_get_clean();
  63  
  64          // Verify the output includes the expected question.
  65          $this->assertStringContainsString('Example question', $html);
  66  
  67          // Verify the question has not been loaded into the cache.
  68          $this->assertFalse($cache->has($questiondata->id));
  69      }
  70  
  71      public function test_unknown_qtype_does_not_break_view() {
  72          global $DB;
  73          $this->resetAfterTest();
  74          $this->setAdminUser();
  75          $generator = $this->getDataGenerator();
  76          /** @var core_question_generator $questiongenerator */
  77          $questiongenerator = $generator->get_plugin_generator('core_question');
  78  
  79          // Create a course.
  80          $course = $generator->create_course();
  81          $context = \context_course::instance($course->id);
  82  
  83          // Create a question in the default category.
  84          $contexts = new question_edit_contexts($context);
  85          $cat = question_make_default_categories($contexts->all());
  86          $questiondata = $questiongenerator->create_question('numerical', null,
  87                  ['name' => 'Example question', 'category' => $cat->id]);
  88          $DB->set_field('question', 'qtype', 'unknownqtype', ['id' => $questiondata->id]);
  89  
  90          $view = new view($contexts, new \moodle_url('/'), $course);
  91          ob_start();
  92          $view->display('editq', 0, 20, $cat->id . ',' . $context->id, false, false, false);
  93          $html = ob_get_clean();
  94  
  95          // Mainly we are verifying that there was no fatal error.
  96  
  97          // Verify the output includes the expected question.
  98          $this->assertStringContainsString('Example question', $html);
  99      }
 100  }