merged fix for MDL-11709, print_header() warning for blogs
[moodle.git] / lib / simpletest / testmoodlelib.php
CommitLineData
d499142e 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.org //
9// //
b7064779 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
d499142e 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///////////////////////////////////////////////////////////////////////////
25
7c9d46c9 26/**
27 * Unit tests for (some of) ../moodlelib.php.
28 *
29 * @copyright &copy; 2006 The Open University
30 * @author T.J.Hunt@open.ac.uk
d499142e 31 * @author nicolas@moodle.com
7c9d46c9 32 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
33 * @package moodlecore
34 */
35
cdbbd26f 36if (!defined('MOODLE_INTERNAL')) {
37 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
38}
7c9d46c9 39
7c9d46c9 40require_once($CFG->libdir . '/moodlelib.php');
41
42class moodlelib_test extends UnitTestCase {
239c1576 43
a205dcdc 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)')
56 ),
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')
60 ),
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)')
65 )
66 );
67
7c9d46c9 68 function setUp() {
69 }
70
71 function tearDown() {
72 }
73
74 function test_address_in_subnet() {
75 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1'));
76 $this->assertFalse(address_in_subnet('123.121.234.2', '123.121.234.1'));
77 $this->assertFalse(address_in_subnet('123.121.134.1', '123.121.234.1'));
78 $this->assertFalse(address_in_subnet('113.121.234.1', '123.121.234.1'));
79 $this->assertTrue(address_in_subnet('123.121.234.0', '123.121.234.2/28'));
80 $this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.2/28'));
81 $this->assertFalse(address_in_subnet('123.121.234.16', '123.121.234.2/28'));
82 $this->assertFalse(address_in_subnet('123.121.234.255', '123.121.234.2/28'));
83 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.'));
84 $this->assertFalse(address_in_subnet('123.122.234.1', '123.121.'));
85 $this->assertFalse(address_in_subnet('223.121.234.1', '123.121.'));
86 $this->assertFalse(address_in_subnet('123.121.234.9', '123.121.234.10-20'));
87 $this->assertTrue(address_in_subnet('123.121.234.10', '123.121.234.10-20'));
88 $this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.10-20'));
89 $this->assertTrue(address_in_subnet('123.121.234.20', '123.121.234.10-20'));
90 $this->assertFalse(address_in_subnet('123.121.234.21', '123.121.234.10-20'));
91 $this->assertTrue(address_in_subnet(' 123.121.234.1 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
92 $this->assertTrue(address_in_subnet(' 1.1.2.3 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
93 $this->assertTrue(address_in_subnet(' 2.2.234.1 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
94 $this->assertTrue(address_in_subnet(' 3.3.3.4 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
95 $this->assertFalse(address_in_subnet(' 123.121.234.2 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
96 $this->assertFalse(address_in_subnet(' 2.1.2.3 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
97 $this->assertFalse(address_in_subnet(' 2.3.234.1 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
98 $this->assertFalse(address_in_subnet(' 3.3.3.7 ', ' 123.121.234.1 , 1.1.1.1/16,2.2.,3.3.3.3-6 '));
99 }
100
239c1576 101 /**
102 * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
103 * works as expected.
104 */
105 function test_check_browser_version()
106 {
a205dcdc 107 global $CFG;
d499142e 108
a205dcdc 109 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
239c1576 110 $this->assertTrue(check_browser_version('Safari', '312'));
111 $this->assertFalse(check_browser_version('Safari', '500'));
112
a205dcdc 113 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
239c1576 114 $this->assertTrue(check_browser_version('Opera', '8.0'));
115 $this->assertFalse(check_browser_version('Opera', '10.0'));
116
a205dcdc 117 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
239c1576 118 $this->assertTrue(check_browser_version('MSIE', '5.0'));
119 $this->assertFalse(check_browser_version('MSIE', '7.0'));
120
a205dcdc 121 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
239c1576 122 $this->assertTrue(check_browser_version('Firefox', '1.5'));
123 $this->assertFalse(check_browser_version('Firefox', '3.0'));
d499142e 124 }
facb512c 125
126 function test_optional_param()
127 {
128 $_POST['username'] = 'post_user';
129 $_GET['username'] = 'get_user';
130 $this->assertEqual(optional_param('username', 'default_user'), 'post_user');
131
132 unset($_POST['username']);
133 $this->assertEqual(optional_param('username', 'default_user'), 'get_user');
134
135 unset($_GET['username']);
136 $this->assertEqual(optional_param('username', 'default_user'), 'default_user');
137 }
138
139 /**
140 * Used by {@link optional_param()} and {@link required_param()} to
141 * clean the variables and/or cast to specific types, based on
142 * an options field.
143 * <code>
144 * $course->format = clean_param($course->format, PARAM_ALPHA);
145 * $selectedgrade_item = clean_param($selectedgrade_item, PARAM_CLEAN);
146 * </code>
147 *
148 * @uses $CFG
149 * @uses PARAM_CLEAN
150 * @uses PARAM_INT
151 * @uses PARAM_INTEGER
152 * @uses PARAM_ALPHA
153 * @uses PARAM_ALPHANUM
154 * @uses PARAM_NOTAGS
155 * @uses PARAM_ALPHAEXT
156 * @uses PARAM_BOOL
157 * @uses PARAM_SAFEDIR
158 * @uses PARAM_CLEANFILE
159 * @uses PARAM_FILE
160 * @uses PARAM_PATH
161 * @uses PARAM_HOST
162 * @uses PARAM_URL
163 * @uses PARAM_LOCALURL
164 * @uses PARAM_CLEANHTML
165 * @uses PARAM_SEQUENCE
166 * @param mixed $param the variable we are cleaning
167 * @param int $type expected format of param after cleaning.
168 * @return mixed
169 */
170 function test_clean_param()
171 {
172 // Test unknown parameter type
173
174 // Test Raw param
175 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW),
176 '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
177
178 $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_CLEAN),
179 '#()*#,9789\\\'\".,');
180
181 }
7c9d46c9 182}
183
184?>