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