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_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 group concatenation distinct aggregation
30
 *
31
 * @package     core_reportbuilder
32
 * @covers      \core_reportbuilder\local\aggregation\base
33
 * @covers      \core_reportbuilder\local\aggregation\groupconcatdistinct
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 groupconcatdistinct_test extends core_reportbuilder_testcase {
1 efrain 38
 
39
    /**
40
     * Test setup, we need to skip these tests on non-supported databases
41
     */
42
    public function setUp(): void {
43
        global $DB;
1441 ariadna 44
        parent::setUp();
1 efrain 45
 
46
        if (!groupconcatdistinct::compatible(column::TYPE_TEXT)) {
47
            $this->markTestSkipped('Distinct group concatenation not supported in ' . $DB->get_dbfamily());
48
        }
49
    }
50
 
51
    /**
52
     * Test aggregation when applied to column
53
     */
54
    public function test_column_aggregation(): void {
55
        $this->resetAfterTest();
56
 
57
        // Test subjects.
58
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
59
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Apple']);
60
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
61
 
62
        /** @var core_reportbuilder_generator $generator */
63
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
64
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
65
 
66
        // Report columns, aggregated/sorted by user lastname.
67
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
68
        $generator->create_column([
69
            'reportid' => $report->get('id'),
70
            'uniqueidentifier' => 'user:lastname',
71
            'aggregation' => groupconcatdistinct::get_class_name(),
72
            'sortenabled' => 1,
73
            'sortdirection' => SORT_ASC,
74
        ]);
75
 
76
        // Assert lastname column was aggregated, and itself also sorted predictably.
77
        $content = $this->get_custom_report_content($report->get('id'));
78
        $this->assertEquals([
79
            ['Bob', 'Apple, Banana'],
80
            ['Admin', 'User'],
81
        ], array_map('array_values', $content));
82
    }
83
 
84
    /**
1441 ariadna 85
     * Test aggregation with custom separator option when applied to column
86
     */
87
    public function test_column_aggregation_separator_option(): void {
88
        $this->resetAfterTest();
89
 
90
        // Test subjects.
91
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
92
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Apple']);
93
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
94
 
95
        /** @var core_reportbuilder_generator $generator */
96
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
97
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
98
 
99
        // Report columns, aggregated/sorted by user lastname.
100
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
101
        $generator->create_column([
102
            'reportid' => $report->get('id'),
103
            'uniqueidentifier' => 'user:lastname',
104
            'aggregation' => groupconcatdistinct::get_class_name(),
105
            'sortenabled' => 1,
106
            'sortdirection' => SORT_ASC,
107
        ]);
108
 
109
        // Set aggregation option for separator.
110
        $instance = manager::get_report_from_persistent($report);
111
        $instance->get_column('user:lastname')
112
            ->set_aggregation_options(groupconcatdistinct::get_class_name(), ['separator' => '<br />']);
113
 
114
        // Assert lastname column was aggregated, with defined separator between each item.
115
        $content = $this->get_custom_report_content($report->get('id'));
116
        $this->assertEquals([
117
            ['Bob', 'Apple<br />Banana'],
118
            ['Admin', 'User'],
119
        ], array_map('array_values', $content));
120
    }
121
 
122
    /**
1 efrain 123
     * Test aggregation when applied to column with multiple fields
124
     */
125
    public function test_column_aggregation_multiple_fields(): void {
126
        $this->resetAfterTest();
127
 
128
        $user = $this->getDataGenerator()->create_user(['firstname' => 'Adam', 'lastname' => 'Apple']);
129
 
130
        /** @var core_reportbuilder_generator $generator */
131
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
132
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
133
 
134
        // This is the column we'll aggregate.
135
        $generator->create_column([
136
            'reportid' => $report->get('id'),
137
            'uniqueidentifier' => 'user:fullnamewithlink',
138
            'aggregation' => groupconcatdistinct::get_class_name(),
139
        ]);
140
 
141
        $content = $this->get_custom_report_content($report->get('id'));
142
        $this->assertCount(1, $content);
143
 
144
        // Ensure users are sorted predictably (Adam -> Admin).
145
        [$userone, $usertwo] = explode(', ', reset($content[0]));
146
        $this->assertStringContainsString(fullname($user, true), $userone);
147
        $this->assertStringContainsString(fullname(get_admin(), true), $usertwo);
148
    }
149
 
150
    /**
151
     * Test aggregation when applied to column with callback
152
     */
153
    public function test_column_aggregation_with_callback(): void {
154
        $this->resetAfterTest();
155
 
156
        // Test subjects.
157
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]);
158
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 0]);
159
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]);
160
 
161
        /** @var core_reportbuilder_generator $generator */
162
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
163
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
164
 
165
        // First column, sorted.
166
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
167
 
168
        // This is the column we'll aggregate.
169
        $generator->create_column([
170
            'reportid' => $report->get('id'),
171
            'uniqueidentifier' => 'user:confirmed',
172
            'aggregation' => groupconcatdistinct::get_class_name(),
173
        ]);
174
 
175
        // Add callback to format the column.
176
        $instance = manager::get_report_from_persistent($report);
177
        $instance->get_column('user:confirmed')
178
            ->add_callback(static function(string $value, stdClass $row, $arguments, ?string $aggregation): string {
179
                // Simple callback to return the given value, and append aggregation type.
180
                return "{$value} ({$aggregation})";
181
            });
182
 
183
        // Assert confirmed column was aggregated, and sorted predictably with callback applied.
184
        $content = $this->get_custom_report_content($report->get('id'));
185
        $this->assertEquals([
1441 ariadna 186
            ['Admin', 'Yes (groupconcatdistinct)'],
187
            ['Bob', 'No (groupconcatdistinct), Yes (groupconcatdistinct)'],
188
        ], array_map('array_values', $content));
1 efrain 189
    }
190
}