[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/lib/simpletest/ -> testmoodlelib.php (source)

   1  <?php // $Id$
   2  
   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  ///////////////////////////////////////////////////////////////////////////
  25  
  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
  31   * @author nicolas@moodle.com
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  33   * @package moodlecore
  34   */
  35  
  36  if (!defined('MOODLE_INTERNAL')) {
  37      die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
  38  }
  39  
  40  require_once($CFG->libdir . '/moodlelib.php');
  41  
  42  class moodlelib_test extends UnitTestCase {
  43      
  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          
  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.0', '123.121.234.0/')); // / is like /32.
  84          $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.0/'));
  85          $this->assertFalse(address_in_subnet('232.232.232.232', '123.121.234.0/0'));
  86          $this->assertFalse(address_in_subnet('123.122.234.1', '123.121.'));
  87          $this->assertFalse(address_in_subnet('223.121.234.1', '123.121.'));
  88          $this->assertTrue(address_in_subnet('123.121.234.1', '123.121'));
  89          $this->assertFalse(address_in_subnet('123.122.234.1', '123.121'));
  90          $this->assertFalse(address_in_subnet('223.121.234.1', '123.121'));
  91          $this->assertFalse(address_in_subnet('123.121.234.100', '123.121.234.10'));
  92          $this->assertFalse(address_in_subnet('123.121.234.9', '123.121.234.10-20'));
  93          $this->assertTrue(address_in_subnet('123.121.234.10', '123.121.234.10-20'));
  94          $this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.10-20'));
  95          $this->assertTrue(address_in_subnet('123.121.234.20', '123.121.234.10-20'));
  96          $this->assertFalse(address_in_subnet('123.121.234.21', '123.121.234.10-20'));
  97          $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  '));
  98          $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  '));
  99          $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  '));
 100          $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  '));
 101          $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  '));
 102          $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  '));
 103          $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  '));
 104          $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  '));
 105      }
 106      
 107      /**
 108       * Modifies $_SERVER['HTTP_USER_AGENT'] manually to check if check_browser_version 
 109       * works as expected.
 110       */
 111      function test_check_browser_version()
 112      {
 113          global $CFG;
 114          
 115          $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Safari']['2.0']['Mac OS X'];        
 116          $this->assertTrue(check_browser_version('Safari', '312'));
 117          $this->assertFalse(check_browser_version('Safari', '500'));
 118          
 119          $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Opera']['9.0']['Windows XP'];
 120          $this->assertTrue(check_browser_version('Opera', '8.0'));
 121          $this->assertFalse(check_browser_version('Opera', '10.0'));
 122          
 123          $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['MSIE']['6.0']['Windows XP SP2'];
 124          $this->assertTrue(check_browser_version('MSIE', '5.0'));
 125          $this->assertFalse(check_browser_version('MSIE', '7.0'));
 126          
 127          $_SERVER['HTTP_USER_AGENT'] = $this->user_agents['Firefox']['2.0']['Windows XP'];
 128          $this->assertTrue(check_browser_version('Firefox', '1.5'));
 129          $this->assertFalse(check_browser_version('Firefox', '3.0'));        
 130      }
 131      
 132      function test_optional_param()
 133      {
 134          $_POST['username'] = 'post_user';   
 135          $_GET['username'] = 'get_user';
 136          $this->assertEqual(optional_param('username', 'default_user'), 'post_user');
 137          
 138          unset($_POST['username']);
 139          $this->assertEqual(optional_param('username', 'default_user'), 'get_user');
 140          
 141          unset($_GET['username']);
 142          $this->assertEqual(optional_param('username', 'default_user'), 'default_user');
 143      }
 144      
 145      /**
 146       * Used by {@link optional_param()} and {@link required_param()} to
 147       * clean the variables and/or cast to specific types, based on
 148       * an options field.
 149       * <code>
 150       * $course->format = clean_param($course->format, PARAM_ALPHA);
 151       * $selectedgrade_item = clean_param($selectedgrade_item, PARAM_CLEAN);
 152       * </code>
 153       *
 154       * @uses $CFG
 155       * @uses PARAM_CLEAN
 156       * @uses PARAM_INT
 157       * @uses PARAM_INTEGER
 158       * @uses PARAM_ALPHA
 159       * @uses PARAM_ALPHANUM
 160       * @uses PARAM_NOTAGS
 161       * @uses PARAM_ALPHAEXT
 162       * @uses PARAM_BOOL
 163       * @uses PARAM_SAFEDIR
 164       * @uses PARAM_CLEANFILE
 165       * @uses PARAM_FILE
 166       * @uses PARAM_PATH
 167       * @uses PARAM_HOST
 168       * @uses PARAM_URL
 169       * @uses PARAM_LOCALURL
 170       * @uses PARAM_CLEANHTML
 171       * @uses PARAM_SEQUENCE
 172       * @param mixed $param the variable we are cleaning
 173       * @param int $type expected format of param after cleaning.
 174       * @return mixed
 175       */
 176      function test_clean_param()
 177      {
 178          global $CFG;
 179          // Test unknown parameter type
 180          
 181          // Test Raw param
 182          $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_RAW), 
 183              '#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)');
 184          
 185          $this->assertEqual(clean_param('#()*#,9789\'".,<42897></?$(*DSFMO#$*)(SDJ)($*)', PARAM_CLEAN), 
 186              '#()*#,9789\\\'\".,');
 187  
 188          // Test PARAM_URL and PARAM_LOCALURL a bit
 189          $this->assertEqual(clean_param('http://google.com/', PARAM_URL), 'http://google.com/');
 190          $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/');
 191          $this->assertEqual(clean_param('http://localhost/', PARAM_URL), 'http://localhost/');
 192          $this->assertEqual(clean_param('http://0.255.1.1/numericip.php', PARAM_URL), 'http://0.255.1.1/numericip.php');
 193          $this->assertEqual(clean_param('/just/a/path', PARAM_URL), '/just/a/path');
 194          $this->assertEqual(clean_param('funny:thing', PARAM_URL), '');
 195  
 196          $this->assertEqual(clean_param('http://google.com/', PARAM_LOCALURL), '');
 197          $this->assertEqual(clean_param('http://some.very.long.and.silly.domain/with/a/path/', PARAM_LOCALURL), '');
 198          $this->assertEqual(clean_param($CFG->wwwroot, PARAM_LOCALURL), $CFG->wwwroot);
 199          $this->assertEqual(clean_param('/just/a/path', PARAM_LOCALURL), '/just/a/path');
 200          $this->assertEqual(clean_param('funny:thing', PARAM_LOCALURL), '');
 201      }
 202  
 203      function test_make_user_directory() {
 204          global $CFG;
 205  
 206          // Test success conditions
 207          $this->assertEqual("$CFG->dataroot/user/0/0", make_user_directory(0, true));
 208          $this->assertEqual("$CFG->dataroot/user/0/1", make_user_directory(1, true));
 209          $this->assertEqual("$CFG->dataroot/user/0/999", make_user_directory(999, true));
 210          $this->assertEqual("$CFG->dataroot/user/1000/1000", make_user_directory(1000, true));
 211          $this->assertEqual("$CFG->dataroot/user/2147483000/2147483647", make_user_directory(2147483647, true)); // Largest int possible
 212  
 213          // Test fail conditions
 214          $this->assertFalse(make_user_directory(2147483648, true)); // outside int boundary
 215          $this->assertFalse(make_user_directory(-1, true));
 216          $this->assertFalse(make_user_directory('string', true));
 217          $this->assertFalse(make_user_directory(false, true));
 218          $this->assertFalse(make_user_directory(true, true));
 219          
 220      }
 221  }
 222  
 223  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7