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\local;
18
use ReflectionMethod;
19
use section_info;
20
use cm_info;
21
 
22
/**
23
 * Base format actions class tests.
24
 *
25
 * @package    core_courseformat
26
 * @copyright  2023 Ferran Recio <ferran@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @coversDefaultClass \core_courseformat\base
29
 */
30
class baseactions_test extends \advanced_testcase {
31
    /**
32
     * Setup to ensure that fixtures are loaded.
33
     */
34
    public static function setUpBeforeClass(): void {
35
        global $CFG;
36
        require_once($CFG->dirroot . '/course/lib.php');
37
    }
38
 
39
    /**
40
     * Get the reflection method for a base class instance.
41
     * @param baseactions $baseinstance
42
     * @param string $methodname
43
     * @return ReflectionMethod
44
     */
45
    private function get_base_reflection_method(baseactions $baseinstance, string $methodname): ReflectionMethod {
46
        $reflectionclass = new \reflectionclass($baseinstance);
47
        $method = $reflectionclass->getMethod($methodname);
48
        return $method;
49
    }
50
 
51
    /**
52
     * Test for get_instance static method.
53
     * @covers ::get_format
54
     */
55
    public function test_get_format(): void {
56
        global $DB;
57
        $this->resetAfterTest();
58
 
59
        $course = $this->getDataGenerator()->create_course(['format' => 'topics']);
60
 
61
        $baseactions = new baseactions($course);
62
        $method = $this->get_base_reflection_method($baseactions, 'get_format');
63
 
64
        $format = $method->invoke($baseactions);
65
        $this->assertEquals('topics', $format->get_format());
66
        $this->assertEquals('format_topics', $format::class);
67
 
68
        // Format should be always the most updated one.
69
        $course->format = 'weeks';
70
        $DB->update_record('course', $course);
71
 
72
        $format = $method->invoke($baseactions);
73
        $this->assertEquals('weeks', $format->get_format());
74
        $this->assertEquals('format_weeks', $format::class);
75
    }
76
 
77
    /**
78
     * Test for get_instance static method.
79
     * @covers ::get_section_info
80
     */
81
    public function test_get_section_info(): void {
82
        $this->resetAfterTest();
83
 
84
        $course = $this->getDataGenerator()->create_course(
85
            ['format' => 'topics', 'numsections' => 4],
86
            ['createsections' => true]
87
        );
88
 
89
        $modinfo = get_fast_modinfo($course->id);
90
        $originalsection = $modinfo->get_section_info(1);
91
 
92
        $baseactions = new baseactions($course);
93
        $method = $this->get_base_reflection_method($baseactions, 'get_section_info');
94
 
95
        $sectioninfo = $method->invoke($baseactions, $originalsection->id);
96
        $this->assertInstanceOf(section_info::class, $sectioninfo);
97
        $this->assertEquals($originalsection->id, $sectioninfo->id);
98
        $this->assertEquals($originalsection->section, $sectioninfo->section);
99
 
100
        // Section info should be always the most updated one.
101
        course_update_section($course, $originalsection, (object)['name' => 'New name']);
102
        move_section_to($course, 1, 3);
103
 
104
        $sectioninfo = $method->invoke($baseactions, $originalsection->id);
105
        $this->assertInstanceOf(section_info::class, $sectioninfo);
106
        $this->assertEquals($originalsection->id, $sectioninfo->id);
107
        $this->assertEquals(3, $sectioninfo->section);
108
 
109
        $this->assertEquals('New name', $sectioninfo->name);
110
    }
111
 
112
    /**
113
     * Test for get_instance static method.
114
     * @covers ::get_cm_info
115
     */
116
    public function test_get_cm_info(): void {
117
        global $DB;
118
        $this->resetAfterTest();
119
        $this->setAdminUser();
120
 
121
        $course = $this->getDataGenerator()->create_course(
122
            ['format' => 'topics', 'numsections' => 4],
123
            ['createsections' => true]
124
        );
125
        $activity = $this->getDataGenerator()->create_module('label', ['course' => $course->id]);
126
 
127
        $modinfo = get_fast_modinfo($course->id);
128
        $destinationsection = $modinfo->get_section_info(3);
129
        $originalcm = $modinfo->get_cm($activity->cmid);
130
 
131
        $baseactions = new baseactions($course);
132
        $method = $this->get_base_reflection_method($baseactions, 'get_cm_info');
133
 
134
        $cm = $method->invoke($baseactions, $originalcm->id);
135
        $this->assertInstanceOf(cm_info::class, $cm);
136
        $this->assertEquals($originalcm->id, $cm->id);
137
        $this->assertEquals($originalcm->sectionnum, $cm->sectionnum);
138
        $this->assertEquals($originalcm->name, $cm->name);
139
 
140
        // CM info should be always the most updated one.
141
        moveto_module($originalcm, $destinationsection);
142
 
143
        $cm = $method->invoke($baseactions, $originalcm->id);
144
        $this->assertInstanceOf(cm_info::class, $cm);
145
        $this->assertEquals($originalcm->id, $cm->id);
146
        $this->assertEquals($destinationsection->section, $cm->sectionnum);
147
    }
148
}