Proyectos de Subversion Moodle

Rev

| 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
 */
38
class generator_test extends advanced_testcase {
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')));
51
        $this->assertEqualsCanonicalizing(['cat', 'dog'],
52
            core_tag_tag::get_item_tags_array('core_reportbuilder', 'reportbuilder_report', $report->get('id')));
53
    }
54
 
55
    /**
56
     * Test creating a column
57
     */
58
    public function test_create_column(): void {
59
        $this->resetAfterTest();
60
 
61
        /** @var core_reportbuilder_generator $generator */
62
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
63
 
64
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
65
        $column = $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
66
 
67
        $this->assertTrue(column::record_exists($column->get('id')));
68
    }
69
 
70
    /**
71
     * Test creating a column, specifying additional properties
72
     */
73
    public function test_create_column_additional_properties(): void {
74
        $this->resetAfterTest();
75
 
76
        /** @var core_reportbuilder_generator $generator */
77
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
78
 
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
        ]);
86
 
87
        $this->assertEquals('My pants', $column->get('heading'));
88
        $this->assertTrue($column->get('sortenabled'));
89
    }
90
 
91
    /**
92
     * Test creating a filter
93
     */
94
    public function test_create_filter(): void {
95
        $this->resetAfterTest();
96
 
97
        /** @var core_reportbuilder_generator $generator */
98
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
99
 
100
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
101
        $filter = $generator->create_filter(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
102
 
103
        $this->assertTrue(filter::record_exists($filter->get('id')));
104
    }
105
 
106
    /**
107
     * Test creating a filter, specifying additional properties
108
     */
109
    public function test_create_filter_additional_properties(): void {
110
        $this->resetAfterTest();
111
 
112
        /** @var core_reportbuilder_generator $generator */
113
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
114
 
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',
120
        ]);
121
 
122
        $this->assertEquals('My pants', $filter->get('heading'));
123
    }
124
 
125
    /**
126
     * Test creating a condition
127
     */
128
    public function test_create_condition(): void {
129
        $this->resetAfterTest();
130
 
131
        /** @var core_reportbuilder_generator $generator */
132
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
133
 
134
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
135
        $condition = $generator->create_condition(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:lastname']);
136
 
137
        $this->assertTrue(filter::record_exists($condition->get('id')));
138
    }
139
 
140
    /**
141
     * Test creating a condition, specifying additional properties
142
     */
143
    public function test_create_condition_additional_properties(): void {
144
        $this->resetAfterTest();
145
 
146
        /** @var core_reportbuilder_generator $generator */
147
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
148
 
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',
154
        ]);
155
 
156
        $this->assertEquals('My pants', $condition->get('heading'));
157
    }
158
 
159
    /**
160
     * Test creating an audience
161
     */
162
    public function test_create_audience(): void {
163
        $this->resetAfterTest();
164
 
165
        /** @var core_reportbuilder_generator $generator */
166
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
167
 
168
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
169
        $audience = $generator->create_audience(['reportid' => $report->get('id'), 'configdata' => []]);
170
 
171
        $this->assertTrue(audience::record_exists($audience->get_persistent()->get('id')));
172
    }
173
 
174
    /**
175
     * Test creating a schedule
176
     */
177
    public function test_create_schedule(): void {
178
        $this->resetAfterTest();
179
 
180
        /** @var core_reportbuilder_generator $generator */
181
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
182
 
183
        $report = $generator->create_report(['name' => 'My report', 'source' => users::class]);
184
        $schedule = $generator->create_schedule(['reportid' => $report->get('id'), 'name' => 'My schedule']);
185
 
186
        $this->assertTrue(schedule::record_exists($schedule->get('id')));
187
    }
188
}