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