ffe40f9e14e320dc629dd5841523e132ecf24e4d
[moodle.git] / lib / ajax / simpletest / testajaxlib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
19 /**
20  * Unit tests for (some of) ../ajaxlib.php.
21  *
22  * @copyright 2009 Tim Hunt
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 if (!defined('MOODLE_INTERNAL')) {
27     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
28 }
29 require_once($CFG->libdir . '/ajax/ajaxlib.php');
32 /**
33  * Helper class, adds some useful stuff to UnitTestCase that the other test cases
34  * classes in this file can benefit from.
35  *
36  * @copyright 2009 Tim Hunt
37  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 abstract class ajaxlib_unit_test_base extends UnitTestCase {
40     protected $requires;
41     public  static $includecoverage = array('lib/ajax/ajaxlib.php');
43     public function setUp() {
44         parent::setUp();
45         $this->requires = new page_requirements_manager();
46     }
48     public function tearDown() {
49         $this->requires = null;
50         parent::tearDown();
51     }
53     public function assertContains($actual, $expectedsubstring) {
54         $this->assertNotIdentical(strpos($actual, (string) $expectedsubstring), false,
55                 "[$actual] does not containg the substring [$expectedsubstring].");
56     }
57 }
60 /**
61  * Unit tests for the requriement_base base class. Don't be confused by the
62  * fact we are using a specific subclass to test with.
63  *
64  * @copyright 2009 Tim Hunt
65  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
66  */
67 class requriement_base_test extends ajaxlib_unit_test_base {
68     protected $classname = 'required_css';
70     public function test_not_done_initially() {
71         $requirement = new $this->classname($this->requires, '');
72         $this->assertFalse($requirement->is_done());
73     }
75     public function test_done_when_marked() {
76         $requirement = new $this->classname($this->requires, '');
77         $requirement->mark_done();
78         $this->assertTrue($requirement->is_done());
79     }
80 }
83 /**
84  * Unit tests for the required_css class.
85  *
86  * @copyright 2009 Tim Hunt
87  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
88  */
89 class required_css_test extends ajaxlib_unit_test_base {
90     protected $cssurl = 'http://example.com/styles.css';
92     public function test_when() {
93         $requirement = new required_css($this->requires, $this->cssurl);
94         $this->assertEqual($requirement->get_when(), page_requirements_manager::WHEN_IN_HEAD);
95     }
97     public function test_round_trip_url_to_html() {
98         $requirement = new required_css($this->requires, $this->cssurl);
99         $html = $requirement->get_html();
100         $this->assertContains($html, $this->cssurl);
101         $this->assertContains($html, '<link ');
102         $this->assertContains($html, 'type="text/css"');
103     }
106 /**
107  * Unit tests for the required_js_code class. Once again we are tesing the
108  * behaviour of an abstract class by creating instances one particular subclass.
109  *
110  * @copyright 2009 Tim Hunt
111  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
112  */
113 class required_js_code_test extends ajaxlib_unit_test_base {
114     protected $classname = 'required_data_for_js';
115 /* TODO: MDL-21361
116     public function test_when() {
117         $requirement = new $this->classname($this->requires, '', '');
118         $this->assertEqual($requirement->get_when(), page_requirements_manager::WHEN_AT_END);
119     }
121     public function test_setting_when_to_head() {
122         $requirement = new $this->classname($this->requires, '', '');
123         $requirement->in_head();
124         $this->assertEqual($requirement->get_when(), page_requirements_manager::WHEN_IN_HEAD);
125     }
127     public function test_in_head_when_too_late_throws_exception() {
128         $requirement = new $this->classname($this->requires, '', '');
129         $this->requires->get_head_code();
131         $this->expectException();
132         $requirement->in_head();
133     }
135     public function test_in_head_when_too_late_no_exception_if_done() {
136         $requirement = new $this->classname($this->requires, '', '');
137         $requirement->mark_done();
138         $this->requires->get_head_code();
140         $requirement->in_head();
141         $this->pass('No exception thrown as expected.');
142     }
143 */
147 /**
148  * Unit tests for the required_js_function_call class.
149  *
150  * @copyright 2009 Tim Hunt
151  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
152  */
153 class required_js_function_call_test extends ajaxlib_unit_test_base {
154     protected $function = 'object.method';
155     protected $params = array('arg1', 2);
157     public function test_round_trip_to_js_code() {
158         $requirement = new required_js_function_call($this->requires, $this->function, $this->params);
159         $js = $requirement->get_js_code();
160         $this->assertContains($js, $this->function . '(');
161         $this->assertContains($js, $this->params[0]);
162         $this->assertContains($js, $this->params[1]);
163     }
165     public function test_setting_when_on_dom_ready() {
166         $requirement = new required_js_function_call($this->requires, $this->function, $this->params);
167         $requirement->on_dom_ready();
168         $this->assertEqual($requirement->get_when(), page_requirements_manager::WHEN_ON_DOM_READY);
169     }
173 /**
174  * Unit tests for the page_requirements_manager class.
175  *
176  * @copyright 2009 Tim Hunt
177  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
178  */
179 class page_requirements_manager_test extends ajaxlib_unit_test_base {
180 /* TODO: MDL-21361
181     public function test_outputting_head_marks_it_done() {
182         $this->requires->get_head_code();
183         $this->assertTrue($this->requires->is_head_done());
184     }
186     public function test_outputting_body_top_marks_it_done() {
187         $this->requires->get_top_of_body_code();
188         $this->assertTrue($this->requires->is_top_of_body_done());
189     }
191     public function test_requiring_js() {
192         global $CFG;
193         $jsfile = 'lib/javascript-static.js'; // Just needs to be a JS file that exists.
194         $this->requires->js($jsfile);
196         $html = $this->requires->get_end_code();
197         $this->assertContains($html, $CFG->httpswwwroot . '/' . $jsfile);
198     }
200     public function test_requiring_js_with_argument() {
201         global $CFG;
202         $jsfile = 'lib/javascript-static.js?d=434'; // Just needs to be a JS file that exists.
203         $this->requires->js($jsfile);
205         $html = $this->requires->get_end_code();
206         $this->assertContains($html, $CFG->httpswwwroot . '/' . $jsfile);
207     }
209     public function test_nonexistant_js_throws_exception() {
210         $jsfile = 'js/file/that/does/not/exist.js';
212         $this->expectException();
213         $this->requires->js($jsfile);
214     }
216     public function test_requiring_skip_link() {
217         $this->requires->skip_link_to('target', 'Link text');
219         $html = $this->requires->get_top_of_body_code();
220         $this->assertContains($html, 'target');
221         $this->assertContains($html, 'Link text');
222     }
224     public function test_requiring_js_function_call() {
225         $this->requires->js_function_call('fn');
227         $html = $this->requires->get_end_code();
228         $this->assertContains($html, 'fn()');
229     }
231     public function test_requiring_string_for_js() {
232         $this->requires->string_for_js('course', 'moodle');
234         $html = $this->requires->get_end_code();
235         $this->assertContains($html, 'mstr');
236         $this->assertContains($html, 'course');
237         $this->assertContains($html, 'moodle');
238     }
240     public function test_repeat_string_different_a_throws_exception() {
241         $this->requires->string_for_js('added', 'moodle', 'this');
242         $this->expectException();
243         $this->requires->string_for_js('added', 'moodle', 'that');
244     }
246     public function test_repeat_string_same_a_is_ok() {
247         $this->requires->string_for_js('added', 'moodle', 'same$a');
248         $this->requires->string_for_js('added', 'moodle', 'same$a');
249         $this->pass('No exception thrown as expected.');
250     }
252     public function test_requiring_js_function_call_on_dom_ready() {
253         $this->requires->js_function_call('fn')->on_dom_ready();
255         $html = $this->requires->get_end_code();
256         $this->assertPattern('/<script.*src=".*event.*\.js"/', $html);
257         $this->assertContains($html, 'YAHOO.util.Event.onDOMReady');
258         $this->assertContains($html, 'fn()');
259     }*/
263 /**
264  * Unit tests for ../ajaxlib.php functions.
265  *
266  * @copyright 2008 Tim Hunt
267  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
268  */
269 class ajax_test extends ajaxlib_unit_test_base {
271     var $user_agents = array(
272             'MSIE' => array(
273                 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
274                 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
275                 '7.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YPC 3.0.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)')
276             ),
277             'Firefox' => array(
278                 '1.0.6'   => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6'),
279                 '1.5'     => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
280                 '1.5.0.1' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'),
281                 '2.0'     => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1',
282                                    'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)')
283             ),
284             'Safari' => array(
285                 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
286                 '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
287             ),
288             'Opera' => array(
289                 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
290                 '9.0'  => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)',
291                                 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)')
292             )
293         );
295     /**
296      * Uses the array of user agents to test ajax_lib::ajaxenabled
297      */
298     function test_ajaxenabled()
299     {
300         global $CFG, $USER;
301         $CFG->enableajax = true;
302         $USER->ajax      = true;
304         // Should be true
305         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
306         $this->assertTrue(ajaxenabled());
308         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['1.5']['Windows XP'];
309         $this->assertTrue(ajaxenabled());
311         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
312         $this->assertTrue(ajaxenabled());
314         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
315         $this->assertTrue(ajaxenabled());
317         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
318         $this->assertTrue(ajaxenabled());
320         // Should be false
321         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['1.0.6']['Windows XP'];
322         $this->assertFalse(ajaxenabled());
324         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['312']['Mac OS X'];
325         $this->assertFalse(ajaxenabled());
327         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['8.51']['Windows XP'];
328         $this->assertFalse(ajaxenabled());
330         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['5.5']['Windows 2000'];
331         $this->assertFalse(ajaxenabled());
333         // Test array of tested browsers
334         $tested_browsers = array('MSIE' => 6.0, 'Gecko' => 20061111);
335         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
336         $this->assertTrue(ajaxenabled($tested_browsers));
338         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
339         $this->assertTrue(ajaxenabled($tested_browsers));
341         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
342         $this->assertFalse(ajaxenabled($tested_browsers));
344         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
345         $this->assertFalse(ajaxenabled($tested_browsers));
347         $tested_browsers = array('Safari' => 412, 'Opera' => 9.0);
348         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
349         $this->assertFalse(ajaxenabled($tested_browsers));
351         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
352         $this->assertFalse(ajaxenabled($tested_browsers));
354         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
355         $this->assertTrue(ajaxenabled($tested_browsers));
357         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
358         $this->assertTrue(ajaxenabled($tested_browsers));
359     }