Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 310]

   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  /**
  18   * Events tests.
  19   *
  20   * @package core_question
  21   * @copyright 2019 the Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  
  29  require_once($CFG->dirroot . '/question/editlib.php');
  30  require_once($CFG->dirroot . '/question/category_class.php');
  31  
  32  class core_question_category_class_testcase extends advanced_testcase {
  33  
  34      /**
  35       * @var question_category_object used in the tests.
  36       */
  37      protected $qcobject;
  38  
  39      /**
  40       * @var context a context to use.
  41       */
  42      protected $context;
  43  
  44      /**
  45       * @var stdClass top category in context.
  46       */
  47      protected $topcat;
  48  
  49      protected function setUp(): void {
  50          parent::setUp();
  51          self::setAdminUser();
  52          $this->resetAfterTest();
  53          $this->context = context_course::instance(SITEID);
  54          $contexts = new question_edit_contexts($this->context);
  55          $this->topcat = question_get_top_category($this->context->id, true);
  56          $this->qcobject = new question_category_object(null,
  57                  new moodle_url('/question/category.php', ['courseid' => SITEID]),
  58                  $contexts->having_one_edit_tab_cap('categories'), 0, null, 0,
  59                  $contexts->having_cap('moodle/question:add'));
  60      }
  61  
  62      /**
  63       * Test creating a category.
  64       */
  65      public function test_add_category_no_idnumber() {
  66          global $DB;
  67  
  68          $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid,
  69                  'New category', '', true, FORMAT_HTML, ''); // No idnumber passed as '' to match form data.
  70  
  71          $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST);
  72          $this->assertSame('New category', $newcat->name);
  73          $this->assertNull($newcat->idnumber);
  74      }
  75  
  76      /**
  77       * Test creating a category with a tricky idnumber.
  78       */
  79      public function test_add_category_set_idnumber_0() {
  80          global $DB;
  81  
  82          $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid,
  83                  'New category', '', true, FORMAT_HTML, '0');
  84  
  85          $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST);
  86          $this->assertSame('New category', $newcat->name);
  87          $this->assertSame('0', $newcat->idnumber);
  88      }
  89  
  90      /**
  91       * Trying to add a category with duplicate idnumber blanks it.
  92       * (In reality, this would probably get caught by form validation.)
  93       */
  94      public function test_add_category_try_to_set_duplicate_idnumber() {
  95          global $DB;
  96  
  97          $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid,
  98                  'Existing category', '', true, FORMAT_HTML, 'frog');
  99  
 100          $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid,
 101                  'New category', '', true, FORMAT_HTML, 'frog');
 102  
 103          $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST);
 104          $this->assertSame('New category', $newcat->name);
 105          $this->assertNull($newcat->idnumber);
 106      }
 107  
 108      /**
 109       * Test updating a category.
 110       */
 111      public function test_update_category() {
 112          global $DB;
 113  
 114          $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid,
 115                  'Old name', 'Description', true, FORMAT_HTML, 'frog');
 116  
 117          $this->qcobject->update_category($id, $this->topcat->id . ',' . $this->topcat->contextid,
 118                  'New name', 'New description', FORMAT_HTML, '0', false);
 119  
 120          $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST);
 121          $this->assertSame('New name', $newcat->name);
 122          $this->assertSame('0', $newcat->idnumber);
 123      }
 124  
 125      /**
 126       * Test updating a category to remove the idnumber.
 127       */
 128      public function test_update_category_removing_idnumber() {
 129          global $DB;
 130  
 131          $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid,
 132                  'Old name', 'Description', true, FORMAT_HTML, 'frog');
 133  
 134          $this->qcobject->update_category($id, $this->topcat->id . ',' . $this->topcat->contextid,
 135                  'New name', 'New description', FORMAT_HTML, '', false);
 136  
 137          $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST);
 138          $this->assertSame('New name', $newcat->name);
 139          $this->assertNull($newcat->idnumber);
 140      }
 141  
 142      /**
 143       * Test updating a category without changing the idnumber.
 144       */
 145      public function test_update_category_dont_change_idnumber() {
 146          global $DB;
 147  
 148          $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid,
 149                  'Old name', 'Description', true, FORMAT_HTML, 'frog');
 150  
 151          $this->qcobject->update_category($id, $this->topcat->id . ',' . $this->topcat->contextid,
 152                  'New name', 'New description', FORMAT_HTML, 'frog', false);
 153  
 154          $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST);
 155          $this->assertSame('New name', $newcat->name);
 156          $this->assertSame('frog', $newcat->idnumber);
 157      }
 158  
 159      /**
 160       * Trying to update a category so its idnumber duplicates idnumber blanks it.
 161       * (In reality, this would probably get caught by form validation.)
 162       */
 163      public function test_update_category_try_to_set_duplicate_idnumber() {
 164          global $DB;
 165  
 166          $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid,
 167                  'Existing category', '', true, FORMAT_HTML, 'toad');
 168          $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid,
 169                  'old name', '', true, FORMAT_HTML, 'frog');
 170  
 171          $this->qcobject->update_category($id, $this->topcat->id . ',' . $this->topcat->contextid,
 172                  'New name', '', FORMAT_HTML, 'toad', false);
 173  
 174          $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST);
 175          $this->assertSame('New name', $newcat->name);
 176          $this->assertNull($newcat->idnumber);
 177      }
 178  }