5db608f4 |
1 | <?php |
2 | |
3 | // This file is part of Moodle - http://moodle.org/ |
4 | // |
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. |
9 | // |
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. |
14 | // |
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/>. |
17 | |
18 | |
19 | /** |
20 | * Unit tests for /lib/filelib.php. |
21 | * |
22 | * @package file |
23 | * @copyright 2009 Jerome Mouneyrac |
24 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
25 | */ |
26 | |
27 | if (!defined('MOODLE_INTERNAL')) { |
28 | die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page |
29 | } |
30 | require_once($CFG->libdir . '/filelib.php'); |
31 | |
32 | class filelib_test extends UnitTestCase { |
33 | public function test_format_postdata_for_curlcall() { |
34 | |
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); |
40 | |
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); |
46 | |
47 | //POST params with an empty value |
48 | $postdatatoconvert =array( 'name' => null, 'roleid' => 22); |
5a18a8fa |
49 | $expectedresult = "name=&roleid=22"; |
5db608f4 |
50 | $postdata = format_postdata_for_curlcall($postdatatoconvert); |
51 | $this->assertEqual($postdata, $expectedresult); |
52 | |
53 | //POST params with complex types |
54 | $postdatatoconvert =array( 'users' => array( |
55 | array( |
56 | 'id' => 2, |
57 | 'customfields' => array( |
58 | array |
59 | ( |
60 | 'type' => 'Color', |
61 | 'value' => 'violet' |
62 | ) |
63 | ) |
64 | ) |
65 | ) |
66 | ); |
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); |
70 | |
277c7a40 |
71 | //POST params with other complex types |
72 | $postdatatoconvert = array ('members' => |
73 | array( |
74 | array('groupid' => 1, 'userid' => 1) |
75 | , array('groupid' => 1, 'userid' => 2) |
76 | ) |
77 | ); |
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); |
5db608f4 |
81 | } |
82 | } |