Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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;
20
 
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;
26
 
27
/**
28
 * Unit tests for the test data generator
29
 *
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
32
 *
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
37
 */
1441 ariadna 38
final class generator_test extends advanced_testcase {
1 efrain 39
 
40
    /**
41
     * Test creating a report
42
     */
43
    public function test_create_report(): void {
44
        $this->resetAfterTest();
45
 
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']]);
49
 
50
        $this->assertTrue(report::record_exists($report->get('id')));
1441 ariadna 51
        $this->assertEqualsCanonicalizing(
52
            ['cat', 'dog'],
53
            array_values(core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $report->get('id'))),
54
        );
1 efrain 55
    }
56
 
57
    /**
58
     * Test creating a column
59
     */
60
    public function test_create_column(): void {
61
        $this->resetAfterTest();
62
 
63
        /** @var core_reportbuilder_generator $generator */
64
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
65
 
66
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
67
        $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
68
 
69
        $this->assertTrue(column::record_exists($column->get('id')));
70
    }
71
 
72
    /**
73
     * Test creating a column, specifying additional properties
74
     */
75
    public function test_create_column_additional_properties(): void {
76
        $this->resetAfterTest();
77
 
78
        /** @var core_reportbuilder_generator $generator */
79
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
80
 
81
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
82
        $column = $generator->create_column([
83
            'reportid' => $report->get('id'),
84
            'uniqueidentifier' => 'user:lastname',
85
            'heading' => 'My pants',
86
            'sortenabled' => 1,
87
        ]);
88
 
89
        $this->assertEquals('My pants', $column->get('heading'));
90
        $this->assertTrue($column->get('sortenabled'));
91
    }
92
 
93
    /**
94
     * Test creating a filter
95
     */
96
    public function test_create_filter(): void {
97
        $this->resetAfterTest();
98
 
99
        /** @var core_reportbuilder_generator $generator */
100
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
101
 
102
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
103
        $filter = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
104
 
105
        $this->assertTrue(filter::record_exists($filter->get('id')));
106
    }
107
 
108
    /**
109
     * Test creating a filter, specifying additional properties
110
     */
111
    public function test_create_filter_additional_properties(): void {
112
        $this->resetAfterTest();
113
 
114
        /** @var core_reportbuilder_generator $generator */
115
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
116
 
117
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
118
        $filter = $generator->create_filter([
119
            'reportid' => $report->get('id'),
120
            'uniqueidentifier' => 'user:lastname',
121
            'heading' => 'My pants',
122
        ]);
123
 
124
        $this->assertEquals('My pants', $filter->get('heading'));
125
    }
126
 
127
    /**
128
     * Test creating a condition
129
     */
130
    public function test_create_condition(): void {
131
        $this->resetAfterTest();
132
 
133
        /** @var core_reportbuilder_generator $generator */
134
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
135
 
136
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
137
        $condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
138
 
139
        $this->assertTrue(filter::record_exists($condition->get('id')));
140
    }
141
 
142
    /**
143
     * Test creating a condition, specifying additional properties
144
     */
145
    public function test_create_condition_additional_properties(): void {
146
        $this->resetAfterTest();
147
 
148
        /** @var core_reportbuilder_generator $generator */
149
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
150
 
151
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class, 'default' => 0]);
152
        $condition = $generator->create_condition([
153
            'reportid' => $report->get('id'),
154
            'uniqueidentifier' => 'user:lastname',
155
            'heading' => 'My pants',
156
        ]);
157
 
158
        $this->assertEquals('My pants', $condition->get('heading'));
159
    }
160
 
161
    /**
162
     * Test creating an audience
163
     */
164
    public function test_create_audience(): void {
165
        $this->resetAfterTest();
166
 
167
        /** @var core_reportbuilder_generator $generator */
168
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
169
 
170
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
171
        $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
172
 
173
        $this->assertTrue(audience::record_exists($audience->get_persistent()->get('id')));
174
    }
175
 
176
    /**
177
     * Test creating a schedule
178
     */
179
    public function test_create_schedule(): void {
180
        $this->resetAfterTest();
181
 
182
        /** @var core_reportbuilder_generator $generator */
183
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
184
 
185
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
186
        $schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
187
 
188
        $this->assertTrue(schedule::record_exists($schedule->get('id')));
189
    }
190
}