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;
1441 ariadna 22
use core_reportbuilder\manager;
23
use core_reportbuilder\local\report\column;
24
use core_reportbuilder\tests\core_reportbuilder_testcase;
1 efrain 25
use core_user\reportbuilder\datasource\users;
1441 ariadna 26
use stdClass;
1 efrain 27
 
28
/**
29
 * Unit tests for max aggregation
30
 *
31
 * @package     core_reportbuilder
32
 * @covers      \core_reportbuilder\local\aggregation\base
33
 * @covers      \core_reportbuilder\local\aggregation\max
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 max_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' => 0]);
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' => max::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', 'Yes'],
66
            ['Admin', 'No'],
67
        ], array_map('array_values', $content));
68
    }
1441 ariadna 69
 
70
    /**
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 (max)',
79
                '1 (max)',
80
            ]],
81
            [column::TYPE_FLOAT, [
82
                '0.0 (max)',
83
                '1.0 (max)',
84
            ]],
85
            [column::TYPE_TIMESTAMP, [
86
                '0 (max)',
87
                '1 (max)',
88
            ]],
89
            [column::TYPE_BOOLEAN, [
90
                'false (max)',
91
                'true (max)',
92
            ]],
93
        ];
94
    }
95
 
96
    /**
97
     * Test aggregation when applied to column with callback
98
     *
99
     * @param int $columntype
100
     * @param string[] $expected
101
     *
102
     * @dataProvider column_aggregation_with_callback_provider
103
     */
104
    public function test_column_aggregation_with_callback(int $columntype, array $expected): void {
105
        $this->resetAfterTest();
106
 
107
        // Test subjects.
108
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
109
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 0]);
110
 
111
        /** @var core_reportbuilder_generator $generator */
112
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
113
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
114
 
115
        // First column, sorted.
116
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
117
 
118
        // This is the column we'll aggregate.
119
        $generator->create_column(
120
            ['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => max::get_class_name()]
121
        );
122
 
123
        // Set callback to format the column (hack column definition to ensure callbacks are executed).
124
        $instance = manager::get_report_from_persistent($report);
125
        $instance->get_column('user:suspended')
126
            ->set_type($columntype)
127
            ->set_callback(static function(int|float|bool $value, stdClass $row, $arguments, ?string $aggregation): string {
128
                // Simple callback to return the given value, and append aggregation type.
129
                return var_export($value, true) . " ({$aggregation})";
130
            });
131
 
132
        $content = $this->get_custom_report_content($report->get('id'));
133
        $this->assertEquals([
134
            ['Admin', $expected[0]],
135
            ['Bob', $expected[1]],
136
        ], array_map('array_values', $content));
137
    }
1 efrain 138
}