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 prepare_private_files_for_edition 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\prepare_private_files_for_edition
29
 */
30
final class prepare_private_files_for_edition_test extends \advanced_testcase {
31
 
32
    public function test_execute(): void {
33
        global $USER;
34
        $this->resetAfterTest();
35
        $this->setAdminUser();
36
 
37
        // Create some files in the user private file area.
38
        $filename = 'faketxt.txt';
39
        $filerecordinline = [
40
            'contextid' => \context_user::instance($USER->id)->id,
41
            'component' => 'user',
42
            'filearea'  => 'private',
43
            'itemid'    => 0,
44
            'filepath'  => '/',
45
            'filename'  => $filename,
46
        ];
47
        $fs = get_file_storage();
48
        $fs->create_file_from_string($filerecordinline, 'fake txt contents.');
49
 
50
        $result = prepare_private_files_for_edition::execute();
51
 
52
        $result = external_api::clean_returnvalue(prepare_private_files_for_edition::execute_returns(), $result);
53
        $draftitemid = $result['draftitemid'];
54
        $this->assertGreaterThan(0, $draftitemid);
55
        $this->assertCount(5, $result['areaoptions']);
56
        $this->assertEmpty($result['warnings']);
57
 
58
        // Check we get the expected user private files in the draft area.
59
        $files = file_get_drafarea_files($draftitemid);
60
        $this->assertCount(1, $files->list);
61
        $this->assertEquals($filename, $files->list[0]->filename);
62
    }
63
 
64
    /**
65
     * Test missing capabilities.
66
     */
67
    public function test_execute_missing_capabilities(): void {
68
        global $CFG, $DB;
69
        $this->resetAfterTest();
70
 
71
        $authrole = $DB->get_record('role', ['id' => $CFG->defaultuserroleid]);
72
        unassign_capability('moodle/user:manageownfiles', $authrole->id);
73
        accesslib_clear_all_caches_for_unit_testing();
74
 
75
        $user = $this->getDataGenerator()->create_user();
76
        $this->setUser($user);
77
 
78
        $this->expectException('moodle_exception');
79
        $result = prepare_private_files_for_edition::execute(0);
80
    }
81
}