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 Toni Barbera <toni@moodle.com>
31
 * @copyright  2018 Toni Barbera <toni@moodle.com>
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\data_controller
33
 */
34
 */
34
class data_controller_test extends \advanced_testcase {
35
final class data_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
        $params = ['instanceid' => $course->id, 'contextid' => \context_course::instance($course->id)->id];
74
        $params = ['instanceid' => $course->id, 'contextid' => \context_course::instance($course->id)->id];
Línea 75... Línea 75...
75
 
75
 
76
        // Generate new data_controller records for these fields, specifying field controller or fieldid or both.
76
        // Generate new data_controller records for these fields, specifying field controller or fieldid or both.
77
        $data0 = data_controller::create(0, (object)$params, $field0);
77
        $data0 = data_controller::create(0, (object)$params, $field0);
78
        $this->assertInstanceOf(customfield_checkbox\data_controller::class, $data0);
78
        $this->assertInstanceOf(customfield_checkbox\data_controller::class, $data0);
-
 
79
        $data1 = data_controller::create(
79
        $data1 = data_controller::create(0,
80
            0,
-
 
81
            (object)($params + ['fieldid' => $field1->get('id')]),
-
 
82
            $field1
80
            (object)($params + ['fieldid' => $field1->get('id')]), $field1);
83
        );
81
        $this->assertInstanceOf(customfield_date\data_controller::class, $data1);
84
        $this->assertInstanceOf(customfield_date\data_controller::class, $data1);
-
 
85
        $data2 = data_controller::create(
82
        $data2 = data_controller::create(0,
86
            0,
-
 
87
            (object)($params + ['fieldid' => $field2->get('id')])
83
            (object)($params + ['fieldid' => $field2->get('id')]));
88
        );
84
        $this->assertInstanceOf(customfield_select\data_controller::class, $data2);
89
        $this->assertInstanceOf(customfield_select\data_controller::class, $data2);
85
        $data3 = data_controller::create(0, (object)$params, $field3);
90
        $data3 = data_controller::create(0, (object)$params, $field3);
86
        $this->assertInstanceOf(customfield_text\data_controller::class, $data3);
91
        $this->assertInstanceOf(customfield_text\data_controller::class, $data3);
87
        $data4 = data_controller::create(0, (object)$params, $field4);
92
        $data4 = data_controller::create(0, (object)$params, $field4);
Línea 97... Línea 102...
97
        // Retrieve data by id.
102
        // Retrieve data by id.
98
        $this->assertInstanceOf(customfield_checkbox\data_controller::class, data_controller::create($data0->get('id')));
103
        $this->assertInstanceOf(customfield_checkbox\data_controller::class, data_controller::create($data0->get('id')));
99
        $this->assertInstanceOf(customfield_date\data_controller::class, data_controller::create($data1->get('id')));
104
        $this->assertInstanceOf(customfield_date\data_controller::class, data_controller::create($data1->get('id')));
Línea 100... Línea 105...
100
 
105
 
-
 
106
        // Retrieve data by id and field.
101
        // Retrieve data by id and field.
107
        $this->assertInstanceOf(
102
        $this->assertInstanceOf(customfield_select\data_controller::class,
108
            customfield_select\data_controller::class,
-
 
109
            data_controller::create($data2->get('id'), null, $field2)
Línea 103... Línea 110...
103
            data_controller::create($data2->get('id'), null, $field2));
110
        );
104
 
111
 
105
        // Retrieve data by record without field.
112
        // Retrieve data by record without field.
Línea 106... Línea 113...
106
        $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data3->get('id')], '*', MUST_EXIST);
113
        $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data3->get('id')], '*', MUST_EXIST);
107
        $this->assertInstanceOf(customfield_text\data_controller::class, data_controller::create(0, $datarecord));
114
        $this->assertInstanceOf(customfield_text\data_controller::class, data_controller::create(0, $datarecord));
108
 
115
 
109
        // Retrieve data by record with field.
-
 
110
        $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data4->get('id')], '*', MUST_EXIST);
116
        // Retrieve data by record with field.
Línea 111... Línea 117...
111
        $this->assertInstanceOf(customfield_textarea\data_controller::class, data_controller::create(0, $datarecord, $field4));
117
        $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data4->get('id')], '*', MUST_EXIST);
112
 
118
        $this->assertInstanceOf(customfield_textarea\data_controller::class, data_controller::create(0, $datarecord, $field4));
113
    }
119
    }
Línea 131... Línea 137...
131
 
137
 
132
        // Both id and record give warning.
138
        // Both id and record give warning.
133
        $d = data_controller::create($datarecord->id, $datarecord);
139
        $d = data_controller::create($datarecord->id, $datarecord);
134
        $debugging = $this->getDebuggingMessages();
140
        $debugging = $this->getDebuggingMessages();
-
 
141
        $this->assertEquals(1, count($debugging));
135
        $this->assertEquals(1, count($debugging));
142
        $this->assertEquals(
136
        $this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
143
            'Too many parameters, either id need to be specified or a record, but not both.',
-
 
144
            $debugging[0]->message
137
            $debugging[0]->message);
145
        );
138
        $this->resetDebugging();
146
            $this->resetDebugging();
Línea 139... Línea 147...
139
        $this->assertInstanceOf(customfield_text\data_controller::class, $d);
147
        $this->assertInstanceOf(customfield_text\data_controller::class, $d);
140
 
148
 
141
        // Retrieve non-existing data.
149
        // Retrieve non-existing data.
142
        try {
150
        try {
143
            data_controller::create($datarecord->id + 1);
151
            data_controller::create($datarecord->id + 1);
144
            $this->fail('Expected exception');
152
            $this->fail('Expected exception');
145
        } catch (\dml_missing_record_exception $e) {
-
 
146
            $this->assertStringMatchesFormat('Can\'t find data record in database table customfield_data%a', $e->getMessage());
153
        } catch (\dml_missing_record_exception $e) {
Línea 147... Línea 154...
147
            $this->assertEquals(\dml_missing_record_exception::class, get_class($e));
154
            $this->assertStringMatchesFormat('Can\'t find data record in database table customfield_data%a', $e->getMessage());
148
        }
155
        }
149
 
156
 
150
        // Missing field id.
157
        // Missing field id.
151
        try {
158
        try {
152
            data_controller::create(0, (object)['instanceid' => $course->id]);
159
            data_controller::create(0, (object)['instanceid' => $course->id]);
153
            $this->fail('Expected exception');
160
            $this->fail('Expected exception');
154
        } catch (\coding_exception $e) {
-
 
155
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
161
        } catch (\coding_exception $e) {
Línea 156... Línea 162...
156
                'initialise data_controller - unknown field', $e->getMessage());
162
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
157
            $this->assertEquals(\coding_exception::class, get_class($e));
163
                'initialise data_controller - unknown field', $e->getMessage());
158
        }
164
        }
159
 
165
 
160
        // Mismatching field id.
166
        // Mismatching field id.
161
        try {
167
        try {
162
            data_controller::create(0, (object)['instanceid' => $course->id, 'fieldid' => $field->get('id') + 1], $field);
168
            data_controller::create(0, (object)['instanceid' => $course->id, 'fieldid' => $field->get('id') + 1], $field);
163
            $this->fail('Expected exception');
-
 
164
        } catch (\coding_exception $e) {
169
            $this->fail('Expected exception');
Línea 165... Línea 170...
165
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Field id from the record ' .
170
        } catch (\coding_exception $e) {
166
                'does not match field from the parameter', $e->getMessage());
171
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Field id from the record ' .
167
            $this->assertEquals(\coding_exception::class, get_class($e));
172
                'does not match field from the parameter', $e->getMessage());
168
        }
173
        }
169
 
174
 
170
        // Nonexisting class.
175
        // Nonexisting class.
171
        try {
176
        try {
172
            $field->set('type', 'invalid');
-
 
173
            data_controller::create(0, (object)['instanceid' => $course->id], $field);
177
            $field->set('type', 'invalid');
174
            $this->fail('Expected exception');
178
            data_controller::create(0, (object)['instanceid' => $course->id], $field);
175
        } catch (\moodle_exception $e) {
179
            $this->fail('Expected exception');