MDL-22236 webservice : Remove filearea restrictions on user draft areas.
[moodle.git] / files / tests / externallib_test.php
CommitLineData
11f20be7
AA
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 * PHPunit tests for external files API.
20 *
21 * @package core_files
22 * @category external
23 * @copyright 2013 Ankit Agarwal
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 * @since Moodle 2.6
26 */
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32require_once($CFG->dirroot . '/files/externallib.php');
33
8252b7c2 34class core_files_externallib_testcase extends advanced_testcase {
11f20be7
AA
35
36 /*
37 * Test core_files_external::upload().
38 */
39
40 public function test_upload() {
41 global $USER;
42
43 $this->resetAfterTest();
44 $this->setAdminUser();
45 $context = context_user::instance($USER->id);
46 $contextid = $context->id;
47 $component = "user";
48 $filearea = "private";
49 $itemid = 0;
50 $filepath = "/";
51 $filename = "Simple.txt";
52 $filecontent = base64_encode("Let us create a nice simple file");
57d16acd
AA
53 $contextlevel = null;
54 $instanceid = null;
11f20be7
AA
55 $browser = get_file_browser();
56
57 // Make sure no file exists.
58 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
59 $this->assertEmpty($file);
60
61 // Call the api to create a file.
57d16acd
AA
62 core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
63 $filename, $filecontent, $contextlevel, $instanceid);
11f20be7
AA
64
65 // Make sure the file was created.
66 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
67 $this->assertNotEmpty($file);
68
69 // Make sure no file exists.
70 $itemid = 2;
71 $filename = "Simple2.txt";
72 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
73 $this->assertEmpty($file);
74
75 // Call the api to create a file.
57d16acd
AA
76 $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid,
77 $filepath, $filename, $filecontent, $contextlevel, $instanceid);
11f20be7
AA
78
79 // Make sure itemid is always set to 0.
80 $this->assertEquals(0, $fileinfo['itemid']);
81
57d16acd
AA
82 // Let us try creating a file using contextlevel and instance id.
83 $itemid = 0;
84 $filename = "Simple5.txt";
85 $contextid = 0;
86 $contextlevel = "user";
87 $instanceid = $USER->id;
88 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
89 $this->assertEmpty($file);
90 $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
91 $filename, $filecontent, $contextlevel, $instanceid);
92 $this->assertEmpty($file);
93
11f20be7
AA
94 // Make sure the same file cannot be created again.
95 $this->setExpectedException("moodle_exception");
57d16acd
AA
96 core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
97 $filename, $filecontent, $contextlevel, $instanceid);
11f20be7
AA
98 }
99
100 /*
101 * Make sure only user component is allowed in core_files_external::upload().
102 */
103 public function test_upload_param_component() {
104 global $USER;
105
106 $this->resetAfterTest();
107 $this->setAdminUser();
108 $context = context_user::instance($USER->id);
109 $contextid = $context->id;
110 $component = "backup";
111 $filearea = "private";
112 $itemid = 0;
113 $filepath = "/";
114 $filename = "Simple3.txt";
115 $filecontent = base64_encode("Let us create a nice simple file");
57d16acd
AA
116 $contextlevel = null;
117 $instanceid = null;
11f20be7
AA
118
119 // Make sure exception is thrown.
120 $this->setExpectedException("coding_exception");
57d16acd
AA
121 core_files_external::upload($contextid, $component, $filearea, $itemid,
122 $filepath, $filename, $filecontent, $contextlevel, $instanceid);
11f20be7
AA
123 }
124
125 /*
126 * Make sure only private area is allowed in core_files_external::upload().
127 */
128 public function test_upload_param_area() {
129 global $USER;
130
131 $this->resetAfterTest();
132 $this->setAdminUser();
133 $context = context_user::instance($USER->id);
134 $contextid = $context->id;
135 $component = "user";
136 $filearea = "draft";
137 $itemid = 0;
138 $filepath = "/";
139 $filename = "Simple4.txt";
140 $filecontent = base64_encode("Let us create a nice simple file");
57d16acd
AA
141 $contextlevel = null;
142 $instanceid = null;
11f20be7
AA
143
144 // Make sure exception is thrown.
145 $this->setExpectedException("coding_exception");
57d16acd
AA
146 core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
147 $filename, $filecontent, $contextlevel, $instanceid);
11f20be7
AA
148 }
149
71e38b12
AA
150 /*
151 * Make sure core_files_external::upload() works without new parameters.
152 */
153 public function test_upload_without_new_param() {
154 global $USER;
155
156 $this->resetAfterTest();
157 $this->setAdminUser();
158 $context = context_user::instance($USER->id);
159 $contextid = $context->id;
160 $component = "user";
161 $filearea = "private";
162 $itemid = 0;
163 $filepath = "/";
164 $filename = "Simple4.txt";
165 $filecontent = base64_encode("Let us create a nice simple file");
166
167 // Make sure the file is created.
168 @core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
169 $browser = get_file_browser();
170 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
171 $this->assertNotEmpty($file);
172 }
11f20be7 173}