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;
18
 
19
use block_contents;
20
use custom_menu;
21
use custom_menu_item;
22
use paging_bar;
23
use renderer_base;
24
use single_button;
25
use single_select;
26
use theme_config;
27
use url_select;
1441 ariadna 28
use core\output\user_picture;
1 efrain 29
 
30
/**
31
 * Unit tests for lib/outputcomponents.php.
32
 *
33
 * @package   core
34
 * @category  test
35
 * @copyright 2011 David Mudrak <david@moodle.com>
36
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
1441 ariadna 38
final class outputcomponents_test extends \advanced_testcase {
1 efrain 39
    /**
40
     * Tests user_picture::fields.
41
     *
42
     * @deprecated since Moodle 3.11 MDL-45242
43
     */
11 efrain 44
    public function test_fields_aliasing(): void {
1 efrain 45
        $fields = user_picture::fields();
46
        $fields = array_map('trim', explode(',', $fields));
47
        $this->assertTrue(in_array('id', $fields));
48
 
49
        $aliased = array();
50
        foreach ($fields as $field) {
51
            if ($field === 'id') {
52
                $aliased['id'] = 'aliasedid';
53
            } else {
54
                $aliased[$field] = 'prefix'.$field;
55
            }
56
        }
57
 
58
        $returned = user_picture::fields('', array('custom1', 'id'), 'aliasedid', 'prefix');
59
        $returned = array_map('trim', explode(',', $returned));
60
        $this->assertEquals(count($returned), count($fields) + 1); // Only one extra field added.
61
 
62
        foreach ($fields as $field) {
63
            if ($field === 'id') {
64
                $expected = "id AS aliasedid";
65
            } else {
66
                $expected = "$field AS prefix$field";
67
            }
68
            $this->assertContains($expected, $returned, "Expected pattern '$expected' not returned");
69
        }
70
        $this->assertContains("custom1 AS prefixcustom1", $returned, "Expected pattern 'custom1 AS prefixcustom1' not returned");
71
 
72
        // Deprecation warnings for user_picture::fields.
73
        $this->assertDebuggingCalledCount(2);
74
    }
75
 
76
    /**
77
     * Tests user_picture::unalias.
78
     */
11 efrain 79
    public function test_fields_unaliasing(): void {
1 efrain 80
        $fields = implode(',', \core_user\fields::get_picture_fields());
81
        $fields = array_map('trim', explode(',', $fields));
82
 
83
        $fakerecord = new \stdClass();
84
        $fakerecord->aliasedid = 42;
85
        foreach ($fields as $field) {
86
            if ($field !== 'id') {
87
                $fakerecord->{'prefix'.$field} = "Value of $field";
88
            }
89
        }
90
        $fakerecord->prefixcustom1 = 'Value of custom1';
91
 
92
        $returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix');
93
 
94
        $this->assertEquals(42, $returned->id);
95
        foreach ($fields as $field) {
96
            if ($field !== 'id') {
97
                $this->assertSame("Value of $field", $returned->{$field});
98
            }
99
        }
100
        $this->assertSame('Value of custom1', $returned->custom1);
101
    }
102
 
103
    /**
104
     * Tests user_picture::unalias with null values.
105
     */
11 efrain 106
    public function test_fields_unaliasing_null(): void {
1 efrain 107
        $fields = implode(',', \core_user\fields::get_picture_fields());
108
        $fields = array_map('trim', explode(',', $fields));
109
 
110
        $fakerecord = new \stdClass();
111
        $fakerecord->aliasedid = 42;
112
        foreach ($fields as $field) {
113
            if ($field !== 'id') {
114
                $fakerecord->{'prefix'.$field} = "Value of $field";
115
            }
116
        }
117
        $fakerecord->prefixcustom1 = 'Value of custom1';
118
        $fakerecord->prefiximagealt = null;
119
 
120
        $returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix');
121
 
122
        $this->assertEquals(42, $returned->id);
123
        $this->assertNull($returned->imagealt);
124
        foreach ($fields as $field) {
125
            if ($field !== 'id' and $field !== 'imagealt') {
126
                $this->assertSame("Value of $field", $returned->{$field});
127
            }
128
        }
129
        $this->assertSame('Value of custom1', $returned->custom1);
130
    }
131
 
11 efrain 132
    public function test_get_url(): void {
1 efrain 133
        global $DB, $CFG, $USER;
134
 
135
        $this->resetAfterTest();
136
 
137
        // Verify new install contains expected defaults.
138
        $this->assertSame(theme_config::DEFAULT_THEME, $CFG->theme);
139
        $this->assertEquals(1, $CFG->slasharguments);
140
        $this->assertEquals(1, $CFG->themerev);
141
        $this->assertEquals(0, $CFG->themedesignermode);
142
        $this->assertSame('https://www.example.com/moodle', $CFG->wwwroot);
143
        $this->assertEquals(0, $CFG->enablegravatar);
144
        $this->assertSame('mm', $CFG->gravatardefaulturl);
145
 
146
        // Create some users.
147
        $page = new \moodle_page();
148
        $page->set_url('/user/profile.php');
149
        $page->set_context(\context_system::instance());
150
        $renderer = $page->get_renderer('core');
151
 
152
        $user1 = $this->getDataGenerator()->create_user(array('picture'=>11, 'email'=>'user1@example.com'));
153
        $context1 = \context_user::instance($user1->id);
154
        $user2 = $this->getDataGenerator()->create_user(array('picture'=>0, 'email'=>'user2@example.com'));
155
        $context2 = \context_user::instance($user2->id);
156
 
157
        // User 3 is deleted.
158
        $user3 = $this->getDataGenerator()->create_user(array('picture'=>1, 'deleted'=>1, 'email'=>'user3@example.com'));
159
        $this->assertNotEmpty(\context_user::instance($user3->id));
160
        $this->assertEquals(0, $user3->picture);
161
        $this->assertNotEquals('user3@example.com', $user3->email);
162
 
163
        // User 4 is incorrectly deleted with its context deleted as well (testing legacy code).
164
        $user4 = $this->getDataGenerator()->create_user(['picture' => 1, 'deleted' => 1, 'email' => 'user4@example.com']);
165
        \context_helper::delete_instance(CONTEXT_USER, $user4->id);
166
        $this->assertEquals(0, $user4->picture);
167
        $this->assertNotEquals('user4@example.com', $user4->email);
168
 
169
        // Try legacy picture == 1.
170
        $user1->picture = 1;
171
        $up1 = new user_picture($user1);
172
        $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=1', $up1->get_url($page, $renderer)->out(false));
173
        $user1->picture = 11;
174
 
175
        // Try valid user with picture when user context is not cached - 1 query expected.
176
        \context_helper::reset_caches();
177
        $reads = $DB->perf_get_reads();
178
        $up1 = new user_picture($user1);
179
        $this->assertEquals($reads, $DB->perf_get_reads());
180
        $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
181
        $this->assertEquals($reads+1, $DB->perf_get_reads());
182
 
183
        // Try valid user with contextid hint - no queries expected.
184
        $user1->contextid = $context1->id;
185
        \context_helper::reset_caches();
186
        $reads = $DB->perf_get_reads();
187
        $up1 = new user_picture($user1);
188
        $this->assertEquals($reads, $DB->perf_get_reads());
189
        $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
190
        $this->assertEquals($reads, $DB->perf_get_reads());
191
 
192
        // Try valid user without image - no queries expected.
193
        \context_helper::reset_caches();
194
        $reads = $DB->perf_get_reads();
195
        $up2 = new user_picture($user2);
196
        $this->assertEquals($reads, $DB->perf_get_reads());
197
        $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up2->get_url($page, $renderer)->out(false));
198
        $this->assertEquals($reads, $DB->perf_get_reads());
199
 
200
        // Try guessing of deleted users - no queries expected.
201
        unset($user3->deleted);
202
        \context_helper::reset_caches();
203
        $reads = $DB->perf_get_reads();
204
        $up3 = new user_picture($user3);
205
        $this->assertEquals($reads, $DB->perf_get_reads());
206
        $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
207
        $this->assertEquals($reads, $DB->perf_get_reads());
208
 
209
        // Try incorrectly deleted users (with valid email and picture flag, but user context removed) - some DB reads expected.
210
        unset($user4->deleted);
211
        $user4->email = 'user4@example.com';
212
        $user4->picture = 1;
213
        $reads = $DB->perf_get_reads();
214
        $up4 = new user_picture($user4);
215
        $this->assertEquals($reads, $DB->perf_get_reads());
216
        $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up4->get_url($page, $renderer)->out(false));
217
        $this->assertGreaterThan($reads, $DB->perf_get_reads());
218
 
219
        // Test gravatar.
220
        set_config('enablegravatar', 1);
221
 
222
        // Deleted user can not have gravatar.
223
        $user3->email = 'deleted';
224
        $user3->picture = 0;
225
        $up3 = new user_picture($user3);
226
        $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
227
        $user4->email = 'deleted';
228
        $user4->picture = 0;
229
        $up4 = new user_picture($user4);
230
        $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up4->get_url($page, $renderer)->out(false));
231
 
232
        // Http version.
233
        $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
234
 
235
        // Verify defaults to misteryman (mm).
236
        $up2 = new user_picture($user2);
237
        $this->assertSame('http://www.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=mm', $up2->get_url($page, $renderer)->out(false));
238
 
239
        // Without gravatardefaulturl, verify we pick own file.
240
        set_config('gravatardefaulturl', '');
241
        $up2 = new user_picture($user2);
242
        $this->assertSame('http://www.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=http%3A%2F%2Fwww.example.com%2Fmoodle%2Fpix%2Fu%2Ff2.png', $up2->get_url($page, $renderer)->out(false));
243
        // Uploaded image takes precedence before gravatar.
244
        $up1 = new user_picture($user1);
245
        $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
246
 
247
        // Uploaded image with token-based access for current user.
248
        $up1 = new user_picture($user1);
249
        $up1->includetoken = true;
250
        $token = get_user_key('core_files', $USER->id);
251
        $this->assertSame($CFG->wwwroot.'/tokenpluginfile.php/'.$token.'/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
252
 
253
        // Uploaded image with token-based access for other user.
254
        $up1 = new user_picture($user1);
255
        $up1->includetoken = $user2->id;
256
        $token = get_user_key('core_files', $user2->id);
257
        $this->assertSame($CFG->wwwroot.'/tokenpluginfile.php/'.$token.'/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
258
 
259
        // Https version.
260
        $CFG->wwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
261
 
262
        $up1 = new user_picture($user1);
263
        $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
264
 
265
        $up2 = new user_picture($user2);
266
        $this->assertSame('https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=https%3A%2F%2Fwww.example.com%2Fmoodle%2Fpix%2Fu%2Ff2.png', $up2->get_url($page, $renderer)->out(false));
267
 
268
        $up3 = new user_picture($user3);
269
        $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
270
 
271
        $up4 = new user_picture($user4);
272
        $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up4->get_url($page, $renderer)->out(false));
273
 
274
        // TODO MDL-44792 Rewrite those tests to use a fixture.
275
        // Now test gravatar with one theme having own images (afterburner).
276
        // $this->assertFileExists("$CFG->dirroot/theme/afterburner/config.php");
277
        // set_config('theme', 'afterburner');
278
        // $page = new \moodle_page();
279
        // $page->set_url('/user/profile.php');
280
        // $page->set_context(\context_system::instance());
281
        // $renderer = $page->get_renderer('core');
282
 
283
        // $up2 = new user_picture($user2);
284
        // $this->assertEquals('http://www.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=http%3A%2F%2Fwww.example.com%2Fmoodle%2Ftheme%2Fafterburner%2Fpix_core%2Fu%2Ff2.png', $up2->get_url($page, $renderer)->out(false));
285
 
286
        // $up2 = new user_picture($user2);
287
        // $this->assertSame('https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=https%3A%2F%2Fwww.example.com%2Fmoodle%2Ftheme%2Fafterburner%2Fpix_core%2Fu%2Ff2.png', $up2->get_url($page, $renderer)->out(false));
288
        // End of gravatar tests.
289
 
290
        // Test themed images.
291
        // set_config('enablegravatar', 0);
292
        // $this->assertFileExists("$CFG->dirroot/theme/formal_white/config.php"); // Use any other theme.
293
        // set_config('theme', 'formal_white');
294
        // $page = new \moodle_page();
295
        // $page->set_url('/user/profile.php');
296
        // $page->set_context(\context_system::instance());
297
        // $renderer = $page->get_renderer('core');
298
 
299
        // $up1 = new user_picture($user1);
300
        // $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/formal_white/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
301
 
302
        // $up2 = new user_picture($user2);
303
        // $this->assertSame($CFG->wwwroot.'/theme/image.php/formal_white/core/1/u/f2', $up2->get_url($page, $renderer)->out(false));
304
 
305
        // Test non-slashargument images.
306
        set_config('theme', 'classic');
307
        $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
308
        $CFG->slasharguments = 0;
309
        $page = new \moodle_page();
310
        $page->set_url('/user/profile.php');
311
        $page->set_context(\context_system::instance());
312
        $renderer = $page->get_renderer('core');
313
 
314
        $up3 = new user_picture($user3);
315
        $this->assertSame($CFG->wwwroot.'/theme/image.php?theme=classic&component=core&rev=1&image=u%2Ff2', $up3->get_url($page, $renderer)->out(false));
316
    }
317
 
11 efrain 318
    public function test_empty_menu(): void {
1 efrain 319
        $emptymenu = new custom_menu();
320
        $this->assertInstanceOf(custom_menu::class, $emptymenu);
321
        $this->assertFalse($emptymenu->has_children());
322
    }
323
 
11 efrain 324
    public function test_basic_syntax(): void {
1 efrain 325
        $definition = <<<EOF
326
Moodle community|http://moodle.org
327
-Moodle free support|http://moodle.org/support
328
-Moodle development|http://moodle.org/development
329
--Moodle Tracker|http://tracker.moodle.org
330
--Moodle Docs|http://docs.moodle.org
331
-Moodle News|http://moodle.org/news
332
Moodle company||Moodle trust pty
333
-Hosting|http://moodle.com/hosting|Commercial hosting
334
-Support|http://moodle.com/support|Commercial support
335
EOF;
336
 
337
        $menu = new custom_menu($definition);
338
        $this->assertInstanceOf('custom_menu', $menu);
339
        $this->assertTrue($menu->has_children());
340
        $firstlevel = $menu->get_children();
341
        $this->assertTrue(is_array($firstlevel));
342
        $this->assertCount(2, $firstlevel);
343
 
344
        $item = array_shift($firstlevel);
345
        $this->assertInstanceOf('custom_menu_item', $item);
346
        $this->assertTrue($item->has_children());
347
        $this->assertCount(3, $item->get_children());
348
        $this->assertEquals('Moodle community', $item->get_text());
349
        $itemurl = $item->get_url();
350
        $this->assertTrue($itemurl instanceof \moodle_url);
351
        $this->assertEquals('http://moodle.org', $itemurl->out());
352
        $this->assertNull($item->get_title()); // Implicit title.
353
 
354
        /** @var custom_menu_item $item */
355
        $item = array_shift($firstlevel);
356
        $this->assertTrue($item->has_children());
357
        $this->assertCount(2, $item->get_children());
358
        $this->assertSame('Moodle company', $item->get_text());
359
        $this->assertNull($item->get_url());
360
        $this->assertSame('Moodle trust pty', $item->get_title());
361
 
362
        $children = $item->get_children();
363
        $subitem = array_shift($children);
364
        $this->assertFalse($subitem->has_children());
365
        $this->assertSame('Hosting', $subitem->get_text());
366
        $this->assertSame('Commercial hosting', $subitem->get_title());
367
    }
368
 
11 efrain 369
    public function test_custommenu_mulitlang(): void {
1 efrain 370
        $definition = <<<EOF
371
Start|http://school.info
372
Info
373
-English|http://school.info/en|Information in English|en
374
--Nested under English
375
--I will be lost|||de
376
-Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
377
--Nested under Deutsch
378
--I will be lost|||en
379
kontaktieren Sie uns|contactus.php||de
380
Contact us|contactus.php||en
381
EOF;
382
        $definitionen = <<<EOF
383
Start|http://school.info
384
Info
385
-English|http://school.info/en|Information in English|en
386
--Nested under English
387
Contact us|contactus.php||en
388
EOF;
389
        $definitionde = <<<EOF
390
Start|http://school.info
391
Info
392
-Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
393
--Nested under Deutsch
394
kontaktieren Sie uns|contactus.php||de
395
EOF;
396
 
397
        $definitiondedu = <<<EOF
398
Start|http://school.info
399
Info
400
-Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
401
--Nested under Deutsch
402
EOF;
403
 
404
        $parsed = $this->custommenu_out(new custom_menu($definition));
405
        $parseden = $this->custommenu_out(new custom_menu($definition, 'en'));
406
        $parsedde = $this->custommenu_out(new custom_menu($definition, 'de'));
407
        $parseddedu = $this->custommenu_out(new custom_menu($definition, 'de_du'));
408
 
409
        $actualen = $this->custommenu_out(new custom_menu($definitionen, 'en'));
410
        $actualde = $this->custommenu_out(new custom_menu($definitionde, 'de'));
411
        $actualdedu = $this->custommenu_out(new custom_menu($definitiondedu, 'de_du'));
412
 
413
        $this->assertSame($actualen, $parseden, 'The parsed English menu does not match the expected English menu');
414
        $this->assertSame($actualde, $parsedde, 'The parsed German menu does not match the expected German menu');
415
        $this->assertSame($actualdedu, $parseddedu, 'The parsed German [Du] menu does not match the expected German [Du] menu');
416
 
417
        $this->assertNotSame($parsed, $parsedde, 'The menu without language is the same as the German menu. They should differ!');
418
        $this->assertNotSame($parsed, $parseden, 'The menu without language is the same as the English menu. They should differ!');
419
        $this->assertNotSame($parsed, $parseddedu, 'The menu without language is the same as the German [Du] menu. They should differ!');
420
        $this->assertNotSame($parseden, $parsedde, 'The English menu is the same as the German menu. They should differ!');
421
        $this->assertNotSame($parseden, $parseddedu, 'The English menu is the same as the German [Du] menu. They should differ!');
422
        $this->assertNotSame($parseddedu, $parsedde, 'The German [Du] menu is the same as the German menu. They should differ!');
423
    }
424
 
425
    /**
426
     * Support function that takes a custom_menu_item and converts it to a string.
427
     *
428
     * @param custom_menu_item $item
429
     * @param int $depth
430
     * @return string
431
     */
432
    protected function custommenu_out(custom_menu_item $item, $depth = 0) {
433
        $str = str_repeat('-', $depth);
434
        $str .= $item->get_text();
435
        $str .= '|' . $item->get_url();
436
        $str .= '|' . $item->get_title();
437
        if ($item->has_children()) {
438
            $str .= '|' . count($item->get_children());
439
            foreach ($item->get_children() as $child) {
440
                $str .= "\n" . $this->custommenu_out($child, $depth + 1);
441
            }
442
        }
443
        return $str;
444
    }
445
 
11 efrain 446
    public function test_prepare(): void {
1 efrain 447
        $expecteda = array('<span class="current-page">1</span>',
448
            '<a href="index.php?page=1">2</a>',
449
            '<a href="index.php?page=2">3</a>',
450
            '<a href="index.php?page=3">4</a>',
451
            '<a href="index.php?page=4">5</a>',
452
            '<a href="index.php?page=5">6</a>',
453
            '<a href="index.php?page=6">7</a>',
454
            '<a href="index.php?page=7">8</a>',
455
        );
456
        $expectedb = array('<a href="page?page=3">4</a>',
457
            '<a href="page?page=4">5</a>',
458
            '<span class="current-page">6</span>',
459
            '<a href="page?page=6">7</a>',
460
            '<a href="page?page=7">8</a>',
461
        );
462
 
463
        $mpage = new \moodle_page();
464
        $rbase = new renderer_base($mpage, "/");
465
        $pbara = new paging_bar(40, 0, 5, 'index.php');
466
        $pbara->prepare($rbase, $mpage, "/");
467
        $pbarb = new paging_bar(100, 5, 5, 'page');
468
        $pbarb->maxdisplay = 5;
469
        $pbarb->prepare($rbase, $mpage, "/");
470
 
471
        $this->assertEquals($expecteda, $pbara->pagelinks);
472
        $this->assertEquals($expectedb, $pbarb->pagelinks);
473
    }
474
 
11 efrain 475
    public function test_pix_icon(): void {
1 efrain 476
        $this->resetAfterTest();
477
 
478
        $page = new \moodle_page();
479
 
480
        set_config('theme', 'boost');
481
        // Need to reset after changing theme.
482
        $page->reset_theme_and_output();
483
        $renderer = $page->get_renderer('core');
484
 
485
        $reason = 'An icon with no alt text is hidden from screenreaders.';
486
        $this->assertStringContainsString('aria-hidden="true"', $renderer->pix_icon('t/print', ''), $reason);
487
 
488
        $reason = 'An icon with alt text is not hidden from screenreaders.';
489
        $this->assertStringNotContainsString('aria-hidden="true"', $renderer->pix_icon('t/print', 'Print'), $reason);
490
 
491
        // Test another theme with a different icon system.
492
        set_config('theme', 'classic');
493
        // Need to reset after changing theme.
494
        $page->reset_theme_and_output();
495
        $renderer = $page->get_renderer('core');
496
 
497
        $reason = 'An icon with no alt text is hidden from screenreaders.';
498
        $this->assertStringContainsString('aria-hidden="true"', $renderer->pix_icon('t/print', ''), $reason);
499
 
500
        $reason = 'An icon with alt text is not hidden from screenreaders.';
501
        $this->assertStringNotContainsString('aria-hidden="true"', $renderer->pix_icon('t/print', 'Print'), $reason);
502
    }
503
 
504
    /**
505
     * Test for checking the template context data for the single_select element.
506
     */
11 efrain 507
    public function test_single_select(): void {
1 efrain 508
        global $PAGE;
509
 
510
        $fakename = 'fakename';
511
        $fakeclass = 'fakeclass';
512
        $faketitle = 'faketitle';
513
        $fakedisabled = true;
514
        $fakefor = 'fakefor';
515
 
516
        $someid = 'someid';
517
        $realname = 'realname';
518
        $realclass = 'realclass';
519
        $realtitle = 'realtitle';
520
        $realdisabled = false;
521
        $reallabel = 'Some cool label';
522
        $labelclass = 'somelabelclass';
523
        $labelstyle = 'font-weight: bold';
524
 
525
        $dataaction = 'actiondata';
526
        $dataother = 'otherdata';
527
 
528
        $attributes = [
529
            'id' => $someid,
530
            'class' => $fakeclass,
531
            'title' => $faketitle,
532
            'disabled' => $fakedisabled,
533
            'name' => $fakename,
534
            'data-action' => $dataaction,
535
            'data-other' => $dataother,
536
        ];
537
        $labelattributes = [
538
            'for' => $fakefor,
539
            'class' => $labelclass,
540
            'style' => $labelstyle
541
        ];
542
 
543
        $options = [ "Option A", "Option B", "Option C" ];
544
        $nothing = ['' => 'choosedots'];
545
 
546
        $url = new \moodle_url('/');
547
 
548
        $singleselect = new single_select($url, $realname, $options, null, $nothing, 'someformid');
549
        $singleselect->class = $realclass;
550
        $singleselect->tooltip = $realtitle;
551
        $singleselect->disabled = $realdisabled;
552
        $singleselect->attributes = $attributes;
553
        $singleselect->label = $reallabel;
554
        $singleselect->labelattributes = $labelattributes;
555
 
556
        $renderer = $PAGE->get_renderer('core');
557
        $data = $singleselect->export_for_template($renderer);
558
 
559
        $this->assertEquals($realtitle, $data->title);
560
        $this->assertEquals($singleselect->class, $data->classes);
561
        $this->assertEquals($realname, $data->name);
562
        $this->assertEquals($reallabel, $data->label);
563
        $this->assertEquals($realdisabled, $data->disabled);
564
        $this->assertEquals($someid, $data->id);
565
 
566
        // Validate attributes array.
567
        // The following should not be included: id, class, name, disabled.
568
        $this->assertFalse(in_array(['name' => 'id', 'value' => $someid], $data->attributes));
569
        $this->assertFalse(in_array(['name' => 'class', 'value' => $fakeclass], $data->attributes));
570
        $this->assertFalse(in_array(['name' => 'name', 'value' => $fakeclass], $data->attributes));
571
        $this->assertFalse(in_array(['name' => 'disabled', 'value' => $fakedisabled], $data->attributes));
572
        // The rest should be fine.
573
        $this->assertTrue(in_array(['name' => 'data-action', 'value' => $dataaction], $data->attributes));
574
        $this->assertTrue(in_array(['name' => 'data-other', 'value' => $dataother], $data->attributes));
575
 
576
        // Validate label attributes.
577
        // The for attribute should not be included.
578
        $this->assertFalse(in_array(['name' => 'for', 'value' => $someid], $data->labelattributes));
579
        // The rest should be fine.
580
        $this->assertTrue(in_array(['name' => 'class', 'value' => $labelclass], $data->labelattributes));
581
        $this->assertTrue(in_array(['name' => 'style', 'value' => $labelstyle], $data->labelattributes));
582
    }
583
    /**
584
     * Test for checking the template context data for the single_select element.
585
     * @covers \single_button
586
     */
11 efrain 587
    public function test_single_button(): void {
1 efrain 588
        global $PAGE;
589
        $url = new \moodle_url('/');
590
        $realname = 'realname';
591
        $attributes = [
592
            'data-dummy' => 'dummy',
593
        ];
594
        $singlebutton = new single_button($url, $realname, 'post', single_button::BUTTON_SECONDARY, $attributes);
595
        $renderer = $PAGE->get_renderer('core');
596
        $data = $singlebutton->export_for_template($renderer);
597
 
598
        $this->assertEquals($realname, $data->label);
599
        $this->assertEquals('post', $data->method);
600
        $this->assertEquals('singlebutton', $data->classes);
601
        $this->assertEquals('secondary', $data->type);
602
        $this->assertEquals($attributes['data-dummy'], $data->attributes[0]['value']);
603
 
604
        $singlebutton = new single_button($url, $realname, 'post', single_button::BUTTON_PRIMARY, $attributes);
605
        $renderer = $PAGE->get_renderer('core');
606
        $data = $singlebutton->export_for_template($renderer);
607
 
608
        $this->assertEquals($realname, $data->label);
609
        $this->assertEquals('post', $data->method);
610
        $this->assertEquals('singlebutton', $data->classes);
611
        $this->assertEquals('primary', $data->type);
612
        $this->assertEquals($attributes['data-dummy'], $data->attributes[0]['value']);
613
    }
614
 
615
    /**
616
     * Test for checking the template context data for the url_select element.
617
     */
11 efrain 618
    public function test_url_select(): void {
1 efrain 619
        global $PAGE;
620
 
621
        $fakename = 'fakename';
622
        $fakeclass = 'fakeclass';
623
        $faketitle = 'faketitle';
624
        $fakedisabled = true;
625
        $fakefor = 'fakefor';
626
 
627
        $someid = 'someid';
628
        $realclass = 'realclass';
629
        $realtitle = 'realtitle';
630
        $realdisabled = false;
631
        $reallabel = 'Some cool label';
632
        $labelclass = 'somelabelclass';
633
        $labelstyle = 'font-weight: bold';
634
 
635
        $dataaction = 'actiondata';
636
        $dataother = 'otherdata';
637
 
638
        $attributes = [
639
            'id' => $someid,
640
            'class' => $fakeclass,
641
            'title' => $faketitle,
642
            'disabled' => $fakedisabled,
643
            'name' => $fakename,
644
            'data-action' => $dataaction,
645
            'data-other' => $dataother,
646
        ];
647
        $labelattributes = [
648
            'for' => $fakefor,
649
            'class' => $labelclass,
650
            'style' => $labelstyle
651
        ];
652
 
653
        $url1 = new \moodle_url("/#a");
654
        $url2 = new \moodle_url("/#b");
655
        $url3 = new \moodle_url("/#c");
656
 
657
        $urls = [
658
            $url1->out() => 'A',
659
            $url2->out() => 'B',
660
            $url3->out() => 'C',
661
        ];
662
        $nothing = ['' => 'choosedots'];
663
 
664
        $urlselect = new url_select($urls, null, $nothing, 'someformid');
665
        $urlselect->class = $realclass;
666
        $urlselect->tooltip = $realtitle;
667
        $urlselect->disabled = $realdisabled;
668
        $urlselect->attributes = $attributes;
669
        $urlselect->label = $reallabel;
670
        $urlselect->labelattributes = $labelattributes;
671
 
672
        $renderer = $PAGE->get_renderer('core');
673
        $data = $urlselect->export_for_template($renderer);
674
 
675
        $this->assertEquals($realtitle, $data->title);
676
        $this->assertEquals($urlselect->class, $data->classes);
677
        $this->assertEquals($reallabel, $data->label);
678
        $this->assertEquals($realdisabled, $data->disabled);
679
        $this->assertEquals($someid, $data->id);
680
 
681
        // Validate attributes array.
682
        // The following should not be included: id, class, name, disabled.
683
        $this->assertFalse(in_array(['name' => 'id', 'value' => $someid], $data->attributes));
684
        $this->assertFalse(in_array(['name' => 'class', 'value' => $fakeclass], $data->attributes));
685
        $this->assertFalse(in_array(['name' => 'name', 'value' => $fakeclass], $data->attributes));
686
        $this->assertFalse(in_array(['name' => 'disabled', 'value' => $fakedisabled], $data->attributes));
687
        // The rest should be fine.
688
        $this->assertTrue(in_array(['name' => 'data-action', 'value' => $dataaction], $data->attributes));
689
        $this->assertTrue(in_array(['name' => 'data-other', 'value' => $dataother], $data->attributes));
690
 
691
        // Validate label attributes.
692
        // The for attribute should not be included.
693
        $this->assertFalse(in_array(['name' => 'for', 'value' => $someid], $data->labelattributes));
694
        // The rest should be fine.
695
        $this->assertTrue(in_array(['name' => 'class', 'value' => $labelclass], $data->labelattributes));
696
        $this->assertTrue(in_array(['name' => 'style', 'value' => $labelstyle], $data->labelattributes));
697
    }
698
 
699
    /**
1441 ariadna 700
     * Test for checking the template context data for the url_select element.
701
     * @covers \url_select::disable_option
702
     * @covers \url_select::enable_option
703
     */
704
    public function test_url_select_disabled_options(): void {
705
        global $PAGE;
706
        $url1 = new \moodle_url("/#a");
707
        $url2 = new \moodle_url("/#b");
708
        $url3 = new \moodle_url("/#c");
709
 
710
        $urls = [
711
            $url1->out() => 'A',
712
            $url2->out() => 'B',
713
            $url3->out() => 'C',
714
        ];
715
        $urlselect = new url_select($urls,
716
            null,
717
            null,
718
            'someformid',
719
            null);
720
        $renderer = $PAGE->get_renderer('core');
721
        $urlselect->set_option_disabled($url2->out(), true);
722
        $data = $urlselect->export_for_template($renderer);
723
        $this->assertFalse($data->options[0]['disabled']);
724
        $this->assertTrue($data->options[1]['disabled']);
725
        $urlselect->set_option_disabled($url2->out(), false);
726
        $data = $urlselect->export_for_template($renderer);
727
        $this->assertFalse($data->options[0]['disabled']);
728
        $this->assertFalse($data->options[1]['disabled']);
729
    }
730
 
731
    /**
1 efrain 732
     * Data provider for test_block_contents_is_fake().
733
     *
734
     * @return array
735
     */
1441 ariadna 736
    public static function block_contents_is_fake_provider(): array {
1 efrain 737
        return [
738
            'Null' => [null, false],
739
            'Not set' => [false, false],
740
            'Fake' => ['_fake', true],
741
            'Real block' => ['activity_modules', false],
742
        ];
743
    }
744
 
745
    /**
746
     * Test block_contents is_fake() method.
747
     *
748
     * @dataProvider block_contents_is_fake_provider
749
     * @param mixed $value Value for the data-block attribute
750
     * @param boolean $expected The expected result
751
     */
11 efrain 752
    public function test_block_contents_is_fake($value, $expected): void {
1 efrain 753
        $bc = new block_contents(array());
754
        if ($value !== false) {
755
            $bc->attributes['data-block'] = $value;
756
        }
757
        $this->assertEquals($expected, $bc->is_fake());
758
    }
759
}