3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * Unit tests for /lib/filelib.php.
23 * @copyright 2009 Jerome Mouneyrac
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 if (!defined('MOODLE_INTERNAL')) {
28 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
30 require_once($CFG->libdir . '/filelib.php');
32 class filelib_test extends UnitTestCase {
33 public function test_format_postdata_for_curlcall() {
35 //POST params with just simple types
36 $postdatatoconvert =array( 'userid' => 1, 'roleid' => 22, 'name' => 'john');
37 $expectedresult = "userid=1&roleid=22&name=john";
38 $postdata = format_postdata_for_curlcall($postdatatoconvert);
39 $this->assertEqual($postdata, $expectedresult);
41 //POST params with a string containing & character
42 $postdatatoconvert =array( 'name' => 'john&emilie', 'roleid' => 22);
43 $expectedresult = "name=john%26emilie&roleid=22"; //urlencode: '%26' => '&'
44 $postdata = format_postdata_for_curlcall($postdatatoconvert);
45 $this->assertEqual($postdata, $expectedresult);
47 //POST params with an empty value
48 $postdatatoconvert =array( 'name' => null, 'roleid' => 22);
49 $expectedresult = "name=&roleid=22";
50 $postdata = format_postdata_for_curlcall($postdatatoconvert);
51 $this->assertEqual($postdata, $expectedresult);
53 //POST params with complex types
54 $postdatatoconvert =array( 'users' => array(
57 'customfields' => array(
67 $expectedresult = "users[0][id]=2&users[0][customfields][0][type]=Color&users[0][customfields][0][value]=violet";
68 $postdata = format_postdata_for_curlcall($postdatatoconvert);
69 $this->assertEqual($postdata, $expectedresult);
71 //POST params with other complex types
72 $postdatatoconvert = array ('members' =>
74 array('groupid' => 1, 'userid' => 1)
75 , array('groupid' => 1, 'userid' => 2)
78 $expectedresult = "members[0][groupid]=1&members[0][userid]=1&members[1][groupid]=1&members[1][userid]=2";
79 $postdata = format_postdata_for_curlcall($postdatatoconvert);
80 $this->assertEqual($postdata, $expectedresult);