Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 16... Línea 16...
16
 
16
 
Línea 17... Línea 17...
17
declare(strict_types=1);
17
declare(strict_types=1);
Línea 18... Línea -...
18
 
-
 
19
namespace core_reportbuilder\local\aggregation;
18
 
20
 
19
namespace core_reportbuilder\local\aggregation;
21
use core_reportbuilder_testcase;
20
 
-
 
21
use core_reportbuilder_generator;
22
use core_reportbuilder_generator;
22
use core_reportbuilder\manager;
23
use core_reportbuilder\manager;
23
use core_reportbuilder\local\report\column;
Línea 24... Línea -...
24
use core_reportbuilder\local\report\column;
-
 
25
use core_user\reportbuilder\datasource\users;
-
 
26
use stdClass;
-
 
27
 
-
 
28
defined('MOODLE_INTERNAL') || die();
-
 
29
 
24
use core_reportbuilder\tests\core_reportbuilder_testcase;
30
global $CFG;
25
use core_user\reportbuilder\datasource\users;
31
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
26
use stdClass;
32
 
27
 
33
/**
28
/**
34
 * Unit tests for sum aggregation
29
 * Unit tests for sum aggregation
35
 *
30
 *
36
 * @package     core_reportbuilder
31
 * @package     core_reportbuilder
37
 * @covers      \core_reportbuilder\local\aggregation\base
32
 * @covers      \core_reportbuilder\local\aggregation\base
38
 * @covers      \core_reportbuilder\local\aggregation\sum
33
 * @covers      \core_reportbuilder\local\aggregation\sum
Línea 39... Línea 34...
39
 * @copyright   2021 Paul Holden <paulh@moodle.com>
34
 * @copyright   2021 Paul Holden <paulh@moodle.com>
40
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
36
 */
42
class sum_test extends core_reportbuilder_testcase {
37
final class sum_test extends core_reportbuilder_testcase {
Línea 71... Línea 66...
71
            ['Admin', 0],
66
            ['Admin', 0],
72
        ], array_map('array_values', $content));
67
        ], array_map('array_values', $content));
73
    }
68
    }
Línea 74... Línea 69...
74
 
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 (sum)',
-
 
79
                '2 (sum)',
-
 
80
            ]],
-
 
81
            [column::TYPE_FLOAT, [
-
 
82
                '0.0 (sum)',
-
 
83
                '2.0 (sum)',
-
 
84
            ]],
-
 
85
            [column::TYPE_BOOLEAN, [
-
 
86
                '0',
-
 
87
                '2',
-
 
88
            ]],
-
 
89
        ];
-
 
90
    }
-
 
91
 
75
    /**
92
    /**
-
 
93
     * Test aggregation when applied to column with callback
-
 
94
     *
-
 
95
     * @param int $columntype
-
 
96
     * @param string[] $expected
-
 
97
     *
76
     * Test aggregation when applied to column with callback
98
     * @dataProvider column_aggregation_with_callback_provider
77
     */
99
     */
78
    public function test_column_aggregation_with_callback(): void {
100
    public function test_column_aggregation_with_callback(int $columntype, array $expected): void {
Línea 79... Línea 101...
79
        $this->resetAfterTest();
101
        $this->resetAfterTest();
80
 
102
 
81
        // Test subjects.
103
        // Test subjects.
Línea 95... Línea 117...
95
        );
117
        );
Línea 96... Línea 118...
96
 
118
 
97
        // Set callback to format the column (hack column definition to ensure callbacks are executed).
119
        // Set callback to format the column (hack column definition to ensure callbacks are executed).
98
        $instance = manager::get_report_from_persistent($report);
120
        $instance = manager::get_report_from_persistent($report);
99
        $instance->get_column('user:suspended')
121
        $instance->get_column('user:suspended')
100
            ->set_type(column::TYPE_INTEGER)
122
            ->set_type($columntype)
101
            ->set_callback(static function(int $value, stdClass $row, $arguments, ?string $aggregation): string {
123
            ->set_callback(static function(int|float $value, stdClass $row, $arguments, ?string $aggregation): string {
102
                // Simple callback to return the given value, and append aggregation type.
124
                // Simple callback to return the given value, and append aggregation type.
103
                return "{$value} ({$aggregation})";
125
                return var_export($value, true) . " ({$aggregation})";
Línea 104... Línea 126...
104
            });
126
            });
105
 
127
 
106
        $content = $this->get_custom_report_content($report->get('id'));
-
 
107
        $this->assertEquals([
128
        $content = $this->get_custom_report_content($report->get('id'));
108
            [
-
 
109
                'c0_firstname' => 'Admin',
-
 
110
                'c1_suspended' => '0 (sum)',
-
 
111
            ],
129
        $this->assertEquals([
112
            [
-
 
113
                'c0_firstname' => 'Bob',
-
 
114
                'c1_suspended' => '2 (sum)',
130
            ['Admin', $expected[0]],
115
            ],
131
            ['Bob', $expected[1]],
116
        ], $content);
132
        ], array_map('array_values', $content));