Proyectos de Subversion Moodle

Rev

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