unit tests MDL-22017 made test_usergetdate() work on machines in different timezones
[moodle.git] / lib / simpletest / testmoodlelib.php
1 <?php
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) ../moodlelib.php.
28  *
29  * Note, tests for get_string are in the separate file testgetstring.php.
30  *
31  * @copyright &copy; 2006 The Open University
32  * @author T.J.Hunt@open.ac.uk
33  * @author nicolas@moodle.com
34  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
35  * @package moodlecore
36  */
38 if (!defined('MOODLE_INTERNAL')) {
39     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
40 }
42 require_once($CFG->libdir . '/moodlelib.php');
44 class moodlelib_test extends UnitTestCase {
46     public static $includecoverage = array('lib/moodlelib.php');
48     var $user_agents = array(
49             'MSIE' => array(
50                 '5.5' => array('Windows 2000' => 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)'),
51                 '6.0' => array('Windows XP SP2' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'),
52                 '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)'),
53                 '8.0' => array('Windows Vista' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648)'),
55             ),
56             'Firefox' => array(
57                 '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'),
58                 '1.5'     => array('Windows XP' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.8) Gecko/20051107 Firefox/1.5'),
59                 '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'),
60                 '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',
61                                    'Ubuntu Linux AMD64' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1) Gecko/20060601 Firefox/2.0 (Ubuntu-edgy)'),
62                 '3.0.6' => array('SUSE' => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.6) Gecko/2009012700 SUSE/3.0.6-1.4 Firefox/3.0.6'),
63             ),
64             'Safari' => array(
65                 '312' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/312.1 (KHTML, like Gecko) Safari/312'),
66                 '2.0' => array('Mac OS X' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412')
67             ),
68             'Opera' => array(
69                 '8.51' => array('Windows XP' => 'Opera/8.51 (Windows NT 5.1; U; en)'),
70                 '9.0'  => array('Windows XP' => 'Opera/9.0 (Windows NT 5.1; U; en)',
71                                 'Debian Linux' => 'Opera/9.01 (X11; Linux i686; U; en)')
72             )
73         );
75     function test_cleanremoteaddr() {
76         //IPv4
77         $this->assertEqual(cleanremoteaddr('1023.121.234.1'), null);
78         $this->assertEqual(cleanremoteaddr('123.121.234.01 '), '123.121.234.1');
80         //IPv6
81         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0:0'), null);
82         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:abh'), null);
83         $this->assertEqual(cleanremoteaddr('0:0:0:::0:0:1'), null);
84         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:0:0', true), '::');
85         $this->assertEqual(cleanremoteaddr('0:0:0:0:0:0:1:1', true), '::1:1');
86         $this->assertEqual(cleanremoteaddr('abcd:00ef:0:0:0:0:0:0', true), 'abcd:ef::');
87         $this->assertEqual(cleanremoteaddr('1:0:0:0:0:0:0:1', true), '1::1');
88         $this->assertEqual(cleanremoteaddr('::10:1', false), '0:0:0:0:0:0:10:1');
89         $this->assertEqual(cleanremoteaddr('01:1::', false), '1:1:0:0:0:0:0:0');
90         $this->assertEqual(cleanremoteaddr('10::10', false), '10:0:0:0:0:0:0:10');
91         $this->assertEqual(cleanremoteaddr('::ffff:192.168.1.1', true), '::ffff:c0a8:11');
92     }
94     function test_address_in_subnet() {
95     /// 1: xxx.xxx.xxx.xxx/nn or xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/nnn          (number of bits in net mask)
96         $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.1/32'));
97         $this->assertFalse(address_in_subnet('123.121.23.1', '123.121.23.0/32'));
98         $this->assertTrue(address_in_subnet('10.10.10.100',  '123.121.23.45/0'));
99         $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/24'));
100         $this->assertFalse(address_in_subnet('123.121.34.1', '123.121.234.0/24'));
101         $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.234.0/30'));
102         $this->assertFalse(address_in_subnet('123.121.23.8', '123.121.23.0/30'));
103         $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
104         $this->assertFalse(address_in_subnet('bab:baba::baba', 'bab:baba::cece/128'));
105         $this->assertTrue(address_in_subnet('baba:baba::baba', 'cece:cece::cece/0'));
106         $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba/128'));
107         $this->assertTrue(address_in_subnet('baba:baba::00ba', 'baba:baba::/120'));
108         $this->assertFalse(address_in_subnet('baba:baba::aba', 'baba:baba::/120'));
109         $this->assertTrue(address_in_subnet('baba::baba:00ba', 'baba::baba:0/112'));
110         $this->assertFalse(address_in_subnet('baba::aba:00ba', 'baba::baba:0/112'));
111         $this->assertFalse(address_in_subnet('aba::baba:0000', 'baba::baba:0/112'));
113         // fixed input
114         $this->assertTrue(address_in_subnet('123.121.23.1   ', ' 123.121.23.0 / 24'));
115         $this->assertTrue(address_in_subnet('::ffff:10.1.1.1', ' 0:0:0:000:0:ffff:a1:10 / 126'));
117         // incorrect input
118         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/-2'));
119         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/64'));
120         $this->assertFalse(address_in_subnet('123.121.234.x', '123.121.234.1/24'));
121         $this->assertFalse(address_in_subnet('123.121.234.0', '123.121.234.xx/24'));
122         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.1/xx0'));
123         $this->assertFalse(address_in_subnet('::1', '::aa:0/xx0'));
124         $this->assertFalse(address_in_subnet('::1', '::aa:0/-5'));
125         $this->assertFalse(address_in_subnet('::1', '::aa:0/130'));
126         $this->assertFalse(address_in_subnet('x:1', '::aa:0/130'));
127         $this->assertFalse(address_in_subnet('::1', '::ax:0/130'));
130     /// 2: xxx.xxx.xxx.xxx-yyy or  xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx::xxxx-yyyy (a range of IP addresses in the last group)
131         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12-14'));
132         $this->assertTrue(address_in_subnet('123.121.234.13', '123.121.234.12-14'));
133         $this->assertTrue(address_in_subnet('123.121.234.14', '123.121.234.12-14'));
134         $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.12-14'));
135         $this->assertFalse(address_in_subnet('123.121.234.20', '123.121.234.12-14'));
136         $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.234.12-14'));
137         $this->assertFalse(address_in_subnet('123.12.234.12', '123.121.234.12-14'));
138         $this->assertTrue(address_in_subnet('baba:baba::baba', 'baba:baba::baba-babe'));
139         $this->assertTrue(address_in_subnet('baba:baba::babc', 'baba:baba::baba-babe'));
140         $this->assertTrue(address_in_subnet('baba:baba::babe', 'baba:baba::baba-babe'));
141         $this->assertFalse(address_in_subnet('bab:baba::bab0', 'bab:baba::baba-babe'));
142         $this->assertFalse(address_in_subnet('bab:baba::babf', 'bab:baba::baba-babe'));
143         $this->assertFalse(address_in_subnet('bab:baba::bfbe', 'bab:baba::baba-babe'));
144         $this->assertFalse(address_in_subnet('bfb:baba::babe', 'bab:baba::baba-babe'));
146         // fixed input
147         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12 - 14 '));
148         $this->assertTrue(address_in_subnet('bab:baba::babe', 'bab:baba::baba - babe  '));
150         // incorrect input
151         $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-234.14'));
152         $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12-256'));
153         $this->assertFalse(address_in_subnet('123.121.234.12', '123.121.234.12--256'));
156     /// 3: xxx.xxx or xxx.xxx. or xxx:xxx:xxxx or xxx:xxx:xxxx.                  (incomplete address, a bit non-technical ;-)
157         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.12'));
158         $this->assertFalse(address_in_subnet('123.121.23.12', '123.121.23.13'));
159         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234.'));
160         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121.234'));
161         $this->assertTrue(address_in_subnet('123.121.234.12', '123.121'));
162         $this->assertTrue(address_in_subnet('123.121.234.12', '123'));
163         $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234.'));
164         $this->assertFalse(address_in_subnet('123.121.234.1', '12.121.234'));
165         $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba::bab'));
166         $this->assertFalse(address_in_subnet('baba:baba::ba', 'baba:baba::bc'));
167         $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:baba'));
168         $this->assertTrue(address_in_subnet('baba:baba::bab', 'baba:'));
169         $this->assertFalse(address_in_subnet('bab:baba::bab', 'baba:'));
172     /// multiple subnets
173         $this->assertTrue(address_in_subnet('123.121.234.12', '::1/64, 124., 123.121.234.10-30'));
174         $this->assertTrue(address_in_subnet('124.121.234.12', '::1/64, 124., 123.121.234.10-30'));
175         $this->assertTrue(address_in_subnet('::2',            '::1/64, 124., 123.121.234.10-30'));
176         $this->assertFalse(address_in_subnet('12.121.234.12', '::1/64, 124., 123.121.234.10-30'));
179     /// other incorrect input
180         $this->assertFalse(address_in_subnet('123.123.123.123', ''));
181     }
183     /**
184      * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version
185      * works as expected.
186      */
187     function test_check_browser_version()
188     {
189         global $CFG;
191         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
192         $this->assertTrue(check_browser_version('Safari', '312'));
193         $this->assertFalse(check_browser_version('Safari', '500'));
195         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
196         $this->assertTrue(check_browser_version('Opera', '8.0'));
197         $this->assertFalse(check_browser_version('Opera', '10.0'));
199         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
200         $this->assertTrue(check_browser_version('MSIE', '5.0'));
201         $this->assertFalse(check_browser_version('MSIE', '7.0'));
203         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
204         $this->assertTrue(check_browser_version('Firefox', '1.5'));
205         $this->assertFalse(check_browser_version('Firefox', '3.0'));
206     }
208     function test_get_browser_version_classes() {
209         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];
210         $this->assertEqual(array('safari'), get_browser_version_classes());
212         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
213         $this->assertEqual(array('opera'), get_browser_version_classes());
215         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
216         $this->assertEqual(array('ie', 'ie6'), get_browser_version_classes());
218         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['7.0']['Windows XP SP2'];
219         $this->assertEqual(array('ie', 'ie7'), get_browser_version_classes());
221         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['8.0']['Windows Vista'];
222         $this->assertEqual(array('ie', 'ie8'), get_browser_version_classes());
224         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
225         $this->assertEqual(array('gecko', 'gecko18'), get_browser_version_classes());
227         $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['3.0.6']['SUSE'];
228         $this->assertEqual(array('gecko', 'gecko19'), get_browser_version_classes());
229     }
231     function test_optional_param() {
232         $_POST['username'] = 'post_user';
233         $_GET['username'] = 'get_user';
234         $this->assertEqual(optional_param('username', 'default_user', PARAM_CLEAN), 'post_user');
236         unset($_POST['username']);
237         $this->assertEqual(optional_param('username', 'default_user', PARAM_CLEAN), 'get_user');
239         unset($_GET['username']);
240         $this->assertEqual(optional_param('username', 'default_user', PARAM_CLEAN), 'default_user');
241     }
243     /**
244      * Used by {@link optional_param()} and {@link required_param()} to
245      * clean the variables and/or cast to specific types, based on
246      * an options field.
247      * <code>
248      * $course->format = clean_param($course->format, PARAM_ALPHA);
249      * $selectedgrade_item = clean_param($selectedgrade_item, PARAM_CLEAN);
250      * </code>
251      *
252      * @uses $CFG
253      * @uses PARAM_CLEAN
254      * @uses PARAM_INT
255      * @uses PARAM_INTEGER
256      * @uses PARAM_ALPHA
257      * @uses PARAM_ALPHANUM
258      * @uses PARAM_NOTAGS
259      * @uses PARAM_ALPHAEXT
260      * @uses PARAM_BOOL
261      * @uses PARAM_SAFEDIR
262      * @uses PARAM_FILE
263      * @uses PARAM_PATH
264      * @uses PARAM_HOST
265      * @uses PARAM_URL
266      * @uses PARAM_LOCALURL
267      * @uses PARAM_CLEANHTML
268      * @uses PARAM_SEQUENCE
269      * @uses PARAM_USERNAME
270      * @param mixed $param the variable we are cleaning
271      * @param int $type expected format of param after cleaning.
272      * @return mixed
273      */
274     function test_clean_param() {
275         global $CFG;
276         // Test unknown parameter type
278         // Test Raw param
279         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW),
280             '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
282         $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_CLEAN),
283             '#()*#,9789\'".,');
285         // Test PARAM_URL and PARAM_LOCALURL a bit
286         $this->assertEqual(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
287         $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_URL), 'http://some.very.long.and.silly.domain/with/a/path/');
288         $this->assertEqual(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
289         $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
290         $this->assertEqual(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
291         $this->assertEqual(clean_param('funny:thing', PARAM_URL), '');
293         $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL), '');
294         $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
295         $this->assertEqual(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
296         $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
297         $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL), '');
298         $this->assertEqual(clean_param('course/view.php?id=3', PARAM_LOCALURL), 'course/view.php?id=3');
300         $currentstatus =  $CFG->extendedusernamechars;
301         
302         // Run tests with extended character == FALSE;
303         $CFG->extendedusernamechars = FALSE;
304         $this->assertEqual(clean_param('johndoe123', PARAM_USERNAME), 'johndoe123' );
305         $this->assertEqual(clean_param('john.doe', PARAM_USERNAME), 'john.doe');
306         $this->assertEqual(clean_param('john-doe', PARAM_USERNAME), 'john-doe');
307         $this->assertEqual(clean_param('john- doe', PARAM_USERNAME), 'john-doe');
308         $this->assertEqual(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
309         $this->assertEqual(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
310         $this->assertEqual(clean_param('john~doe', PARAM_USERNAME), 'johndoe');
311         $this->assertEqual(clean_param('john´doe', PARAM_USERNAME), 'johndoe');
312         $this->assertEqual(clean_param('john#$%&() ', PARAM_USERNAME), 'john');
313         $this->assertEqual(clean_param('JOHNdóé ', PARAM_USERNAME), 'johnd');
314         $this->assertEqual(clean_param('john.,:;-_/|\ñÑ[]A_X-,D {} ~!@#$%^&*()_+ ?><[] ščřžžý ?ýá\9e?\9eý??\9adoe ', PARAM_USERNAME), 'john.-_a_x-d@_doe');
315         
317         // Test success condition, if extendedusernamechars == ENABLE;
318         $CFG->extendedusernamechars = TRUE;
319         $this->assertEqual(clean_param('john_doe', PARAM_USERNAME), 'john_doe');
320         $this->assertEqual(clean_param('john@doe', PARAM_USERNAME), 'john@doe');
321         $this->assertEqual(clean_param('john# $%&()+_^', PARAM_USERNAME), 'john#$%&()+_^');
322         $this->assertEqual(clean_param('john~doe', PARAM_USERNAME), 'john~doe');
323         $this->assertEqual(clean_param('joHN´doe', PARAM_USERNAME), 'john´doe');
324         $this->assertEqual(clean_param('johnDOE', PARAM_USERNAME), 'johndoe');
325         $this->assertEqual(clean_param('johndóé ', PARAM_USERNAME), 'johndóé');
326                 
327         $CFG->extendedusernamechars = $currentstatus;
328     }
330     function test_validate_param() {
331         try {
332             $param = validate_param('11a', PARAM_INT);
333             $this->fail('invalid_parameter_exception expected');
334         } catch (invalid_parameter_exception $ex) {
335             $this->assertTrue(true);
336         }
337         try {
338             $param = validate_param('11', PARAM_INT);
339             $this->assertEqual($param, 11);
340         } catch (invalid_parameter_exception $ex) {
341             $this->fail('invalid_parameter_exception not expected');
342         }
343         try {
344             $param = validate_param(null, PARAM_INT, false);
345             $this->fail('invalid_parameter_exception expected');
346         } catch (invalid_parameter_exception $ex) {
347             $this->assertTrue(true);
348         }
349         try {
350             $param = validate_param(null, PARAM_INT, true);
351             $this->assertTrue($param===null);
352         } catch (invalid_parameter_exception $ex) {
353             $this->fail('invalid_parameter_exception expected');
354         }
355         try {
356             $param = validate_param(array(), PARAM_INT);
357             $this->fail('invalid_parameter_exception expected');
358         } catch (invalid_parameter_exception $ex) {
359             $this->assertTrue(true);
360         }
361         try {
362             $param = validate_param(new stdClass, PARAM_INT);
363             $this->fail('invalid_parameter_exception expected');
364         } catch (invalid_parameter_exception $ex) {
365             $this->assertTrue(true);
366         }
367     }
369     function test_make_user_directory() {
370         global $CFG;
372         // Test success conditions
373         $this->assertEqual("$CFG->dataroot/user/0/0", make_user_directory(0, true));
374         $this->assertEqual("$CFG->dataroot/user/0/1", make_user_directory(1, true));
375         $this->assertEqual("$CFG->dataroot/user/0/999", make_user_directory(999, true));
376         $this->assertEqual("$CFG->dataroot/user/1000/1000", make_user_directory(1000, true));
377         $this->assertEqual("$CFG->dataroot/user/2147483000/2147483647", make_user_directory(2147483647, true)); // Largest int possible
379         // Test fail conditions
380         $this->assertFalse(make_user_directory(2147483648, true)); // outside int boundary
381         $this->assertFalse(make_user_directory(-1, true));
382         $this->assertFalse(make_user_directory('string', true));
383         $this->assertFalse(make_user_directory(false, true));
384         $this->assertFalse(make_user_directory(true, true));
386     }
388     function test_shorten_text() {
389         $text = "short text already no tags";
390         $this->assertEqual($text, shorten_text($text));
392         $text = "<p>short <b>text</b> already</p><p>with tags</p>";
393         $this->assertEqual($text, shorten_text($text));
395         $text = "long text without any tags blah de blah blah blah what";
396         $this->assertEqual('long text without any tags ...', shorten_text($text));
398         $text = "<div class='frog'><p><blockquote>Long text with tags that will ".
399             "be chopped off but <b>should be added back again</b></blockquote></p></div>";
400         $this->assertEqual("<div class='frog'><p><blockquote>Long text with " .
401             "tags that ...</blockquote></p></div>", shorten_text($text));
403         $text = "some text which shouldn't &nbsp; break there";
404         $this->assertEqual("some text which shouldn't &nbsp; ...",
405             shorten_text($text, 31));
406         $this->assertEqual("some text which shouldn't ...",
407             shorten_text($text, 30));
409         // This case caused a bug up to 1.9.5
410         $text = "<h3>standard 'break-out' sub groups in TGs?</h3>&nbsp;&lt;&lt;There are several";
411         $this->assertEqual("<h3>standard 'break-out' sub groups in ...</h3>",
412             shorten_text($text, 43));
414         $text = "<h1>123456789</h1>";//a string with no convenient breaks
415         $this->assertEqual("<h1>12345...</h1>",
416             shorten_text($text, 8));
417     }
419     function test_usergetdate() {
420         global $USER;
422         $userstimezone = $USER->timezone;
423         $USER->timezone = 2;//set the timezone to a known state
425         $ts = 1261540267; //the time this function was created
427         $arr = usergetdate($ts,1);//specify the timezone as an argument
428         $arr = array_values($arr);
430         list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
431         $this->assertEqual($seconds,7);
432         $this->assertEqual($minutes,51);
433         $this->assertEqual($hours,4);
434         $this->assertEqual($mday,23);
435         $this->assertEqual($wday,3);
436         $this->assertEqual($mon,12);
437         $this->assertEqual($year,2009);
438         $this->assertEqual($yday,357);
439         $this->assertEqual($weekday,'Wednesday');
440         $this->assertEqual($month,'December');
442         $arr = usergetdate($ts);//gets the timezone from the $USER object
443         $arr = array_values($arr);
445         list($seconds,$minutes,$hours,$mday,$wday,$mon,$year,$yday,$weekday,$month) = $arr;
446         $this->assertEqual($seconds,7);
447         $this->assertEqual($minutes,51);
448         $this->assertEqual($hours,5);
449         $this->assertEqual($mday,23);
450         $this->assertEqual($wday,3);
451         $this->assertEqual($mon,12);
452         $this->assertEqual($year,2009);
453         $this->assertEqual($yday,357);
454         $this->assertEqual($weekday,'Wednesday');
455         $this->assertEqual($month,'December');
457         //set the timezone back to what it was
458         $USER->timezone = $userstimezone;
459     }