/** * Test add_rating */ public function test_add_rating() { $this->setUser($this->teacher1); // First rating of 50. $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, 50, $this->student1->id, RATING_AGGREGATE_AVERAGE); // We need to execute the return values cleaning process to simulate the web service server. $rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating); $this->assertTrue($rating['success']); $this->assertEquals(50, $rating['aggregate']); $this->assertEquals(1, $rating['count']); // New different rate (it will replace the existing one). $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, 100, $this->student1->id, RATING_AGGREGATE_AVERAGE); $rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating); $this->assertTrue($rating['success']); $this->assertEquals(100, $rating['aggregate']); $this->assertEquals(1, $rating['count']); // Rate as other user. $this->setUser($this->teacher2); $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, 50, $this->student1->id, RATING_AGGREGATE_AVERAGE); $rating = external_api::clean_returnvalue(core_rating_external::add_rating_returns(), $rating); $this->assertEquals(75, $rating['aggregate']); $this->assertEquals(2, $rating['count']); // Try to rate my own post. $this->setUser($this->student1); $this->expectException('moodle_exception'); $this->expectExceptionMessage(get_string('ratepermissiondenied', 'rating')); $rating = core_rating_external::add_rating('module', $this->forum->cmid, 'mod_forum', 'post', $this->post->id, 100, 100, $this->student1->id, RATING_AGGREGATE_AVERAGE); }