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_reportbuilder\local\aggregation;
20
 
21
use core_reportbuilder_testcase;
22
use core_reportbuilder_generator;
23
use core_reportbuilder\manager;
24
use core_reportbuilder\local\report\column;
25
use core_user\reportbuilder\datasource\users;
26
use stdClass;
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 group concatenation distinct aggregation
35
 *
36
 * @package     core_reportbuilder
37
 * @covers      \core_reportbuilder\local\aggregation\base
38
 * @covers      \core_reportbuilder\local\aggregation\groupconcatdistinct
39
 * @copyright   2021 Paul Holden <paulh@moodle.com>
40
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class groupconcatdistinct_test extends core_reportbuilder_testcase {
43
 
44
    /**
45
     * Test setup, we need to skip these tests on non-supported databases
46
     */
47
    public function setUp(): void {
48
        global $DB;
49
 
50
        if (!groupconcatdistinct::compatible(column::TYPE_TEXT)) {
51
            $this->markTestSkipped('Distinct group concatenation not supported in ' . $DB->get_dbfamily());
52
        }
53
    }
54
 
55
    /**
56
     * Test aggregation when applied to column
57
     */
58
    public function test_column_aggregation(): void {
59
        $this->resetAfterTest();
60
 
61
        // Test subjects.
62
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
63
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Apple']);
64
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
65
 
66
        /** @var core_reportbuilder_generator $generator */
67
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
68
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
69
 
70
        // Report columns, aggregated/sorted by user lastname.
71
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
72
        $generator->create_column([
73
            'reportid' => $report->get('id'),
74
            'uniqueidentifier' => 'user:lastname',
75
            'aggregation' => groupconcatdistinct::get_class_name(),
76
            'sortenabled' => 1,
77
            'sortdirection' => SORT_ASC,
78
        ]);
79
 
80
        // Assert lastname column was aggregated, and itself also sorted predictably.
81
        $content = $this->get_custom_report_content($report->get('id'));
82
        $this->assertEquals([
83
            ['Bob', 'Apple, Banana'],
84
            ['Admin', 'User'],
85
        ], array_map('array_values', $content));
86
    }
87
 
88
    /**
89
     * Test aggregation when applied to column with multiple fields
90
     */
91
    public function test_column_aggregation_multiple_fields(): void {
92
        $this->resetAfterTest();
93
 
94
        $user = $this->getDataGenerator()->create_user(['firstname' => 'Adam', 'lastname' => 'Apple']);
95
 
96
        /** @var core_reportbuilder_generator $generator */
97
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
98
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
99
 
100
        // This is the column we'll aggregate.
101
        $generator->create_column([
102
            'reportid' => $report->get('id'),
103
            'uniqueidentifier' => 'user:fullnamewithlink',
104
            'aggregation' => groupconcatdistinct::get_class_name(),
105
        ]);
106
 
107
        $content = $this->get_custom_report_content($report->get('id'));
108
        $this->assertCount(1, $content);
109
 
110
        // Ensure users are sorted predictably (Adam -> Admin).
111
        [$userone, $usertwo] = explode(', ', reset($content[0]));
112
        $this->assertStringContainsString(fullname($user, true), $userone);
113
        $this->assertStringContainsString(fullname(get_admin(), true), $usertwo);
114
    }
115
 
116
    /**
117
     * Test aggregation when applied to column with callback
118
     */
119
    public function test_column_aggregation_with_callback(): void {
120
        $this->resetAfterTest();
121
 
122
        // Test subjects.
123
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]);
124
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 0]);
125
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'confirmed' => 1]);
126
 
127
        /** @var core_reportbuilder_generator $generator */
128
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
129
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
130
 
131
        // First column, sorted.
132
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
133
 
134
        // This is the column we'll aggregate.
135
        $generator->create_column([
136
            'reportid' => $report->get('id'),
137
            'uniqueidentifier' => 'user:confirmed',
138
            'aggregation' => groupconcatdistinct::get_class_name(),
139
        ]);
140
 
141
        // Add callback to format the column.
142
        $instance = manager::get_report_from_persistent($report);
143
        $instance->get_column('user:confirmed')
144
            ->add_callback(static function(string $value, stdClass $row, $arguments, ?string $aggregation): string {
145
                // Simple callback to return the given value, and append aggregation type.
146
                return "{$value} ({$aggregation})";
147
            });
148
 
149
        // Assert confirmed column was aggregated, and sorted predictably with callback applied.
150
        $content = $this->get_custom_report_content($report->get('id'));
151
        $this->assertEquals([
152
            [
153
                'c0_firstname' => 'Admin',
154
                'c1_confirmed' => 'Yes (groupconcatdistinct)',
155
            ],
156
            [
157
                'c0_firstname' => 'Bob',
158
                'c1_confirmed' => 'No (groupconcatdistinct), Yes (groupconcatdistinct)',
159
            ],
160
        ], $content);
161
    }
162
}