3ce6a717a14dd79a044ff33b9a80454f2f0386b9
[moodle.git] / rating / tests / externallib_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31 require_once($CFG->dirroot . '/rating/lib.php');
33 /**
34  * External rating functions unit tests
35  *
36  * @package    core_rating
37  * @category   external
38  * @copyright  2015 Costantino Cito <ccito@cvaconsulting.com>
39  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40  */
41 class core_rating_externallib_testcase extends externallib_advanced_testcase {
43     /**
44      * Test get_item_ratings
45      */
46     public function test_get_item_ratings() {
48         global $DB, $USER;
50         $this->resetAfterTest(true);
52         $course = self::getDataGenerator()->create_course();
53         $student = $this->getDataGenerator()->create_user();
54         $teacher1 = $this->getDataGenerator()->create_user();
55         $teacher2 = $this->getDataGenerator()->create_user();
56         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
57         $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
59         $this->getDataGenerator()->enrol_user($student->id,  $course->id, $studentrole->id);
60         $this->getDataGenerator()->enrol_user($teacher1->id, $course->id, $teacherrole->id);
61         $this->getDataGenerator()->enrol_user($teacher2->id, $course->id, $teacherrole->id);
63         // Create the forum.
64         $record = new stdClass();
65         $record->introformat = FORMAT_HTML;
66         $record->course = $course->id;
67         // Set Aggregate type = Average of ratings.
68         $record->assessed = RATING_AGGREGATE_AVERAGE;
69         $forum = self::getDataGenerator()->create_module('forum', $record);
71         $contextid = context_module::instance($forum->cmid)->id;
73         // Add discussion to the forums.
74         $record = new stdClass();
75         $record->course = $course->id;
76         $record->userid = $student->id;
77         $record->forum = $forum->id;
78         $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
80         // Rete the discussion as teacher1.
81         $rating1 = new stdClass();
82         $rating1->contextid = $contextid;
83         $rating1->component = 'mod_forum';
84         $rating1->ratingarea = 'post';
85         $rating1->itemid = $discussion->id;
86         $rating1->rating = 90;
87         $rating1->scaleid = 100;
88         $rating1->userid = $teacher1->id;
89         $rating1->timecreated = time();
90         $rating1->timemodified = time();
91         $rating1->id = $DB->insert_record('rating', $rating1);
93         // Rete the discussion as teacher2.
94         $rating2 = new stdClass();
95         $rating2->contextid = $contextid;
96         $rating2->component = 'mod_forum';
97         $rating2->ratingarea = 'post';
98         $rating2->itemid = $discussion->id;
99         $rating2->rating = 95;
100         $rating2->scaleid = 100;
101         $rating2->userid = $teacher2->id;
102         $rating2->timecreated = time() + 1;
103         $rating2->timemodified = time() + 1;
104         $rating2->id = $DB->insert_record('rating', $rating2);
106         // Delete teacher2, we must still receive the ratings.
107         delete_user($teacher2);
109         // Teachers can see all the ratings.
110         $this->setUser($teacher1);
112         $ratings = core_rating_external::get_item_ratings('module', $forum->cmid, 'mod_forum', 'post', $discussion->id, 100, '');
113         // We need to execute the return values cleaning process to simulate the web service server.
114         $ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings);
115         $this->assertCount(2, $ratings['ratings']);
117         $indexedratings = array();
118         foreach ($ratings['ratings'] as $rating) {
119             $indexedratings[$rating['id']] = $rating;
120         }
121         $this->assertEquals($rating1->rating.' / '.$rating1->scaleid, $indexedratings[$rating1->id]['rating']);
122         $this->assertEquals($rating2->rating.' / '.$rating2->scaleid, $indexedratings[$rating2->id]['rating']);
124         $this->assertEquals($rating1->userid, $indexedratings[$rating1->id]['userid']);
125         $this->assertEquals($rating2->userid, $indexedratings[$rating2->id]['userid']);
127         // Student can see ratings.
128         $this->setUser($student);
130         $ratings = core_rating_external::get_item_ratings('module', $forum->cmid, 'mod_forum', 'post', $discussion->id, 100, '');
131         // We need to execute the return values cleaning process to simulate the web service server.
132         $ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings);
133         $this->assertCount(2, $ratings['ratings']);
135         // Invalid item.
136         $ratings = core_rating_external::get_item_ratings('module', $forum->cmid, 'mod_forum', 'post', 0, 100, '');
137         // We need to execute the return values cleaning process to simulate the web service server.
138         $ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings);
139         $this->assertCount(0, $ratings['ratings']);
141         // Invalid area.
142         $ratings = core_rating_external::get_item_ratings('module', $forum->cmid, 'mod_forum', 'xyz', $discussion->id, 100, '');
143         // We need to execute the return values cleaning process to simulate the web service server.
144         $ratings = external_api::clean_returnvalue(core_rating_external::get_item_ratings_returns(), $ratings);
145         $this->assertCount(0, $ratings['ratings']);
147         // Invalid context. invalid_parameter_exception.
148         try {
149             $ratings = core_rating_external::get_item_ratings('module', 0, 'mod_forum', 'post', $discussion->id, 100, '');
150             $this->fail('Exception expected due invalid context.');
151         } catch (invalid_parameter_exception $e) {
152             $this->assertEquals('invalidparameter', $e->errorcode);
153         }
154     }