| 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_reportbuilder_generator;
|
|
|
22 |
use core_reportbuilder\manager;
|
|
|
23 |
use core_reportbuilder\local\report\column;
|
| 1441 |
ariadna |
24 |
use core_reportbuilder\tests\core_reportbuilder_testcase;
|
| 1 |
efrain |
25 |
use core_user\reportbuilder\datasource\users;
|
|
|
26 |
use stdClass;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Unit tests for sum aggregation
|
|
|
30 |
*
|
|
|
31 |
* @package core_reportbuilder
|
|
|
32 |
* @covers \core_reportbuilder\local\aggregation\base
|
|
|
33 |
* @covers \core_reportbuilder\local\aggregation\sum
|
|
|
34 |
* @copyright 2021 Paul Holden <paulh@moodle.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
| 1441 |
ariadna |
37 |
final class sum_test extends core_reportbuilder_testcase {
|
| 1 |
efrain |
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Test aggregation when applied to column
|
|
|
41 |
*/
|
|
|
42 |
public function test_column_aggregation(): void {
|
|
|
43 |
$this->resetAfterTest();
|
|
|
44 |
|
|
|
45 |
// Test subjects.
|
|
|
46 |
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
|
|
|
47 |
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
|
|
|
48 |
|
|
|
49 |
/** @var core_reportbuilder_generator $generator */
|
|
|
50 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
51 |
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
|
|
|
52 |
|
|
|
53 |
// Report columns, aggregated/sorted by user suspended.
|
|
|
54 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
|
|
|
55 |
$generator->create_column([
|
|
|
56 |
'reportid' => $report->get('id'),
|
|
|
57 |
'uniqueidentifier' => 'user:suspended',
|
|
|
58 |
'aggregation' => sum::get_class_name(),
|
|
|
59 |
'sortenabled' => 1,
|
|
|
60 |
'sortdirection' => SORT_DESC,
|
|
|
61 |
]);
|
|
|
62 |
|
|
|
63 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
64 |
$this->assertEquals([
|
|
|
65 |
['Bob', 2],
|
|
|
66 |
['Admin', 0],
|
|
|
67 |
], array_map('array_values', $content));
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
| 1441 |
ariadna |
71 |
* Data provider for {@see test_column_aggregation_with_callback}
|
|
|
72 |
*
|
|
|
73 |
* @return array[]
|
|
|
74 |
*/
|
|
|
75 |
public static function column_aggregation_with_callback_provider(): array {
|
|
|
76 |
return [
|
|
|
77 |
[column::TYPE_INTEGER, [
|
|
|
78 |
'0 (sum)',
|
|
|
79 |
'2 (sum)',
|
|
|
80 |
]],
|
|
|
81 |
[column::TYPE_FLOAT, [
|
|
|
82 |
'0.0 (sum)',
|
|
|
83 |
'2.0 (sum)',
|
|
|
84 |
]],
|
|
|
85 |
[column::TYPE_BOOLEAN, [
|
|
|
86 |
'0',
|
|
|
87 |
'2',
|
|
|
88 |
]],
|
|
|
89 |
];
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
| 1 |
efrain |
93 |
* Test aggregation when applied to column with callback
|
| 1441 |
ariadna |
94 |
*
|
|
|
95 |
* @param int $columntype
|
|
|
96 |
* @param string[] $expected
|
|
|
97 |
*
|
|
|
98 |
* @dataProvider column_aggregation_with_callback_provider
|
| 1 |
efrain |
99 |
*/
|
| 1441 |
ariadna |
100 |
public function test_column_aggregation_with_callback(int $columntype, array $expected): void {
|
| 1 |
efrain |
101 |
$this->resetAfterTest();
|
|
|
102 |
|
|
|
103 |
// Test subjects.
|
|
|
104 |
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
|
|
|
105 |
$this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
|
|
|
106 |
|
|
|
107 |
/** @var core_reportbuilder_generator $generator */
|
|
|
108 |
$generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
|
|
|
109 |
$report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
|
|
|
110 |
|
|
|
111 |
// First column, sorted.
|
|
|
112 |
$generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
|
|
|
113 |
|
|
|
114 |
// This is the column we'll aggregate.
|
|
|
115 |
$generator->create_column(
|
|
|
116 |
['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => sum::get_class_name()]
|
|
|
117 |
);
|
|
|
118 |
|
|
|
119 |
// Set callback to format the column (hack column definition to ensure callbacks are executed).
|
|
|
120 |
$instance = manager::get_report_from_persistent($report);
|
|
|
121 |
$instance->get_column('user:suspended')
|
| 1441 |
ariadna |
122 |
->set_type($columntype)
|
|
|
123 |
->set_callback(static function(int|float $value, stdClass $row, $arguments, ?string $aggregation): string {
|
| 1 |
efrain |
124 |
// Simple callback to return the given value, and append aggregation type.
|
| 1441 |
ariadna |
125 |
return var_export($value, true) . " ({$aggregation})";
|
| 1 |
efrain |
126 |
});
|
|
|
127 |
|
|
|
128 |
$content = $this->get_custom_report_content($report->get('id'));
|
|
|
129 |
$this->assertEquals([
|
| 1441 |
ariadna |
130 |
['Admin', $expected[0]],
|
|
|
131 |
['Bob', $expected[1]],
|
|
|
132 |
], array_map('array_values', $content));
|
| 1 |
efrain |
133 |
}
|
|
|
134 |
}
|