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

   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   * Tests for class \core_customfield\api.
  19   *
  20   * @package    core_customfield
  21   * @category   test
  22   * @copyright  2018 Toni Barbera <toni@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use \core_customfield\api;
  29  use \core_customfield\category_controller;
  30  
  31  /**
  32   * Functional test for class \core_customfield\api
  33   *
  34   * @package    core_customfield
  35   * @copyright  2018 Toni Barbera <toni@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class core_customfield_api_testcase extends advanced_testcase {
  39  
  40      /**
  41       * Get generator.
  42       *
  43       * @return core_customfield_generator
  44       */
  45      protected function get_generator(): core_customfield_generator {
  46          return $this->getDataGenerator()->get_plugin_generator('core_customfield');
  47      }
  48  
  49      /**
  50       * Help to assert that the given property in an array of object has the expected value
  51       *
  52       * @param array $expected
  53       * @param array $array array of objects with "get($property)" method
  54       * @param string $propertyname
  55       */
  56      protected function assert_property_in_array($expected, $array, $propertyname) {
  57          $this->assertEquals($expected, array_values(array_map(function($a) use ($propertyname) {
  58              return $a->get($propertyname);
  59          }, $array)));
  60      }
  61  
  62      /**
  63       * Tests for \core_customfield\api::move_category() behaviour.
  64       *
  65       * This replicates what is happening when categories are moved
  66       * in the interface using drag-drop.
  67       */
  68      public function test_move_category() {
  69          $this->resetAfterTest();
  70  
  71          // Create the categories.
  72          $params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0];
  73          $id0 = $this->get_generator()->create_category($params)->get('id');
  74          $id1 = $this->get_generator()->create_category($params)->get('id');
  75          $id2 = $this->get_generator()->create_category($params)->get('id');
  76          $id3 = $this->get_generator()->create_category($params)->get('id');
  77          $id4 = $this->get_generator()->create_category($params)->get('id');
  78          $id5 = $this->get_generator()->create_category($params)->get('id');
  79  
  80          // Check order after re-fetch.
  81          $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
  82          $this->assertEquals([$id0, $id1, $id2, $id3, $id4, $id5], array_keys($categories));
  83          $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
  84  
  85          // Move up 1 position.
  86          api::move_category(category_controller::create($id3), $id2);
  87          $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
  88          $this->assertEquals([$id0, $id1, $id3, $id2, $id4, $id5], array_keys($categories));
  89          $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
  90  
  91          // Move down 1 position.
  92          api::move_category(category_controller::create($id2), $id3);
  93          $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
  94          $this->assertEquals([$id0, $id1, $id2, $id3, $id4, $id5], array_keys($categories));
  95          $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
  96  
  97          // Move up 2 positions.
  98          api::move_category(category_controller::create($id4), $id2);
  99          $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
 100          $this->assertEquals([$id0, $id1, $id4, $id2, $id3, $id5], array_keys($categories));
 101          $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
 102  
 103          // Move down 2 positions.
 104          api::move_category(category_controller::create($id4), $id5);
 105          $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
 106          $this->assertEquals([$id0, $id1, $id2, $id3, $id4, $id5], array_keys($categories));
 107          $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
 108  
 109          // Move to the end of the list.
 110          api::move_category(category_controller::create($id2));
 111          $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
 112          $this->assertEquals([$id0, $id1, $id3, $id4, $id5, $id2], array_keys($categories));
 113          $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
 114      }
 115  
 116      /**
 117       * Tests for \core_customfield\api::get_categories_with_fields() behaviour.
 118       */
 119      public function test_get_categories_with_fields() {
 120          $this->resetAfterTest();
 121  
 122          // Create the categories.
 123          $options = [
 124              'component' => 'core_course',
 125              'area'      => 'course',
 126              'itemid'    => 0,
 127              'contextid' => context_system::instance()->id
 128          ];
 129          $category0 = $this->get_generator()->create_category(['name' => 'aaaa'] + $options);
 130          $category1 = $this->get_generator()->create_category(['name' => 'bbbb'] + $options);
 131          $category2 = $this->get_generator()->create_category(['name' => 'cccc'] + $options);
 132          $category3 = $this->get_generator()->create_category(['name' => 'dddd'] + $options);
 133          $category4 = $this->get_generator()->create_category(['name' => 'eeee'] + $options);
 134          $category5 = $this->get_generator()->create_category(['name' => 'ffff'] + $options);
 135  
 136          // Let's test counts.
 137          $this->assertCount(6, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
 138          api::delete_category($category5);
 139          $this->assertCount(5, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
 140          api::delete_category($category4);
 141          $this->assertCount(4, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
 142          api::delete_category($category3);
 143          $this->assertCount(3, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
 144          api::delete_category($category2);
 145          $this->assertCount(2, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
 146          api::delete_category($category1);
 147          $this->assertCount(1, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
 148          api::delete_category($category0);
 149          $this->assertCount(0, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
 150      }
 151  
 152      /**
 153       * Test for functions api::save_category() and rename_category)
 154       */
 155      public function test_save_category() {
 156          $this->resetAfterTest();
 157  
 158          $params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 'name' => 'Cat1',
 159              'contextid' => context_system::instance()->id];
 160          $c1 = category_controller::create(0, (object)$params);
 161          api::save_category($c1);
 162          $this->assertNotEmpty($c1->get('id'));
 163  
 164          $c1 = category_controller::create($c1->get('id'));
 165          $expected = $params + ['sortorder' => 0, 'id' => $c1->get('id'), 'description' => '', 'descriptionformat' => 0];
 166          $actual = array_intersect_key((array)$c1->to_record(), $expected); // Ignore timecreated, timemodified.
 167          ksort($expected);
 168          ksort($actual);
 169          $this->assertEquals($expected, $actual);
 170  
 171          // Create new category and check that the sortorder will be 1.
 172          $params['name'] = 'Cat2';
 173          $c2 = category_controller::create(0, (object)$params);
 174          api::save_category($c2);
 175          $this->assertNotEmpty($c2->get('id'));
 176          $this->assertEquals(1, $c2->get('sortorder'));
 177          $c2 = category_controller::create($c2->get('id'));
 178          $this->assertEquals(1, $c2->get('sortorder'));
 179  
 180          // Rename a category.
 181          $c1->set('name', 'Cat3');
 182          $c1->save();
 183          $c1 = category_controller::create($c1->get('id'));
 184          $this->assertEquals('Cat3', $c1->get('name'));
 185      }
 186  
 187      /**
 188       * Test for function handler::create_category
 189       */
 190      public function test_create_category() {
 191          $this->resetAfterTest();
 192  
 193          $handler = \core_course\customfield\course_handler::create();
 194          $c1id = $handler->create_category();
 195          $c1 = $handler->get_categories_with_fields()[$c1id];
 196          $this->assertEquals('Other fields', $c1->get('name'));
 197          $this->assertEquals($handler->get_component(), $c1->get('component'));
 198          $this->assertEquals($handler->get_area(), $c1->get('area'));
 199          $this->assertEquals($handler->get_itemid(), $c1->get('itemid'));
 200          $this->assertEquals($handler->get_configuration_context()->id, $c1->get('contextid'));
 201  
 202          // Generate more categories and make sure they have different names.
 203          $c2id = $handler->create_category();
 204          $c3id = $handler->create_category();
 205          $c2 = $handler->get_categories_with_fields()[$c2id];
 206          $c3 = $handler->get_categories_with_fields()[$c3id];
 207          $this->assertEquals('Other fields 1', $c2->get('name'));
 208          $this->assertEquals('Other fields 2', $c3->get('name'));
 209      }
 210  
 211      /**
 212       * Tests for \core_customfield\api::delete_category() behaviour.
 213       */
 214      public function test_delete_category_with_fields() {
 215          $this->resetAfterTest();
 216  
 217          global $DB;
 218          // Create two categories with fields and data.
 219          $options = [
 220              'component' => 'core_course',
 221              'area'      => 'course',
 222              'itemid'    => 0,
 223              'contextid' => context_system::instance()->id
 224          ];
 225          $lpg = $this->get_generator();
 226          $course = $this->getDataGenerator()->create_course();
 227          $dataparams = ['instanceid' => $course->id, 'contextid' => context_course::instance($course->id)->id];
 228          $category0 = $lpg->create_category($options);
 229          $category1 = $lpg->create_category($options);
 230          for ($i = 0; $i < 6; $i++) {
 231              $f = $lpg->create_field(['categoryid' => $category0->get('id')]);
 232              \core_customfield\data_controller::create(0, (object)$dataparams, $f)->save();
 233              $f = $lpg->create_field(['categoryid' => $category1->get('id')]);
 234              \core_customfield\data_controller::create(0, (object)$dataparams, $f)->save();
 235          }
 236  
 237          // Check that each category have fields and store ids for future checks.
 238          list($category0, $category1) = array_values(api::get_categories_with_fields($options['component'],
 239              $options['area'], $options['itemid']));
 240          $category0fieldsids = array_keys($category0->get_fields());
 241          $category1fieldsids = array_keys($category1->get_fields());
 242  
 243          // There are 6 records in field table and 6 records in data table for each category.
 244          list($sql, $p) = $DB->get_in_or_equal($category0fieldsids);
 245          $this->assertCount(6, $DB->get_records_select(\core_customfield\field::TABLE, 'id '.$sql, $p));
 246          $this->assertCount(6, $DB->get_records_select(\core_customfield\data::TABLE, 'fieldid '.$sql, $p));
 247  
 248          list($sql, $p) = $DB->get_in_or_equal($category1fieldsids);
 249          $this->assertCount(6, $DB->get_records_select(\core_customfield\field::TABLE, 'id '.$sql, $p));
 250          $this->assertCount(6, $DB->get_records_select(\core_customfield\data::TABLE, 'fieldid '.$sql, $p));
 251  
 252          // Delete one category.
 253          $this->assertTrue($category0->get_handler()->delete_category($category0));
 254  
 255          // Check that the category fields and data were deleted.
 256          list($sql, $p) = $DB->get_in_or_equal($category0fieldsids);
 257          $this->assertEmpty($DB->get_records_select(\core_customfield\field::TABLE, 'id '.$sql, $p));
 258          $this->assertEmpty($DB->get_records_select(\core_customfield\data::TABLE, 'fieldid '.$sql, $p));
 259  
 260          // Check that fields and data for the other category remain.
 261          list($sql, $p) = $DB->get_in_or_equal($category1fieldsids);
 262          $this->assertCount(6, $DB->get_records_select(\core_customfield\field::TABLE, 'id '.$sql, $p));
 263          $this->assertCount(6, $DB->get_records_select(\core_customfield\data::TABLE, 'fieldid '.$sql, $p));
 264      }
 265  }