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_cohort\reportbuilder\datasource;
20
 
21
use context_coursecat;
22
use context_system;
23
use core_customfield_generator;
24
use core_reportbuilder_generator;
25
use core_reportbuilder_testcase;
26
use core_reportbuilder\local\filters\{boolean_select, date, select, text};
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
global $CFG;
31
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
32
 
33
/**
34
 * Unit tests for cohorts datasource
35
 *
36
 * @package     core_cohort
37
 * @covers      \core_cohort\reportbuilder\datasource\cohorts
38
 * @copyright   2021 Paul Holden <paulh@moodle.com>
39
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
41
class cohorts_test extends core_reportbuilder_testcase {
42
 
43
    /**
44
     * Test default datasource
45
     */
46
    public function test_datasource_default(): void {
47
        $this->resetAfterTest();
48
 
49
        // Test subject.
50
        $contextsystem = context_system::instance();
51
        $cohortone = $this->getDataGenerator()->create_cohort([
52
            'contextid' => $contextsystem->id,
53
            'name' => 'Legends',
54
            'idnumber' => 'C101',
55
            'description' => 'Cohort for the legends',
56
        ]);
57
 
58
        $category = $this->getDataGenerator()->create_category();
59
        $contextcategory = context_coursecat::instance($category->id);
60
        $cohorttwo = $this->getDataGenerator()->create_cohort([
61
            'contextid' => $contextcategory->id,
62
            'name' => 'Category cohort',
63
            'description' => 'This is my category cohort',
64
        ]);
65
 
66
        // Non-visible cohort (excluded by default).
67
        $cohortnonvisible = $this->getDataGenerator()->create_cohort([
68
            'contextid' => $contextsystem->id,
69
            'name' => 'Non-visible',
70
            'visible' => false,
71
        ]);
72
 
73
        /** @var core_reportbuilder_generator $generator */
74
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
75
        $report = $generator->create_report(['name' => 'Cohorts', 'source' => cohorts::class, 'default' => 1]);
76
 
77
        $content = $this->get_custom_report_content($report->get('id'));
78
 
79
        // Default columns are name, context, idnumber, description. Sorted by name.
80
        $this->assertEquals([
81
            [$cohorttwo->name, $contextcategory->get_context_name(false), $cohorttwo->idnumber,
82
                format_text($cohorttwo->description)],
83
            [$cohortone->name, $contextsystem->get_context_name(false), $cohortone->idnumber,
84
                format_text($cohortone->description)],
85
        ], array_map('array_values', $content));
86
    }
87
 
88
    /**
89
     * Test datasource columns that aren't added by default
90
     */
91
    public function test_datasource_non_default_columns(): void {
92
        $this->resetAfterTest();
93
        $this->setAdminUser();
94
 
95
        set_config('allowcohortthemes', true);
96
 
97
        /** @var core_customfield_generator $generator */
98
        $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
99
 
100
        $fieldcategory = $generator->create_category(['component' => 'core_cohort', 'area' => 'cohort']);
101
        $field = $generator->create_field(['categoryid' => $fieldcategory->get('id'), 'shortname' => 'hi']);
102
 
103
        // Test subject.
104
        $cohort = $this->getDataGenerator()->create_cohort([
105
            'name' => 'Legends',
106
            'idnumber' => 'C101',
107
            'description' => 'Cohort for the legends',
108
            'theme' => 'boost',
109
            'customfield_hi' => 'Hello',
110
        ]);
111
 
112
        $user = $this->getDataGenerator()->create_user(['firstname' => 'Lionel', 'lastname' => 'Richards']);
113
        cohort_add_member($cohort->id, $user->id);
114
 
115
        /** @var core_reportbuilder_generator $generator */
116
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
117
        $report = $generator->create_report(['name' => 'Cohorts', 'source' => cohorts::class, 'default' => 0]);
118
 
119
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:visible']);
120
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:timecreated']);
121
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:timemodified']);
122
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:component']);
123
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:theme']);
124
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:customfield_hi']);
125
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort_member:timeadded']);
126
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:fullname']);
127
 
128
        $content = $this->get_custom_report_content($report->get('id'));
129
        $this->assertCount(1, $content);
130
 
131
        [$visible, $timecreated, $timemodified, $component, $theme, $custom, $timeadded, $fullname] = array_values($content[0]);
132
 
133
        $this->assertEquals('Yes', $visible);
134
        $this->assertNotEmpty($timecreated);
135
        $this->assertNotEmpty($timemodified);
136
        $this->assertEquals('Created manually', $component);
137
        $this->assertEquals('Boost', $theme);
138
        $this->assertEquals('Hello', $custom);
139
        $this->assertNotEmpty($timeadded);
140
        $this->assertEquals(fullname($user), $fullname);
141
    }
142
 
143
    /**
144
     * Data provider for {@see test_datasource_filters}
145
     *
146
     * @return array[]
147
     */
148
    public function datasource_filters_provider(): array {
149
        return [
150
            // Cohort.
151
            'Filter cohort' => ['cohort:cohortselect', [
152
                'cohort:cohortselect_values' => [-1],
153
            ], false],
154
            'Filter context' => ['cohort:context', [
155
                'cohort:context_operator' => select::EQUAL_TO,
156
                'cohort:context_value' => context_system::instance()->id,
157
            ], true],
158
            'Filter content (no match)' => ['cohort:context', [
159
                'cohort:context_operator' => select::EQUAL_TO,
160
                'cohort:context_value' => -1,
161
            ], false],
162
            'Filter name' => ['cohort:name', [
163
                'cohort:name_operator' => text::IS_EQUAL_TO,
164
                'cohort:name_value' => 'Legends',
165
            ], true],
166
            'Filter name (no match)' => ['cohort:name', [
167
                'cohort:name_operator' => text::IS_EQUAL_TO,
168
                'cohort:name_value' => 'Dancing',
169
            ], false],
170
            'Filter idnumber' => ['cohort:idnumber', [
171
                'cohort:idnumber_operator' => text::IS_EQUAL_TO,
172
                'cohort:idnumber_value' => 'C101',
173
            ], true],
174
            'Filter idnumber (no match)' => ['cohort:idnumber', [
175
                'cohort:idnumber_operator' => text::IS_EQUAL_TO,
176
                'cohort:idnumber_value' => 'C102',
177
            ], false],
178
            'Filter time created' => ['cohort:timecreated', [
179
                'cohort:timecreated_operator' => date::DATE_RANGE,
180
                'cohort:timecreated_from' => 1622502000,
181
            ], true],
182
            'Filter time created (no match)' => ['cohort:timecreated', [
183
                'cohort:timecreated_operator' => date::DATE_RANGE,
184
                'cohort:timecreated_to' => 1622502000,
185
            ], false],
186
            'Filter description' => ['cohort:description', [
187
                'cohort:description_operator' => text::CONTAINS,
188
                'cohort:description_value' => 'legends',
189
            ], true],
190
            'Filter description (no match)' => ['cohort:description', [
191
                'cohort:description_operator' => text::IS_EMPTY,
192
            ], false],
193
            'Filter theme' => ['cohort:theme', [
194
                'cohort:theme_operator' => select::EQUAL_TO,
195
                'cohort:theme_value' => 'boost',
196
            ], true],
197
            'Filter theme (no match)' => ['cohort:theme', [
198
                'cohort:theme_operator' => select::EQUAL_TO,
199
                'cohort:theme_value' => 'classic',
200
            ], false],
201
            'Filter visible' => ['cohort:visible', [
202
                'cohort:visible_operator' => boolean_select::CHECKED,
203
            ], true],
204
            'Filter visible (no match)' => ['cohort:visible', [
205
                'cohort:visible_operator' => boolean_select::NOT_CHECKED,
206
            ], false],
207
 
208
            // Cohort member.
209
            'Filter time added' => ['cohort_member:timeadded', [
210
                'cohort_member:timeadded_operator' => date::DATE_RANGE,
211
                'cohort_member:timeadded_from' => 1622502000,
212
            ], true],
213
            'Filter time added (no match)' => ['cohort_member:timeadded', [
214
                'cohort_member:timeadded_operator' => date::DATE_RANGE,
215
                'cohort_member:timeadded_to' => 1622502000,
216
            ], false],
217
 
218
            // User.
219
            'Filter user' => ['user:username', [
220
                'user:username_operator' => text::IS_EQUAL_TO,
221
                'user:username_value' => 'lionel',
222
            ], true],
223
            'Filter user (no match)' => ['user:username', [
224
                'user:username_operator' => text::IS_EQUAL_TO,
225
                'user:username_value' => 'rick',
226
            ], false],
227
        ];
228
    }
229
 
230
    /**
231
     * Test datasource filters
232
     *
233
     * @param string $filtername
234
     * @param array $filtervalues
235
     * @param bool $expectmatch
236
     *
237
     * @dataProvider datasource_filters_provider
238
     */
239
    public function test_datasource_filters(string $filtername, array $filtervalues, bool $expectmatch): void {
240
        $this->resetAfterTest();
241
 
242
        set_config('allowcohortthemes', true);
243
 
244
        // Test subject.
245
        $cohort = $this->getDataGenerator()->create_cohort([
246
            'name' => 'Legends',
247
            'idnumber' => 'C101',
248
            'description' => 'Cohort for the legends',
249
            'theme' => 'boost',
250
        ]);
251
 
252
        $user = $this->getDataGenerator()->create_user(['username' => 'lionel']);
253
        cohort_add_member($cohort->id, $user->id);
254
 
255
        /** @var core_reportbuilder_generator $generator */
256
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
257
 
258
        // Create report containing single column, and given filter.
259
        $report = $generator->create_report(['name' => 'Cohorts', 'source' => cohorts::class, 'default' => 0]);
260
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'cohort:name']);
261
 
262
        // Add filter, set it's values.
263
        $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => $filtername]);
264
        $content = $this->get_custom_report_content($report->get('id'), 0, $filtervalues);
265
 
266
        if ($expectmatch) {
267
            $this->assertCount(1, $content);
268
            $this->assertEquals('Legends', reset($content[0]));
269
        } else {
270
            $this->assertEmpty($content);
271
        }
272
    }
273
 
274
    /**
275
     * Stress test datasource
276
     *
277
     * In order to execute this test PHPUNIT_LONGTEST should be defined as true in phpunit.xml or directly in config.php
278
     */
279
    public function test_stress_datasource(): void {
280
        if (!PHPUNIT_LONGTEST) {
281
            $this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
282
        }
283
 
284
        $this->resetAfterTest();
285
 
286
        $cohort = $this->getDataGenerator()->create_cohort();
287
        $user = $this->getDataGenerator()->create_user();
288
        cohort_add_member($cohort->id, $user->id);
289
 
290
        $this->datasource_stress_test_columns(cohorts::class);
291
        $this->datasource_stress_test_columns_aggregation(cohorts::class);
292
        $this->datasource_stress_test_conditions(cohorts::class, 'cohort:name');
293
    }
294
}