weekly release 2.3dev
[moodle.git] / lib / tests / filelib_test.php
CommitLineData
a3d5830a
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17
18/**
19 * Unit tests for /lib/filelib.php.
20 *
21 * @package core_files
22 * @category phpunit
23 * @copyright 2009 Jerome Mouneyrac
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->libdir . '/filelib.php');
31
4b027431 32class filelib_testcase extends basic_testcase {
a3d5830a
PS
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->assertEquals($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->assertEquals($postdata, $expectedresult);
46
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->assertEquals($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->assertEquals($postdata, $expectedresult);
70
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->assertEquals($postdata, $expectedresult);
81 }
82
83 public function test_download_file_content() {
84 $testhtml = "http://download.moodle.org/unittest/test.html";
85 $contents = download_file_content($testhtml);
86 $this->assertEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents));
87 }
88}