Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 21... Línea 21...
21
 * @category   phpunit
21
 * @category   phpunit
22
 * @copyright  © 2006 The Open University
22
 * @copyright  © 2006 The Open University
23
 * @author     T.J.Hunt@open.ac.uk
23
 * @author     T.J.Hunt@open.ac.uk
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU Public License
25
 */
25
 */
26
class weblib_test extends advanced_testcase {
26
final class weblib_test extends advanced_testcase {
27
    /**
27
    /**
28
     * @covers ::s
28
     * @covers ::s
29
     */
29
     */
30
    public function test_s(): void {
30
    public function test_s(): void {
31
        // Special cases.
31
        // Special cases.
Línea 358... Línea 358...
358
    }
358
    }
Línea 359... Línea 359...
359
 
359
 
360
    /**
360
    /**
361
     * Data provider for trusttext_pre_edit() tests.
361
     * Data provider for trusttext_pre_edit() tests.
362
     */
362
     */
363
    public function trusttext_pre_edit_provider(): array {
363
    public static function trusttext_pre_edit_provider(): array {
364
        return [
364
        return [
365
            [true, 0, 'editingteacher', FORMAT_HTML, 1],
365
            [true, 0, 'editingteacher', FORMAT_HTML, 1],
366
            [true, 0, 'editingteacher', FORMAT_MOODLE, 1],
366
            [true, 0, 'editingteacher', FORMAT_MOODLE, 1],
367
            [false, 0, 'editingteacher', FORMAT_MARKDOWN, 1],
367
            [false, 0, 'editingteacher', FORMAT_MARKDOWN, 1],
Línea 509... Línea 509...
509
        $PAGE->set_url('/course/view.php', array('id'=>1));
509
        $PAGE->set_url('/course/view.php', array('id'=>1));
510
        $this->assertSame($CFG->wwwroot.'/course/view.php?id=1', qualified_me());
510
        $this->assertSame($CFG->wwwroot.'/course/view.php?id=1', qualified_me());
511
    }
511
    }
Línea 512... Línea 512...
512
 
512
 
513
    /**
-
 
514
     * @covers \null_progress_trace
-
 
515
     */
-
 
516
    public function test_null_progress_trace(): void {
-
 
517
        $this->resetAfterTest(false);
-
 
518
 
-
 
519
        $trace = new null_progress_trace();
-
 
520
        $trace->output('do');
-
 
521
        $trace->output('re', 1);
-
 
522
        $trace->output('mi', 2);
-
 
523
        $trace->finished();
-
 
524
        $output = ob_get_contents();
-
 
525
        $this->assertSame('', $output);
-
 
526
        $this->expectOutputString('');
-
 
527
    }
-
 
528
 
-
 
529
    /**
-
 
530
     * @covers \null_progress_trace
-
 
531
     */
-
 
532
    public function test_text_progress_trace(): void {
-
 
533
        $this->resetAfterTest(false);
-
 
534
 
-
 
535
        $trace = new text_progress_trace();
-
 
536
        $trace->output('do');
-
 
537
        $trace->output('re', 1);
-
 
538
        $trace->output('mi', 2);
-
 
539
        $trace->finished();
-
 
540
        $this->expectOutputString("do\n  re\n    mi\n");
-
 
541
    }
-
 
542
 
-
 
543
    /**
-
 
544
     * @covers \html_progress_trace
-
 
545
     */
-
 
546
    public function test_html_progress_trace(): void {
-
 
547
        $this->resetAfterTest(false);
-
 
548
 
-
 
549
        $trace = new html_progress_trace();
-
 
550
        $trace->output('do');
-
 
551
        $trace->output('re', 1);
-
 
552
        $trace->output('mi', 2);
-
 
553
        $trace->finished();
-
 
554
        $this->expectOutputString("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n");
-
 
555
    }
-
 
556
 
-
 
557
    /**
-
 
558
     * @covers \html_list_progress_trace
-
 
559
     */
-
 
560
    public function test_html_list_progress_trace(): void {
-
 
561
        $this->resetAfterTest(false);
-
 
562
 
-
 
563
        $trace = new html_list_progress_trace();
-
 
564
        $trace->output('do');
-
 
565
        $trace->output('re', 1);
-
 
566
        $trace->output('mi', 2);
-
 
567
        $trace->finished();
-
 
568
        $this->expectOutputString("<ul>\n<li>do<ul>\n<li>re<ul>\n<li>mi</li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n");
-
 
569
    }
-
 
570
 
-
 
571
    /**
-
 
572
     * @covers \progress_trace_buffer
-
 
573
     */
-
 
574
    public function test_progress_trace_buffer(): void {
-
 
575
        $this->resetAfterTest(false);
-
 
576
 
-
 
577
        $trace = new progress_trace_buffer(new html_progress_trace());
-
 
578
        ob_start();
-
 
579
        $trace->output('do');
-
 
580
        $trace->output('re', 1);
-
 
581
        $trace->output('mi', 2);
-
 
582
        $trace->finished();
-
 
583
        $output = ob_get_contents();
-
 
584
        ob_end_clean();
-
 
585
        $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $output);
-
 
586
        $this->assertSame($output, $trace->get_buffer());
-
 
587
 
-
 
588
        $trace = new progress_trace_buffer(new html_progress_trace(), false);
-
 
589
        $trace->output('do');
-
 
590
        $trace->output('re', 1);
-
 
591
        $trace->output('mi', 2);
-
 
592
        $trace->finished();
-
 
593
        $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
-
 
594
        $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace->get_buffer());
-
 
595
        $trace->reset_buffer();
-
 
596
        $this->assertSame('', $trace->get_buffer());
-
 
597
        $this->expectOutputString('');
-
 
598
    }
-
 
599
 
-
 
600
    /**
-
 
601
     * @covers \combined_progress_trace
-
 
602
     */
-
 
603
    public function test_combined_progress_trace(): void {
-
 
604
        $this->resetAfterTest(false);
-
 
605
 
-
 
606
        $trace1 = new progress_trace_buffer(new html_progress_trace(), false);
-
 
607
        $trace2 = new progress_trace_buffer(new text_progress_trace(), false);
-
 
608
 
-
 
609
        $trace = new combined_progress_trace(array($trace1, $trace2));
-
 
610
        $trace->output('do');
-
 
611
        $trace->output('re', 1);
-
 
612
        $trace->output('mi', 2);
-
 
613
        $trace->finished();
-
 
614
        $this->assertSame("<p>do</p>\n<p>&#160;&#160;re</p>\n<p>&#160;&#160;&#160;&#160;mi</p>\n", $trace1->get_buffer());
-
 
615
        $this->assertSame("do\n  re\n    mi\n", $trace2->get_buffer());
-
 
616
        $this->expectOutputString('');
-
 
617
    }
-
 
618
 
-
 
619
    /**
513
    /**
620
     * @covers ::set_debugging
514
     * @covers ::set_debugging
621
     */
515
     */
622
    public function test_set_debugging(): void {
516
    public function test_set_debugging(): void {
Línea 642... Línea 536...
642
        $this->assertEquals(-1, $CFG->debug);
536
        $this->assertEquals(-1, $CFG->debug);
643
        $this->assertTrue($CFG->debugdeveloper);
537
        $this->assertTrue($CFG->debugdeveloper);
Línea 644... Línea 538...
644
 
538
 
645
        set_debugging(DEBUG_ALL);
539
        set_debugging(DEBUG_ALL);
646
        $this->assertEquals(DEBUG_ALL, $CFG->debug);
540
        $this->assertEquals(DEBUG_ALL, $CFG->debug);
Línea 647... Línea 541...
647
        $this->assertFalse($CFG->debugdeveloper);
541
        $this->assertTrue($CFG->debugdeveloper);
648
 
542
 
649
        set_debugging(DEBUG_NORMAL);
543
        set_debugging(DEBUG_NORMAL);
Línea 733... Línea 627...
733
    }
627
    }
Línea 734... Línea 628...
734
 
628
 
735
    /**
629
    /**
736
     * Data provider for test_content_to_text.
630
     * Data provider for test_content_to_text.
737
     */
631
     */
738
    public static function provider_content_to_text() {
632
    public static function provider_content_to_text(): array {
739
        return array(
633
        return array(
740
            array('asd', false, 'asd'),
634
            array('asd', false, 'asd'),
741
            // Trim '\r\n '.
635
            // Trim '\r\n '.
742
            array("Note that:\n\n3 > 1 ", FORMAT_PLAIN, "Note that:\n\n3 > 1"),
636
            array("Note that:\n\n3 > 1 ", FORMAT_PLAIN, "Note that:\n\n3 > 1"),
Línea 759... Línea 653...
759
    /**
653
    /**
760
     * Data provider for validate_email() function.
654
     * Data provider for validate_email() function.
761
     *
655
     *
762
     * @return array Returns aray of test data for the test_validate_email function
656
     * @return array Returns aray of test data for the test_validate_email function
763
     */
657
     */
764
    public function data_validate_email() {
658
    public static function data_validate_email(): array {
765
        return [
659
        return [
766
            // Test addresses that should pass.
660
            // Test addresses that should pass.
767
            [
661
            [
768
                'email' => 'moodle@example.com',
662
                'email' => 'moodle@example.com',
769
                'result' => true
663
                'result' => true
Línea 927... Línea 821...
927
    }
821
    }
Línea 928... Línea 822...
928
 
822
 
929
    /**
823
    /**
930
     * Data provider for test_get_file_argument.
824
     * Data provider for test_get_file_argument.
931
     */
825
     */
932
    public static function provider_get_file_argument() {
826
    public static function provider_get_file_argument(): array {
933
        return array(
827
        return array(
934
            // Serving SCORM content w/o HTTP GET params.
828
            // Serving SCORM content w/o HTTP GET params.
935
            array(array(
829
            array(array(
936
                    'SERVER_SOFTWARE' => 'Apache',
830
                    'SERVER_SOFTWARE' => 'Apache',
Línea 1095... Línea 989...
1095
        $url1 = "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999999/test1.jpg";
989
        $url1 = "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999999/test1.jpg";
1096
        $url2 = "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999998/test2.jpg";
990
        $url2 = "{$CFG->wwwroot}/draftfile.php/5/user/draft/99999998/test2.jpg";
Línea 1097... Línea 991...
1097
 
991
 
1098
        $html = "<p>This is a test.</p><p><img src=\"{$url1}\" alt=\"\"></p>
992
        $html = "<p>This is a test.</p><p><img src=\"{$url1}\" alt=\"\"></p>
1099
                <br>Test content.<p></p><p><img src=\"{$url2}\" alt=\"\" width=\"2048\" height=\"1536\"
993
                <br>Test content.<p></p><p><img src=\"{$url2}\" alt=\"\" width=\"2048\" height=\"1536\"
1100
                class=\"img-fluid atto_image_button_text-bottom\"><br></p>";
994
                class=\"img-fluid \"><br></p>";
1101
        $draftareas = array(
995
        $draftareas = array(
1102
            array(
996
            array(
1103
                'urlbase' => 'draftfile.php',
997
                'urlbase' => 'draftfile.php',
1104
                'contextid' => '5',
998
                'contextid' => '5',
Línea 1175... Línea 1069...
1175
    /**
1069
    /**
1176
     * Data provider for the testing get_html_lang_attribute_value().
1070
     * Data provider for the testing get_html_lang_attribute_value().
1177
     *
1071
     *
1178
     * @return string[][]
1072
     * @return string[][]
1179
     */
1073
     */
1180
    public function get_html_lang_attribute_value_provider() {
1074
    public static function get_html_lang_attribute_value_provider(): array {
1181
        return [
1075
        return [
1182
            'Empty lang code' => ['    ', 'en'],
1076
            'Empty lang code' => ['    ', 'en'],
1183
            'English' => ['en', 'en'],
1077
            'English' => ['en', 'en'],
1184
            'English, US' => ['en_us', 'en'],
1078
            'English, US' => ['en_us', 'en'],
1185
            'Unknown' => ['xx', 'en'],
1079
            'Unknown' => ['xx', 'en'],
Línea 1198... Línea 1092...
1198
    public function test_get_html_lang_attribute_value(string $langcode, string $expected): void {
1092
    public function test_get_html_lang_attribute_value(string $langcode, string $expected): void {
1199
        $this->assertEquals($expected, get_html_lang_attribute_value($langcode));
1093
        $this->assertEquals($expected, get_html_lang_attribute_value($langcode));
1200
    }
1094
    }
Línea 1201... Línea 1095...
1201
 
1095
 
1202
    /**
-
 
1203
     * Test the coding exceptions when returning URL as relative path from $CFG->wwwroot.
-
 
1204
     *
-
 
1205
     * @param moodle_url $url The URL pointing to a web resource.
-
 
1206
     * @param string $exmessage The expected output URL.
-
 
1207
     * @throws coding_exception If called on a non-local URL.
-
 
1208
     * @see \moodle_url::out_as_local_url()
-
 
1209
     * @covers \moodle_url::out_as_local_url
-
 
1210
     * @dataProvider out_as_local_url_coding_exception_provider
-
 
1211
     */
-
 
1212
    public function test_out_as_local_url_coding_exception(\moodle_url $url, string $exmessage): void {
-
 
1213
        $this->expectException(\coding_exception::class);
-
 
1214
        $this->expectExceptionMessage($exmessage);
-
 
1215
        $localurl = $url->out_as_local_url();
-
 
1216
    }
-
 
1217
 
-
 
1218
    /**
-
 
1219
     * Data provider for throwing coding exceptions in <u>\moodle_url::out_as_local_url()</u>.
-
 
1220
     *
-
 
1221
     * @return array
-
 
1222
     * @throws moodle_exception On seriously malformed URLs (<u>parse_url</u>).
-
 
1223
     * @see \moodle_url::out_as_local_url()
-
 
1224
     * @see parse_url()
-
 
1225
     */
-
 
1226
    public function out_as_local_url_coding_exception_provider() {
-
 
1227
        return [
-
 
1228
            'Google Maps CDN (HTTPS)' => [
-
 
1229
                new \moodle_url('https://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
-
 
1230
                'Coding error detected, it must be fixed by a programmer: out_as_local_url called on a non-local URL'
-
 
1231
            ],
-
 
1232
            'Google Maps CDN (HTTP)' => [
-
 
1233
                new \moodle_url('http://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
-
 
1234
                'Coding error detected, it must be fixed by a programmer: out_as_local_url called on a non-local URL'
-
 
1235
            ],
-
 
1236
        ];
-
 
1237
    }
-
 
1238
 
-
 
1239
    /**
-
 
1240
     * Test URL as relative path from $CFG->wwwroot.
-
 
1241
     *
-
 
1242
     * @param moodle_url $url The URL pointing to a web resource.
-
 
1243
     * @param string $expected The expected local URL.
-
 
1244
     * @throws coding_exception If called on a non-local URL.
-
 
1245
     * @see \moodle_url::out_as_local_url()
-
 
1246
     * @covers \moodle_url::out_as_local_url
-
 
1247
     * @dataProvider out_as_local_url_provider
-
 
1248
     */
-
 
1249
    public function test_out_as_local_url(\moodle_url $url, string $expected): void {
-
 
1250
        $this->assertEquals($expected, $url->out_as_local_url(false));
-
 
1251
    }
-
 
1252
 
-
 
1253
    /**
-
 
1254
     * Data provider for returning local paths via <u>\moodle_url::out_as_local_url()</u>.
-
 
1255
     *
-
 
1256
     * @return array
-
 
1257
     * @throws moodle_exception On seriously malformed URLs (<u>parse_url</u>).
-
 
1258
     * @see \moodle_url::out_as_local_url()
-
 
1259
     * @see parse_url()
-
 
1260
     */
-
 
1261
    public function out_as_local_url_provider() {
-
 
1262
        global $CFG;
-
 
1263
        $wwwroot = rtrim($CFG->wwwroot, '/');
-
 
1264
 
-
 
1265
        return [
-
 
1266
            'Environment XML file' => [
-
 
1267
                new \moodle_url('/admin/environment.xml'),
-
 
1268
                '/admin/environment.xml'
-
 
1269
            ],
-
 
1270
            'H5P JS internal resource' => [
-
 
1271
                new \moodle_url('/h5p/js/embed.js'),
-
 
1272
                '/h5p/js/embed.js'
-
 
1273
            ],
-
 
1274
            'A Moodle JS resource using the full path including the proper JS Handler' => [
-
 
1275
                new \moodle_url($wwwroot . '/lib/javascript.php/1/lib/editor/tiny/js/tinymce/tinymce.js'),
-
 
1276
                '/lib/javascript.php/1/lib/editor/tiny/js/tinymce/tinymce.js'
-
 
1277
            ],
-
 
1278
        ];
-
 
1279
    }
-
 
1280
 
-
 
1281
    /**
-
 
1282
     * Test URL as relative path from $CFG->wwwroot.
-
 
1283
     *
-
 
1284
     * @param moodle_url $url The URL pointing to a web resource.
-
 
1285
     * @param bool $expected The expected result.
-
 
1286
     * @see \moodle_url::is_local_url()
-
 
1287
     * @covers \moodle_url::is_local_url
-
 
1288
     * @dataProvider is_local_url_provider
-
 
1289
     */
-
 
1290
    public function test_is_local_url(\moodle_url $url, bool $expected): void {
-
 
1291
        $this->assertEquals($expected, $url->is_local_url(), "'{$url}' is not a local URL!");
-
 
1292
    }
-
 
1293
 
-
 
1294
    /**
-
 
1295
     * Data provider for testing <u>\moodle_url::is_local_url()</u>.
-
 
1296
     *
-
 
1297
     * @return array
-
 
1298
     * @see \moodle_url::is_local_url()
-
 
1299
     */
-
 
1300
    public function is_local_url_provider() {
-
 
1301
        global $CFG;
-
 
1302
        $wwwroot = rtrim($CFG->wwwroot, '/');
-
 
1303
 
-
 
1304
        return [
-
 
1305
            'Google Maps CDN (HTTPS)' => [
-
 
1306
                new \moodle_url('https://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
-
 
1307
                false
-
 
1308
            ],
-
 
1309
            'Google Maps CDN (HTTP)' => [
-
 
1310
                new \moodle_url('http://maps.googleapis.com/maps/api/js', ['key' => 'googlemapkey3', 'sensor' => 'false']),
-
 
1311
                false
-
 
1312
            ],
-
 
1313
            'wwwroot' => [
-
 
1314
                new \moodle_url($wwwroot),
-
 
1315
                true
-
 
1316
            ],
-
 
1317
            'wwwroot/' => [
-
 
1318
                new \moodle_url($wwwroot . '/'),
-
 
1319
                true
-
 
1320
            ],
-
 
1321
            'Environment XML file' => [
-
 
1322
                new \moodle_url('/admin/environment.xml'),
-
 
1323
                true
-
 
1324
            ],
-
 
1325
            'H5P JS internal resource' => [
-
 
1326
                new \moodle_url('/h5p/js/embed.js'),
-
 
1327
                true
-
 
1328
            ],
-
 
1329
        ];
-
 
1330
    }
-
 
1331
 
-
 
1332
    /**
1096
    /**
1333
     * Data provider for strip_querystring tests.
1097
     * Data provider for strip_querystring tests.
1334
     *
1098
     *
1335
     * @return array
1099
     * @return array
1336
     */
1100
     */
1337
    public function strip_querystring_provider(): array {
1101
    public static function strip_querystring_provider(): array {
1338
        return [
1102
        return [
1339
            'Null' => [null, ''],
1103
            'Null' => [null, ''],
1340
            'Empty string' => ['', ''],
1104
            'Empty string' => ['', ''],
1341
            'No querystring' => ['https://example.com', 'https://example.com'],
1105
            'No querystring' => ['https://example.com', 'https://example.com'],