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
 
21
/**
22
 * Functional test for class \core_customfield\category_controller.
23
 *
24
 * @package    core_customfield
25
 * @category   test
26
 * @copyright  2018 Toni Barbera <toni@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 28
 * @covers \core_customfield\category_controller
1 efrain 29
 */
1441 ariadna 30
final class category_controller_test extends \advanced_testcase {
1 efrain 31
    /**
32
     * Get generator.
33
     *
34
     * @return core_customfield_generator
35
     */
36
    protected function get_generator(): core_customfield_generator {
37
        return $this->getDataGenerator()->get_plugin_generator('core_customfield');
38
    }
39
 
40
    /**
41
     * Test for the field_controller::__construct function.
42
     */
11 efrain 43
    public function test_constructor(): void {
1 efrain 44
        $this->resetAfterTest();
45
 
46
        $c = category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0]);
47
        $handler = $c->get_handler();
48
        $this->assertTrue($c instanceof category_controller);
49
 
50
        $cat = $this->get_generator()->create_category();
51
        $c = category_controller::create($cat->get('id'));
52
        $this->assertTrue($c instanceof category_controller);
53
 
54
        $c = category_controller::create($cat->get('id'), null, $handler);
55
        $this->assertTrue($c instanceof category_controller);
56
 
57
        $c = category_controller::create(0, $cat->to_record());
58
        $this->assertTrue($c instanceof category_controller);
59
 
60
        $c = category_controller::create(0, $cat->to_record(), $handler);
61
        $this->assertTrue($c instanceof category_controller);
62
    }
63
 
64
    /**
1441 ariadna 65
     * Test creation of category instance from pre-defined object
66
     */
67
    public function test_constructor_from_record(): void {
68
        $this->resetAfterTest();
69
 
70
        // Create field object that matches the persistent/schema definition.
71
        $category = category_controller::create(0, (object) [
72
            'name' => 'Test',
73
            'description' => null,
74
            'descriptionformat' => null,
75
            'component' => 'core_course',
76
            'area' => 'course',
77
            'itemid' => 0,
78
            'sortorder' => null,
79
        ]);
80
 
81
        // Saving the category will validate the persistent internally.
82
        $category->save();
83
 
84
        $this->assertInstanceOf(category_controller::class, $category);
85
    }
86
 
87
    /**
1 efrain 88
     * Test for function \core_customfield\field_controller::create() in case of wrong parameters
89
     */
11 efrain 90
    public function test_constructor_errors(): void {
1 efrain 91
        global $DB;
92
        $this->resetAfterTest();
93
 
94
        $cat = $this->get_generator()->create_category();
95
        $catrecord = $cat->to_record();
96
 
97
        // Both id and record give warning.
98
        $c = category_controller::create($catrecord->id, $catrecord);
99
        $debugging = $this->getDebuggingMessages();
100
        $this->assertEquals(1, count($debugging));
1441 ariadna 101
        $this->assertEquals(
102
            'Too many parameters, either id need to be specified or a record, but not both.',
103
            $debugging[0]->message
104
        );
1 efrain 105
        $this->resetDebugging();
106
        $this->assertTrue($c instanceof category_controller);
107
 
108
        // Retrieve non-existing data.
109
        try {
110
            category_controller::create($catrecord->id + 1);
111
            $this->fail('Expected exception');
112
        } catch (\moodle_exception $e) {
113
            $this->assertEquals('Category not found', $e->getMessage());
114
        }
115
 
116
        // Missing required elements.
117
        try {
118
            category_controller::create(0, (object)['area' => 'course', 'itemid' => 0]);
119
            $this->fail('Expected exception');
120
        } catch (\coding_exception $e) {
121
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
122
                'to initialise category_controller - unknown component', $e->getMessage());
123
        }
124
 
125
        // Missing required elements.
126
        try {
127
            category_controller::create(0, (object)['component' => 'core_course', 'itemid' => 0]);
128
            $this->fail('Expected exception');
129
        } catch (\coding_exception $e) {
130
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
131
                'to initialise category_controller - unknown area', $e->getMessage());
132
        }
133
 
134
        // Missing required elements.
135
        try {
136
            category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course']);
137
            $this->fail('Expected exception');
138
        } catch (\coding_exception $e) {
139
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
140
                'to initialise category_controller - unknown itemid', $e->getMessage());
141
        }
142
 
143
        $handler = \core_course\customfield\course_handler::create();
144
        // Missing required elements.
145
        try {
146
            category_controller::create(0, (object)['component' => 'x', 'area' => 'course', 'itemid' => 0], $handler);
147
            $this->fail('Expected exception');
148
        } catch (\coding_exception $e) {
149
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Component of the handler ' .
150
                'does not match the one from the record', $e->getMessage());
151
        }
152
 
153
        try {
154
            category_controller::create(0, (object)['component' => 'core_course', 'area' => 'x', 'itemid' => 0], $handler);
155
            $this->fail('Expected exception');
156
        } catch (\coding_exception $e) {
157
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Area of the handler ' .
158
                'does not match the one from the record', $e->getMessage());
159
        }
160
 
161
        try {
162
            category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 1], $handler);
163
            $this->fail('Expected exception');
164
        } catch (\coding_exception $e) {
165
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Itemid of the ' .
166
                'handler does not match the one from the record', $e->getMessage());
167
        }
168
 
169
        try {
170
            $user = $this->getDataGenerator()->create_user();
1441 ariadna 171
            category_controller::create(0, (object) [
172
                'component' => 'core_course',
173
                'area' => 'course',
174
                'itemid' => 0,
175
                'contextid' => \context_user::instance($user->id)->id,
176
            ], $handler);
1 efrain 177
            $this->fail('Expected exception');
178
        } catch (\coding_exception $e) {
179
            $this->assertEquals('Coding error detected, it must be fixed by a programmer: Context of the ' .
180
                'handler does not match the one from the record', $e->getMessage());
181
        }
182
    }
183
 
184
    /**
185
     * Tests for behaviour of:
186
     * \core_customfield\category_controller::save()
187
     * \core_customfield\category_controller::get()
188
     */
11 efrain 189
    public function test_create_category(): void {
1 efrain 190
        $this->resetAfterTest();
191
 
192
        // Create the category.
193
        $lpg = $this->get_generator();
194
        $categorydata            = new \stdClass();
195
        $categorydata->name      = 'Category1';
196
        $categorydata->component = 'core_course';
197
        $categorydata->area      = 'course';
198
        $categorydata->itemid    = 0;
199
        $categorydata->contextid = \context_system::instance()->id;
200
        $category = category_controller::create(0, $categorydata);
201
        $category->save();
202
        $this->assertNotEmpty($category->get('id'));
203
 
204
        // Confirm record exists.
205
        $this->assertTrue(\core_customfield\category::record_exists($category->get('id')));
206
 
207
        // Confirm that base data was inserted correctly.
208
        $category = category_controller::create($category->get('id'));
209
        $this->assertSame($category->get('name'), $categorydata->name);
210
        $this->assertSame($category->get('component'), $categorydata->component);
211
        $this->assertSame($category->get('area'), $categorydata->area);
212
        $this->assertSame((int)$category->get('itemid'), $categorydata->itemid);
213
    }
214
 
215
    /**
216
     * Tests for \core_customfield\category_controller::set() behaviour.
217
     */
11 efrain 218
    public function test_rename_category(): void {
1 efrain 219
        $this->resetAfterTest();
220
 
221
        // Create the category.
222
        $params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 'name' => 'Cat1',
223
            'contextid' => \context_system::instance()->id];
224
        $c1 = category_controller::create(0, (object)$params);
225
        $c1->save();
226
        $this->assertNotEmpty($c1->get('id'));
227
 
228
        // Checking new name are correct updated.
229
        $category = category_controller::create($c1->get('id'));
230
        $category->set('name', 'Cat2');
231
        $this->assertSame('Cat2', $category->get('name'));
232
 
233
        // Checking new name are correct updated after save.
234
        $category->save();
235
 
236
        $category = category_controller::create($c1->get('id'));
237
        $this->assertSame('Cat2', $category->get('name'));
238
    }
239
 
240
    /**
241
     * Tests for \core_customfield\category_controller::delete() behaviour.
242
     */
11 efrain 243
    public function test_delete_category(): void {
1 efrain 244
        $this->resetAfterTest();
245
 
246
        // Create the category.
247
        $lpg = $this->get_generator();
248
        $category0 = $lpg->create_category();
249
        $id0 = $category0->get('id');
250
 
251
        $category1 = $lpg->create_category();
252
        $id1 = $category1->get('id');
253
 
254
        $category2 = $lpg->create_category();
255
        $id2 = $category2->get('id');
256
 
257
        // Confirm that exist in the database.
258
        $this->assertTrue(\core_customfield\category::record_exists($id0));
259
 
260
        // Delete and confirm that is deleted.
261
        $category0->delete();
262
        $this->assertFalse(\core_customfield\category::record_exists($id0));
263
 
264
        // Confirm correct order after delete.
265
        // Check order after re-fetch.
266
        $category1 = category_controller::create($id1);
267
        $category2 = category_controller::create($id2);
268
 
269
        $this->assertSame((int) $category1->get('sortorder'), 1);
270
        $this->assertSame((int) $category2->get('sortorder'), 2);
271
    }
272
}