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   * core_customfield test data generator test.
  19   *
  20   * @package    core_customfield
  21   * @category   test
  22   * @copyright  2018 Ruslan Kabalin
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * core_customfield test data generator testcase.
  30   *
  31   * @package    core_customfield
  32   * @category   test
  33   * @copyright  2018 Ruslan Kabalin
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class core_customfield_generator_testcase extends advanced_testcase {
  37  
  38      /**
  39       * Get generator
  40       * @return core_customfield_generator
  41       */
  42      protected function get_generator(): core_customfield_generator {
  43          return $this->getDataGenerator()->get_plugin_generator('core_customfield');
  44      }
  45  
  46      /**
  47       * Test creating category
  48       */
  49      public function test_create_category() {
  50          $this->resetAfterTest(true);
  51  
  52          $lpg = $this->get_generator();
  53          $category = $lpg->create_category();
  54  
  55          $this->assertInstanceOf('\core_customfield\category_controller', $category);
  56          $this->assertTrue(\core_customfield\category::record_exists($category->get('id')));
  57      }
  58  
  59      /**
  60       * Test creating field
  61       */
  62      public function test_create_field() {
  63          $this->resetAfterTest(true);
  64  
  65          $lpg = $this->get_generator();
  66          $category = $lpg->create_category();
  67          $field = $lpg->create_field(['categoryid' => $category->get('id')]);
  68  
  69          $this->assertInstanceOf('\core_customfield\field_controller', $field);
  70          $this->assertTrue(\core_customfield\field::record_exists($field->get('id')));
  71  
  72          $category = core_customfield\category_controller::create($category->get('id'));
  73          $category = \core_customfield\api::get_categories_with_fields($category->get('component'),
  74              $category->get('area'), $category->get('itemid'))[$category->get('id')];
  75          $this->assertCount(1, $category->get_fields());
  76      }
  77  
  78      /**
  79       * Test for function add_instance_data()
  80       */
  81      public function test_add_instance_data() {
  82          $this->resetAfterTest(true);
  83  
  84          $lpg = $this->get_generator();
  85          $c1 = $lpg->create_category();
  86          $course1 = $this->getDataGenerator()->create_course();
  87  
  88          $f11 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'checkbox']);
  89          $f12 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'date']);
  90          $f13 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'),
  91              'type' => 'select', 'configdata' => ['options' => "a\nb\nc"]]);
  92          $f14 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'text']);
  93          $f15 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'textarea']);
  94  
  95          $this->get_generator()->add_instance_data($f11, $course1->id, 1);
  96          $this->get_generator()->add_instance_data($f12, $course1->id, 1546300800);
  97          $this->get_generator()->add_instance_data($f13, $course1->id, 2);
  98          $this->get_generator()->add_instance_data($f14, $course1->id, 'Hello');
  99          $this->get_generator()->add_instance_data($f15, $course1->id, ['text' => '<p>Hi there</p>', 'format' => FORMAT_HTML]);
 100  
 101          $handler = $c1->get_handler();
 102          list($data1, $data2, $data3, $data4, $data5) = array_values($handler->get_instance_data($course1->id));
 103          $this->assertNotEmpty($data1->get('id'));
 104          $this->assertEquals(1, $data1->get_value());
 105          $this->assertNotEmpty($data2->get('id'));
 106          $this->assertEquals(1546300800, $data2->get_value());
 107          $this->assertNotEmpty($data3->get('id'));
 108          $this->assertEquals(2, $data3->get_value());
 109          $this->assertNotEmpty($data4->get('id'));
 110          $this->assertEquals('Hello', $data4->get_value());
 111          $this->assertNotEmpty($data5->get('id'));
 112          $this->assertEquals('<p>Hi there</p>', $data5->get_value());
 113      }
 114  }