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
declare(strict_types=1);
18
 
19
namespace core_courseformat\external;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
use moodle_exception;
24
use stdClass;
25
 
26
global $CFG;
27
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
28
 
29
/**
30
 * Tests for the new_module class.
31
 *
32
 * @package    core_courseformat
33
 * @category   test
34
 * @copyright  2024 Mikel Martín <mikel@moodle.com>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 * @coversDefaultClass \core_courseformat\external\new_module
37
 */
38
final class new_module_test extends \externallib_advanced_testcase {
39
 
40
    /**
41
     * Setup to ensure that fixtures are loaded.
42
     */
43
    public static function setupBeforeClass(): void { // phpcs:ignore
44
        global $CFG;
45
 
46
        require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest.php');
47
        require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_output_course_format_state.php');
48
        require_once($CFG->dirroot . '/course/format/tests/fixtures/format_theunittest_stateactions.php');
49
    }
50
 
51
    /**
52
     * Test the webservice can execute the new_module action.
53
     *
54
     * @covers ::execute
55
     */
56
    public function test_execute(): void {
57
        $this->resetAfterTest();
58
 
59
        $modname = 'subsection';
60
        $manager = \core_plugin_manager::resolve_plugininfo_class('mod');
61
        $manager::enable_plugin($modname, 1);
62
 
63
        // Create a course with an activity.
64
        $course = $this->getDataGenerator()->create_course(['numsections' => 1]);
65
        $activity = $this->getDataGenerator()->create_module('book', ['course' => $course->id]);
66
        $targetsection = get_fast_modinfo($course->id)->get_section_info(1);
67
 
68
        $this->setAdminUser();
69
 
70
        // Execute course action.
71
        $results = json_decode(
72
            new_module::execute((int)$course->id, $modname, (int)$targetsection->id, (int)$activity->id),
73
        );
74
 
75
        // Check result.
76
        $cmupdate = $this->find_update_by_fieldname($results, 'put', 'cm', get_string('quickcreatename', 'mod_' . $modname));
77
        $this->assertNotEmpty($cmupdate);
78
        $this->assertEquals($modname, $cmupdate->fields->module);
79
        $this->assertEquals($targetsection->id, $cmupdate->fields->sectionid);
80
        $this->assertEquals(1, $cmupdate->fields->sectionnumber);
81
    }
82
 
83
    /**
84
     * Test the webservice can execute the new_module action with a format override.
85
     *
86
     * @covers ::execute
87
     */
88
    public function test_execute_with_format_override(): void {
89
        $this->resetAfterTest();
90
 
91
        $manager = \core_plugin_manager::resolve_plugininfo_class('mod');
92
        $manager::enable_plugin('subsection', 1);
93
 
94
        // Create a course.
95
        $course = $this->getDataGenerator()->create_course(['format' => 'theunittest', 'numsections' => 1, 'initsections' => 1]);
96
        $targetsection = get_fast_modinfo($course->id)->get_section_info(1);
97
 
98
        $this->setAdminUser();
99
 
100
        // Execute course action.
101
        $modname = 'subsection';
102
        $results = json_decode(
103
            new_module::execute((int)$course->id, $modname, (int)$targetsection->id),
104
        );
105
 
106
        // Some course formats doesn't have the renderer file, so a debugging message will be displayed.
107
        $this->assertDebuggingCalled();
108
 
109
        // Check result.
110
        $this->assertEmpty($results);
111
    }
112
 
113
    /**
114
     * Test the webservice can execute the new_module action with an invalid module.
115
     *
116
     * @covers ::execute
117
     */
118
    public function test_execute_with_invalid_module(): void {
119
        $this->resetAfterTest();
120
 
121
        // Create a course.
122
        $course = $this->getDataGenerator()->create_course(['numsections' => 1]);
123
        $targetsection = get_fast_modinfo($course->id)->get_section_info(1);
124
 
125
        $this->setAdminUser();
126
 
127
        // Expect exception. Book module doesn't support quickcreate feature.
128
        $this->expectException(moodle_exception::class);
129
 
130
        // Execute course action.
131
        $modname = 'book';
132
        new_module::execute((int)$course->id, $modname, (int)$targetsection->id);
133
    }
134
 
135
    /**
136
     * Helper methods to find a specific update in the updadelist.
137
     *
138
     * @param array $updatelist the update list
139
     * @param string $action the action to find
140
     * @param string $name the element name to find
141
     * @param string $fieldname the element identifiername
142
     * @return stdClass|null the object found, if any.
143
     */
144
    private function find_update_by_fieldname(
145
        array $updatelist,
146
        string $action,
147
        string $name,
148
        string $fieldname,
149
 
150
    ): ?stdClass {
151
        foreach ($updatelist as $update) {
152
            if ($update->action != $action || $update->name != $name) {
153
                continue;
154
            }
155
            if (!isset($update->fields->name)) {
156
                continue;
157
            }
158
            if ($update->fields->name == $fieldname) {
159
                return $update;
160
            }
161
        }
162
        return null;
163
    }
164
}