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 customfield_number\external;
20
 
21
use core_customfield_generator;
22
use core_external\external_api;
23
use moodle_exception;
24
use stdClass;
25
 
26
defined('MOODLE_INTERNAL') || die;
27
 
28
global $CFG;
29
 
30
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31
 
32
/**
33
 * Unit tests for the customfield_number\external\recalculate.
34
 *
35
 * @package    customfield_number
36
 * @category   external
37
 * @copyright  2024 Ilya Tregubov <ilya.tregubov@proton.me>
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 * @covers     \customfield_number\external\recalculate
40
 */
41
final class recalculate_test extends \externallib_advanced_testcase {
42
 
43
    /**
44
     * Tests when teacher can not edit locked field.
45
     */
46
    public function test_execute_no_permission(): void {
47
        global $DB;
48
 
49
        $this->resetAfterTest();
50
        [$course, $field] = $this->prepare_course();
51
        $configdata = [
52
            'fieldtype' => 'customfield_number\\local\\numberproviders\\nofactivities',
53
            'activitytypes' => ['assign', 'forum'],
54
            'locked' => 1,
55
        ];
56
        $field->set('configdata', json_encode($configdata));
57
        $field->save();
58
 
59
        $context = \context_course::instance($course->id);
60
        $roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
61
        $this->unassignUserCapability('moodle/course:changelockedcustomfields', $context->id, $roleid);
62
 
63
        $this->expectException(moodle_exception::class);
64
        $this->expectExceptionMessage(get_string('update'));
65
        recalculate::execute($field->get('id'), (int)$course->id);
66
    }
67
 
68
    /**
69
     * Tests when all data is valid.
70
     */
71
    public function test_execute(): void {
72
        $this->resetAfterTest();
73
        [$course, $field] = $this->prepare_course();
74
        $result = recalculate::execute($field->get('id'), (int)$course->id);
75
        $result = external_api::clean_returnvalue(recalculate::execute_returns(), $result);
76
        $this->assertEquals(3.0, $result['value']);
77
    }
78
 
79
    /**
80
     * Create a course with number custom field.
81
     * @return array An array with the course object and field object.
82
     */
83
    private function prepare_course(): array {
84
        global $DB;
85
 
86
        $course = $this->getDataGenerator()->create_course();
87
 
88
        // Add teacher to a course.
89
        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
90
        $teacher = $this->getDataGenerator()->create_user();
91
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleids['editingteacher']);
92
 
93
        $this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'name' => 'Assign1', 'visible' => 1]);
94
        $this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'name' => 'Assign2', 'visible' => 1]);
95
        $this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'name' => 'Assign3', 'visible' => 0]);
96
 
97
        $this->getDataGenerator()->create_module('quiz', ['course' => $course->id, 'name' => 'Quiz1', 'visible' => 1]);
98
        $this->getDataGenerator()->create_module('quiz', ['course' => $course->id, 'name' => 'Quiz2', 'visible' => 0]);
99
 
100
        $this->getDataGenerator()->create_module('forum', ['course' => $course->id, 'name' => 'Forum1', 'visible' => 1]);
101
 
102
        /** @var core_customfield_generator $generator */
103
        $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
104
 
105
        $category = $generator->create_category();
106
        $field = $generator->create_field(
107
            [
108
                'categoryid' => $category->get('id'),
109
                'shortname' => 'myfield', 'type' => 'number',
110
                'configdata' => [
111
                    'fieldtype' => 'customfield_number\\local\\numberproviders\\nofactivities',
112
                    'activitytypes' => ['assign', 'forum'],
113
                ],
114
            ]
115
        );
116
        $this->setUser($teacher);
117
 
118
        return [$course, $field];
119
    }
120
 
121
}