Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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_course;
18
 
19
use context_course;
20
use moodle_url;
21
 
22
/**
23
 * Tests for the \core_course_renderer class.
24
 *
25
 * @package    core_course
26
 * @copyright  2025 Ilya Tregubov <ilya.tregubov@proton.me>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers     \core_course_renderer
29
 */
30
final class course_renderer_test extends \advanced_testcase {
31
    /**
32
     * Data provider for {@see self::test_enrolment_options}.
33
     *
34
     * @return array
35
     */
36
    public static function enrolment_options_provider(): array {
37
        return [
38
            'with forms' => [
39
                'forms' => ['<form>Form 1</form>', '<form>Form 2</form>'],
40
                'isguestuser' => false,
41
                'expected' => ['<form>Form 1</form>', '<form>Form 2</form>'],
42
                'notexpected' => [get_string('noguestaccess', 'enrol'), get_string('notenrollable', 'enrol')],
43
            ],
44
            'no forms guest user' => [
45
                'forms' => [],
46
                'isguestuser' => true,
47
                'expected' => [get_string('noguestaccess', 'enrol')],
48
                'notexpected' => [get_string('notenrollable', 'enrol')],
49
            ],
50
            'no forms not guest user' => [
51
                'forms' => [],
52
                'isguestuser' => false,
53
                'expected' => [get_string('notenrollable', 'enrol')],
54
                'notexpected' => [get_string('noguestaccess', 'enrol')],
55
            ],
56
        ];
57
    }
58
 
59
    /**
60
     * Test render_enrolment_options with data provider.
61
     *
62
     * @param array $forms
63
     * @param bool $isguestuser
64
     * @param array $expected
65
     * @param array $notexpected
66
     * @dataProvider enrolment_options_provider
67
     */
68
    public function test_enrolment_options(array $forms, bool $isguestuser, array $expected, array $notexpected): void {
69
        global $PAGE;
70
 
71
        $this->resetAfterTest();
72
        $isguestuser ? $this->setGuestUser() : $this->setAdminUser();
73
 
74
        $course = $this->getDataGenerator()->create_course();
75
        $context = context_course::instance($course->id);
76
        $PAGE->set_context($context);
77
        $PAGE->set_course($course);
78
 
79
        /** @var \core_course_renderer $renderer */
80
        $renderer = $PAGE->get_renderer('core', 'course');
81
        $returnurl = new moodle_url('/course/view.php', ['id' => $course->id]);
82
 
83
        $output = $renderer->enrolment_options($course, $forms, $returnurl);
84
 
85
        foreach ($expected as $expectedstring) {
86
            $this->assertStringContainsString($expectedstring, $output);
87
        }
88
        foreach ($notexpected as $notexpectedstring) {
89
            $this->assertStringNotContainsString($notexpectedstring, $output);
90
        }
91
    }
92
}