2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for lib/outputrequirementslibphp.
22 * @copyright 2012 Petr Škoda
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->libdir
. '/outputrequirementslib.php');
32 class core_outputrequirementslib_testcase
extends advanced_testcase
{
33 public function test_string_for_js() {
34 $this->resetAfterTest();
36 $page = new moodle_page();
37 $page->requires
->string_for_js('course', 'moodle', 1);
38 $page->requires
->string_for_js('course', 'moodle', 1);
39 $this->expectException('coding_exception');
40 $page->requires
->string_for_js('course', 'moodle', 2);
42 // Note: we can not switch languages in phpunit yet,
43 // it would be nice to test that the strings are actually fetched in the footer.
46 public function test_one_time_output_normal_case() {
47 $page = new moodle_page();
48 $this->assertTrue($page->requires
->should_create_one_time_item_now('test_item'));
49 $this->assertFalse($page->requires
->should_create_one_time_item_now('test_item'));
52 public function test_one_time_output_repeat_output_throws() {
53 $page = new moodle_page();
54 $page->requires
->set_one_time_item_created('test_item');
55 $this->expectException('coding_exception');
56 $page->requires
->set_one_time_item_created('test_item');
59 public function test_one_time_output_different_pages_independent() {
60 $firstpage = new moodle_page();
61 $secondpage = new moodle_page();
62 $this->assertTrue($firstpage->requires
->should_create_one_time_item_now('test_item'));
63 $this->assertTrue($secondpage->requires
->should_create_one_time_item_now('test_item'));
67 * Test for the jquery_plugin method.
69 * Test to make sure that backslashes are not generated with either slasharguments set to on or off.
71 public function test_jquery_plugin() {
74 $this->resetAfterTest();
76 // With slasharguments on.
77 $CFG->slasharguments
= 1;
79 $page = new moodle_page();
80 $requirements = $page->requires
;
81 // Assert successful method call.
82 $this->assertTrue($requirements->jquery_plugin('jquery'));
83 $this->assertTrue($requirements->jquery_plugin('ui'));
85 // Get the code containing the required jquery plugins.
86 $renderer = $PAGE->get_renderer('core', null, RENDERER_TARGET_MAINTENANCE
);
87 $requirecode = $requirements->get_top_of_body_code($renderer);
88 // Make sure that the generated code does not contain backslashes.
89 $this->assertFalse(strpos($requirecode, '\\'), "Output contains backslashes: " . $requirecode);
91 // With slasharguments off.
92 $CFG->slasharguments
= 0;
94 $page = new moodle_page();
95 $requirements = $page->requires
;
96 // Assert successful method call.
97 $this->assertTrue($requirements->jquery_plugin('jquery'));
98 $this->assertTrue($requirements->jquery_plugin('ui'));
100 // Get the code containing the required jquery plugins.
101 $requirecode = $requirements->get_top_of_body_code($renderer);
102 // Make sure that the generated code does not contain backslashes.
103 $this->assertFalse(strpos($requirecode, '\\'), "Output contains backslashes: " . $requirecode);