Proyectos de Subversion Moodle

Rev

Rev 1 | | 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 block 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\block
30
 */
31
class block_test extends \advanced_testcase {
32
    /**
33
     * Tests legacy class name.
34
     * @covers \context_block
35
     */
11 efrain 36
    public function test_legacy_classname(): void {
1 efrain 37
        $this->resetAfterTest();
38
 
39
        $block = $this->getDataGenerator()->create_block('online_users');
40
        $context = block::instance($block->id);
41
 
42
        $this->assertInstanceOf(block::class, $context);
43
        $this->assertInstanceOf(\context_block::class, $context);
44
    }
45
 
46
    /**
47
     * Tests covered methods.
48
     * @covers ::instance
49
     * @covers \core\context::instance_by_id
50
     */
11 efrain 51
    public function test_factory_methods(): void {
1 efrain 52
        $this->resetAfterTest();
53
 
54
        $block = $this->getDataGenerator()->create_block('online_users');
55
        $context = block::instance($block->id);
56
 
57
        $this->assertInstanceOf(block::class, $context);
58
        $this->assertSame((string)$block->id, $context->instanceid);
59
 
60
        $context = context::instance_by_id($context->id);
61
        $this->assertInstanceOf(block::class, $context);
62
        $this->assertSame((string)$block->id, $context->instanceid);
63
    }
64
 
65
    /**
66
     * Tests covered method.
67
     * @covers ::get_short_name
68
     */
11 efrain 69
    public function test_get_short_name(): void {
1 efrain 70
        $this->assertSame('block', block::get_short_name());
71
    }
72
 
73
    /**
74
     * Tests levels.
75
     * @coversNothing
76
     */
11 efrain 77
    public function test_level(): void {
1 efrain 78
        $this->assertSame(80, block::LEVEL);
79
        $this->assertSame(CONTEXT_BLOCK, block::LEVEL);
80
    }
81
 
82
    /**
83
     * Tests covered method.
84
     * @covers ::get_level_name
85
     */
11 efrain 86
    public function test_get_level_name(): void {
1 efrain 87
        $this->assertSame('Block', block::get_level_name());
88
    }
89
 
90
    /**
91
     * Tests covered method.
92
     * @covers ::get_context_name
93
     */
11 efrain 94
    public function test_get_context_name(): void {
1 efrain 95
        $this->resetAfterTest();
96
 
97
        $block = $this->getDataGenerator()->create_block('online_users');
98
        $context = block::instance($block->id);
99
 
100
        $this->assertSame('Block: Online users', $context->get_context_name());
101
        $this->assertSame('Block: Online users', $context->get_context_name(true));
102
        $this->assertSame('Online users', $context->get_context_name(false));
103
        $this->assertSame('Online users', $context->get_context_name(false, true));
104
        $this->assertSame('Block: Online users', $context->get_context_name(true, true, false));
105
    }
106
 
107
    /**
108
     * Tests covered method.
109
     * @covers ::get_url
110
     */
11 efrain 111
    public function test_get_url(): void {
1 efrain 112
        $this->resetAfterTest();
113
 
114
        $block = $this->getDataGenerator()->create_block('online_users');
115
        $context = block::instance($block->id);
116
 
117
        $expected = new \moodle_url('/');
118
        $url = $context->get_url();
119
        $this->assertInstanceOf(\moodle_url::class, $url);
120
        $this->assertSame($expected->out(), $url->out());
121
    }
122
 
123
    /**
124
     * Tests covered method.
125
     * @covers ::get_compatible_role_archetypes
126
     */
11 efrain 127
    public function test_get_compatible_role_archetypes(): void {
1 efrain 128
        global $DB;
129
 
130
        $allarchetypes = $DB->get_fieldset_select('role', 'DISTINCT archetype', 'archetype IS NOT NULL');
131
        foreach ($allarchetypes as $allarchetype) {
132
            $levels = context_helper::get_compatible_levels($allarchetype);
133
            $this->assertNotContains(block::LEVEL, $levels, "$allarchetype is not expected to be compatible with context");
134
        }
135
    }
136
 
137
    /**
138
     * Tests covered method.
139
     * @covers ::get_possible_parent_levels
140
     */
11 efrain 141
    public function test_get_possible_parent_levels(): void {
1 efrain 142
        $result = block::get_possible_parent_levels();
143
        // All except itself.
144
        $this->assertContains(system::LEVEL, $result);
145
        $this->assertContains(user::LEVEL, $result);
146
        $this->assertContains(coursecat::LEVEL, $result);
147
        $this->assertContains(course::LEVEL, $result);
148
        $this->assertContains(module::LEVEL, $result);
149
        $this->assertNotContains(block::LEVEL, $result);
150
 
151
        // Make sure plugin contexts are covered too.
152
        $all = \core\context_helper::get_all_levels();
153
        $this->assertCount(count($all) - 1, $result);
154
    }
155
 
156
    /**
157
     * Tests covered method.
158
     * @covers ::get_capabilities
159
     */
11 efrain 160
    public function test_get_capabilities(): void {
1 efrain 161
        $this->resetAfterTest();
162
 
163
        $block = $this->getDataGenerator()->create_block('online_users');
164
        $context = block::instance($block->id);
165
 
166
        $capabilities = $context->get_capabilities();
167
        $capabilities = convert_to_array($capabilities);
168
        $capabilities = array_column($capabilities, 'name');
169
        $this->assertNotContains('moodle/site:config', $capabilities);
170
        $this->assertNotContains('moodle/course:view', $capabilities);
171
        $this->assertNotContains('moodle/category:manage', $capabilities);
172
        $this->assertNotContains('moodle/user:viewalldetails', $capabilities);
173
        $this->assertNotContains('mod/page:view', $capabilities);
174
        $this->assertNotContains('mod/url:view', $capabilities);
175
    }
176
 
177
    /**
178
     * Tests covered method.
179
     * @covers ::create_level_instances
180
     */
11 efrain 181
    public function test_create_level_instances(): void {
1 efrain 182
        global $DB;
183
        $this->resetAfterTest();
184
 
185
        $block = $this->getDataGenerator()->create_block('online_users');
186
        $context = block::instance($block->id);
187
 
188
        $DB->delete_records('context', ['id' => $context->id]);
189
        context_helper::create_instances(block::LEVEL);
190
        $record = $DB->get_record('context', ['contextlevel' => block::LEVEL, 'instanceid' => $block->id], '*', MUST_EXIST);
191
    }
192
 
193
    /**
194
     * Tests covered method.
195
     * @covers ::get_child_contexts
196
     */
11 efrain 197
    public function test_get_child_contexts(): void {
1 efrain 198
        $this->resetAfterTest();
199
 
200
        $block = $this->getDataGenerator()->create_block('online_users');
201
        $context = block::instance($block->id);
202
 
203
        $children = $context->get_child_contexts();
204
        $this->assertCount(0, $children);
205
    }
206
 
207
    /**
208
     * Tests covered method.
209
     * @covers ::get_cleanup_sql
210
     */
11 efrain 211
    public function test_get_cleanup_sql(): void {
1 efrain 212
        global $DB;
213
        $this->resetAfterTest();
214
 
215
        $block = $this->getDataGenerator()->create_block('online_users');
216
        $context = block::instance($block->id);
217
 
218
        $DB->delete_records('block_instances', ['id' => $block->id]);
219
 
220
        context_helper::cleanup_instances();
221
        $this->assertFalse($DB->record_exists('context', ['contextlevel' => block::LEVEL, 'instanceid' => $block->id]));
222
    }
223
 
224
    /**
225
     * Tests covered method.
226
     * @covers ::build_paths
227
     */
11 efrain 228
    public function test_build_paths(): void {
1 efrain 229
        global $DB;
230
        $this->resetAfterTest();
231
 
232
        $syscontext = system::instance();
233
        $block = $this->getDataGenerator()->create_block('online_users');
234
        $context = block::instance($block->id);
235
 
236
        $DB->set_field('context', 'depth', 1, ['id' => $context->id]);
237
        $DB->set_field('context', 'path', '/0', ['id' => $context->id]);
238
 
239
        context_helper::build_all_paths(true);
240
 
241
        $record = $DB->get_record('context', ['id' => $context->id]);
242
        $this->assertSame('2', $record->depth);
243
        $this->assertSame('/' . $syscontext->id . '/' . $record->id, $record->path);
244
    }
245
 
246
    /**
247
     * Tests covered method.
248
     * @covers ::set_locked
249
     */
11 efrain 250
    public function test_set_locked(): void {
1 efrain 251
        global $DB;
252
        $this->resetAfterTest();
253
 
254
        $block = $this->getDataGenerator()->create_block('online_users');
255
        $context = block::instance($block->id);
256
 
257
        $context->set_locked(true);
258
        $context = block::instance($block->id);
259
        $this->assertTrue($context->locked);
260
        $record = $DB->get_record('context', ['id' => $context->id]);
261
        $this->assertSame('1', $record->locked);
262
 
263
        $context->set_locked(false);
264
        $context = block::instance($block->id);
265
        $this->assertFalse($context->locked);
266
        $record = $DB->get_record('context', ['id' => $context->id]);
267
        $this->assertSame('0', $record->locked);
268
    }
269
}