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
namespace core_grades\output;
18
 
19
use advanced_testcase;
20
use grade_helper;
21
use context_course;
22
use moodle_url;
23
 
24
/**
25
 * A test class used to test general_action_bar.
26
 *
27
 * @package    core_grades
28
 * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
1441 ariadna 31
final class general_action_bar_test extends advanced_testcase {
1 efrain 32
 
33
    /**
34
     * Load required test libraries
35
     */
36
    public static function setUpBeforeClass(): void {
37
        global $CFG;
38
        require_once("{$CFG->dirroot}/grade/lib.php");
1441 ariadna 39
        parent::setUpBeforeClass();
1 efrain 40
    }
41
 
42
    /**
43
     * Search array $options for an element which is an array containing 'name' => $name.
44
     *
45
     * @param array $options the array of options.
46
     * @param string $name the name to find.
47
     * @return array|null the particular option if found, else null.
48
     */
49
    protected function find_option_by_name(array $options, string $name): ?array {
50
        foreach ($options as $option) {
51
            if ($option['name'] == $name) {
52
                return $option;
53
            }
54
        }
55
        return null;
56
    }
57
 
58
    /**
59
     * Test the exported data for the general action bar for different user roles and settings.
60
     *
61
     * @dataProvider export_for_template_provider
62
     * @param string $userrole The user role to test
63
     * @param bool $enableoutcomes Whether to enable outcomes
64
     * @param array $expectedoptions The expected options returned in the general action selector
65
     * @covers \core_grades\output\general_action_bar::export_for_template
66
     */
67
    public function test_export_for_template(string $userrole, bool $enableoutcomes, array $expectedoptions): void {
68
        global $PAGE;
69
 
70
        // There may be additional plugins installed in the codebase where this
71
        // test is being run, therefore, we need to know which links can be
72
        // present in a standard Moodle install, and only check them.
73
        $allcorenavlinks = [
74
            get_string('view') => [
75
                get_string('pluginname', 'gradereport_grader'),
76
                get_string('pluginname', 'gradereport_history'),
77
                get_string('pluginname', 'gradereport_outcomes'),
78
                get_string('pluginname', 'gradereport_overview'),
79
                get_string('pluginname', 'gradereport_singleview'),
80
                get_string('pluginname', 'gradereport_summary'),
81
                get_string('pluginname', 'gradereport_user'),
82
            ],
83
            get_string('setup', 'grades') => [
84
                get_string('gradebooksetup', 'grades'),
85
                get_string('coursegradesettings', 'grades'),
86
                get_string('preferences', 'grades') . ': ' . get_string('pluginname', 'gradereport_grader'),
87
            ],
88
            get_string('moremenu') => [
89
                get_string('scales'),
90
                get_string('outcomes', 'grades'),
91
                get_string('gradeletters', 'grades'),
92
                get_string('import', 'grades'),
93
                get_string('export', 'grades'),
94
            ],
95
        ];
96
 
97
        $this->resetAfterTest();
98
        // Reset the cache.
99
        grade_helper::reset_caches();
100
 
101
        // Create a course.
102
        $course = $this->getDataGenerator()->create_course();
103
        $coursecontext = context_course::instance($course->id);
104
 
105
        if ($userrole === 'admin') {
106
            $this->setAdminUser();
107
        } else {
108
            // Enrol user to the course.
109
            $user = $this->getDataGenerator()->create_and_enrol($course, $userrole);
110
            $this->setUser($user);
111
        }
112
 
113
        if ($enableoutcomes) {
114
            set_config('enableoutcomes', 1);
115
        }
116
 
117
        $generalactionbar = new general_action_bar($coursecontext,
118
            new moodle_url('/grade/report/user/index.php', ['id' => $course->id]), 'report', 'user');
119
        $renderer = $PAGE->get_renderer('core');
120
        $generalactionbardata = $generalactionbar->export_for_template($renderer);
121
 
122
        $this->assertCount(1, $generalactionbardata);
123
        $this->assertArrayHasKey('generalnavselector', $generalactionbardata);
124
 
125
        $generalnavselector = $generalactionbardata['generalnavselector'];
126
 
127
        // Assert that the right links are present in each group.
128
        foreach ($allcorenavlinks as $groupname => $corelinks) {
129
            $actualgroup = $this->find_option_by_name($generalnavselector->options, $groupname);
130
 
131
            if (!isset($expectedoptions[$groupname])) {
132
                // This group should not be present.
133
                $this->assertNull($actualgroup, "Nav link group '$groupname' should not be present, but is.");
134
                continue;
135
            }
136
 
137
            $this->assertNotNull($actualgroup, "Nav link group '$groupname' should be present, but is not.");
138
            $this->assertTrue($actualgroup['isgroup'], "the thing claiming to be nav link group '$groupname' is not a group.");
139
 
140
            foreach ($corelinks as $corelinkname) {
141
                $actuallink = $this->find_option_by_name($actualgroup['options'], $corelinkname);
142
 
143
                if (!in_array($corelinkname, $expectedoptions[$groupname])) {
144
                    $this->assertNull($actuallink,
145
                            "Nav link '$corelinkname' should not be present in group '$groupname', but is.");
146
                } else {
147
                    $this->assertNotNull($actuallink,
148
                            "Nav link '$corelinkname' should be present in group '$groupname', but is not.");
149
                }
150
            }
151
        }
152
    }
153
 
154
    /**
155
     * Data provider for the test_export_for_template test.
156
     *
157
     * @return array
158
     */
1441 ariadna 159
    public static function export_for_template_provider(): array {
1 efrain 160
        $graderpluginname = get_string('pluginname', 'gradereport_grader');
161
        $historypluginname = get_string('pluginname', 'gradereport_history');
162
        $outcomespluginname = get_string('pluginname', 'gradereport_outcomes');
163
        $overviewpluginname = get_string('pluginname', 'gradereport_overview');
164
        $singleviewpluginname = get_string('pluginname', 'gradereport_singleview');
165
        $summarypluginname = get_string('pluginname', 'gradereport_summary');
166
        $userpluginname = get_string('pluginname', 'gradereport_user');
167
 
168
        $viewstr = get_string('view');
169
        $setupstr = get_string('setup', 'grades');
170
        $morestr = get_string('moremenu');
171
 
172
        $gradebooksetupstr = get_string('gradebooksetup', 'grades');
173
        $coursegradesettingsstr = get_string('coursegradesettings', 'grades');
174
        $graderpreferencesstr = get_string('preferences', 'grades') . ': ' . get_string('pluginname', 'gradereport_grader');
175
 
176
        $scalesstr = get_string('scales');
177
        $outcomesstr = get_string('outcomes', 'grades');
178
        $gradelettersstr = get_string('gradeletters', 'grades');
179
        $importstr = get_string('import', 'grades');
180
        $exportstr = get_string('export', 'grades');
181
 
182
        return [
183
            'Gradebook general navigation for admin; outcomes disabled.' => [
184
                'admin',
185
                false,
186
                [
187
                    $viewstr => [
188
                        $graderpluginname,
189
                        $historypluginname,
190
                        $overviewpluginname,
191
                        $singleviewpluginname,
192
                        $summarypluginname,
193
                        $userpluginname,
194
                    ],
195
                    $setupstr => [
196
                        $gradebooksetupstr,
197
                        $coursegradesettingsstr,
198
                        $graderpreferencesstr,
199
                    ],
200
                    $morestr => [
201
                        $scalesstr,
202
                        $gradelettersstr,
203
                        $importstr,
204
                        $exportstr,
205
                    ],
206
                ],
207
            ],
208
            'Gradebook general navigation for admin; outcomes enabled.' => [
209
                'admin',
210
                true,
211
                [
212
                    $viewstr => [
213
                        $graderpluginname,
214
                        $historypluginname,
215
                        $outcomespluginname,
216
                        $overviewpluginname,
217
                        $singleviewpluginname,
218
                        $summarypluginname,
219
                        $userpluginname,
220
                    ],
221
                    $setupstr => [
222
                        $gradebooksetupstr,
223
                        $coursegradesettingsstr,
224
                        $graderpreferencesstr,
225
                    ],
226
                    $morestr => [
227
                        $scalesstr,
228
                        $outcomesstr,
229
                        $gradelettersstr,
230
                        $importstr,
231
                        $exportstr,
232
                    ],
233
                ],
234
            ],
235
            'Gradebook general navigation for editing teacher; outcomes disabled.' => [
236
                'editingteacher',
237
                false,
238
                [
239
                    $viewstr => [
240
                        $graderpluginname,
241
                        $historypluginname,
242
                        $overviewpluginname,
243
                        $singleviewpluginname,
244
                        $summarypluginname,
245
                        $userpluginname,
246
                    ],
247
                    $setupstr => [
248
                        $gradebooksetupstr,
249
                        $coursegradesettingsstr,
250
                        $graderpreferencesstr,
251
                    ],
252
                    $morestr => [
253
                        $scalesstr,
254
                        $gradelettersstr,
255
                        $importstr,
256
                        $exportstr,
257
                    ],
258
                ],
259
            ],
260
            'Gradebook general navigation for editing teacher; outcomes enabled.' => [
261
                'editingteacher',
262
                true,
263
                [
264
                    $viewstr => [
265
                        $graderpluginname,
266
                        $historypluginname,
267
                        $outcomespluginname,
268
                        $overviewpluginname,
269
                        $singleviewpluginname,
270
                        $summarypluginname,
271
                        $userpluginname,
272
                    ],
273
                    $setupstr => [
274
                        $gradebooksetupstr,
275
                        $coursegradesettingsstr,
276
                        $graderpreferencesstr,
277
                    ],
278
                    $morestr => [
279
                        $scalesstr,
280
                        $outcomesstr,
281
                        $gradelettersstr,
282
                        $importstr,
283
                        $exportstr,
284
                    ],
285
                ],
286
            ],
287
            'Gradebook general navigation for non-editing teacher; outcomes enabled.' => [
288
                'teacher',
289
                true,
290
                [
291
                    $viewstr => [
292
                        $graderpluginname,
293
                        $historypluginname,
294
                        $outcomespluginname,
295
                        $overviewpluginname,
296
                        $summarypluginname,
297
                        $userpluginname,
298
                    ],
299
                    $setupstr => [
300
                        $graderpreferencesstr,
301
                    ],
302
                    $morestr => [
303
                        $exportstr,
304
                    ],
305
                ],
306
            ],
307
            'Gradebook general navigation for student; outcomes enabled.' => [
308
                'student',
309
                true,
310
                [
311
                    $viewstr => [
312
                        $overviewpluginname,
313
                        $userpluginname,
314
                    ],
315
                ],
316
            ],
317
        ];
318
    }
319
}