Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_user\external;
18
 
19
use core_external\external_api;
20
 
21
/**
22
 * Tests for the \core_user\external\update_private_files class.
23
 *
24
 * @package   core_user
25
 * @category  external
26
 * @copyright 2024 Juan Leyva
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers \core_user\external\update_private_files
29
 */
30
final class update_private_files_test extends \advanced_testcase {
31
 
32
    /**
33
     * Test base cases.
34
     */
35
    public function test_execute(): void {
36
        global $CFG;
37
        require_once($CFG->dirroot . '/files/externallib.php');
38
 
39
        $this->resetAfterTest();
40
        $user = $this->getDataGenerator()->create_user();
41
        $anotheruser = $this->getDataGenerator()->create_user();
42
        $this->setUser($user);
43
        $context = \context_user::instance($user->id);
44
 
45
        // Create one file in the user private file area.
46
        $filename = 'faketxt.txt';
47
        $filerecordinline = [
48
            'contextid' => $context->id,
49
            'component' => 'user',
50
            'filearea'  => 'private',
51
            'itemid'    => 0,
52
            'filepath'  => '/',
53
            'filename'  => $filename,
54
        ];
55
        $fs = get_file_storage();
56
        $fs->create_file_from_string($filerecordinline, 'fake txt contents.');
57
 
58
        // Retrieve draft area with existing files.
59
        $result = prepare_private_files_for_edition::execute();
60
        $result = external_api::clean_returnvalue(prepare_private_files_for_edition::execute_returns(), $result);
61
        $draftitemid = $result['draftitemid'];
62
 
63
        // Add one file to the draft area reusing previous structure.
64
        $newfilename = 'newfaketxt.txt';
65
        $filerecordinline['itemid'] = $draftitemid;
66
        $filerecordinline['filearea'] = 'draft';
67
        $filerecordinline['filename'] = $newfilename;
68
        $fs->create_file_from_string($filerecordinline, 'new fake txt contents.');
69
        $files = file_get_drafarea_files($draftitemid);
70
        $this->assertCount(2, $files->list);    // 2 files in the draft area.
71
 
72
        // Update the private files with the new files.
73
        $result = update_private_files::execute($draftitemid);
74
        $result = external_api::clean_returnvalue(update_private_files::execute_returns(), $result);
75
        $this->assertTrue($result['status']);
76
        $this->assertEmpty($result['warnings']);
77
        // Check that the new files are in the private file area.
78
        $files = \core_files_external::get_files($context->id, 'user', 'private', 0, '/', '');
79
        $this->assertCount(2, $files['files']);    // 2 files in the private area.
80
 
81
        // Now, try deleting one.
82
        $result = prepare_private_files_for_edition::execute();
83
        $result = external_api::clean_returnvalue(prepare_private_files_for_edition::execute_returns(), $result);
84
        $draftitemid = $result['draftitemid'];
85
 
86
        \core_files\external\delete\draft::execute($draftitemid, [
87
            [
88
                'filepath' => '/',
89
                'filename' => $newfilename,
90
            ],
91
        ]);
92
        // Update to force deletion.
93
        $result = update_private_files::execute($draftitemid);
94
        $result = external_api::clean_returnvalue(update_private_files::execute_returns(), $result);
95
        $this->assertTrue($result['status']);
96
        $this->assertEmpty($result['warnings']);
97
        // Check we only have one now.
98
        $files = \core_files_external::get_files($context->id, 'user', 'private', 0, '/', '');
99
        $this->assertCount(1, $files['files']);
100
        $this->assertEquals($filename, $files['files'][0]['filename']);
101
 
102
        // Use other's user draft item area.
103
        $result = prepare_private_files_for_edition::execute();
104
        $result = external_api::clean_returnvalue(prepare_private_files_for_edition::execute_returns(), $result);
105
        $draftitemid = $result['draftitemid'];
106
 
107
        $this->setUser($anotheruser);
108
        $this->expectException('moodle_exception');
109
        update_private_files::execute($draftitemid);
110
    }
111
 
112
    /**
113
     * Test invalid draftitemid.
114
     */
115
    public function test_execute_invalid_draftitemid(): void {
116
        $this->resetAfterTest();
117
        $this->setAdminUser();
118
 
119
        $this->expectException('moodle_exception');
120
        update_private_files::execute(0);
121
    }
122
 
123
    /**
124
     * Test quota reached.
125
     */
126
    public function test_execute_quota_reached(): void {
127
        global $CFG;
128
        $this->resetAfterTest();
129
 
130
        $user = $this->getDataGenerator()->create_user();
131
        $this->setUser($user);
132
        $context = \context_user::instance($user->id);
133
 
134
        // Force the quota so we are sure it won't be space to add the new file.
135
        $fileareainfo = file_get_file_area_info($context->id, 'user', 'private');
136
        $CFG->userquota = 1;
137
 
138
        $result = prepare_private_files_for_edition::execute();
139
        $result = external_api::clean_returnvalue(prepare_private_files_for_edition::execute_returns(), $result);
140
        $draftitemid = $result['draftitemid'];
141
        $filerecordinline = [
142
            'contextid' => $context->id,
143
            'component' => 'user',
144
            'filearea'  => 'draft',
145
            'itemid'    => $draftitemid,
146
            'filepath'  => '/',
147
            'filename'  => 'faketxt.txt',
148
        ];
149
        $fs = get_file_storage();
150
        $fs->create_file_from_string($filerecordinline, 'new fake txt contents.');
151
 
152
        $result = update_private_files::execute($draftitemid);
153
        $result = external_api::clean_returnvalue(update_private_files::execute_returns(), $result);
154
        // We should get a warning as because of quota we can add files.
155
        $this->assertFalse($result['status']);
156
        $this->assertNotEmpty($result['warnings']);
157
    }
158
 
159
    /**
160
     * Test missing capabilities.
161
     */
162
    public function test_execute_missing_capabilities(): void {
163
        global $CFG, $DB;
164
        $this->resetAfterTest();
165
 
166
        $authrole = $DB->get_record('role', ['id' => $CFG->defaultuserroleid]);
167
        unassign_capability('moodle/user:manageownfiles', $authrole->id);
168
        accesslib_clear_all_caches_for_unit_testing();
169
 
170
        $user = $this->getDataGenerator()->create_user();
171
        $this->setUser($user);
172
 
173
        $this->expectException('moodle_exception');
174
        $result = update_private_files::execute(0);
175
    }
176
}