MDL-31901, MDL-28666 merged files API changes
[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
67233725 32class filelib_testcase extends advanced_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 }
67233725
DC
88
89 /**
90 * Testing prepare draft area
91 *
92 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
93 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
94 */
95 public function test_prepare_draft_area() {
96 global $USER, $DB;
97
98 $this->resetAfterTest(true);
99
100 $generator = $this->getDataGenerator();
101 $user = $generator->create_user();
102 $usercontext = context_user::instance($user->id);
103 $USER = $DB->get_record('user', array('id'=>$user->id));
104
105
106 $fs = get_file_storage();
107 $syscontext = context_system::instance();
108 $component = 'core';
109 $filearea = 'unittest';
110 $itemid = 0;
111 $filepath = '/';
112 $filename = 'test.txt';
113 $sourcefield = 'Copyright stuff';
114
115 $filerecord = array(
116 'contextid' => $syscontext->id,
117 'component' => $component,
118 'filearea' => $filearea,
119 'itemid' => $itemid,
120 'filepath' => $filepath,
121 'filename' => $filename,
122 'source' => $sourcefield,
123 );
124 $ref = $fs->pack_reference($filerecord);
125
126 $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
127
128 $fileid = $originalfile->get_id();
129 $this->assertInstanceOf('stored_file', $originalfile);
130
131 $draftitemid = 0;
132 file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
133
134 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
135 $this->assertEquals(2, count($draftfiles));
136
137 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
138 $source = unserialize($draftfile->get_source());
139 $this->assertEquals($ref, $source->original);
140 $this->assertEquals($sourcefield, $source->source);
141
142
143 // change some information
144 $author = 'Dongsheng Cai';
145 $draftfile->set_author($author);
146 $newsourcefield = 'Get from flickr';
147 $license = 'GPLv3';
148 $draftfile->set_license($license);
149 // if you want to really just change source field, do this:
150 $source = unserialize($draftfile->get_source());
151 $newsourcefield = 'From flickr';
152 $source->source = $newsourcefield;
153 $draftfile->set_source(serialize($source));
154
155 // Save changed file
156 file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
157
158 $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
159
160 // Make sure it's the original file id
161 $this->assertEquals($fileid, $file->get_id());
162 $this->assertInstanceOf('stored_file', $file);
163 $this->assertEquals($author, $file->get_author());
164 $this->assertEquals($license, $file->get_license());
165 $this->assertEquals($newsourcefield, $file->get_source());
166 }
a3d5830a 167}