Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / simpletest / testajaxlib.php
blob611220c8eca179549df7c29cc5066531613e2aff
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
9 // //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27 * Unit tests for (some of) ../ajax/ajaxlib.php.
29 * @copyright &copy; 2006 The Open University
30 * @author nicolas@moodle.com
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package moodlecore
35 if (!defined('MOODLE_INTERNAL')) {
36 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
39 require_once($CFG->libdir . '/moodlelib.php');
40 require_once($CFG->libdir . '/ajax/ajaxlib.php');
42 class ajaxlib_test extends UnitTestCase {
44 var $user_agents = array(
45 'MSIE' => array(
46 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
47 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
48 '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)')
49 ),
50 'Firefox' => array(
51 '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'),
52 '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
53 '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'),
54 '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',
55 'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)')
57 'Safari' => array(
58 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
59 '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
61 'Opera' => array(
62 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
63 '9.0' => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)',
64 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)')
68 function setUp() {
71 function tearDown() {
74 /**
75 * Uses the array of user agents to test ajax_lib::ajaxenabled
77 function test_ajaxenabled()
79 global $CFG, $USER;
80 $CFG->enableajax = true;
81 $USER->ajax = true;
83 // Should be true
84 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
85 $this->assertTrue(ajaxenabled());
87 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['1.5']['Windows XP'];
88 $this->assertTrue(ajaxenabled());
90 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
91 $this->assertTrue(ajaxenabled());
93 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
94 $this->assertTrue(ajaxenabled());
96 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
97 $this->assertTrue(ajaxenabled());
99 // Should be false
100 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['1.0.6']['Windows XP'];
101 $this->assertFalse(ajaxenabled());
103 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['312']['Mac OS X'];
104 $this->assertFalse(ajaxenabled());
106 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['8.51']['Windows XP'];
107 $this->assertFalse(ajaxenabled());
109 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['5.5']['Windows 2000'];
110 $this->assertFalse(ajaxenabled());
112 // Test array of tested browsers
113 $tested_browsers = array('MSIE' => 6.0, 'Gecko' => 20061111);
114 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
115 $this->assertTrue(ajaxenabled($tested_browsers));
117 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
118 $this->assertTrue(ajaxenabled($tested_browsers));
120 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
121 $this->assertFalse(ajaxenabled($tested_browsers));
123 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
124 $this->assertFalse(ajaxenabled($tested_browsers));
126 $tested_browsers = array('Safari' => 412, 'Opera' => 9.0);
127 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
128 $this->assertFalse(ajaxenabled($tested_browsers));
130 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
131 $this->assertFalse(ajaxenabled($tested_browsers));
133 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
134 $this->assertTrue(ajaxenabled($tested_browsers));
136 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
137 $this->assertTrue(ajaxenabled($tested_browsers));