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
 
1441 ariadna 21
use core\context\system;
1 efrain 22
use core_reportbuilder_generator;
1441 ariadna 23
use core_reportbuilder\local\models\report;
24
use core_reportbuilder\local\report\base;
25
use core_reportbuilder\exception\{source_invalid_exception, source_unavailable_exception};
26
use core_reportbuilder\tests\core_reportbuilder_testcase;
1 efrain 27
use core_user\reportbuilder\datasource\users;
28
use stdClass;
29
 
30
/**
31
 * Unit tests for the report manager class
32
 *
33
 * @package     core_reportbuilder
34
 * @covers      \core_reportbuilder\manager
35
 * @copyright   2020 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 manager_test extends core_reportbuilder_testcase {
1 efrain 39
 
40
    /**
41
     * Test creating a report instance from persistent
42
     */
43
    public function test_get_report_from_persistent(): void {
44
        global $CFG;
45
        require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php");
46
 
47
        $this->resetAfterTest();
48
 
49
        $report = manager::create_report_persistent((object) [
50
            'type' => base::TYPE_SYSTEM_REPORT,
51
            'source' => system_report_available::class,
52
        ]);
53
 
54
        $systemreport = manager::get_report_from_persistent($report);
55
        $this->assertInstanceOf(system_report::class, $systemreport);
56
    }
57
 
58
    /**
59
     * Test creating a report instance from persistent differs per-user, using a report source whose own initialization is
60
     * dependent on the current user (the users report source, loading available user profile fields)
61
     *
62
     * Note: internally the {@see get_custom_report_content} test helper calls {@see manager::get_report_from_persistent}
63
     */
64
    public function test_get_report_from_persistent_per_user(): void {
65
        $this->resetAfterTest();
66
        $this->setAdminUser();
67
 
68
        // Custom profile field, visible only to the admin.
69
        $this->getDataGenerator()->create_custom_profile_field([
70
            'shortname' => 'text', 'name' => 'Text field', 'datatype' => 'text', 'visible' => 0]);
71
        $user = $this->getDataGenerator()->create_user(['username' => 'usertwo', 'profile_field_text' => 'Hello']);
72
 
73
        /** @var core_reportbuilder_generator $generator */
74
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
75
 
76
        $report = $generator->create_report(['name' => 'Hidden profile field', 'source' => users::class, 'default' => 0]);
77
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:username', 'sortenabled' => 1]);
78
        $generator->create_column(['reportid' => $report->get('id'), 'uniqueidentifier' => 'user:profilefield_text']);
79
 
80
        $content = $this->get_custom_report_content($report->get('id'));
81
        $this->assertEquals([
82
            ['admin', ''],
83
            ['usertwo', 'Hello'],
84
        ], array_map('array_values', $content));
85
 
86
        // Now switch to second, non-admin, user.
87
        $this->setUser($user);
88
 
89
        $content = $this->get_custom_report_content($report->get('id'));
90
        $this->assertEquals([
91
            ['admin'],
92
            ['usertwo'],
93
        ], array_map('array_values', $content));
94
    }
95
 
96
    /**
97
     * Test creating a report instance from persistent with an invalid source
98
     */
99
    public function test_get_report_from_persistent_invalid(): void {
100
        $this->resetAfterTest();
101
 
102
        $report = manager::create_report_persistent((object) [
103
            'type' => base::TYPE_SYSTEM_REPORT,
104
            'source' => stdClass::class,
105
        ]);
106
 
107
        $this->expectException(source_invalid_exception::class);
108
        manager::get_report_from_persistent($report);
109
    }
110
 
111
    /**
112
     * Test creating a report instance from persistent with an unavailable source
113
     */
114
    public function test_get_report_from_persistent_unavailable(): void {
115
        global $CFG;
116
        require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_unavailable.php");
117
 
118
        $this->resetAfterTest();
119
 
120
        $report = manager::create_report_persistent((object) [
121
            'type' => base::TYPE_SYSTEM_REPORT,
122
            'source' => system_report_unavailable::class,
123
        ]);
124
 
125
        $this->expectException(source_unavailable_exception::class);
126
        manager::get_report_from_persistent($report);
127
    }
128
 
129
    /**
130
     * Test report source exists
131
     */
132
    public function test_report_source_exists(): void {
133
        global $CFG;
134
 
135
        require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php");
136
        $this->assertTrue(manager::report_source_exists(system_report_available::class));
137
 
138
        $this->assertFalse(manager::report_source_exists(stdClass::class));
139
    }
140
 
141
    /**
142
     * Test report source available
143
     */
144
    public function test_report_source_available(): void {
145
        global $CFG;
146
 
147
        require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php");
148
        $this->assertTrue(manager::report_source_available(system_report_available::class));
149
 
150
        require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_unavailable.php");
151
        $this->assertFalse(manager::report_source_available(system_report_unavailable::class));
152
    }
153
 
154
    /**
155
     * Test creating a report persistent model
156
     */
157
    public function test_create_report_persistent(): void {
158
        global $CFG;
159
        require_once("{$CFG->dirroot}/reportbuilder/tests/fixtures/system_report_available.php");
160
 
161
        $this->resetAfterTest();
162
 
163
        $report = manager::create_report_persistent((object) [
164
            'type' => base::TYPE_SYSTEM_REPORT,
165
            'source' => \core_reportbuilder\system_report_available::class,
166
        ]);
167
 
168
        $this->assertInstanceOf(report::class, $report);
169
        $this->assertEquals(base::TYPE_SYSTEM_REPORT, $report->get('type'));
170
        $this->assertEquals(system_report_available::class, $report->get('source'));
1441 ariadna 171
        $this->assertInstanceOf(system::class, $report->get_context());
1 efrain 172
    }
173
 
174
    /**
175
     * Data provider for {@see test_report_limit_reached}
176
     *
177
     * @return array
178
     */
1441 ariadna 179
    public static function report_limit_reached_provider(): array {
1 efrain 180
        return [
181
            [0, 1, false],
182
            [1, 1, true],
183
            [2, 1, false],
184
            [1, 2, true],
185
        ];
186
    }
187
 
188
    /**
189
     * Test test_report_limit_reached method to check site custom reports limit
190
     *
191
     * @param int $customreportslimit
192
     * @param int $existingreports
193
     * @param bool $expected
194
     * @dataProvider report_limit_reached_provider
195
     */
196
    public function test_report_limit_reached(int $customreportslimit, int $existingreports, bool $expected): void {
197
        global $CFG;
198
 
199
        $this->resetAfterTest();
200
 
201
        /** @var core_reportbuilder_generator $generator */
202
        $generator = $this->getDataGenerator()->get_plugin_generator('core_reportbuilder');
203
        for ($i = 1; $i <= $existingreports; $i++) {
204
            $generator->create_report(['name' => 'Limited report '.$i, 'source' => users::class]);
205
        }
206
 
207
        // Set current custom report limit, and check whether the limit has been reached.
208
        $CFG->customreportslimit = $customreportslimit;
209
        $this->assertEquals($expected, manager::report_limit_reached());
210
    }
211
}