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_external\tests;
18
 
19
use core\context;
20
use core\exception\coding_exception;
21
use core_external\external_settings;
22
use filter_manager;
23
 
24
// phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
25
 
26
/**
27
 * Helper base class for external tests. Helpfull to test capabilities.
28
 *
29
 * @package    core_external
30
 * @copyright  2012 Jerome Mouneyrac
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
abstract class externallib_testcase extends \advanced_testcase {
34
    /**
35
     * Assign a capability to $USER
36
     * The function creates a student $USER if $USER->id is empty
37
     *
38
     * @param string $capability capability name
39
     * @param int|context $contextid
40
     * @param int $roleid
41
     * @return int the role id - mainly returned for creation, so calling function can reuse it
42
     */
43
    public static function assignUserCapability($capability, $contextid, $roleid = null) {
44
        global $USER;
45
 
46
        // Create a new student $USER if $USER doesn't exist.
47
        if (empty($USER->id)) {
48
            $user  = self::getDataGenerator()->create_user();
49
            self::setUser($user);
50
        }
51
 
52
        if (empty($roleid)) {
53
            $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
54
        }
55
 
56
        assign_capability($capability, CAP_ALLOW, $roleid, $contextid);
57
 
58
        role_assign($roleid, $USER->id, $contextid);
59
 
60
        accesslib_clear_all_caches_for_unit_testing();
61
 
62
        return $roleid;
63
    }
64
 
65
    /**
66
     * Configure some filters for external tests.
67
     *
68
     * @param array $filters Filters to enable. Each filter should contain:
69
     *                           - name: name of the filter.
70
     *                           - state: the state of the filter.
71
     *                           - move: -1 means up, 0 means the same, 1 means down.
72
     *                           - applytostrings: true to apply the filter to content and headings, false for just content.
73
     */
74
    public static function configure_filters($filters) {
75
        global $CFG;
76
 
77
        $filterstrings = false;
78
 
79
        // Enable the filters.
80
        foreach ($filters as $filter) {
81
            $filter = (array) $filter;
82
            filter_set_global_state($filter['name'], $filter['state'], $filter['move']);
83
            filter_set_applies_to_strings($filter['name'], $filter['applytostrings']);
84
 
85
            $filterstrings = $filterstrings || $filter['applytostrings'];
86
        }
87
 
88
        // Set WS filtering.
89
        $wssettings = external_settings::get_instance();
90
        $wssettings->set_filter(true);
91
 
92
        // Reset filter caches.
93
        $filtermanager = filter_manager::instance();
94
        $filtermanager->reset_caches();
95
 
96
        if ($filterstrings) {
97
            // Don't strip tags in strings.
98
            $CFG->formatstringstriptags = false;
99
        }
100
    }
101
 
102
    /**
103
     * Unassign a capability to $USER.
104
     *
105
     * @param string $capability capability name.
106
     * @param int $contextid set the context id if you used assignUserCapability.
107
     * @param int $roleid set the role id if you used assignUserCapability.
108
     * @param int $courseid set the course id if you used getDataGenerator->enrol_users.
109
     * @param string $enrol set the enrol plugin name if you used
110
     *                      getDataGenerator->enrol_users with a different plugin than 'manual'.
111
     */
112
    public static function unassignUserCapability(
113
        $capability,
114
        $contextid = null,
115
        $roleid = null,
116
        $courseid = null,
117
        $enrol = 'manual',
118
    ) {
119
        global $DB;
120
 
121
        if (!empty($courseid)) {
122
            // Retrieve the role id.
123
            $instances = $DB->get_records('enrol', ['courseid' => $courseid, 'enrol' => $enrol]);
124
            if (count($instances) != 1) {
125
                 throw new coding_exception('No found enrol instance for courseid: ' . $courseid . ' and enrol: ' . $enrol);
126
            }
127
            $instance = reset($instances);
128
 
129
            if (is_null($roleid) && $instance->roleid) {
130
                $roleid = $instance->roleid;
131
            }
132
        } else {
133
            if (empty($contextid) || empty($roleid)) {
134
                throw new coding_exception('unassignUserCapaibility requires contextid/roleid or courseid');
135
            }
136
        }
137
 
138
        unassign_capability($capability, $roleid, $contextid);
139
 
140
        accesslib_clear_all_caches_for_unit_testing();
141
    }
142
}