MDL-28666, Fixed source field when download a file using filepicker
[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
7051415c
DC
105 $repositorypluginname = 'user';
106
107 $args = array();
108 $args['type'] = $repositorypluginname;
109 $repos = repository::get_instances($args);
110 $userrepository = reset($repos);
111 $this->assertInstanceOf('repository', $userrepository);
67233725
DC
112
113 $fs = get_file_storage();
7051415c 114
67233725
DC
115 $syscontext = context_system::instance();
116 $component = 'core';
117 $filearea = 'unittest';
118 $itemid = 0;
119 $filepath = '/';
120 $filename = 'test.txt';
121 $sourcefield = 'Copyright stuff';
122
123 $filerecord = array(
124 'contextid' => $syscontext->id,
125 'component' => $component,
126 'filearea' => $filearea,
127 'itemid' => $itemid,
128 'filepath' => $filepath,
129 'filename' => $filename,
130 'source' => $sourcefield,
131 );
132 $ref = $fs->pack_reference($filerecord);
67233725 133 $originalfile = $fs->create_file_from_string($filerecord, 'Test content');
67233725
DC
134 $fileid = $originalfile->get_id();
135 $this->assertInstanceOf('stored_file', $originalfile);
136
7051415c
DC
137 // create a user private file
138 $userfilerecord = new stdClass;
139 $userfilerecord->contextid = $usercontext->id;
140 $userfilerecord->component = 'user';
141 $userfilerecord->filearea = 'private';
142 $userfilerecord->itemid = 0;
143 $userfilerecord->filepath = '/';
144 $userfilerecord->filename = 'userfile.txt';
145 $userfilerecord->source = 'test';
146 $userfile = $fs->create_file_from_string($userfilerecord, 'User file content');
147 $userfileref = $fs->pack_reference($userfilerecord);
148
149 $filerefrecord = clone((object)$filerecord);
150 $filerefrecord->filename = 'testref.txt';
151 // create a file reference
152 $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref);
153 $this->assertInstanceOf('stored_file', $fileref);
154 $this->assertEquals($userrepository->id, $fileref->repository->id);
155 $this->assertEquals($userfile->get_contenthash(), $fileref->get_contenthash());
156 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize());
157 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details());
158
67233725
DC
159 $draftitemid = 0;
160 file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid);
161
162 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid);
7051415c 163 $this->assertEquals(3, count($draftfiles));
67233725
DC
164
165 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename);
166 $source = unserialize($draftfile->get_source());
167 $this->assertEquals($ref, $source->original);
168 $this->assertEquals($sourcefield, $source->source);
169
7051415c
DC
170 $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename);
171 $this->assertInstanceOf('stored_file', $draftfileref);
172 $this->assertEquals(true, $draftfileref->is_external_file());
67233725
DC
173
174 // change some information
175 $author = 'Dongsheng Cai';
176 $draftfile->set_author($author);
7051415c 177 $newsourcefield = 'Get from Flickr';
67233725
DC
178 $license = 'GPLv3';
179 $draftfile->set_license($license);
180 // if you want to really just change source field, do this:
181 $source = unserialize($draftfile->get_source());
182 $newsourcefield = 'From flickr';
183 $source->source = $newsourcefield;
184 $draftfile->set_source(serialize($source));
185
186 // Save changed file
187 file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid);
188
189 $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename);
190
191 // Make sure it's the original file id
192 $this->assertEquals($fileid, $file->get_id());
193 $this->assertInstanceOf('stored_file', $file);
194 $this->assertEquals($author, $file->get_author());
195 $this->assertEquals($license, $file->get_license());
196 $this->assertEquals($newsourcefield, $file->get_source());
197 }
a3d5830a 198}