Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
declare(strict_types=1);
18
 
19
namespace core_role\reportbuilder\datasource;
20
 
21
use core\context\course;
22
use core_reportbuilder_generator;
23
use core_reportbuilder_testcase;
24
use core_reportbuilder\local\filters\{date, select, text};
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
global $CFG;
29
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
30
 
31
/**
32
 * Unit tests for roles datasource
33
 *
34
 * @package     core_role
35
 * @covers      \core_role\reportbuilder\datasource\roles;
36
 * @copyright   2024 Paul Holden <paulh@moodle.com>
37
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
final class roles_test extends core_reportbuilder_testcase {
40
 
41
    /**
42
     * Test default datasource
43
     */
44
    public function test_datasource_default(): void {
45
        $this->resetAfterTest();
46
 
47
        $course = $this->getDataGenerator()->create_course();
48
        $context = course::instance($course->id);
49
 
50
        $studentone = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
51
        $studenttwo = $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Amy']);
52
        $manager = $this->getDataGenerator()->create_and_enrol($course, 'manager');
53
 
54
        /** @var core_reportbuilder_generator $generator */
55
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
56
        $report = $generator->create_report(['name' => 'Roles', 'source' => roles::class, 'default' => 1]);
57
 
58
        $content = $this->get_custom_report_content($report->get('id'));
59
        $this->assertCount(3, $content);
60
 
61
        // Default columns are context link, original role name and user link. Sorted by each.
62
        [$contextlink, $rolename, $userlink] = array_values($content[0]);
63
        $this->assertStringContainsString($context->get_context_name(), $contextlink);
64
        $this->assertEquals('Manager', $rolename);
65
        $this->assertStringContainsString(fullname($manager), $userlink);
66
 
67
        [$contextlink, $rolename, $userlink] = array_values($content[1]);
68
        $this->assertStringContainsString($context->get_context_name(), $contextlink);
69
        $this->assertEquals('Student', $rolename);
70
        $this->assertStringContainsString(fullname($studenttwo), $userlink);
71
 
72
        [$contextlink, $rolename, $userlink] = array_values($content[2]);
73
        $this->assertStringContainsString($context->get_context_name(), $contextlink);
74
        $this->assertEquals('Student', $rolename);
75
        $this->assertStringContainsString(fullname($studentone), $userlink);
76
    }
77
 
78
    /**
79
     * Test datasource columns that aren't added by default
80
     */
81
    public function test_datasource_non_default_columns(): void {
82
        global $DB;
83
 
84
        $this->resetAfterTest();
85
 
86
        $course = $this->getDataGenerator()->create_course();
87
        $context = course::instance($course->id);
88
 
89
        // Create an alias for our role.
90
        $roleid = $DB->get_field('role', 'id', ['shortname' => 'manager']);
91
        $DB->insert_record('role_names', (object) [
92
            'contextid' => $context->id,
93
            'roleid' => $roleid,
94
            'name' => 'Moocher',
95
        ]);
96
 
97
        $manager = $this->getDataGenerator()->create_user();
98
        $this->getDataGenerator()->enrol_user($manager->id, $course->id, $roleid);
99
 
100
        /** @var core_reportbuilder_generator $generator */
101
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
102
        $report = $generator->create_report(['name' => 'Roles', 'source' => roles::class, 'default' => 0]);
103
 
104
        // Role.
105
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:name']);
106
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:shortname']);
107
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:description']);
108
 
109
        // Role assignment.
110
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role_assignment:timemodified']);
111
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role_assignment:component']);
112
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role_assignment:itemid']);
113
 
114
        $content = $this->get_custom_report_content($report->get('id'));
115
        $this->assertCount(1, $content);
116
 
117
        [$rolename, $roleshortname, $roledescription, $timemodified, $component, $itemid] = array_values($content[0]);
118
 
119
        // Role.
120
        $this->assertEquals('Moocher (Manager)', $rolename);
121
        $this->assertEquals('manager', $roleshortname);
122
        $this->assertEquals('Managers can access courses and modify them, but usually do not participate in them.',
123
            $roledescription);
124
 
125
        // Role assignment.
126
        $this->assertNotEmpty($timemodified);
127
        $this->assertEquals('', $component);
128
        $this->assertEquals(0, $itemid);
129
    }
130
 
131
    /**
132
     * Data provider for {@see test_datasource_filters}
133
     *
134
     * @return array[]
135
     */
136
    public static function datasource_filters_provider(): array {
137
        global $DB;
138
 
139
        return [
140
            // Role.
141
            'Filter role name' => ['role:name', [
142
                'role:name_operator' => select::EQUAL_TO,
143
                'role:name_value' => $DB->get_field('role', 'id', ['shortname' => 'student']),
144
            ], true],
145
            'Filter role name (no match)' => ['role:name', [
146
                'role:name_operator' => select::EQUAL_TO,
147
                'role:name_value' => -1,
148
            ], false],
149
 
150
            // Role assignment.
151
            'Filter role assignment time modified' => ['role_assignment:timemodified', [
152
                'role_assignment:timemodified_operator' => date::DATE_RANGE,
153
                'role_assignment:timemodified_from' => 1622502000,
154
            ], true],
155
            'Filter role assignment time modified (no match)' => ['role_assignment:timemodified', [
156
                'role_assignment:timemodified_operator' => date::DATE_RANGE,
157
                'role_assignment:timemodified_to' => 1622502000,
158
            ], false],
159
 
160
            // Context.
161
            'Filter context level' => ['context:level', [
162
                'context:level_operator' => select::EQUAL_TO,
163
                'context:level_value' => CONTEXT_COURSE,
164
            ], true],
165
            'Filter context level (no match)' => ['context:level', [
166
                'context:level_operator' => select::EQUAL_TO,
167
                'context:level_value' => CONTEXT_COURSECAT,
168
            ], false],
169
 
170
            // User.
171
            'Filter user firstname' => ['user:firstname', [
172
                'user:firstname_operator' => text::IS_EQUAL_TO,
173
                'user:firstname_value' => 'Zoe',
174
            ], true],
175
            'Filter user firstname (no match)' => ['user:firstname', [
176
                'user:firstname_operator' => text::IS_EQUAL_TO,
177
                'user:firstname_value' => 'Amy',
178
            ], false],
179
        ];
180
    }
181
 
182
    /**
183
     * Test datasource filters
184
     *
185
     * @param string $filtername
186
     * @param array $filtervalues
187
     * @param bool $expectmatch
188
     *
189
     * @dataProvider datasource_filters_provider
190
     */
191
    public function test_datasource_filters(
192
        string $filtername,
193
        array $filtervalues,
194
        bool $expectmatch,
195
    ): void {
196
        $this->resetAfterTest();
197
 
198
        $course = $this->getDataGenerator()->create_course();
199
        $this->getDataGenerator()->create_and_enrol($course, 'student', ['firstname' => 'Zoe']);
200
 
201
        /** @var core_reportbuilder_generator $generator */
202
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
203
 
204
        // Create report containing single column, and given filter.
205
        $report = $generator->create_report(['name' => 'Roles', 'source' => roles::class, 'default' => 0]);
206
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'role:shortname']);
207
 
208
        // Add filter, set it's values.
209
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
210
        $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
211
 
212
        if ($expectmatch) {
213
            $this->assertCount(1, $content);
214
            $this->assertEquals('student', reset($content[0]));
215
        } else {
216
            $this->assertEmpty($content);
217
        }
218
    }
219
 
220
    /**
221
     * Stress test datasource
222
     *
223
     * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
224
     */
225
    public function test_stress_datasource(): void {
226
        if (!PHPUNIT_LONGTEST) {
227
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
228
        }
229
 
230
        $this->resetAfterTest();
231
 
232
        $course = $this->getDataGenerator()->create_course();
233
        $this->getDataGenerator()->create_and_enrol($course);
234
 
235
        $this->datasource_stress_test_columns(roles::class);
236
        $this->datasource_stress_test_columns_aggregation(roles::class);
237
        $this->datasource_stress_test_conditions(roles::class, 'role:shortname');
238
    }
239
}