MDL-21400 finalising JS api - removing ->on_dom_ready (now bool param in js() and...
[moodle.git] / lib / ajax / simpletest / testajaxlib.php
CommitLineData
b2330db6 1<?php
2
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/>.
17
e874af28 18
19/**
20 * Unit tests for (some of) ../ajaxlib.php.
21 *
b2330db6 22 * @copyright 2009 Tim Hunt
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
e874af28 24 */
25
26if (!defined('MOODLE_INTERNAL')) {
27 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28}
e874af28 29require_once($CFG->libdir . '/ajax/ajaxlib.php');
30
b2330db6 31
e874af28 32/**
b2330db6 33 * Unit tests for ../ajaxlib.php functions.
e874af28 34 *
b2330db6 35 * @copyright 2008 Tim Hunt
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
e874af28 37 */
b2330db6 38class ajax_test extends ajaxlib_unit_test_base {
39
32941d74 40 var $user_agents = array(
41 'MSIE' => array(
42 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
43 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
44 '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)')
45 ),
46 'Firefox' => array(
47 '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'),
48 '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
49 '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'),
50 '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',
51 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)')
52 ),
53 'Safari' => array(
54 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
55 '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
56 ),
57 'Opera' => array(
58 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
59 '9.0' => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)',
60 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)')
61 )
62 );
63
64 /**
65 * Uses the array of user agents to test ajax_lib::ajaxenabled
66 */
67 function test_ajaxenabled()
68 {
69 global $CFG, $USER;
70 $CFG->enableajax = true;
71 $USER->ajax = true;
72
73 // Should be true
74 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
75 $this->assertTrue(ajaxenabled());
76
77 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['1.5']['Windows XP'];
78 $this->assertTrue(ajaxenabled());
79
80 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
81 $this->assertTrue(ajaxenabled());
82
83 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
84 $this->assertTrue(ajaxenabled());
85
86 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
87 $this->assertTrue(ajaxenabled());
88
89 // Should be false
90 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['1.0.6']['Windows XP'];
91 $this->assertFalse(ajaxenabled());
92
93 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['312']['Mac OS X'];
94 $this->assertFalse(ajaxenabled());
95
96 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['8.51']['Windows XP'];
97 $this->assertFalse(ajaxenabled());
98
99 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['5.5']['Windows 2000'];
100 $this->assertFalse(ajaxenabled());
101
102 // Test array of tested browsers
103 $tested_browsers = array('MSIE' => 6.0, 'Gecko' => 20061111);
104 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
105 $this->assertTrue(ajaxenabled($tested_browsers));
106
107 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
108 $this->assertTrue(ajaxenabled($tested_browsers));
109
110 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
111 $this->assertFalse(ajaxenabled($tested_browsers));
112
113 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
114 $this->assertFalse(ajaxenabled($tested_browsers));
115
116 $tested_browsers = array('Safari' => 412, 'Opera' => 9.0);
117 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
118 $this->assertFalse(ajaxenabled($tested_browsers));
119
120 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
121 $this->assertFalse(ajaxenabled($tested_browsers));
122
123 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
124 $this->assertTrue(ajaxenabled($tested_browsers));
125
126 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
127 $this->assertTrue(ajaxenabled($tested_browsers));
128 }
e874af28 129}