Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

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