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 create_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\create_module
37
 */
38
final class create_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 create_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(create_module::execute((int)$course->id, $modname, (int)$targetsection->id, (int)$activity->id));
72
        $this->assertDebuggingCalled();
73
 
74
        // Check result.
75
        $cmupdate = $this->find_update_by_fieldname($results, 'put', 'cm', get_string('quickcreatename', 'mod_' . $modname));
76
        $this->assertNotEmpty($cmupdate);
77
        $this->assertEquals($modname, $cmupdate->fields->module);
78
        $this->assertEquals($targetsection->id, $cmupdate->fields->sectionnumber);
79
    }
80
 
81
    /**
82
     * Test the webservice can execute the create_module action with a format override.
83
     *
84
     * @covers ::execute
85
     */
86
    public function test_execute_with_format_override(): void {
87
        $this->resetAfterTest();
88
 
89
        // Create a course.
90
        $course = $this->getDataGenerator()->create_course(['format' => 'theunittest', 'numsections' => 1, 'initsections' => 1]);
91
        $targetsection = get_fast_modinfo($course->id)->get_section_info(1);
92
 
93
        $this->setAdminUser();
94
 
95
        // Execute course action.
96
        $modname = 'subsection';
97
        $results = json_decode(create_module::execute((int)$course->id, $modname, (int)$targetsection->id));
98
 
99
        // Some course formats doesn't have the renderer file, so a debugging message will be displayed.
100
        $this->assertDebuggingCalled();
101
 
102
        // Check result.
103
        $this->assertEmpty($results);
104
    }
105
 
106
    /**
107
     * Test the webservice can execute the create_module action with an invalid module.
108
     *
109
     * @covers ::execute
110
     */
111
    public function test_execute_with_invalid_module(): void {
112
        $this->resetAfterTest();
113
 
114
        // Create a course.
115
        $course = $this->getDataGenerator()->create_course(['numsections' => 1]);
116
        $targetsection = get_fast_modinfo($course->id)->get_section_info(1);
117
 
118
        $this->setAdminUser();
119
 
120
        // Expect exception. Book module doesn't support quickcreate feature.
121
        $this->expectException(moodle_exception::class);
122
 
123
        // Execute course action.
124
        $modname = 'book';
125
        create_module::execute((int)$course->id, $modname, (int)$targetsection->id);
126
        $this->assertDebuggingCalled();
127
    }
128
 
129
    /**
130
     * Helper methods to find a specific update in the updadelist.
131
     *
132
     * @param array $updatelist the update list
133
     * @param string $action the action to find
134
     * @param string $name the element name to find
135
     * @param string $fieldname the element identifiername
136
     * @return stdClass|null the object found, if any.
137
     */
138
    private function find_update_by_fieldname(
139
        array $updatelist,
140
        string $action,
141
        string $name,
142
        string $fieldname,
143
 
144
    ): ?stdClass {
145
        foreach ($updatelist as $update) {
146
            if ($update->action != $action || $update->name != $name) {
147
                continue;
148
            }
149
            if (!isset($update->fields->name)) {
150
                continue;
151
            }
152
            if ($update->fields->name == $fieldname) {
153
                return $update;
154
            }
155
        }
156
        return null;
157
    }
158
}