Merge branch 'MDL-81073' of https://github.com/paulholden/moodle
[moodle.git] / customfield / tests / generator_test.php
blob20f17b078f7b93b8f3cfb151094abe0c7aec009e
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.
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/>.
17 namespace core_customfield;
19 /**
20 * core_customfield test data generator testcase.
22 * @package core_customfield
23 * @category test
24 * @copyright 2018 Ruslan Kabalin
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 class generator_test extends \advanced_testcase {
29 /**
30 * Get generator
31 * @return core_customfield_generator
33 protected function get_generator(): \core_customfield_generator {
34 return $this->getDataGenerator()->get_plugin_generator('core_customfield');
37 /**
38 * Test creating category
40 public function test_create_category() {
41 $this->resetAfterTest(true);
43 $lpg = $this->get_generator();
44 $category = $lpg->create_category();
46 $this->assertInstanceOf('\core_customfield\category_controller', $category);
47 $this->assertTrue(\core_customfield\category::record_exists($category->get('id')));
50 /**
51 * Test creating field
53 public function test_create_field() {
54 $this->resetAfterTest(true);
56 $lpg = $this->get_generator();
57 $category = $lpg->create_category();
58 $field = $lpg->create_field(['categoryid' => $category->get('id')]);
60 $this->assertInstanceOf('\core_customfield\field_controller', $field);
61 $this->assertTrue(\core_customfield\field::record_exists($field->get('id')));
63 $category = \core_customfield\category_controller::create($category->get('id'));
64 $category = \core_customfield\api::get_categories_with_fields($category->get('component'),
65 $category->get('area'), $category->get('itemid'))[$category->get('id')];
66 $this->assertCount(1, $category->get_fields());
69 /**
70 * Test for function add_instance_data()
72 public function test_add_instance_data() {
73 $this->resetAfterTest(true);
75 $lpg = $this->get_generator();
76 $c1 = $lpg->create_category();
77 $course1 = $this->getDataGenerator()->create_course();
79 $f11 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'checkbox']);
80 $f12 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'date']);
81 $f13 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'),
82 'type' => 'select', 'configdata' => ['options' => "a\nb\nc"]]);
83 $f14 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'text']);
84 $f15 = $this->get_generator()->create_field(['categoryid' => $c1->get('id'), 'type' => 'textarea']);
86 $this->get_generator()->add_instance_data($f11, $course1->id, 1);
87 $this->get_generator()->add_instance_data($f12, $course1->id, 1546300800);
88 $this->get_generator()->add_instance_data($f13, $course1->id, 2);
89 $this->get_generator()->add_instance_data($f14, $course1->id, 'Hello');
90 $this->get_generator()->add_instance_data($f15, $course1->id, ['text' => '<p>Hi there</p>', 'format' => FORMAT_HTML]);
92 $handler = $c1->get_handler();
93 list($data1, $data2, $data3, $data4, $data5) = array_values($handler->get_instance_data($course1->id));
94 $this->assertNotEmpty($data1->get('id'));
95 $this->assertEquals(1, $data1->get_value());
96 $this->assertNotEmpty($data2->get('id'));
97 $this->assertEquals(1546300800, $data2->get_value());
98 $this->assertNotEmpty($data3->get('id'));
99 $this->assertEquals(2, $data3->get_value());
100 $this->assertNotEmpty($data4->get('id'));
101 $this->assertEquals('Hello', $data4->get_value());
102 $this->assertNotEmpty($data5->get('id'));
103 $this->assertEquals('<p>Hi there</p>', $data5->get_value());