Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
use customfield_checkbox;
21
use customfield_date;
22
use customfield_select;
23
use customfield_text;
24
use customfield_textarea;
25
 
26
/**
27
 * Functional test for class \core_customfield\field_controller.
28
 *
29
 * @package    core_customfield
30
 * @category   test
31
 * @copyright  2018 Ruslan Kabalin
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 33
 * @covers \core_customfield\field_controller
1 efrain 34
 */
1441 ariadna 35
final class field_controller_test extends \advanced_testcase {
1 efrain 36
    /**
37
     * Get generator.
38
     *
39
     * @return core_customfield_generator
40
     */
41
    protected function get_generator(): core_customfield_generator {
42
        return $this->getDataGenerator()->get_plugin_generator('core_customfield');
43
    }
44
 
45
    /**
46
     * Test for function \core_customfield\field_controller::create()
47
     */
11 efrain 48
    public function test_constructor(): void {
1 efrain 49
        global $DB;
50
        $this->resetAfterTest();
51
 
52
        // Create the category.
53
        $category0 = $this->get_generator()->create_category();
54
 
55
        // Initiate objects without id, try with the category object or with category id or with both.
56
        $field0 = field_controller::create(0, (object)['type' => 'checkbox'], $category0);
57
        $this->assertInstanceOf(customfield_checkbox\field_controller::class, $field0);
58
        $field1 = field_controller::create(0, (object)['type' => 'date', 'categoryid' => $category0->get('id')]);
59
        $this->assertInstanceOf(customfield_date\field_controller::class, $field1);
60
        $field2 = field_controller::create(0, (object)['type' => 'select', 'categoryid' => $category0->get('id')], $category0);
61
        $this->assertInstanceOf(customfield_select\field_controller::class, $field2);
62
        $field3 = field_controller::create(0, (object)['type' => 'text'], $category0);
63
        $this->assertInstanceOf(customfield_text\field_controller::class, $field3);
64
        $field4 = field_controller::create(0, (object)['type' => 'textarea'], $category0);
65
        $this->assertInstanceOf(customfield_textarea\field_controller::class, $field4);
66
 
67
        // Save fields to the db so we have ids.
68
        \core_customfield\api::save_field_configuration($field0, (object)['name' => 'a', 'shortname' => 'a']);
69
        \core_customfield\api::save_field_configuration($field1, (object)['name' => 'b', 'shortname' => 'b']);
70
        \core_customfield\api::save_field_configuration($field2, (object)['name' => 'c', 'shortname' => 'c']);
71
        \core_customfield\api::save_field_configuration($field3, (object)['name' => 'd', 'shortname' => 'd']);
72
        \core_customfield\api::save_field_configuration($field4, (object)['name' => 'e', 'shortname' => 'e']);
73
 
74
        // Retrieve fields by id.
75
        $this->assertInstanceOf(customfield_checkbox\field_controller::class, field_controller::create($field0->get('id')));
76
        $this->assertInstanceOf(customfield_date\field_controller::class, field_controller::create($field1->get('id')));
77
 
78
        // Retrieve field by id and category.
1441 ariadna 79
        $this->assertInstanceOf(
80
            customfield_select\field_controller::class,
81
            field_controller::create($field2->get('id'), null, $category0)
82
        );
1 efrain 83
 
84
        // Retrieve fields by record without category.
85
        $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field3->get('id')], '*', MUST_EXIST);
86
        $this->assertInstanceOf(customfield_text\field_controller::class, field_controller::create(0, $fieldrecord));
87
 
88
        // Retrieve fields by record with category.
89
        $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field4->get('id')], '*', MUST_EXIST);
1441 ariadna 90
        $this->assertInstanceOf(
91
            customfield_textarea\field_controller::class,
92
            field_controller::create(0, $fieldrecord, $category0)
93
        );
1 efrain 94
    }
95
 
96
    /**
1441 ariadna 97
     * Test creation of field instance from pre-defined object
98
     */
99
    public function test_constructor_from_record(): void {
100
        $this->resetAfterTest();
101
 
102
        // Create field object that matches the persistent/schema definition.
103
        $category = $this->get_generator()->create_category();
104
        $field = field_controller::create(0, (object) [
105
            'name' => 'Test',
106
            'shortname' => 'test',
107
            'type' => 'text',
108
            'description' => null,
109
            'descriptionformat' => null,
110
            'sortorder' => null,
111
            'configdata' => null,
112
        ], $category);
113
 
114
        // Saving the field will validate the persistent internally.
115
        $field->save();
116
 
117
        $this->assertInstanceOf(\customfield_text\field_controller::class, $field);
118
    }
119
 
120
    /**
1 efrain 121
     * Test for function \core_customfield\field_controller::create() in case of wrong parameters
122
     */
11 efrain 123
    public function test_constructor_errors(): void {
1 efrain 124
        global $DB;
125
        $this->resetAfterTest();
126
 
127
        // Create a category and a field.
128
        $category = $this->get_generator()->create_category();
129
        $field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
130
 
131
        $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field->get('id')], '*', MUST_EXIST);
132
 
133
        // Both id and record give warning.
134
        $field = field_controller::create($fieldrecord->id, $fieldrecord);
135
        $debugging = $this->getDebuggingMessages();
136
        $this->assertEquals(1, count($debugging));
1441 ariadna 137
        $this->assertEquals(
138
            'Too many parameters, either id need to be specified or a record, but not both.',
139
            $debugging[0]->message
140
        );
1 efrain 141
        $this->resetDebugging();
142
        $this->assertInstanceOf(customfield_text\field_controller::class, $field);
143
 
144
        // Retrieve non-existing field.
145
        try {
146
            field_controller::create($fieldrecord->id + 1);
147
            $this->fail('Expected exception');
148
        } catch (\moodle_exception $e) {
149
            $this->assertEquals('Field not found', $e->getMessage());
150
        }
151
 
152
        // Retrieve without id and without type.
153
        try {
154
            field_controller::create(0, (object)['name' => 'a'], $category);
155
            $this->fail('Expected exception');
156
        } catch (\coding_exception $e) {
157
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
158
                'initialise field_controller - unknown field type', $e->getMessage());
159
        }
160
 
161
        // Missing category id.
162
        try {
163
            field_controller::create(0, (object)['type' => 'text']);
164
            $this->fail('Expected exception');
165
        } catch (\coding_exception $e) {
166
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
167
                'to initialise field_controller - unknown category', $e->getMessage());
168
        }
169
 
170
        // Mismatching category id.
171
        try {
172
            field_controller::create(0, (object)['type' => 'text', 'categoryid' => $category->get('id') + 1], $category);
173
            $this->fail('Expected exception');
174
        } catch (\coding_exception $e) {
175
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Category of the field ' .
176
                'does not match category from the parameter', $e->getMessage());
177
        }
178
 
179
        // Non-existing type.
180
        try {
181
            field_controller::create(0, (object)['type' => 'nonexisting'], $category);
182
            $this->fail('Expected exception');
183
        } catch (\moodle_exception $e) {
184
            $this->assertEquals('Field type nonexisting not found', $e->getMessage());
185
        }
186
    }
187
 
188
    /**
189
     * Tests for behaviour of:
190
     * \core_customfield\field_controller::save()
191
     * \core_customfield\field_controller::get()
192
     * \core_customfield\field_controller::get_category()
193
     */
11 efrain 194
    public function test_create_field(): void {
1 efrain 195
        global $DB;
196
        $this->resetAfterTest();
197
 
198
        $lpg = $this->get_generator();
199
        $category = $lpg->create_category();
200
        $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
201
        $this->assertCount(0, $fields);
202
 
203
        // Create field.
204
        $fielddata = new \stdClass();
205
        $fielddata->name = 'Field';
206
        $fielddata->shortname = 'field';
207
        $fielddata->type = 'text';
208
        $fielddata->categoryid = $category->get('id');
209
        $field = field_controller::create(0, $fielddata);
210
        $field->save();
211
 
212
        $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
213
        $this->assertCount(1, $fields);
214
        $this->assertTrue(\core_customfield\field::record_exists($field->get('id')));
215
        $this->assertInstanceOf(\customfield_text\field_controller::class, $field);
216
        $this->assertSame($field->get('name'), $fielddata->name);
217
        $this->assertSame($field->get('type'), $fielddata->type);
218
        $this->assertEquals($field->get_category()->get('id'), $category->get('id'));
219
    }
220
 
221
    /**
222
     * Tests for \core_customfield\field_controller::delete() behaviour.
223
     */
11 efrain 224
    public function test_delete_field(): void {
1 efrain 225
        global $DB;
226
        $this->resetAfterTest();
227
 
228
        $lpg = $this->get_generator();
229
        $category = $lpg->create_category();
230
        $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
231
        $this->assertCount(0, $fields);
232
 
233
        // Create field using generator.
1441 ariadna 234
        $field1 = $lpg->create_field(['categoryid' => $category->get('id')]);
235
        $field2 = $lpg->create_field(['categoryid' => $category->get('id')]);
1 efrain 236
        $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
237
        $this->assertCount(2, $fields);
238
 
239
        // Delete fields.
240
        $this->assertTrue($field1->delete());
241
        $this->assertTrue($field2->delete());
242
 
243
        // Check that the fields have been deleted.
244
        $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
245
        $this->assertCount(0, $fields);
246
        $this->assertFalse(\core_customfield\field::record_exists($field1->get('id')));
247
        $this->assertFalse(\core_customfield\field::record_exists($field2->get('id')));
248
    }
249
 
250
    /**
251
     * Tests for \core_customfield\field_controller::get_configdata_property() behaviour.
252
     */
11 efrain 253
    public function test_get_configdata_property(): void {
1 efrain 254
        $this->resetAfterTest();
255
 
256
        $lpg = $this->get_generator();
257
        $category = $lpg->create_category();
258
        $configdata = ['a' => 'b', 'c' => ['d', 'e']];
259
        $field = field_controller::create(0, (object)['type' => 'text',
260
            'configdata' => json_encode($configdata), 'shortname' => 'a', 'name' => 'a'], $category);
261
        $field->save();
262
 
263
        // Retrieve field and check configdata.
264
        $field = field_controller::create($field->get('id'));
265
        $this->assertEquals($configdata, $field->get('configdata'));
266
        $this->assertEquals('b', $field->get_configdata_property('a'));
267
        $this->assertEquals(['d', 'e'], $field->get_configdata_property('c'));
268
        $this->assertEquals(null, $field->get_configdata_property('x'));
269
    }
270
}