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
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
 */
30
class sectiondelegate_test extends \advanced_testcase {
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');
38
    }
39
 
40
    /**
41
     * Test that the instance method returns the correct class.
42
     * @covers ::instance
43
     */
44
    public function test_instance(): void {
45
        global $DB;
46
        $this->resetAfterTest();
47
 
48
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 3]);
49
 
50
        // Section 2 has an existing delegate class.
51
        course_update_section(
52
            $course,
53
            $DB->get_record('course_sections', ['course' => $course->id, 'section' => 2]),
54
            [
55
                'component' => 'test_component',
56
                'itemid' => 1,
57
            ]
58
        );
59
 
60
        // Section 3 has a missing delegate class.
61
        course_update_section(
62
            $course,
63
            $DB->get_record('course_sections', ['course' => $course->id, 'section' => 3]),
64
            [
65
                'component' => 'missing_component',
66
                'itemid' => 1,
67
            ]
68
        );
69
 
70
        $modinfo = get_fast_modinfo($course->id);
71
        $sectioninfos = $modinfo->get_section_info_all();
72
 
73
        $this->assertNull(sectiondelegate::instance($sectioninfos[1]));
74
        $this->assertInstanceOf('\test_component\courseformat\sectiondelegate', sectiondelegate::instance($sectioninfos[2]));
75
        $this->assertNull(sectiondelegate::instance($sectioninfos[3]));
76
    }
77
 
78
    /**
79
     * Test has_delegate_class().
80
     *
81
     * @covers ::has_delegate_class
82
     */
83
    public function test_has_delegate_class(): void {
84
        $this->assertFalse(sectiondelegate::has_delegate_class('missing_component'));
85
        $this->assertFalse(sectiondelegate::has_delegate_class('mod_label'));
86
        $this->assertTrue(sectiondelegate::has_delegate_class('test_component'));
87
    }
88
 
89
    /**
90
     * Test get_section_action_menu().
91
     *
92
     * @covers ::get_section_action_menu
93
     */
94
    public function test_get_section_action_menu(): void {
95
        global $DB, $PAGE;
96
 
97
        $this->resetAfterTest();
98
 
99
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
100
 
101
        $sectioninfo = formatactions::section($course)->create_delegated('test_component', 1);
102
 
103
        /** @var testsectiondelegate */
104
        $delegated = $sectioninfo->get_component_instance();
105
 
106
        $format = course_get_format($course);
107
 
108
        $outputclass = $format->get_output_classname('content\\section\\controlmenu');
109
        /** @var \core_courseformat\output\local\content\section\controlmenu */
110
        $controlmenu = new $outputclass($format, $sectioninfo);
111
        $renderer = $PAGE->get_renderer('format_' . $course->format);
112
        $sectionmenu = $controlmenu->get_action_menu($renderer);
113
 
114
        // When the delegate class returns the same action menu, calculated from the given $controlmenu.
115
        $result = $delegated->get_section_action_menu($format, $controlmenu, $renderer);
116
        // The $result and $sectionmenu are the same but can't be compared directly because they have different ids.
117
        $this->assertEquals(
118
            count($result->get_primary_actions()),
119
            count($sectionmenu->get_primary_actions()),
120
        );
121
        $this->assertEquals(
122
            count($result->get_secondary_actions()),
123
            count($sectionmenu->get_secondary_actions())
124
        );
125
        $this->assertEquals(
126
            $result->get_secondary_actions()[0]->url,
127
            $sectionmenu->get_secondary_actions()[0]->url
128
        );
129
 
130
        // When the delegated class returns an empty action menu.
131
        $delegated->set_section_action_menu(testsectiondelegate::MENUEMPTY);
132
        $result = $delegated->get_section_action_menu($format, $controlmenu, $renderer);
133
        // The $result and $sectionmenu are different.
134
        $this->assertNotEquals(
135
            count($result->get_secondary_actions()),
136
            count($sectionmenu->get_secondary_actions())
137
        );
138
 
139
        // When the delegated class return a null action menu.
140
        $delegated->set_section_action_menu(null);
141
        $result = $delegated->get_section_action_menu($format, $controlmenu, $renderer);
142
        $this->assertNull($result);
143
    }
144
}