Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletest/testexternallib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 
00027 if (!defined('MOODLE_INTERNAL')) {
00028     die('Direct access to this script is forbidden.');    
00029 }
00030 require_once($CFG->libdir . '/externallib.php');
00031 
00032 class externallib_test extends UnitTestCase {
00033     public function test_validate_params() {
00034         $params = array('text'=>'aaa', 'someid'=>'6',);
00035         $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value'),
00036                                                               'text'   => new external_value(PARAM_ALPHA, 'Some text value')));
00037         $result = external_api::validate_parameters($description, $params);
00038         $this->assertEqual(count($result), 2);
00039         reset($result);
00040         $this->assertTrue(key($result) === 'someid');
00041         $this->assertTrue($result['someid'] === 6);
00042         $this->assertTrue($result['text'] === 'aaa');
00043 
00044 
00045         $params = array('someids'=>array('1', 2, 'a'=>'3'), 'scalar'=>666);
00046         $description = new external_function_parameters(array('someids' => new external_multiple_structure(new external_value(PARAM_INT, 'Some ID')),
00047                                                               'scalar'  => new external_value(PARAM_ALPHANUM, 'Some text value')));
00048         $result = external_api::validate_parameters($description, $params);
00049         $this->assertEqual(count($result), 2);
00050         reset($result);
00051         $this->assertTrue(key($result) === 'someids');
00052         $this->assertTrue($result['someids'] == array(0=>1, 1=>2, 2=>3));
00053         $this->assertTrue($result['scalar'] === '666');
00054 
00055 
00056         $params = array('text'=>'aaa');
00057         $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value', false),
00058                                                               'text'   => new external_value(PARAM_ALPHA, 'Some text value')));
00059         $result = external_api::validate_parameters($description, $params);
00060         $this->assertEqual(count($result), 2);
00061         reset($result);
00062         $this->assertTrue(key($result) === 'someid');
00063         $this->assertTrue($result['someid'] === null);
00064         $this->assertTrue($result['text'] === 'aaa');
00065 
00066 
00067         $params = array('text'=>'aaa');
00068         $description = new external_function_parameters(array('someid' => new external_value(PARAM_INT, 'Some int value', false, 6),
00069                                                               'text'   => new external_value(PARAM_ALPHA, 'Some text value')));
00070         $result = external_api::validate_parameters($description, $params);
00071         $this->assertEqual(count($result), 2);
00072         reset($result);
00073         $this->assertTrue(key($result) === 'someid');
00074         $this->assertTrue($result['someid'] === 6);
00075         $this->assertTrue($result['text'] === 'aaa');
00076     }
00077 }
 All Data Structures Namespaces Files Functions Variables Enumerations