Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 28... Línea 28...
28
 *
28
 *
29
 * @package    core_customfield
29
 * @package    core_customfield
30
 * @category   test
30
 * @category   test
31
 * @copyright  2018 Ruslan Kabalin
31
 * @copyright  2018 Ruslan Kabalin
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
33
 * @covers \core_customfield\field_controller
33
 */
34
 */
34
class field_controller_test extends \advanced_testcase {
35
final class field_controller_test extends \advanced_testcase {
35
 
-
 
36
    /**
36
    /**
37
     * Get generator.
37
     * Get generator.
38
     *
38
     *
39
     * @return core_customfield_generator
39
     * @return core_customfield_generator
40
     */
40
     */
Línea 74... Línea 74...
74
        // Retrieve fields by id.
74
        // Retrieve fields by id.
75
        $this->assertInstanceOf(customfield_checkbox\field_controller::class, field_controller::create($field0->get('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')));
76
        $this->assertInstanceOf(customfield_date\field_controller::class, field_controller::create($field1->get('id')));
Línea 77... Línea 77...
77
 
77
 
-
 
78
        // Retrieve field by id and category.
78
        // Retrieve field by id and category.
79
        $this->assertInstanceOf(
79
        $this->assertInstanceOf(customfield_select\field_controller::class,
80
            customfield_select\field_controller::class,
-
 
81
            field_controller::create($field2->get('id'), null, $category0)
Línea 80... Línea 82...
80
            field_controller::create($field2->get('id'), null, $category0));
82
        );
81
 
83
 
82
        // Retrieve fields by record without category.
84
        // Retrieve fields by record without category.
Línea 83... Línea 85...
83
        $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field3->get('id')], '*', MUST_EXIST);
85
        $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field3->get('id')], '*', MUST_EXIST);
84
        $this->assertInstanceOf(customfield_text\field_controller::class, field_controller::create(0, $fieldrecord));
86
        $this->assertInstanceOf(customfield_text\field_controller::class, field_controller::create(0, $fieldrecord));
-
 
87
 
85
 
88
        // Retrieve fields by record with category.
86
        // Retrieve fields by record with category.
89
        $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field4->get('id')], '*', MUST_EXIST);
-
 
90
        $this->assertInstanceOf(
-
 
91
            customfield_textarea\field_controller::class,
-
 
92
            field_controller::create(0, $fieldrecord, $category0)
-
 
93
        );
-
 
94
    }
-
 
95
 
-
 
96
    /**
-
 
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.
87
        $fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field4->get('id')], '*', MUST_EXIST);
115
        $field->save();
Línea 88... Línea 116...
88
        $this->assertInstanceOf(customfield_textarea\field_controller::class,
116
 
89
            field_controller::create(0, $fieldrecord, $category0));
117
        $this->assertInstanceOf(\customfield_text\field_controller::class, $field);
90
    }
118
    }
Línea 104... Línea 132...
104
 
132
 
105
        // Both id and record give warning.
133
        // Both id and record give warning.
106
        $field = field_controller::create($fieldrecord->id, $fieldrecord);
134
        $field = field_controller::create($fieldrecord->id, $fieldrecord);
107
        $debugging = $this->getDebuggingMessages();
135
        $debugging = $this->getDebuggingMessages();
-
 
136
        $this->assertEquals(1, count($debugging));
108
        $this->assertEquals(1, count($debugging));
137
        $this->assertEquals(
109
        $this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
138
            'Too many parameters, either id need to be specified or a record, but not both.',
-
 
139
            $debugging[0]->message
110
            $debugging[0]->message);
140
        );
111
        $this->resetDebugging();
141
        $this->resetDebugging();
Línea 112... Línea 142...
112
        $this->assertInstanceOf(customfield_text\field_controller::class, $field);
142
        $this->assertInstanceOf(customfield_text\field_controller::class, $field);
113
 
143
 
114
        // Retrieve non-existing field.
144
        // Retrieve non-existing field.
115
        try {
145
        try {
116
            field_controller::create($fieldrecord->id + 1);
146
            field_controller::create($fieldrecord->id + 1);
117
            $this->fail('Expected exception');
147
            $this->fail('Expected exception');
118
        } catch (\moodle_exception $e) {
-
 
119
            $this->assertEquals('Field not found', $e->getMessage());
148
        } catch (\moodle_exception $e) {
Línea 120... Línea 149...
120
            $this->assertEquals(\moodle_exception::class, get_class($e));
149
            $this->assertEquals('Field not found', $e->getMessage());
121
        }
150
        }
122
 
151
 
123
        // Retrieve without id and without type.
152
        // Retrieve without id and without type.
124
        try {
153
        try {
125
            field_controller::create(0, (object)['name' => 'a'], $category);
154
            field_controller::create(0, (object)['name' => 'a'], $category);
126
            $this->fail('Expected exception');
155
            $this->fail('Expected exception');
127
        } catch (\coding_exception $e) {
-
 
128
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
156
        } catch (\coding_exception $e) {
Línea 129... Línea 157...
129
                'initialise field_controller - unknown field type', $e->getMessage());
157
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
130
            $this->assertEquals(\coding_exception::class, get_class($e));
158
                'initialise field_controller - unknown field type', $e->getMessage());
131
        }
159
        }
132
 
160
 
133
        // Missing category id.
161
        // Missing category id.
134
        try {
162
        try {
135
            field_controller::create(0, (object)['type' => 'text']);
163
            field_controller::create(0, (object)['type' => 'text']);
136
            $this->fail('Expected exception');
-
 
137
        } catch (\coding_exception $e) {
164
            $this->fail('Expected exception');
Línea 138... Línea 165...
138
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
165
        } catch (\coding_exception $e) {
139
                'to initialise field_controller - unknown category', $e->getMessage());
166
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
140
            $this->assertEquals(\coding_exception::class, get_class($e));
167
                'to initialise field_controller - unknown category', $e->getMessage());
141
        }
168
        }
142
 
169
 
143
        // Mismatching category id.
170
        // Mismatching category id.
144
        try {
171
        try {
145
            field_controller::create(0, (object)['type' => 'text', 'categoryid' => $category->get('id') + 1], $category);
-
 
146
            $this->fail('Expected exception');
172
            field_controller::create(0, (object)['type' => 'text', 'categoryid' => $category->get('id') + 1], $category);
Línea 147... Línea 173...
147
        } catch (\coding_exception $e) {
173
            $this->fail('Expected exception');
148
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Category of the field ' .
174
        } catch (\coding_exception $e) {
149
                'does not match category from the parameter', $e->getMessage());
175
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Category of the field ' .
150
            $this->assertEquals(\coding_exception::class, get_class($e));
176
                'does not match category from the parameter', $e->getMessage());
151
        }
177
        }
152
 
178
 
153
        // Non-existing type.
-
 
154
        try {
179
        // Non-existing type.
155
            field_controller::create(0, (object)['type' => 'nonexisting'], $category);
180
        try {
Línea 156... Línea 181...
156
            $this->fail('Expected exception');
181
            field_controller::create(0, (object)['type' => 'nonexisting'], $category);
157
        } catch (\moodle_exception $e) {
182
            $this->fail('Expected exception');
Línea 204... Línea 229...
204
        $category = $lpg->create_category();
229
        $category = $lpg->create_category();
205
        $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
230
        $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
206
        $this->assertCount(0, $fields);
231
        $this->assertCount(0, $fields);
Línea 207... Línea 232...
207
 
232
 
208
        // Create field using generator.
233
        // Create field using generator.
209
        $field1 = $lpg->create_field(array('categoryid' => $category->get('id')));
234
        $field1 = $lpg->create_field(['categoryid' => $category->get('id')]);
210
        $field2 = $lpg->create_field(array('categoryid' => $category->get('id')));
235
        $field2 = $lpg->create_field(['categoryid' => $category->get('id')]);
211
        $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
236
        $fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
Línea 212... Línea 237...
212
        $this->assertCount(2, $fields);
237
        $this->assertCount(2, $fields);
213
 
238