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 group concatenation distinct aggregation
29
 * Unit tests for group concatenation distinct 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\groupconcatdistinct
33
 * @covers      \core_reportbuilder\local\aggregation\groupconcatdistinct
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 groupconcatdistinct_test extends core_reportbuilder_testcase {
37
final class groupconcatdistinct_test extends core_reportbuilder_testcase {
43
 
38
 
-
 
39
    /**
Línea 44... Línea 40...
44
    /**
40
     * Test setup, we need to skip these tests on non-supported databases
45
     * Test setup, we need to skip these tests on non-supported databases
41
     */
46
     */
42
    public function setUp(): void {
47
    public function setUp(): void {
43
        global $DB;
Línea 84... Línea 80...
84
            ['Admin', 'User'],
80
            ['Admin', 'User'],
85
        ], array_map('array_values', $content));
81
        ], array_map('array_values', $content));
86
    }
82
    }
Línea 87... Línea 83...
87
 
83
 
-
 
84
    /**
-
 
85
     * Test aggregation with custom separator option when applied to column
-
 
86
     */
-
 
87
    public function test_column_aggregation_separator_option(): void {
-
 
88
        $this->resetAfterTest();
-
 
89
 
-
 
90
        // Test subjects.
-
 
91
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
-
 
92
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Apple']);
-
 
93
        $this->getDataGenerator()->create_user(['firstname' => 'Bob', 'lastname' => 'Banana']);
-
 
94
 
-
 
95
        /** @var core_reportbuilder_generator $generator */
-
 
96
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
-
 
97
        $report = $generator->create_report(['name' => 'Users', 'source' => users::class, 'default' => 0]);
-
 
98
 
-
 
99
        // Report columns, aggregated/sorted by user lastname.
-
 
100
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:firstname']);
-
 
101
        $generator->create_column([
-
 
102
            'reportid' => $report->get('id'),
-
 
103
            'uniqueidentifier' => 'user:lastname',
-
 
104
            'aggregation' => groupconcatdistinct::get_class_name(),
-
 
105
            'sortenabled' => 1,
-
 
106
            'sortdirection' => SORT_ASC,
-
 
107
        ]);
-
 
108
 
-
 
109
        // Set aggregation option for separator.
-
 
110
        $instance = manager::get_report_from_persistent($report);
-
 
111
        $instance->get_column('user:lastname')
-
 
112
            ->set_aggregation_options(groupconcatdistinct::get_class_name(), ['separator' => '<br />']);
-
 
113
 
-
 
114
        // Assert lastname column was aggregated, with defined separator between each item.
-
 
115
        $content = $this->get_custom_report_content($report->get('id'));
-
 
116
        $this->assertEquals([
-
 
117
            ['Bob', 'Apple<br />Banana'],
-
 
118
            ['Admin', 'User'],
-
 
119
        ], array_map('array_values', $content));
-
 
120
    }
-
 
121
 
88
    /**
122
    /**
89
     * Test aggregation when applied to column with multiple fields
123
     * Test aggregation when applied to column with multiple fields
90
     */
124
     */
91
    public function test_column_aggregation_multiple_fields(): void {
125
    public function test_column_aggregation_multiple_fields(): void {
Línea 147... Línea 181...
147
            });
181
            });
Línea 148... Línea 182...
148
 
182
 
149
        // Assert confirmed column was aggregated, and sorted predictably with callback applied.
183
        // Assert confirmed column was aggregated, and sorted predictably with callback applied.
150
        $content = $this->get_custom_report_content($report->get('id'));
184
        $content = $this->get_custom_report_content($report->get('id'));
151
        $this->assertEquals([
-
 
152
            [
-
 
153
                'c0_firstname' => 'Admin',
185
        $this->assertEquals([
154
                'c1_confirmed' => 'Yes (groupconcatdistinct)',
-
 
155
            ],
-
 
156
            [
-
 
157
                'c0_firstname' => 'Bob',
186
            ['Admin', 'Yes (groupconcatdistinct)'],
158
                'c1_confirmed' => 'No (groupconcatdistinct), Yes (groupconcatdistinct)',
-
 
159
            ],
187
            ['Bob', 'No (groupconcatdistinct), Yes (groupconcatdistinct)'],
160
        ], $content);
188
        ], array_map('array_values', $content));
161
    }
189
    }