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 avg aggregation
30
 *
31
 * @package     core_reportbuilder
32
 * @covers      \core_reportbuilder\local\aggregation\avg
33
 * @copyright   2022 Paul Holden <paulh@moodle.com>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class avg_test extends core_reportbuilder_testcase {
1 efrain 37
 
38
    /**
39
     * Test aggregation when applied to column
40
     */
41
    public function test_column_aggregation(): void {
42
        $this->resetAfterTest();
43
 
44
        // Test subjects.
45
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
46
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 0]);
47
 
48
        /** @var core_reportbuilder_generator $generator */
49
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
50
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
51
 
52
        // Report columns, aggregated/sorted by user suspended.
53
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
54
        $generator->create_column([
55
            'reportid' => $report->get('id'),
56
            'uniqueidentifier' => 'user:suspended',
57
            'aggregation' => avg::get_class_name(),
58
            'sortenabled' => 1,
59
            'sortdirection' => SORT_DESC,
60
        ]);
61
 
62
        $content = $this->get_custom_report_content($report->get('id'));
63
        $this->assertEquals([
64
            ['Bob', '0.5'],
65
            ['Admin', '0.0'],
66
        ], array_map('array_values', $content));
67
    }
68
 
69
    /**
70
     * Test aggregation when applied to column with callback
71
     */
72
    public function test_column_aggregation_with_callback(): void {
73
        $this->resetAfterTest();
74
 
75
        // Test subjects.
76
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 1]);
77
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'suspended' => 0]);
78
 
79
        /** @var core_reportbuilder_generator $generator */
80
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
81
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
82
 
83
        // First column, sorted.
84
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname', 'sortenabled' => 1]);
85
 
86
        // This is the column we'll aggregate.
87
        $generator->create_column(
88
            ['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:suspended', 'aggregation' => avg::get_class_name()]
89
        );
90
 
91
        // Set callback to format the column (hack column definition to ensure callbacks are executed).
92
        $instance = manager::get_report_from_persistent($report);
93
        $instance->get_column('user:suspended')
94
            ->set_type(column::TYPE_FLOAT)
95
            ->set_callback(static function(float $value, stdClass $row, $arguments, ?string $aggregation): string {
96
                // Simple callback to return the given value, and append aggregation type.
97
                return number_format($value, 1) . " ({$aggregation})";
98
            });
99
 
100
        $content = $this->get_custom_report_content($report->get('id'));
101
        $this->assertEquals([
102
            [
103
                'c0_firstname' => 'Admin',
104
                'c1_suspended' => '0.0 (avg)',
105
            ],
106
            [
107
                'c0_firstname' => 'Bob',
108
                'c1_suspended' => '0.5 (avg)',
109
            ],
110
        ], $content);
111
    }
112
}