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 - https://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 <https://www.gnu.org/licenses/>.
16
 
17
namespace core\context;
18
 
19
use core\context, core\context_helper;
20
 
21
/**
22
 * Unit tests for module context class.
23
 *
24
 * NOTE: more tests are in lib/tests/accesslib_test.php
25
 *
26
 * @package   core
27
 * @copyright Petr Skoda
28
 * @license   https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @coversDefaultClass \core\context\module
30
 */
1441 ariadna 31
final class module_test extends \advanced_testcase {
1 efrain 32
    /**
33
     * Tests legacy class.
34
     * @coversNothing
35
     */
11 efrain 36
    public function test_legacy_classname(): void {
1 efrain 37
        $this->resetAfterTest();
38
 
39
        $course = $this->getDataGenerator()->create_course();
40
        $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id]);
41
        $context = module::instance($page->cmid);
42
 
43
        $this->assertInstanceOf(module::class, $context);
44
        $this->assertInstanceOf(\context_module::class, $context);
45
    }
46
 
47
    /**
48
     * Tests covered methods.
49
     * @covers ::instance
50
     * @covers \core\context::instance_by_id
51
     */
11 efrain 52
    public function test_factory_methods(): void {
1 efrain 53
        $this->resetAfterTest();
54
 
55
        $course = $this->getDataGenerator()->create_course();
56
        $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id]);
57
        $context = module::instance($page->cmid);
58
 
59
        $this->assertInstanceOf(module::class, $context);
60
        $this->assertSame((string)$page->cmid, $context->instanceid);
61
 
62
        $context = context::instance_by_id($context->id);
63
        $this->assertInstanceOf(module::class, $context);
64
        $this->assertSame((string)$page->cmid, $context->instanceid);
65
    }
66
 
67
    /**
68
     * Tests covered method.
69
     * @covers ::get_short_name
70
     */
11 efrain 71
    public function test_get_short_name(): void {
1 efrain 72
        $this->assertSame('module', module::get_short_name());
73
    }
74
 
75
    /**
76
     * Tests context level.
77
     * @coversNothing
78
     */
11 efrain 79
    public function test_level(): void {
1 efrain 80
        $this->assertSame(70, module::LEVEL);
81
        $this->assertSame(CONTEXT_MODULE, module::LEVEL);
82
    }
83
 
84
    /**
85
     * Tests covered method.
86
     * @covers ::get_level_name
87
     */
11 efrain 88
    public function test_get_level_name(): void {
1 efrain 89
        $this->assertSame('Activity module', module::get_level_name());
90
    }
91
 
92
    /**
93
     * Tests covered method.
94
     * @covers ::get_context_name
95
     */
11 efrain 96
    public function test_get_context_name(): void {
1 efrain 97
        $this->resetAfterTest();
98
 
99
        $course = $this->getDataGenerator()->create_course();
100
        $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
101
        $context = module::instance($page->cmid);
102
 
103
        $this->assertSame('Page: Pokus', $context->get_context_name());
104
        $this->assertSame('Page: Pokus', $context->get_context_name(true));
105
        $this->assertSame('Pokus', $context->get_context_name(false));
106
        $this->assertSame('Pokus', $context->get_context_name(false, true));
107
        $this->assertSame('Page: Pokus', $context->get_context_name(true, true, false));
108
    }
109
 
110
    /**
111
     * Tests covered method.
112
     * @covers ::get_url
113
     */
11 efrain 114
    public function test_get_url(): void {
1 efrain 115
        $this->resetAfterTest();
116
 
117
        $course = $this->getDataGenerator()->create_course();
118
        $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
119
        $context = module::instance($page->cmid);
120
 
121
        $expected = new \moodle_url('/mod/page/view.php', ['id' => $page->cmid]);
122
        $url = $context->get_url();
123
        $this->assertInstanceOf(\moodle_url::class, $url);
124
        $this->assertSame($expected->out(), $url->out());
125
    }
126
 
127
    /**
128
     * Tests covered methods.
129
     * @covers ::get_instance_table()
130
     * @covers ::get_behat_reference_columns()
131
     * @covers \core\context_helper::resolve_behat_reference
132
     */
11 efrain 133
    public function test_resolve_behat_reference(): void {
1 efrain 134
        global $DB;
135
        $this->resetAfterTest();
136
 
137
        $course = $this->getDataGenerator()->create_course();
138
        $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'idnumber' => 'xyz']);
139
        $instance = $DB->get_record('course_modules', ['id' => $page->cmid], '*', MUST_EXIST);
140
        $context = module::instance($instance->id);
141
 
142
        $result = context_helper::resolve_behat_reference('Activity module', $instance->idnumber);
143
        $this->assertSame($context->id, $result->id);
144
 
145
        $result = context_helper::resolve_behat_reference('module', $instance->idnumber);
146
        $this->assertSame($context->id, $result->id);
147
 
148
        $result = context_helper::resolve_behat_reference('70', $instance->idnumber);
149
        $this->assertSame($context->id, $result->id);
150
 
151
        $result = context_helper::resolve_behat_reference('Activity module', 'dshjkdshjkhjsadjhdsa');
152
        $this->assertNull($result);
153
 
154
        $result = context_helper::resolve_behat_reference('Activity module', '');
155
        $this->assertNull($result);
156
    }
157
 
158
    /**
159
     * Tests covered method.
160
     * @covers ::get_compatible_role_archetypes
161
     */
11 efrain 162
    public function test_get_compatible_role_archetypes(): void {
1 efrain 163
        global $DB;
164
 
165
        $allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL');
166
        foreach ($allarchetypes as $allarchetype) {
167
            $levels = context_helper::get_compatible_levels($allarchetype);
168
            if ($allarchetype === 'editingteacher' || $allarchetype === 'teacher' || $allarchetype === 'student') {
169
                $this->assertContains(module::LEVEL, $levels, "$allarchetype is expected to be compatible with context");
170
            } else {
171
                $this->assertNotContains(module::LEVEL, $levels, "$allarchetype is not expected to be compatible with context");
172
            }
173
        }
174
    }
175
 
176
    /**
177
     * Tests covered method.
178
     * @covers ::get_possible_parent_levels
179
     */
11 efrain 180
    public function test_get_possible_parent_levels(): void {
1 efrain 181
        $this->assertSame([course::LEVEL], module::get_possible_parent_levels());
182
    }
183
 
184
    /**
185
     * Tests covered method.
186
     * @covers ::get_capabilities
187
     */
11 efrain 188
    public function test_get_capabilities(): void {
1 efrain 189
        $this->resetAfterTest();
190
 
191
        $course = $this->getDataGenerator()->create_course();
1441 ariadna 192
        $mod = $this->getDataGenerator()->create_module('book', ['course' => $course->id, 'name' => 'Pokus']);
193
        $context = module::instance($mod->cmid);
1 efrain 194
 
195
        $capabilities = $context->get_capabilities();
196
        $capabilities = convert_to_array($capabilities);
197
        $capabilities = array_column($capabilities, 'name');
1441 ariadna 198
 
199
        $this->assertContains('mod/book:read', $capabilities);
200
        $this->assertContains('booktool/exportimscp:export', $capabilities);
201
        $this->assertContains('booktool/importhtml:import', $capabilities);
1 efrain 202
        $this->assertNotContains('mod/url:view', $capabilities);
203
        $this->assertNotContains('moodle/course:view', $capabilities);
204
        $this->assertNotContains('moodle/category:manage', $capabilities);
205
        $this->assertNotContains('moodle/user:viewalldetails', $capabilities);
206
    }
207
 
208
    /**
209
     * Tests covered method.
210
     * @covers ::create_level_instances
211
     */
11 efrain 212
    public function test_create_level_instances(): void {
1 efrain 213
        global $DB;
214
        $this->resetAfterTest();
215
 
216
        $course = $this->getDataGenerator()->create_course();
217
        $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
218
        $context = module::instance($page->cmid);
219
 
220
        $DB->delete_records('context', ['id' => $context->id]);
221
        context_helper::create_instances(module::LEVEL);
222
        $record = $DB->get_record('context', ['contextlevel' => module::LEVEL, 'instanceid' => $page->cmid], '*', MUST_EXIST);
223
    }
224
 
225
    /**
226
     * Tests covered method.
227
     * @covers ::get_child_contexts
228
     */
11 efrain 229
    public function test_get_child_contexts(): void {
1 efrain 230
        $this->resetAfterTest();
231
 
232
        $course = $this->getDataGenerator()->create_course();
233
        $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
234
        $context = module::instance($page->cmid);
235
 
236
        $children = $context->get_child_contexts();
237
        $this->assertCount(0, $children);
238
    }
239
 
240
    /**
241
     * Tests covered method.
242
     * @covers ::get_cleanup_sql
243
     */
11 efrain 244
    public function test_get_cleanup_sql(): void {
1 efrain 245
        global $DB;
246
        $this->resetAfterTest();
247
 
248
        $course = $this->getDataGenerator()->create_course();
249
        $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
250
        $context = module::instance($page->cmid);
251
 
252
        $DB->delete_records('course_modules', ['id' => $page->cmid]);
253
        $DB->delete_records('page', ['id' => $page->id]);
254
 
255
        context_helper::cleanup_instances();
256
        $this->assertFalse($DB->record_exists('context', ['contextlevel' => module::LEVEL, 'instanceid' => $page->cmid]));
257
    }
258
 
259
    /**
260
     * Tests covered method.
261
     * @covers ::build_paths
262
     */
11 efrain 263
    public function test_build_paths(): void {
1 efrain 264
        global $DB;
265
        $this->resetAfterTest();
266
 
267
        $syscontext = system::instance();
268
        $course = $this->getDataGenerator()->create_course();
269
        $coursecontext = course::instance($course->id);
270
        $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
271
        $pagecontext = module::instance($page->cmid);
272
 
273
        $DB->set_field('context', 'depth', 1, ['id' => $pagecontext->id]);
274
        $DB->set_field('context', 'path', '/0', ['id' => $pagecontext->id]);
275
 
276
        context_helper::build_all_paths(true);
277
 
278
        $record = $DB->get_record('context', ['id' => $pagecontext->id]);
279
        $categorycontext = coursecat::instance($course->category);
280
        $this->assertSame('4', $record->depth);
281
        $this->assertSame('/' . $syscontext->id . '/' . $categorycontext->id . '/'
282
            . $coursecontext->id . '/' . $record->id, $record->path);
283
    }
284
 
285
    /**
286
     * Tests covered method.
287
     * @covers ::set_locked
288
     */
11 efrain 289
    public function test_set_locked(): void {
1 efrain 290
        global $DB;
291
        $this->resetAfterTest();
292
 
293
        $course = $this->getDataGenerator()->create_course();
294
        $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id, 'name' => 'Pokus']);
295
        $context = module::instance($page->cmid);
296
 
297
        $context->set_locked(true);
298
        $context = module::instance($page->cmid);
299
        $this->assertTrue($context->locked);
300
        $record = $DB->get_record('context', ['id' => $context->id]);
301
        $this->assertSame('1', $record->locked);
302
 
303
        $context->set_locked(false);
304
        $context = module::instance($page->cmid);
305
        $this->assertFalse($context->locked);
306
        $record = $DB->get_record('context', ['id' => $context->id]);
307
        $this->assertSame('0', $record->locked);
308
    }
309
}