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