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
 
-
 
19
namespace core_reportbuilder\local\aggregation;
-
 
20
 
-
 
21
use core_reportbuilder_generator;
20
 
22
use core_reportbuilder\manager;
21
use core_reportbuilder_testcase;
-
 
22
use core_reportbuilder_generator;
-
 
23
use core_user\reportbuilder\datasource\users;
-
 
24
 
23
use core_reportbuilder\local\report\column;
25
defined('MOODLE_INTERNAL') || die();
-
 
Línea 26... Línea 24...
26
 
24
use core_reportbuilder\tests\core_reportbuilder_testcase;
27
global $CFG;
25
use core_user\reportbuilder\datasource\users;
28
require_once("{$CFG->dirroot}/reportbuilder/tests/helpers.php");
26
use stdClass;
29
 
27
 
30
/**
28
/**
31
 * Unit tests for max aggregation
29
 * Unit tests for max aggregation
32
 *
30
 *
33
 * @package     core_reportbuilder
31
 * @package     core_reportbuilder
34
 * @covers      \core_reportbuilder\local\aggregation\base
32
 * @covers      \core_reportbuilder\local\aggregation\base
35
 * @covers      \core_reportbuilder\local\aggregation\max
33
 * @covers      \core_reportbuilder\local\aggregation\max
Línea 36... Línea 34...
36
 * @copyright   2021 Paul Holden <paulh@moodle.com>
34
 * @copyright   2021 Paul Holden <paulh@moodle.com>
37
 * @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
38
 */
36
 */
39
class max_test extends core_reportbuilder_testcase {
37
final class max_test extends core_reportbuilder_testcase {
Línea 66... Línea 64...
66
        $this->assertEquals([
64
        $this->assertEquals([
67
            ['Bob', 'Yes'],
65
            ['Bob', 'Yes'],
68
            ['Admin', 'No'],
66
            ['Admin', 'No'],
69
        ], array_map('array_values', $content));
67
        ], array_map('array_values', $content));
70
    }
68
    }
-
 
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
    }
71
}
138
}