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_courseformat;
18
 
19
use test_component\courseformat\sectiondelegate as testsectiondelegate;
20
 
21
/**
22
 * Section delegate tests.
23
 *
24
 * @package    core_courseformat
25
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @covers     \core_courseformat\sectiondelegate
28
 * @coversDefaultClass \core_courseformat\sectiondelegate
29
 */
1441 ariadna 30
final class sectiondelegate_test extends \advanced_testcase {
1 efrain 31
 
32
    /**
33
     * Setup to ensure that fixtures are loaded.
34
     */
35
    public static function setUpBeforeClass(): void {
36
        global $CFG;
37
        require_once($CFG->libdir . '/tests/fixtures/sectiondelegatetest.php');
1441 ariadna 38
        parent::setUpBeforeClass();
1 efrain 39
    }
40
 
41
    /**
42
     * Test that the instance method returns the correct class.
43
     * @covers ::instance
44
     */
45
    public function test_instance(): void {
46
        global $DB;
47
        $this->resetAfterTest();
48
 
49
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 3]);
50
 
51
        // Section 2 has an existing delegate class.
52
        course_update_section(
53
            $course,
54
            $DB->get_record('course_sections', ['course' => $course->id, 'section' => 2]),
55
            [
56
                'component' => 'test_component',
57
                'itemid' => 1,
58
            ]
59
        );
60
 
61
        // Section 3 has a missing delegate class.
62
        course_update_section(
63
            $course,
64
            $DB->get_record('course_sections', ['course' => $course->id, 'section' => 3]),
65
            [
66
                'component' => 'missing_component',
67
                'itemid' => 1,
68
            ]
69
        );
70
 
71
        $modinfo = get_fast_modinfo($course->id);
72
        $sectioninfos = $modinfo->get_section_info_all();
73
 
74
        $this->assertNull(sectiondelegate::instance($sectioninfos[1]));
75
        $this->assertInstanceOf('\test_component\courseformat\sectiondelegate', sectiondelegate::instance($sectioninfos[2]));
76
        $this->assertNull(sectiondelegate::instance($sectioninfos[3]));
77
    }
78
 
79
    /**
1441 ariadna 80
     * Test that the instance method returns null when the delegate class is disabled.
81
     *
82
     * @covers ::instance
83
     */
84
    public function test_instance_disabled(): void {
85
        global $DB;
86
        $this->resetAfterTest();
87
 
88
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 3]);
89
 
90
        // Section 2 has an existing delegate class.
91
        course_update_section(
92
            $course,
93
            $DB->get_record('course_sections', ['course' => $course->id, 'section' => 2]),
94
            [
95
                'component' => 'test_component',
96
                'itemid' => testsectiondelegate::DISABLEDITEMID,
97
            ]
98
        );
99
 
100
        $modinfo = get_fast_modinfo($course->id);
101
        $sectioninfos = $modinfo->get_section_info_all();
102
 
103
        $this->assertNull(sectiondelegate::instance($sectioninfos[2]));
104
    }
105
 
106
    /**
1 efrain 107
     * Test has_delegate_class().
108
     *
109
     * @covers ::has_delegate_class
110
     */
111
    public function test_has_delegate_class(): void {
112
        $this->assertFalse(sectiondelegate::has_delegate_class('missing_component'));
113
        $this->assertFalse(sectiondelegate::has_delegate_class('mod_label'));
114
        $this->assertTrue(sectiondelegate::has_delegate_class('test_component'));
115
    }
116
 
117
    /**
118
     * Test get_section_action_menu().
119
     *
120
     * @covers ::get_section_action_menu
121
     */
122
    public function test_get_section_action_menu(): void {
123
        global $DB, $PAGE;
124
 
125
        $this->resetAfterTest();
126
 
127
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
128
 
129
        $sectioninfo = formatactions::section($course)->create_delegated('test_component', 1);
130
 
1441 ariadna 131
        /** @var testsectiondelegate $delegated */
1 efrain 132
        $delegated = $sectioninfo->get_component_instance();
133
 
134
        $format = course_get_format($course);
135
 
136
        $outputclass = $format->get_output_classname('content\\section\\controlmenu');
1441 ariadna 137
        /** @var \core_courseformat\output\local\content\section\controlmenu $controlmenu */
1 efrain 138
        $controlmenu = new $outputclass($format, $sectioninfo);
139
        $renderer = $PAGE->get_renderer('format_' . $course->format);
140
        $sectionmenu = $controlmenu->get_action_menu($renderer);
141
 
142
        // When the delegate class returns the same action menu, calculated from the given $controlmenu.
143
        $result = $delegated->get_section_action_menu($format, $controlmenu, $renderer);
144
        // The $result and $sectionmenu are the same but can't be compared directly because they have different ids.
145
        $this->assertEquals(
146
            count($result->get_primary_actions()),
147
            count($sectionmenu->get_primary_actions()),
148
        );
149
        $this->assertEquals(
150
            count($result->get_secondary_actions()),
151
            count($sectionmenu->get_secondary_actions())
152
        );
153
        $this->assertEquals(
154
            $result->get_secondary_actions()[0]->url,
155
            $sectionmenu->get_secondary_actions()[0]->url
156
        );
157
 
158
        // When the delegated class returns an empty action menu.
159
        $delegated->set_section_action_menu(testsectiondelegate::MENUEMPTY);
160
        $result = $delegated->get_section_action_menu($format, $controlmenu, $renderer);
161
        // The $result and $sectionmenu are different.
162
        $this->assertNotEquals(
163
            count($result->get_secondary_actions()),
164
            count($sectionmenu->get_secondary_actions())
165
        );
166
 
167
        // When the delegated class return a null action menu.
168
        $delegated->set_section_action_menu(null);
169
        $result = $delegated->get_section_action_menu($format, $controlmenu, $renderer);
170
        $this->assertNull($result);
171
    }
1441 ariadna 172
 
173
    /**
174
     * Test get_parent_section().
175
     *
176
     * @covers ::get_parent_section
177
     */
178
    public function test_get_parent_section(): void {
179
        $this->resetAfterTest();
180
 
181
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
182
        $sectioninfo = formatactions::section($course)->create_delegated('test_component', 1);
183
 
184
        /** @var testsectiondelegate $delegated */
185
        $delegated = $sectioninfo->get_component_instance();
186
 
187
        $this->assertNull($delegated->get_parent_section());
188
    }
1 efrain 189
}