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
/**
18
 * External function test for get_user_attempts.
19
 *
20
 * @package    mod_h5pactivity
21
 * @category   external
22
 * @since      Moodle 3.11
23
 * @copyright  2020 Ilya Tregubov <ilya@moodle.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
 
27
namespace mod_h5pactivity\external;
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
global $CFG;
32
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
33
 
34
use mod_h5pactivity\local\manager;
35
use core_external\external_api;
36
use externallib_advanced_testcase;
37
 
38
/**
39
 * External function test for get_user_attempts.
40
 *
41
 * @package    mod_h5pactivity
42
 * @copyright  2020 Ilya Tregubov <ilya@moodle.com>
43
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44
 */
1441 ariadna 45
final class get_user_attempts_test extends externallib_advanced_testcase {
1 efrain 46
 
47
    /**
48
     * Test the behaviour of get_user_attempts getting more than one user at once.
49
     *
50
     * @dataProvider execute_multipleusers_data
51
     * @param string $loginuser the user which calls the webservice
52
     * @param string[] $participants the users to get the data
53
     * @param string[] $warnings the expected users with warnings
54
     * @param string[] $resultusers expected users in the resultusers
55
     */
56
    public function test_execute_multipleusers(string $loginuser, array $participants,
57
            array $warnings, array $resultusers): void {
58
        $this->resetAfterTest();
59
        $this->setAdminUser();
60
 
61
        $course = $this->getDataGenerator()->create_course();
62
        $activity = $this->getDataGenerator()->create_module('h5pactivity',
63
            ['course' => $course]);
64
 
65
        $manager = manager::create_from_instance($activity);
66
        $cm = $manager->get_coursemodule();
67
 
68
        $users = ['editingteacher' => $this->getDataGenerator()->create_and_enrol($course, 'editingteacher')];
69
 
70
        // Prepare users.
71
        foreach ($participants as $participant) {
72
            if ($participant == 'noenrolled') {
73
                $users[$participant] = $this->getDataGenerator()->create_user();
74
            } else {
75
                $users[$participant] = $this->getDataGenerator()->create_and_enrol($course, 'student');
76
            }
77
        }
78
 
79
        // Generate attempts (student1 with 1 attempt, student2 with 2 etc).
80
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_h5pactivity');
81
 
82
        $attemptcount = 1;
1441 ariadna 83
        $totalattempts = 0;
1 efrain 84
        foreach ($users as $key => $user) {
85
            if (($key == 'noattempts') || ($key == 'noenrolled') || ($key == 'editingteacher')) {
86
                $countattempts[$user->id] = 0;
87
            } else {
88
                $params = ['cmid' => $cm->id, 'userid' => $user->id];
89
                for ($i = 1; $i <= $attemptcount; $i++) {
90
                    $generator->create_content($activity, $params);
1441 ariadna 91
                    $totalattempts++;
1 efrain 92
                }
93
                $countattempts[$user->id] = $attemptcount;
94
                $attemptcount++;
95
            }
96
        }
97
 
98
        // Execute external method.
99
        $this->setUser($users[$loginuser]);
100
 
101
        if ($loginuser == 'student1') {
102
            $this->expectException('moodle_exception');
103
            $this->expectExceptionMessage('h5pactivity:reviewattempts required view attempts' .
104
                ' of all enrolled users');
105
        }
106
        $result = get_user_attempts::execute($activity->id);
107
        $result = external_api::clean_returnvalue(
108
            get_user_attempts::execute_returns(),
109
            $result
110
        );
111
 
1441 ariadna 112
        $this->assertEquals($totalattempts, $result['totalattempts']);
1 efrain 113
        $this->assertCount(count($warnings), $result['warnings']);
114
        // Teacher is excluded.
115
        $this->assertCount(count($resultusers), $result['usersattempts']);
116
 
117
        $expectedwarnings = [];
118
        foreach ($warnings as $warninguser) {
119
            $id = $users[$warninguser]->id;
120
            $expectedwarnings[$id] = $warninguser;
121
        }
122
 
123
        foreach ($result['warnings'] as $warning) {
124
            $this->assertEquals('user', $warning['item']);
125
            $this->assertEquals(1, $warning['warningcode']);
126
            $this->assertArrayHasKey($warning['itemid'], $expectedwarnings);
127
        }
128
 
129
        $expectedusers = [];
130
        foreach ($resultusers as $resultuser) {
131
            $id = $users[$resultuser]->id;
132
            $expectedusers[$id] = $resultuser;
133
        }
134
 
135
        foreach ($result['usersattempts'] as $usersattempts) {
136
            $this->assertArrayHasKey('userid', $usersattempts);
137
            $userid = $usersattempts['userid'];
138
            $this->assertArrayHasKey($userid, $expectedusers);
139
            $this->assertCount($countattempts[$userid], $usersattempts['attempts']);
140
            if ($countattempts[$userid]) {
141
                $this->assertArrayHasKey('scored', $usersattempts);
142
            }
143
        }
144
    }
145
 
146
    /**
147
     * Data provider for the test_execute_multipleusers.
148
     *
149
     * @return  array
150
     */
1441 ariadna 151
    public static function execute_multipleusers_data(): array {
1 efrain 152
        return [
153
            // Teacher checks.
154
            'Teacher checking students with attempts' => [
155
                'editingteacher',
156
                ['student1', 'student2', 'student3', 'student4', 'student5'],
157
                [],
158
                ['student1', 'student2', 'student3', 'student4', 'student5'],
159
            ],
160
            'Teacher checking 2 students with atempts and one not' => [
161
                'editingteacher',
162
                ['student1', 'student2', 'noattempts'],
163
                [],
164
                ['student1', 'student2', 'noattempts'],
165
            ],
166
            'Teacher checking no students' => [
167
                'editingteacher',
168
                [],
169
                [],
170
                [],
171
            ],
172
            'Teacher checking one student and a no enrolled user' => [
173
                'editingteacher',
174
                ['student1', 'noenrolled'],
175
                [],
176
                ['student1'],
177
            ],
178
 
179
            // Permission check.
180
            'Student checking attempts and another user' => [
181
                'student1',
182
                ['student1', 'student2'],
183
                ['student2'],
184
                ['student1'],
185
            ],
186
        ];
187
    }
188
 
189
    /**
190
     * Data provider for {@see test_execute_with_sortorder}
191
     *
192
     * @return array[]
193
     */
1441 ariadna 194
    public static function execute_with_sortorder(): array {
1 efrain 195
        return [
196
            'Sort by id' => ['id', ['user01', 'user02']],
197
            'Sort by id desc' => ['id desc', ['user02', 'user01']],
198
            'Sort by id asc' => ['id asc', ['user01', 'user02']],
199
            'Sort by firstname' => ['firstname', ['user01', 'user02']],
200
            'Sort by firstname desc' => ['firstname desc', ['user02', 'user01']],
201
            'Sort by firstname asc' => ['firstname asc', ['user01', 'user02']],
202
            'Sort by lastname' => ['lastname', ['user02', 'user01']],
203
            'Sort by lastname desc' => ['lastname desc', ['user01', 'user02']],
204
            'Sort by lastname asc' => ['lastname asc', ['user02', 'user01']],
205
            // Edge cases (should fall back to default).
206
            'Sort by empty string' => ['', ['user01', 'user02']],
207
            'Sort by invalid field' => ['invalid', ['user01', 'user02']],
208
        ];
209
    }
210
 
211
    /**
212
     * Test external execute method with sortorder
213
     *
214
     * @param string $sortorder
215
     * @param string[] $expectedorder
216
     *
217
     * @dataProvider execute_with_sortorder
218
     */
219
    public function test_execute_with_sortorder(string $sortorder, array $expectedorder): void {
220
        $this->resetAfterTest();
221
        $this->setAdminUser();
222
 
223
        // Create course, module.
224
        $course = $this->getDataGenerator()->create_course();
225
        $module = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
226
 
227
        // Couple of enrolled users in the course.
228
        $users['user01'] = $this->getDataGenerator()->create_and_enrol($course, 'student', [
229
            'username' => 'user01',
230
            'firstname' => 'Adam',
231
            'lastname' => 'Zebra',
232
        ]);
233
        $users['user02'] = $this->getDataGenerator()->create_and_enrol($course, 'student', [
234
            'username' => 'user02',
235
            'firstname' => 'Zoe',
236
            'lastname' => 'Apples',
237
        ]);
238
 
239
        $result = external_api::clean_returnvalue(
240
            get_user_attempts::execute_returns(),
241
            get_user_attempts::execute($module->id, $sortorder)
242
        );
243
 
244
        // Map expected order of usernames to user IDs.
245
        $expectedorderbyuserid = array_map(static function(string $username) use ($users): int {
246
            return $users[$username]->id;
247
        }, $expectedorder);
248
 
249
        // The order should match the ordering of user attempt user IDs.
250
        $this->assertEquals($expectedorderbyuserid, array_column($result['usersattempts'], 'userid'));
251
    }
252
}