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
/**
18
 * External rating functions unit tests
19
 *
20
 * @package    core_rating
21
 * @category   external
22
 * @copyright  2015 Costantino Cito <ccito@cvaconsulting.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace core_rating;
27
 
28
use core_external\external_api;
29
use core_rating_external;
30
use externallib_advanced_testcase;
31
 
32
defined('MOODLE_INTERNAL') || die();
33
 
34
global $CFG;
35
 
36
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
37
require_once($CFG->dirroot . '/rating/lib.php');
38
 
39
/**
40
 * External rating functions unit tests
41
 *
42
 * @package    core_rating
43
 * @category   external
44
 * @copyright  2015 Costantino Cito <ccito@cvaconsulting.com>
45
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46
 */
1441 ariadna 47
final class externallib_test extends externallib_advanced_testcase {
1 efrain 48
 
49
    /** @var \stdClass course record. */
50
    protected $course;
51
 
52
    /** @var \stdClass user record. */
53
    protected $student1;
54
 
55
    /** @var \stdClass user record. */
56
    protected $teacher1;
57
 
58
    /** @var \stdClass user record. */
59
    protected $student2;
60
 
61
    /** @var \stdClass user record. */
62
    protected $teacher2;
63
 
64
    /** @var \stdClass user record. */
65
    protected $student3;
66
 
67
    /** @var \stdClass user record. */
68
    protected $teacher3;
69
 
70
    /** @var \stdClass activity record. */
71
    protected $forum;
72
 
73
    /** @var \stdClass activity record. */
74
    protected $discussion;
75
 
76
    /** @var int context instance ID. */
77
    protected $contextid;
78
 
79
    /** @var \stdClass forum post. */
80
    protected $post;
81
 
82
    /** @var \stdClass a fieldset object, false or exception if error not found. */
83
    protected $studentrole;
84
 
85
    /** @var \stdClass a fieldset object, false or exception if error not found. */
86
    protected $teacherrole;
87
 
88
    /*
89
     * Set up for every test
90
     */
91
    public function setUp(): void {
92
        global $DB;
1441 ariadna 93
        parent::setUp();
1 efrain 94
        $this->resetAfterTest();
95
 
96
        $this->course = self::getDataGenerator()->create_course();
97
        $this->student1 = $this->getDataGenerator()->create_user();
98
        $this->student2 = $this->getDataGenerator()->create_user();
99
        $this->teacher1 = $this->getDataGenerator()->create_user();
100
        $this->teacher2 = $this->getDataGenerator()->create_user();
101
        $this->teacher3 = $this->getDataGenerator()->create_user();
102
        $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
103
        $this->teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
104
        unassign_capability('moodle/site:accessallgroups', $this->teacherrole->id);
105
 
106
        $this->getDataGenerator()->enrol_user($this->student1->id, $this->course->id, $this->studentrole->id);
107
        $this->getDataGenerator()->enrol_user($this->student2->id, $this->course->id, $this->studentrole->id);
108
        $this->getDataGenerator()->enrol_user($this->teacher1->id, $this->course->id, $this->teacherrole->id);
109
        $this->getDataGenerator()->enrol_user($this->teacher2->id, $this->course->id, $this->teacherrole->id);
110
        $this->getDataGenerator()->enrol_user($this->teacher3->id, $this->course->id, $this->teacherrole->id);
111
 
112
        // Create the forum.
113
        $record = new \stdClass();
114
        $record->introformat = FORMAT_HTML;
115
        $record->course = $this->course->id;
116
        // Set Aggregate type = Average of ratings.
117
        $record->assessed = RATING_AGGREGATE_AVERAGE;
118
        $record->scale = 100;
119
        $this->forum = self::getDataGenerator()->create_module('forum', $record);
120
 
121
        $this->contextid = \context_module::instance($this->forum->cmid)->id;
122
 
123
        // Add discussion to the forums.
124
        $record = new \stdClass();
125
        $record->course = $this->course->id;
126
        $record->userid = $this->student1->id;
127
        $record->forum = $this->forum->id;
128
        $this->discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
129
        // Retrieve the first post.
130
        $this->post = $DB->get_record('forum_posts', array('discussion' => $this->discussion->id));
131
    }
132
 
133
    /**
134
     * Test get_item_ratings
135
     */
11 efrain 136
    public function test_get_item_ratings(): void {
1 efrain 137
        global $DB;
138
 
139
        // Rete the discussion as teacher1.
140
        $rating1 = new \stdClass();
141
        $rating1->contextid = $this->contextid;
142
        $rating1->component = 'mod_forum';
143
        $rating1->ratingarea = 'post';
144
        $rating1->itemid = $this->post->id;
145
        $rating1->rating = 90;
146
        $rating1->scaleid = 100;
147
        $rating1->userid = $this->teacher1->id;
148
        $rating1->timecreated = time();
149
        $rating1->timemodified = time();
150
        $rating1->id = $DB->insert_record('rating', $rating1);
151
 
152
        // Rete the discussion as teacher2.
153
        $rating2 = new \stdClass();
154
        $rating2->contextid = $this->contextid;
155
        $rating2->component = 'mod_forum';
156
        $rating2->ratingarea = 'post';
157
        $rating2->itemid = $this->post->id;
158
        $rating2->rating = 95;
159
        $rating2->scaleid = 100;
160
        $rating2->userid = $this->teacher2->id;
161
        $rating2->timecreated = time() + 1;
162
        $rating2->timemodified = time() + 1;
163
        $rating2->id = $DB->insert_record('rating', $rating2);
164
 
165
        // Delete teacher2, we must still receive the ratings.
166
        delete_user($this->teacher2);
167
 
168
        // Teachers can see all the ratings.
169
        $this->setUser($this->teacher1);
170
 
171
        $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, '');
172
        // We need to execute the return values cleaning process to simulate the web service server.
173
        $ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings);
174
        $this->assertCount(2, $ratings['ratings']);
175
 
176
        $indexedratings = array();
177
        foreach ($ratings['ratings'] as $rating) {
178
            $indexedratings[$rating['id']] = $rating;
179
        }
180
        $this->assertEquals($rating1->rating.' / '.$rating1->scaleid, $indexedratings[$rating1->id]['rating']);
181
        $this->assertEquals($rating2->rating.' / '.$rating2->scaleid, $indexedratings[$rating2->id]['rating']);
182
 
183
        $this->assertEquals($rating1->userid, $indexedratings[$rating1->id]['userid']);
184
        $this->assertEquals($rating2->userid, $indexedratings[$rating2->id]['userid']);
185
 
186
        // Student can see ratings.
187
        $this->setUser($this->student1);
188
 
189
        $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, '');
190
        // We need to execute the return values cleaning process to simulate the web service server.
191
        $ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings);
192
        $this->assertCount(2, $ratings['ratings']);
193
 
194
        // Invalid item.
195
        try {
196
            $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', 0, 100, '');
197
            $this->fail('Exception expected due invalid itemid.');
198
        } catch (\moodle_exception $e) {
199
            $this->assertEquals('invalidrecord', $e->errorcode);
200
        }
201
 
202
        // Invalid area.
203
        try {
204
            $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'xyz', $this->post->id, 100, '');
205
            $this->fail('Exception expected due invalid rating area.');
206
        } catch (\moodle_exception $e) {
207
            $this->assertEquals('invalidratingarea', $e->errorcode);
208
        }
209
 
210
        // Invalid context. invalid_parameter_exception.
211
        try {
212
            $ratings = core_rating_external::get_item_ratings('module', 0, 'mod_forum', 'post', $this->post->id, 100, '');
213
            $this->fail('Exception expected due invalid context.');
214
        } catch (\invalid_parameter_exception $e) {
215
            $this->assertEquals('invalidparameter', $e->errorcode);
216
        }
217
 
218
        // Test for groupmode.
219
        set_coursemodule_groupmode($this->forum->cmid, SEPARATEGROUPS);
220
        $group = $this->getDataGenerator()->create_group(array('courseid' => $this->course->id));
221
        groups_add_member($group, $this->teacher1);
222
 
223
        $this->discussion->groupid = $group->id;
224
        $DB->update_record('forum_discussions', $this->discussion);
225
 
226
        // Error for teacher3 and 2 ratings for teacher1 should be returned.
227
        $this->setUser($this->teacher1);
228
        $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, '');
229
        // We need to execute the return values cleaning process to simulate the web service server.
230
        $ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings);
231
        $this->assertCount(2, $ratings['ratings']);
232
 
233
        $this->setUser($this->teacher3);
234
        try {
235
            $ratings = core_rating_external::get_item_ratings('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, '');
236
            $this->fail('Exception expected due invalid group permissions.');
237
        } catch (\moodle_exception $e) {
238
            $this->assertEquals('noviewrate', $e->errorcode);
239
        }
240
 
241
    }
242
 
243
    /**
244
     * Test add_rating
245
     */
11 efrain 246
    public function test_add_rating(): void {
1 efrain 247
        $this->setUser($this->teacher1);
248
 
249
        // First rating of 50.
250
        $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100,
251
                                                    50, $this->student1->id, RATING_AGGREGATE_AVERAGE);
252
        // We need to execute the return values cleaning process to simulate the web service server.
253
        $rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating);
254
        $this->assertTrue($rating['success']);
255
        $this->assertEquals(50, $rating['aggregate']);
256
        $this->assertEquals(1, $rating['count']);
257
 
258
        // New different rate (it will replace the existing one).
259
        $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100,
260
                                                    100, $this->student1->id, RATING_AGGREGATE_AVERAGE);
261
        $rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating);
262
                $this->assertTrue($rating['success']);
263
        $this->assertEquals(100, $rating['aggregate']);
264
        $this->assertEquals(1, $rating['count']);
265
 
266
        // Rate as other user.
267
        $this->setUser($this->teacher2);
268
        $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100,
269
                                                    50, $this->student1->id, RATING_AGGREGATE_AVERAGE);
270
        $rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating);
271
        $this->assertEquals(75, $rating['aggregate']);
272
        $this->assertEquals(2, $rating['count']);
273
 
274
        // Try to rate my own post.
275
        $this->setUser($this->student1);
276
        $this->expectException('moodle_exception');
277
        $this->expectExceptionMessage(get_string('ratepermissiondenied', 'rating'));
278
        $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100,
279
                                                        100, $this->student1->id, RATING_AGGREGATE_AVERAGE);
280
    }
281
}