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 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_customfield;
  18  
  19  use core_customfield_generator;
  20  
  21  /**
  22   * Functional test for class \core_customfield\category_controller.
  23   *
  24   * @package    core_customfield
  25   * @category   test
  26   * @copyright  2018 Toni Barbera <toni@moodle.com>
  27   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class category_controller_test extends \advanced_testcase {
  30  
  31      /**
  32       * Get generator.
  33       *
  34       * @return core_customfield_generator
  35       */
  36      protected function get_generator(): core_customfield_generator {
  37          return $this->getDataGenerator()->get_plugin_generator('core_customfield');
  38      }
  39  
  40      /**
  41       * Test for the field_controller::__construct function.
  42       */
  43      public function test_constructor() {
  44          $this->resetAfterTest();
  45  
  46          $c = category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0]);
  47          $handler = $c->get_handler();
  48          $this->assertTrue($c instanceof category_controller);
  49  
  50          $cat = $this->get_generator()->create_category();
  51          $c = category_controller::create($cat->get('id'));
  52          $this->assertTrue($c instanceof category_controller);
  53  
  54          $c = category_controller::create($cat->get('id'), null, $handler);
  55          $this->assertTrue($c instanceof category_controller);
  56  
  57          $c = category_controller::create(0, $cat->to_record());
  58          $this->assertTrue($c instanceof category_controller);
  59  
  60          $c = category_controller::create(0, $cat->to_record(), $handler);
  61          $this->assertTrue($c instanceof category_controller);
  62      }
  63  
  64      /**
  65       * Test for function \core_customfield\field_controller::create() in case of wrong parameters
  66       */
  67      public function test_constructor_errors() {
  68          global $DB;
  69          $this->resetAfterTest();
  70  
  71          $cat = $this->get_generator()->create_category();
  72          $catrecord = $cat->to_record();
  73  
  74          // Both id and record give warning.
  75          $c = category_controller::create($catrecord->id, $catrecord);
  76          $debugging = $this->getDebuggingMessages();
  77          $this->assertEquals(1, count($debugging));
  78          $this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
  79              $debugging[0]->message);
  80          $this->resetDebugging();
  81          $this->assertTrue($c instanceof category_controller);
  82  
  83          // Retrieve non-existing data.
  84          try {
  85              category_controller::create($catrecord->id + 1);
  86              $this->fail('Expected exception');
  87          } catch (\moodle_exception $e) {
  88              $this->assertEquals('Category not found', $e->getMessage());
  89              $this->assertEquals(\moodle_exception::class, get_class($e));
  90          }
  91  
  92          // Missing required elements.
  93          try {
  94              category_controller::create(0, (object)['area' => 'course', 'itemid' => 0]);
  95              $this->fail('Expected exception');
  96          } catch (\coding_exception $e) {
  97              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
  98                  'to initialise category_controller - unknown component', $e->getMessage());
  99              $this->assertEquals(\coding_exception::class, get_class($e));
 100          }
 101  
 102          // Missing required elements.
 103          try {
 104              category_controller::create(0, (object)['component' => 'core_course', 'itemid' => 0]);
 105              $this->fail('Expected exception');
 106          } catch (\coding_exception $e) {
 107              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
 108                  'to initialise category_controller - unknown area', $e->getMessage());
 109              $this->assertEquals(\coding_exception::class, get_class($e));
 110          }
 111  
 112          // Missing required elements.
 113          try {
 114              category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course']);
 115              $this->fail('Expected exception');
 116          } catch (\coding_exception $e) {
 117              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
 118                  'to initialise category_controller - unknown itemid', $e->getMessage());
 119              $this->assertEquals(\coding_exception::class, get_class($e));
 120          }
 121  
 122          $handler = \core_course\customfield\course_handler::create();
 123          // Missing required elements.
 124          try {
 125              category_controller::create(0, (object)['component' => 'x', 'area' => 'course', 'itemid' => 0], $handler);
 126              $this->fail('Expected exception');
 127          } catch (\coding_exception $e) {
 128              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Component of the handler ' .
 129                  'does not match the one from the record', $e->getMessage());
 130              $this->assertEquals(\coding_exception::class, get_class($e));
 131          }
 132  
 133          try {
 134              category_controller::create(0, (object)['component' => 'core_course', 'area' => 'x', 'itemid' => 0], $handler);
 135              $this->fail('Expected exception');
 136          } catch (\coding_exception $e) {
 137              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Area of the handler ' .
 138                  'does not match the one from the record', $e->getMessage());
 139              $this->assertEquals(\coding_exception::class, get_class($e));
 140          }
 141  
 142          try {
 143              category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 1], $handler);
 144              $this->fail('Expected exception');
 145          } catch (\coding_exception $e) {
 146              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Itemid of the ' .
 147                  'handler does not match the one from the record', $e->getMessage());
 148              $this->assertEquals(\coding_exception::class, get_class($e));
 149          }
 150  
 151          try {
 152              $user = $this->getDataGenerator()->create_user();
 153              category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0,
 154                  'contextid' => \context_user::instance($user->id)->id], $handler);
 155              $this->fail('Expected exception');
 156          } catch (\coding_exception $e) {
 157              $this->assertEquals('Coding error detected, it must be fixed by a programmer: Context of the ' .
 158                  'handler does not match the one from the record', $e->getMessage());
 159              $this->assertEquals(\coding_exception::class, get_class($e));
 160          }
 161      }
 162  
 163      /**
 164       * Tests for behaviour of:
 165       * \core_customfield\category_controller::save()
 166       * \core_customfield\category_controller::get()
 167       */
 168      public function test_create_category() {
 169          $this->resetAfterTest();
 170  
 171          // Create the category.
 172          $lpg = $this->get_generator();
 173          $categorydata            = new \stdClass();
 174          $categorydata->name      = 'Category1';
 175          $categorydata->component = 'core_course';
 176          $categorydata->area      = 'course';
 177          $categorydata->itemid    = 0;
 178          $categorydata->contextid = \context_system::instance()->id;
 179          $category = category_controller::create(0, $categorydata);
 180          $category->save();
 181          $this->assertNotEmpty($category->get('id'));
 182  
 183          // Confirm record exists.
 184          $this->assertTrue(\core_customfield\category::record_exists($category->get('id')));
 185  
 186          // Confirm that base data was inserted correctly.
 187          $category = category_controller::create($category->get('id'));
 188          $this->assertSame($category->get('name'), $categorydata->name);
 189          $this->assertSame($category->get('component'), $categorydata->component);
 190          $this->assertSame($category->get('area'), $categorydata->area);
 191          $this->assertSame((int)$category->get('itemid'), $categorydata->itemid);
 192      }
 193  
 194      /**
 195       * Tests for \core_customfield\category_controller::set() behaviour.
 196       */
 197      public function test_rename_category() {
 198          $this->resetAfterTest();
 199  
 200          // Create the category.
 201          $params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 'name' => 'Cat1',
 202              'contextid' => \context_system::instance()->id];
 203          $c1 = category_controller::create(0, (object)$params);
 204          $c1->save();
 205          $this->assertNotEmpty($c1->get('id'));
 206  
 207          // Checking new name are correct updated.
 208          $category = category_controller::create($c1->get('id'));
 209          $category->set('name', 'Cat2');
 210          $this->assertSame('Cat2', $category->get('name'));
 211  
 212          // Checking new name are correct updated after save.
 213          $category->save();
 214  
 215          $category = category_controller::create($c1->get('id'));
 216          $this->assertSame('Cat2', $category->get('name'));
 217      }
 218  
 219      /**
 220       * Tests for \core_customfield\category_controller::delete() behaviour.
 221       */
 222      public function test_delete_category() {
 223          $this->resetAfterTest();
 224  
 225          // Create the category.
 226          $lpg = $this->get_generator();
 227          $category0 = $lpg->create_category();
 228          $id0 = $category0->get('id');
 229  
 230          $category1 = $lpg->create_category();
 231          $id1 = $category1->get('id');
 232  
 233          $category2 = $lpg->create_category();
 234          $id2 = $category2->get('id');
 235  
 236          // Confirm that exist in the database.
 237          $this->assertTrue(\core_customfield\category::record_exists($id0));
 238  
 239          // Delete and confirm that is deleted.
 240          $category0->delete();
 241          $this->assertFalse(\core_customfield\category::record_exists($id0));
 242  
 243          // Confirm correct order after delete.
 244          // Check order after re-fetch.
 245          $category1 = category_controller::create($id1);
 246          $category2 = category_controller::create($id2);
 247  
 248          $this->assertSame((int) $category1->get('sortorder'), 1);
 249          $this->assertSame((int) $category2->get('sortorder'), 2);
 250      }
 251  }