Merge branch 'MDL-63702-master' of git://github.com/mihailges/moodle
[moodle.git] / lib / tests / requirejs_test.php
blobef2732a18e905bfdde31a6d87f6a24712d9fa340
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.
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/>.
17 /**
18 * Unit tests for requirejs loader.
20 * @package core
21 * @author Damyon Wiese <damyon@moodle.com>
22 * @copyright 2016 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Unit tests for requirejs.
31 * @package core
32 * @author Damyon Wiese <damyon@moodle.com>
33 * @copyright 2016 Damyon Wiese
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_requirejs_testcase extends advanced_testcase {
38 /**
39 * Test requirejs loader
41 public function test_requirejs() {
42 global $CFG;
44 // Find a core module.
45 $result = core_requirejs::find_one_amd_module('core', 'templates', false);
46 $expected = ['core/templates' => $CFG->dirroot . '/lib/amd/build/templates.min.js'];
47 $this->assertEquals($expected, $result);
49 $result = core_requirejs::find_one_amd_module('core', 'templates', true);
50 $expected = ['core/templates' => $CFG->dirroot . '/lib/amd/src/templates.js'];
51 $this->assertEquals($expected, $result);
53 // Find a subsystem module (none exist yet).
54 $result = core_requirejs::find_one_amd_module('core_group', 'doesnotexist', false);
55 $expected = [];
56 $this->assertEquals($expected, $result);
58 // Find a plugin module.
59 $result = core_requirejs::find_one_amd_module('mod_assign', 'grading_panel', true);
60 $expected = ['mod_assign/grading_panel' => $CFG->dirroot . '/mod/assign/amd/src/grading_panel.js'];
61 $this->assertEquals($expected, $result);
63 // Find all modules - no debugging.
64 $result = core_requirejs::find_all_amd_modules(true);
65 foreach ($result as $key => $path) {
66 // Lets verify the first part of the key is a valid component name and the second part correctly contains "min" or not.
67 list($component, $template) = explode('/', $key, 2);
68 // Can we resolve it to a valid dir?
69 $dir = core_component::get_component_directory($component);
70 $this->assertNotEmpty($dir);
72 // Only "core" is allowed to have no _ in component names.
73 if (strpos($component, '_') === false) {
74 $this->assertEquals('core', $component);
76 $this->assertNotContains('.min', $path);
79 // Find all modules - debugging.
80 $result = core_requirejs::find_all_amd_modules(false);
81 foreach ($result as $key => $path) {
82 // Lets verify the first part of the key is a valid component name and the second part correctly contains "min" or not.
83 list($component, $template) = explode('/', $key, 2);
84 $dir = core_component::get_component_directory($component);
85 $this->assertNotEmpty($dir);
86 // Only "core" is allowed to have no _ in component names.
87 if (strpos($component, '_') === false) {
88 $this->assertEquals('core', $component);
91 $this->assertContains('.min', $path);