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_reportbuilder\local\aggregation;
|
|
|
20 |
|
|
|
21 |
use core_badges_generator;
|
|
|
22 |
use core_badges\reportbuilder\datasource\badges;
|
|
|
23 |
use core_reportbuilder_testcase;
|
|
|
24 |
use core_reportbuilder_generator;
|
|
|
25 |
use core_reportbuilder\manager;
|
|
|
26 |
use core_user\reportbuilder\datasource\users;
|
|
|
27 |
use stdClass;
|
|
|
28 |
|
|
|
29 |
defined('MOODLE_INTERNAL') || die();
|
|
|
30 |
|
|
|
31 |
global $CFG;
|
|
|
32 |
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Unit tests for group concatenation aggregation
|
|
|
36 |
*
|
|
|
37 |
* @package core_reportbuilder
|
|
|
38 |
* @covers \core_reportbuilder\local\aggregation\base
|
|
|
39 |
* @covers \core_reportbuilder\local\aggregation\groupconcat
|
|
|
40 |
* @copyright 2021 Paul Holden <paulh@moodle.com>
|
|
|
41 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
42 |
*/
|
|
|
43 |
class groupconcat_test extends core_reportbuilder_testcase {
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Test aggregation when applied to column
|
|
|
47 |
*/
|
|
|
48 |
public function test_column_aggregation(): void {
|
|
|
49 |
$this->resetAfterTest();
|
|
|
50 |
|
|
|
51 |
// Test subjects.
|
|
|
52 |
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
|
|
|
53 |
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Apple']);
|
|
|
54 |
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
|
|
|
55 |
|
|
|
56 |
/** @var core_reportbuilder_generator $generator */
|
|
|
57 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
58 |
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
|
|
|
59 |
|
|
|
60 |
// Report columns, aggregated/sorted by user lastname.
|
|
|
61 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
|
|
|
62 |
$generator->create_column([
|
|
|
63 |
'reportid' => $report->get('id'),
|
|
|
64 |
'uniqueidentifier' => 'user:lastname',
|
|
|
65 |
'aggregation' => groupconcat::get_class_name(),
|
|
|
66 |
'sortenabled' => 1,
|
|
|
67 |
'sortdirection' => SORT_ASC,
|
|
|
68 |
]);
|
|
|
69 |
|
|
|
70 |
// Assert lastname column was aggregated, and itself also sorted predictably.
|
|
|
71 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
72 |
$this->assertEquals([
|
|
|
73 |
['Bob', 'Apple, Banana, Banana'],
|
|
|
74 |
['Admin', 'User'],
|
|
|
75 |
], array_map('array_values', $content));
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Test aggregation when applied to column with multiple fields
|
|
|
80 |
*/
|
|
|
81 |
public function test_column_aggregation_multiple_fields(): void {
|
|
|
82 |
$this->resetAfterTest();
|
|
|
83 |
|
|
|
84 |
$user = $this->getDataGenerator()->create_user(['firstname' => 'Adam', 'lastname' => 'Apple']);
|
|
|
85 |
|
|
|
86 |
/** @var core_reportbuilder_generator $generator */
|
|
|
87 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
88 |
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
|
|
|
89 |
|
|
|
90 |
// This is the column we'll aggregate.
|
|
|
91 |
$generator->create_column([
|
|
|
92 |
'reportid' => $report->get('id'),
|
|
|
93 |
'uniqueidentifier' => 'user:fullnamewithlink',
|
|
|
94 |
'aggregation' => groupconcat::get_class_name(),
|
|
|
95 |
]);
|
|
|
96 |
|
|
|
97 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
98 |
$this->assertCount(1, $content);
|
|
|
99 |
|
|
|
100 |
// Ensure users are sorted predictably (Adam -> Admin).
|
|
|
101 |
[$userone, $usertwo] = explode(', ', reset($content[0]));
|
|
|
102 |
$this->assertStringContainsString(fullname($user, true), $userone);
|
|
|
103 |
$this->assertStringContainsString(fullname(get_admin(), true), $usertwo);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Test aggregation when applied to column with callback
|
|
|
108 |
*/
|
|
|
109 |
public function test_column_aggregation_with_callback(): void {
|
|
|
110 |
$this->resetAfterTest();
|
|
|
111 |
|
|
|
112 |
// Test subjects.
|
|
|
113 |
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]);
|
|
|
114 |
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 0]);
|
|
|
115 |
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]);
|
|
|
116 |
|
|
|
117 |
/** @var core_reportbuilder_generator $generator */
|
|
|
118 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
119 |
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
|
|
|
120 |
|
|
|
121 |
// First column, sorted.
|
|
|
122 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
|
|
|
123 |
|
|
|
124 |
// This is the column we'll aggregate.
|
|
|
125 |
$generator->create_column([
|
|
|
126 |
'reportid' => $report->get('id'),
|
|
|
127 |
'uniqueidentifier' => 'user:confirmed',
|
|
|
128 |
'aggregation' => groupconcat::get_class_name(),
|
|
|
129 |
]);
|
|
|
130 |
|
|
|
131 |
// Add callback to format the column.
|
|
|
132 |
$instance = manager::get_report_from_persistent($report);
|
|
|
133 |
$instance->get_column('user:confirmed')
|
|
|
134 |
->add_callback(static function(string $value, stdClass $row, $arguments, ?string $aggregation): string {
|
|
|
135 |
// Simple callback to return the given value, and append aggregation type.
|
|
|
136 |
return "{$value} ({$aggregation})";
|
|
|
137 |
});
|
|
|
138 |
|
|
|
139 |
// Assert confirmed column was aggregated, and sorted predictably with callback applied.
|
|
|
140 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
141 |
$this->assertEquals([
|
|
|
142 |
[
|
|
|
143 |
'c0_firstname' => 'Admin',
|
|
|
144 |
'c1_confirmed' => 'Yes (groupconcat)',
|
|
|
145 |
],
|
|
|
146 |
[
|
|
|
147 |
'c0_firstname' => 'Bob',
|
|
|
148 |
'c1_confirmed' => 'No (groupconcat), Yes (groupconcat), Yes (groupconcat)',
|
|
|
149 |
],
|
|
|
150 |
], $content);
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/**
|
|
|
154 |
* Test aggregation when applied to column with callback that expects/handles null values
|
|
|
155 |
*/
|
|
|
156 |
public function test_datasource_aggregate_column_callback_with_null(): void {
|
|
|
157 |
$this->resetAfterTest();
|
|
|
158 |
$this->setAdminUser();
|
|
|
159 |
|
|
|
160 |
$userone = $this->getDataGenerator()->create_user(['description' => 'First user']);
|
|
|
161 |
$usertwo = $this->getDataGenerator()->create_user(['description' => 'Second user']);
|
|
|
162 |
|
|
|
163 |
/** @var core_badges_generator $generator */
|
|
|
164 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_badges');
|
|
|
165 |
|
|
|
166 |
// Create course badge, issue to both users.
|
|
|
167 |
$badgeone = $generator->create_badge(['name' => 'First badge']);
|
|
|
168 |
$badgeone->issue($userone->id, true);
|
|
|
169 |
$badgeone->issue($usertwo->id, true);
|
|
|
170 |
|
|
|
171 |
// Create second badge, without issuing to anyone.
|
|
|
172 |
$badgetwo = $generator->create_badge(['name' => 'Second badge']);
|
|
|
173 |
|
|
|
174 |
/** @var core_reportbuilder_generator $generator */
|
|
|
175 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
176 |
$report = $generator->create_report(['name' => 'Badges', 'source' => badges::class, 'default' => 0]);
|
|
|
177 |
|
|
|
178 |
// First column, sorted.
|
|
|
179 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'badge:name', 'sortenabled' => 1]);
|
|
|
180 |
|
|
|
181 |
// This is the column we'll aggregate.
|
|
|
182 |
$generator->create_column([
|
|
|
183 |
'reportid' => $report->get('id'),
|
|
|
184 |
'uniqueidentifier' => 'user:description',
|
|
|
185 |
'aggregation' => groupconcat::get_class_name(),
|
|
|
186 |
]);
|
|
|
187 |
|
|
|
188 |
// Assert description column was aggregated, with callbacks accounting for null values.
|
|
|
189 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
190 |
$this->assertEquals([
|
|
|
191 |
[
|
|
|
192 |
'c0_name' => $badgeone->name,
|
|
|
193 |
'c1_description' => "{$userone->description}, {$usertwo->description}",
|
|
|
194 |
],
|
|
|
195 |
[
|
|
|
196 |
'c0_name' => $badgetwo->name,
|
|
|
197 |
'c1_description' => '',
|
|
|
198 |
],
|
|
|
199 |
], $content);
|
|
|
200 |
}
|
|
|
201 |
}
|