Moodle release 4.4rc1
[moodle.git] / reportbuilder / tests / generator_test.php
blob1c9a13f603320d1ac84706bd883214633862f0a1
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.
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/>.
17 declare(strict_types=1);
19 namespace core_reportbuilder;
21 use advanced_testcase;
22 use core_reportbuilder_generator;
23 use core_reportbuilder\local\models\{audience, column, filter, report, schedule};
24 use core_tag_tag;
25 use core_user\reportbuilder\datasource\users;
27 /**
28 * Unit tests for the test data generator
30 * Note that assertions of created data content is performed in other testcases of the relevant classes, in the majority of cases
31 * here we just want to assert that the thing we created actually exists
33 * @package core_reportbuilder
34 * @covers \core_reportbuilder_generator
35 * @copyright 2022 Paul Holden <paulh@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class generator_test extends advanced_testcase {
40 /**
41 * Test creating a report
43 public function test_create_report(): void {
44 $this->resetAfterTest();
46 /** @var core_reportbuilder_generator $generator */
47 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
48 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'tags' => ['cat', 'dog']]);
50 $this->assertTrue(report::record_exists($report->get('id')));
51 $this->assertEqualsCanonicalizing(['cat', 'dog'],
52 core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $report->get('id')));
55 /**
56 * Test creating a column
58 public function test_create_column(): void {
59 $this->resetAfterTest();
61 /** @var core_reportbuilder_generator $generator */
62 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
64 $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
65 $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
67 $this->assertTrue(column::record_exists($column->get('id')));
70 /**
71 * Test creating a column, specifying additional properties
73 public function test_create_column_additional_properties(): void {
74 $this->resetAfterTest();
76 /** @var core_reportbuilder_generator $generator */
77 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
79 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
80 $column = $generator->create_column([
81 'reportid' => $report->get('id'),
82 'uniqueidentifier' => 'user:lastname',
83 'heading' => 'My pants',
84 'sortenabled' => 1,
85 ]);
87 $this->assertEquals('My pants', $column->get('heading'));
88 $this->assertTrue($column->get('sortenabled'));
91 /**
92 * Test creating a filter
94 public function test_create_filter(): void {
95 $this->resetAfterTest();
97 /** @var core_reportbuilder_generator $generator */
98 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
100 $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
101 $filter = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
103 $this->assertTrue(filter::record_exists($filter->get('id')));
107 * Test creating a filter, specifying additional properties
109 public function test_create_filter_additional_properties(): void {
110 $this->resetAfterTest();
112 /** @var core_reportbuilder_generator $generator */
113 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
115 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
116 $filter = $generator->create_filter([
117 'reportid' => $report->get('id'),
118 'uniqueidentifier' => 'user:lastname',
119 'heading' => 'My pants',
122 $this->assertEquals('My pants', $filter->get('heading'));
126 * Test creating a condition
128 public function test_create_condition(): void {
129 $this->resetAfterTest();
131 /** @var core_reportbuilder_generator $generator */
132 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
134 $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
135 $condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
137 $this->assertTrue(filter::record_exists($condition->get('id')));
141 * Test creating a condition, specifying additional properties
143 public function test_create_condition_additional_properties(): void {
144 $this->resetAfterTest();
146 /** @var core_reportbuilder_generator $generator */
147 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
149 $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
150 $condition = $generator->create_condition([
151 'reportid' => $report->get('id'),
152 'uniqueidentifier' => 'user:lastname',
153 'heading' => 'My pants',
156 $this->assertEquals('My pants', $condition->get('heading'));
160 * Test creating an audience
162 public function test_create_audience(): void {
163 $this->resetAfterTest();
165 /** @var core_reportbuilder_generator $generator */
166 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
168 $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
169 $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
171 $this->assertTrue(audience::record_exists($audience->get_persistent()->get('id')));
175 * Test creating a schedule
177 public function test_create_schedule(): void {
178 $this->resetAfterTest();
180 /** @var core_reportbuilder_generator $generator */
181 $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
183 $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
184 $schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
186 $this->assertTrue(schedule::record_exists($schedule->get('id')));