Fixes bug MDL-8695 "Form 'required' icon is hidden", see commit 13 Jan.
[moodle.git] / lib / simpletest / testmoodlelib.php
CommitLineData
7c9d46c9 1<?php
2/**
3 * Unit tests for (some of) ../moodlelib.php.
4 *
5 * @copyright &copy; 2006 The Open University
6 * @author T.J.Hunt@open.ac.uk
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package moodlecore
9 */
10
239c1576 11/** $Id */
7c9d46c9 12require_once(dirname(__FILE__) . '/../../config.php');
13
14global $CFG;
15require_once($CFG->libdir . '/simpletestlib.php');
16require_once($CFG->libdir . '/moodlelib.php');
17
18class moodlelib_test extends UnitTestCase {
239c1576 19
20 /**
21 * An array of possible user_agent strings
22 *
23 * @var array possible user_agent strings
24 * @TODO Complete that list using http://www.pgts.com.au/pgtsj/pgtsj0208c.html
25 */
26 var $user_agents = array(
27 'MSIE' => array(
28 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
29 '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)')
30 ),
31 'Firefox' => array(
32 '1.5' => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
33 '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')
34 ),
35 'Safari' => array(
36 '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412'),
37 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312')
38 ),
39 'Opera' => array(
40 '9.0' => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)')
41 )
42 );
43
7c9d46c9 44 function setUp() {
45 }
46
47 function tearDown() {
48 }
49
50 function test_address_in_subnet() {
51 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1'));
52 $this->assertFalse(address_in_subnet('123.121.234.2', '123.121.234.1'));
53 $this->assertFalse(address_in_subnet('123.121.134.1', '123.121.234.1'));
54 $this->assertFalse(address_in_subnet('113.121.234.1', '123.121.234.1'));
55 $this->assertTrue(address_in_subnet('123.121.234.0', '123.121.234.2/28'));
56 $this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.2/28'));
57 $this->assertFalse(address_in_subnet('123.121.234.16', '123.121.234.2/28'));
58 $this->assertFalse(address_in_subnet('123.121.234.255', '123.121.234.2/28'));
59 $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.'));
60 $this->assertFalse(address_in_subnet('123.122.234.1', '123.121.'));
61 $this->assertFalse(address_in_subnet('223.121.234.1', '123.121.'));
62 $this->assertFalse(address_in_subnet('123.121.234.9', '123.121.234.10-20'));
63 $this->assertTrue(address_in_subnet('123.121.234.10', '123.121.234.10-20'));
64 $this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.10-20'));
65 $this->assertTrue(address_in_subnet('123.121.234.20', '123.121.234.10-20'));
66 $this->assertFalse(address_in_subnet('123.121.234.21', '123.121.234.10-20'));
67 $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 '));
68 $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 '));
69 $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 '));
70 $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 '));
71 $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 '));
72 $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 '));
73 $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 '));
74 $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 '));
75 }
76
239c1576 77 /**
78 * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
79 * works as expected.
80 */
81 function test_check_browser_version()
82 {
83 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
84 $this->assertTrue(check_browser_version('Safari', '312'));
85 $this->assertFalse(check_browser_version('Safari', '500'));
86
87 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
88 $this->assertTrue(check_browser_version('Opera', '8.0'));
89 $this->assertFalse(check_browser_version('Opera', '10.0'));
90
91 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
92 $this->assertTrue(check_browser_version('MSIE', '5.0'));
93 $this->assertFalse(check_browser_version('MSIE', '7.0'));
94
95 $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
96 $this->assertTrue(check_browser_version('Firefox', '1.5'));
97 $this->assertFalse(check_browser_version('Firefox', '3.0'));
98 }
7c9d46c9 99}
100
101?>