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 mod_subsection\courseformat;
18
 
19
use mod_subsection\courseformat\sectiondelegate as testsectiondelegatemodule;
20
use section_info;
21
use cm_info;
22
use stdClass;
23
 
24
/**
25
 * Section delegate module tests.
26
 *
27
 * @package    mod_subsection
28
 * @copyright  2024 Mikel Martín <mikel@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 * @covers     \core_courseformat\sectiondelegatemodule
31
 * @coversDefaultClass \core_courseformat\sectiondelegatemodule
32
 */
33
final class sectiondelegatemodule_test extends \advanced_testcase {
34
 
35
    /**
36
     * Test get_parent_section.
37
     *
38
     * @covers ::get_parent_section
39
     */
40
    public function test_get_parent_section(): void {
41
        $this->resetAfterTest();
42
 
43
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 2]);
44
        $module = $this->getDataGenerator()->create_module('subsection', (object)['course' => $course->id, 'section' => 2]);
45
 
46
        // Get the section info for the delegated section.
47
        $sectioninfo = get_fast_modinfo($course)->get_section_info_by_component('mod_subsection', $module->id);
48
 
49
        /** @var testsectiondelegatemodule */
50
        $delegated = sectiondelegate::instance($sectioninfo);
51
 
52
        $parentsectioninfo = $delegated->get_parent_section();
53
 
54
        $this->assertInstanceOf(section_info::class, $parentsectioninfo);
55
        $this->assertEquals(2, $parentsectioninfo->sectionnum);
56
    }
57
 
58
    /**
59
     * Test get_cm.
60
     *
61
     * @covers ::get_cm
62
     */
63
    public function test_get_cm(): void {
64
        $this->resetAfterTest();
65
 
66
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
67
        $module = $this->getDataGenerator()->create_module('subsection', (object)['course' => $course->id, 'section' => 1]);
68
 
69
        // Get the section info for the delegated section.
70
        $sectioninfo = get_fast_modinfo($course)->get_section_info_by_component('mod_subsection', $module->id);
71
 
72
        /** @var testsectiondelegatemodule */
73
        $delegated = sectiondelegate::instance($sectioninfo);
74
 
75
        $delegatedsectioncm = $delegated->get_cm();
76
 
77
        $this->assertInstanceOf(cm_info::class, $delegatedsectioncm);
78
        $this->assertEquals($module->id, $delegatedsectioncm->instance);
79
    }
80
 
81
    /**
82
     * Test get_course.
83
     *
84
     * @covers ::get_course
85
     */
86
    public function test_get_course(): void {
87
        $this->resetAfterTest();
88
 
89
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 1]);
90
        $module = $this->getDataGenerator()->create_module('subsection', (object)['course' => $course->id, 'section' => 1]);
91
 
92
        // Get the section info for the delegated section.
93
        $sectioninfo = get_fast_modinfo($course)->get_section_info_by_component('mod_subsection', $module->id);
94
 
95
        /** @var testsectiondelegatemodule */
96
        $delegated = sectiondelegate::instance($sectioninfo);
97
 
98
        $delegatedsectioncourse = $delegated->get_course();
99
 
100
        $this->assertInstanceOf(stdClass::class, $delegatedsectioncourse);
101
        $this->assertEquals($course->id, $delegatedsectioncourse->id);
102
    }
103
 
104
    public function test_instance_plugin_disabled(): void {
105
        $this->resetAfterTest();
106
 
107
        $course = $this->getDataGenerator()->create_course(['format' => 'topics', 'numsections' => 2]);
108
        $module = $this->getDataGenerator()->create_module(
109
            'subsection',
110
            (object) ['course' => $course->id, 'section' => 2]
111
        );
112
 
113
        // Get the section info for the delegated section.
114
        $sectioninfo = get_fast_modinfo($course)->get_section_info_by_component('mod_subsection', $module->id);
115
 
116
        /** @var testsectiondelegatemodule $delegated */
117
        $delegated = sectiondelegate::instance($sectioninfo);
118
        $this->assertTrue($delegated->is_enabled());
119
 
120
        // Disabling the plugin should disable the delegate.
121
        $manager = \core_plugin_manager::resolve_plugininfo_class('mod');
122
        $manager::enable_plugin('subsection', 0);
123
        rebuild_course_cache($course->id, true);
124
 
125
        $sectioninfo = get_fast_modinfo($course)->get_section_info_by_component('mod_subsection', $module->id);
126
 
127
        /** @var testsectiondelegatemodule $delegated */
128
        $delegated = sectiondelegate::instance($sectioninfo);
129
        // Delegated from a disabled plugin are considered orphaned, not delegated.
130
        $this->assertNull($delegated);
131
 
132
        // Section delegate should not be created directly but we do it
133
        // here to validate the is_enabled() method neverthless.
134
        $delegatedinstance = new sectiondelegate($sectioninfo);
135
        $this->assertFalse($delegatedinstance->is_enabled());
136
    }
137
}