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 data_controller.
28
 *
29
 * @package    core_customfield
30
 * @category   test
31
 * @copyright  2018 Toni Barbera <toni@moodle.com>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 33
 * @covers \core_customfield\data_controller
1 efrain 34
 */
1441 ariadna 35
final class data_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 data_controller::create()
47
     */
11 efrain 48
    public function test_constructor(): void {
1 efrain 49
        global $DB;
50
        $this->resetAfterTest();
51
 
52
        // Create a course, fields category and fields.
53
        $course = $this->getDataGenerator()->create_course();
54
        $category0 = $this->get_generator()->create_category(['name' => 'aaaa']);
55
 
56
        // Add fields to this category.
57
        $fielddata                = new \stdClass();
58
        $fielddata->categoryid    = $category0->get('id');
59
        $fielddata->configdata    = "{\"required\":\"0\",\"uniquevalues\":\"0\",\"locked\":\"0\",\"visibility\":\"0\",
60
                                    \"defaultvalue\":\"\",\"displaysize\":0,\"maxlength\":0,\"ispassword\":\"0\",
61
                                    \"link\":\"\",\"linktarget\":\"\"}";
62
 
63
        $fielddata->type = 'checkbox';
64
        $field0 = $this->get_generator()->create_field($fielddata);
65
        $fielddata->type = 'date';
66
        $field1 = $this->get_generator()->create_field($fielddata);
67
        $fielddata->type = 'select';
68
        $field2 = $this->get_generator()->create_field($fielddata);
69
        $fielddata->type = 'text';
70
        $field3 = $this->get_generator()->create_field($fielddata);
71
        $fielddata->type = 'textarea';
72
        $field4 = $this->get_generator()->create_field($fielddata);
73
 
74
        $params = ['instanceid' => $course->id, 'contextid' => \context_course::instance($course->id)->id];
75
 
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);
78
        $this->assertInstanceOf(customfield_checkbox\data_controller::class, $data0);
1441 ariadna 79
        $data1 = data_controller::create(
80
            0,
81
            (object)($params + ['fieldid' => $field1->get('id')]),
82
            $field1
83
        );
1 efrain 84
        $this->assertInstanceOf(customfield_date\data_controller::class, $data1);
1441 ariadna 85
        $data2 = data_controller::create(
86
            0,
87
            (object)($params + ['fieldid' => $field2->get('id')])
88
        );
1 efrain 89
        $this->assertInstanceOf(customfield_select\data_controller::class, $data2);
90
        $data3 = data_controller::create(0, (object)$params, $field3);
91
        $this->assertInstanceOf(customfield_text\data_controller::class, $data3);
92
        $data4 = data_controller::create(0, (object)$params, $field4);
93
        $this->assertInstanceOf(customfield_textarea\data_controller::class, $data4);
94
 
95
        // Save data so we can have ids.
96
        $data0->save();
97
        $data1->save();
98
        $data2->save();
99
        $data3->save();
100
        $data4->save();
101
 
102
        // Retrieve data by id.
103
        $this->assertInstanceOf(customfield_checkbox\data_controller::class, data_controller::create($data0->get('id')));
104
        $this->assertInstanceOf(customfield_date\data_controller::class, data_controller::create($data1->get('id')));
105
 
106
        // Retrieve data by id and field.
1441 ariadna 107
        $this->assertInstanceOf(
108
            customfield_select\data_controller::class,
109
            data_controller::create($data2->get('id'), null, $field2)
110
        );
1 efrain 111
 
112
        // Retrieve data by record without field.
113
        $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data3->get('id')], '*', MUST_EXIST);
114
        $this->assertInstanceOf(customfield_text\data_controller::class, data_controller::create(0, $datarecord));
115
 
116
        // Retrieve data by record with field.
117
        $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data4->get('id')], '*', MUST_EXIST);
118
        $this->assertInstanceOf(customfield_textarea\data_controller::class, data_controller::create(0, $datarecord, $field4));
119
    }
120
 
121
    /**
122
     * Test for function \core_customfield\field_controller::create() in case of wrong parameters
123
     */
11 efrain 124
    public function test_constructor_errors(): void {
1 efrain 125
        global $DB;
126
        $this->resetAfterTest();
127
 
128
        // Create a category, field and data.
129
        $category = $this->get_generator()->create_category();
130
        $field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
131
        $course = $this->getDataGenerator()->create_course();
132
        $data = data_controller::create(0, (object)['instanceid' => $course->id,
133
            'contextid' => \context_course::instance($course->id)->id], $field);
134
        $data->save();
135
 
136
        $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data->get('id')], '*', MUST_EXIST);
137
 
138
        // Both id and record give warning.
139
        $d = data_controller::create($datarecord->id, $datarecord);
140
        $debugging = $this->getDebuggingMessages();
141
        $this->assertEquals(1, count($debugging));
1441 ariadna 142
        $this->assertEquals(
143
            'Too many parameters, either id need to be specified or a record, but not both.',
144
            $debugging[0]->message
145
        );
146
            $this->resetDebugging();
1 efrain 147
        $this->assertInstanceOf(customfield_text\data_controller::class, $d);
148
 
149
        // Retrieve non-existing data.
150
        try {
151
            data_controller::create($datarecord->id + 1);
152
            $this->fail('Expected exception');
153
        } catch (\dml_missing_record_exception $e) {
154
            $this->assertStringMatchesFormat('Can\'t find data record in database table customfield_data%a', $e->getMessage());
155
        }
156
 
157
        // Missing field id.
158
        try {
159
            data_controller::create(0, (object)['instanceid' => $course->id]);
160
            $this->fail('Expected exception');
161
        } catch (\coding_exception $e) {
162
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
163
                'initialise data_controller - unknown field', $e->getMessage());
164
        }
165
 
166
        // Mismatching field id.
167
        try {
168
            data_controller::create(0, (object)['instanceid' => $course->id, 'fieldid' => $field->get('id') + 1], $field);
169
            $this->fail('Expected exception');
170
        } catch (\coding_exception $e) {
171
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Field id from the record ' .
172
                'does not match field from the parameter', $e->getMessage());
173
        }
174
 
175
        // Nonexisting class.
176
        try {
177
            $field->set('type', 'invalid');
178
            data_controller::create(0, (object)['instanceid' => $course->id], $field);
179
            $this->fail('Expected exception');
180
        } catch (\moodle_exception $e) {
181
            $this->assertEquals('Field type invalid not found', $e->getMessage());
182
        }
183
    }
184
}