Proyectos de Subversion Moodle

Rev

Rev 11 | | 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_grades\external;
18
 
19
defined('MOODLE_INTERNAL') || die;
20
 
21
global $CFG;
22
 
23
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
24
 
25
/**
26
 * Unit tests for the core_grades\external\get_feedback webservice.
27
 *
28
 * @package    core_grades
29
 * @category   external
30
 * @copyright  2023 Kevin Percy <kevin.percy@moodle.com>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @since      Moodle 4.2
1441 ariadna 33
 * @covers \core_grades\external\get_feedback
1 efrain 34
 */
1441 ariadna 35
final class get_feedback_test extends \externallib_advanced_testcase {
1 efrain 36
 
37
    /**
38
     * Test get_feedback.
39
     *
40
     * @dataProvider get_feedback_provider
41
     * @param string|null $feedback The feedback text added for the grade item.
42
     * @param array $expected The expected feedback data.
43
     * @return void
44
     */
11 efrain 45
    public function test_get_feedback(?string $feedback, array $expected): void {
1 efrain 46
 
47
        $this->resetAfterTest(true);
48
        $course = $this->getDataGenerator()->create_course();
49
        $user = $this->getDataGenerator()->create_user(['firstname' => 'John', 'lastname' => 'Doe',
50
            'email' => 'johndoe@example.com']);
51
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
52
        $gradeitem = $this->getDataGenerator()->create_grade_item(['itemname' => 'Grade item 1',
53
            'courseid' => $course->id]);
54
 
55
        $gradegradedata = [
56
            'itemid' => $gradeitem->id,
57
            'userid' => $user->id,
58
        ];
59
 
60
        if ($feedback) {
61
            $gradegradedata['feedback'] = $feedback;
62
        }
63
 
64
        $this->getDataGenerator()->create_grade_grade($gradegradedata);
65
        $this->setAdminUser();
66
 
67
        $feedbackdata = get_feedback::execute($course->id, $user->id, $gradeitem->id);
68
 
69
        $this->assertEquals($expected['feedbacktext'], $feedbackdata['feedbacktext']);
70
        $this->assertEquals($expected['title'], $feedbackdata['title']);
71
        $this->assertEquals($expected['fullname'], $feedbackdata['fullname']);
72
        $this->assertEquals($expected['additionalfield'], $feedbackdata['additionalfield']);
73
    }
74
 
75
    /**
76
     * Data provider for test_get_feedback().
77
     *
78
     * @return array
79
     */
1441 ariadna 80
    public static function get_feedback_provider(): array {
1 efrain 81
        return [
82
            'Return when feedback is set.' => [
83
                'Test feedback',
84
                [
85
                    'feedbacktext' => 'Test feedback',
86
                    'title' => 'Grade item 1',
87
                    'fullname' => 'John Doe',
88
                    'additionalfield' => 'johndoe@example.com'
89
                ]
90
            ],
91
            'Return when feedback is not set.' => [
92
                null,
93
                [
94
                    'feedbacktext' => null,
95
                    'title' => 'Grade item 1',
96
                    'fullname' => 'John Doe',
97
                    'additionalfield' => 'johndoe@example.com'
98
                ]
99
            ]
100
        ];
101
    }
102
 
103
    /**
104
     * Test get_feedback with invalid requests.
105
     *
106
     * @dataProvider get_feedback_invalid_request_provider
107
     * @param string $loggeduserrole The role of the logged user.
108
     * @param bool $feedbacknotincourse Whether to request a feedback for a grade item which is not a part of the course.
109
     * @param array $expectedexception The expected exception.
110
     * @return void
111
     */
112
    public function test_get_feedback_invalid_request(string $loggeduserrole, bool $feedbacknotincourse,
11 efrain 113
            array $expectedexception = []): void {
1 efrain 114
 
115
        $this->resetAfterTest(true);
116
        // Create a course with a user and a grade item.
117
        $course = $this->getDataGenerator()->create_course();
118
        $user = $this->getDataGenerator()->create_user();
119
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
120
        $gradeitem = $this->getDataGenerator()->create_grade_item(['courseid' => $course->id]);
121
        // Add feedback for the grade item in course.
122
        $gradegradedata = [
123
            'itemid' => $gradeitem->id,
124
            'userid' => $user->id,
125
            'feedback' => 'Test feedback',
126
        ];
127
 
128
        $this->getDataGenerator()->create_grade_grade($gradegradedata);
129
        // Set the current user as specified.
130
        if ($loggeduserrole === 'user') {
131
            $this->setUser($user);
132
        } else if ($loggeduserrole === 'guest') {
133
            $this->setGuestUser();
134
        } else {
135
            $this->setAdminUser();
136
        }
137
 
138
        if ($feedbacknotincourse) { // Create a new course which will be later used in the feedback request call.
139
            $course = $this->getDataGenerator()->create_course();
140
        }
141
 
142
        $this->expectException($expectedexception['exceptionclass']);
143
 
144
        if (!empty($expectedexception['exceptionmessage'])) {
145
            $this->expectExceptionMessage($expectedexception['exceptionmessage']);
146
        }
147
 
148
        get_feedback::execute($course->id, $user->id, $gradeitem->id);
149
    }
150
 
151
    /**
152
     * Data provider for test_get_feedback_invalid_request().
153
     *
154
     * @return array
155
     */
1441 ariadna 156
    public static function get_feedback_invalid_request_provider(): array {
1 efrain 157
        return [
158
            'Logged user does not have permissions to view feedback.' => [
159
                'user',
160
                false,
161
                ['exceptionclass' => \required_capability_exception::class]
162
            ],
163
            'Guest user cannot view feedback.' => [
164
                'guest',
165
                false,
166
                ['exceptionclass' => \require_login_exception::class]
167
            ],
168
            'Request feedback for a grade item which is not a part of the course.' => [
169
                'admin',
170
                true,
171
                [
172
                    'exceptionclass' => \invalid_parameter_exception::class,
173
                    'exceptionmessage' => 'Course ID and item ID mismatch',
174
                ]
175
            ]
176
        ];
177
    }
178
}