weekly release 2.6dev
[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
34class test_external_files extends advanced_testcase {
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");
53 $browser = get_file_browser();
54
55 // Make sure no file exists.
56 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
57 $this->assertEmpty($file);
58
59 // Call the api to create a file.
60 core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
61
62 // Make sure the file was created.
63 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
64 $this->assertNotEmpty($file);
65
66 // Make sure no file exists.
67 $itemid = 2;
68 $filename = "Simple2.txt";
69 $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
70 $this->assertEmpty($file);
71
72 // Call the api to create a file.
73 $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
74
75 // Make sure itemid is always set to 0.
76 $this->assertEquals(0, $fileinfo['itemid']);
77
78 // Make sure the same file cannot be created again.
79 $this->setExpectedException("moodle_exception");
80 core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
81 }
82
83 /*
84 * Make sure only user component is allowed in core_files_external::upload().
85 */
86 public function test_upload_param_component() {
87 global $USER;
88
89 $this->resetAfterTest();
90 $this->setAdminUser();
91 $context = context_user::instance($USER->id);
92 $contextid = $context->id;
93 $component = "backup";
94 $filearea = "private";
95 $itemid = 0;
96 $filepath = "/";
97 $filename = "Simple3.txt";
98 $filecontent = base64_encode("Let us create a nice simple file");
99
100 // Make sure exception is thrown.
101 $this->setExpectedException("coding_exception");
102 core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
103 }
104
105 /*
106 * Make sure only private area is allowed in core_files_external::upload().
107 */
108 public function test_upload_param_area() {
109 global $USER;
110
111 $this->resetAfterTest();
112 $this->setAdminUser();
113 $context = context_user::instance($USER->id);
114 $contextid = $context->id;
115 $component = "user";
116 $filearea = "draft";
117 $itemid = 0;
118 $filepath = "/";
119 $filename = "Simple4.txt";
120 $filecontent = base64_encode("Let us create a nice simple file");
121
122 // Make sure exception is thrown.
123 $this->setExpectedException("coding_exception");
124 core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
125 }
126
127}