From 9fac7c8640ddb4d1c3dfcc8b0c80c35a02a391ea Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 9 Feb 2017 15:06:16 +0100 Subject: [PATCH] MDL-57915 mod_data: New Web Service mod_data_view_database --- mod/data/classes/external.php | 62 ++++++++++++++++++++ mod/data/db/services.php | 10 +++- mod/data/tests/externallib_test.php | 89 +++++++++++++++++++++++++++++ mod/data/version.php | 2 +- 4 files changed, 161 insertions(+), 2 deletions(-) diff --git a/mod/data/classes/external.php b/mod/data/classes/external.php index 7aa8afca0b8..c68f639a53c 100644 --- a/mod/data/classes/external.php +++ b/mod/data/classes/external.php @@ -27,6 +27,7 @@ defined('MOODLE_INTERNAL') || die; require_once("$CFG->libdir/externallib.php"); +require_once($CFG->dirroot . "/mod/data/locallib.php"); use mod_data\external\database_summary_exporter; @@ -149,4 +150,65 @@ class mod_data_external extends external_api { ); } + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.3 + */ + public static function view_database_parameters() { + return new external_function_parameters( + array( + 'databaseid' => new external_value(PARAM_INT, 'data instance id') + ) + ); + } + + /** + * Simulate the data/view.php web interface page: trigger events, completion, etc... + * + * @param int $databaseid the data instance id + * @return array of warnings and status result + * @since Moodle 3.3 + * @throws moodle_exception + */ + public static function view_database($databaseid) { + global $DB; + + $params = self::validate_parameters(self::view_database_parameters(), array('databaseid' => $databaseid)); + $warnings = array(); + + // Request and permission validation. + $data = $DB->get_record('data', array('id' => $params['databaseid']), '*', MUST_EXIST); + list($course, $cm) = get_course_and_cm_from_instance($data, 'data'); + + $context = context_module::instance($cm->id); + self::validate_context($context); + + require_capability('mod/data:viewentry', $context); + + // Call the data/lib API. + data_view($data, $course, $cm, $context); + + $result = array(); + $result['status'] = true; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.3 + */ + public static function view_database_returns() { + return new external_single_structure( + array( + 'status' => new external_value(PARAM_BOOL, 'status: true if success'), + 'warnings' => new external_warnings() + ) + ); + } + } diff --git a/mod/data/db/services.php b/mod/data/db/services.php index 980188d8ffe..213474fba74 100644 --- a/mod/data/db/services.php +++ b/mod/data/db/services.php @@ -34,5 +34,13 @@ $functions = array( 'type' => 'read', 'capabilities' => 'mod/data:viewentry', 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE) - ) + ), + 'mod_data_view_database' => array( + 'classname' => 'mod_data_external', + 'methodname' => 'view_database', + 'description' => 'Simulate the view.php web interface data: trigger events, completion, etc...', + 'type' => 'write', + 'capabilities' => 'mod/data:viewentry', + 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE) + ), ); diff --git a/mod/data/tests/externallib_test.php b/mod/data/tests/externallib_test.php index 2ac015dac75..46bd139e0df 100644 --- a/mod/data/tests/externallib_test.php +++ b/mod/data/tests/externallib_test.php @@ -41,6 +41,33 @@ require_once($CFG->dirroot . '/webservice/tests/helpers.php'); */ class mod_data_external_testcase extends externallib_advanced_testcase { + /** + * Set up for every test + */ + public function setUp() { + global $DB; + $this->resetAfterTest(); + $this->setAdminUser(); + + // Setup test data. + $this->course = $this->getDataGenerator()->create_course(); + $this->data = $this->getDataGenerator()->create_module('data', array('course' => $this->course->id)); + $this->context = context_module::instance($this->data->cmid); + $this->cm = get_coursemodule_from_instance('data', $this->data->id); + + // Create users. + $this->student1 = self::getDataGenerator()->create_user(); + $this->student2 = self::getDataGenerator()->create_user(); + $this->teacher = self::getDataGenerator()->create_user(); + + // Users enrolments. + $this->studentrole = $DB->get_record('role', array('shortname' => 'student')); + $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); + $this->getDataGenerator()->enrol_user($this->student1->id, $this->course->id, $this->studentrole->id, 'manual'); + $this->getDataGenerator()->enrol_user($this->student2->id, $this->course->id, $this->studentrole->id, 'manual'); + $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual'); + } + /** * Test get databases by courses */ @@ -168,4 +195,66 @@ class mod_data_external_testcase extends externallib_advanced_testcase { $result = external_api::clean_returnvalue(mod_data_external::get_databases_by_courses_returns(), $result); $this->assertEquals($expecteddatabases, $result['databases']); } + + /** + * Test view_database invalid id. + */ + public function test_view_database_invalid_id() { + + // Test invalid instance id. + $this->setExpectedException('moodle_exception'); + mod_data_external::view_database(0); + } + + /** + * Test view_database not enrolled user. + */ + public function test_view_database_not_enrolled_user() { + + $usernotenrolled = self::getDataGenerator()->create_user(); + $this->setUser($usernotenrolled); + + $this->setExpectedException('moodle_exception'); + mod_data_external::view_database(0); + } + + /** + * Test view_database no capabilities. + */ + public function test_view_database_no_capabilities() { + // Test user with no capabilities. + // We need a explicit prohibit since this capability is allowed for students by default. + assign_capability('mod/data:viewpage', CAP_PROHIBIT, $this->studentrole->id, $this->context->id); + accesslib_clear_all_caches_for_unit_testing(); + + $this->setExpectedException('moodle_exception'); + mod_data_external::view_database(0); + } + + /** + * Test view_database. + */ + public function test_view_database() { + + // Test user with full capabilities. + $this->setUser($this->student1); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + $result = mod_data_external::view_database($this->data->id); + $result = external_api::clean_returnvalue(mod_data_external::view_database_returns(), $result); + + $events = $sink->get_events(); + $this->assertCount(1, $events); + $event = array_shift($events); + + // Checking that the event contains the expected values. + $this->assertInstanceOf('\mod_data\event\course_module_viewed', $event); + $this->assertEquals($this->context, $event->get_context()); + $moodledata = new \moodle_url('/mod/data/view.php', array('id' => $this->cm->id)); + $this->assertEquals($moodledata, $event->get_url()); + $this->assertEventContextNotUsed($event); + $this->assertNotEmpty($event->get_name()); + } } diff --git a/mod/data/version.php b/mod/data/version.php index 210e7e87e1e..b92d8a61bd1 100644 --- a/mod/data/version.php +++ b/mod/data/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016120500; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2016120501; // The current module version (Date: YYYYMMDDXX) $plugin->requires = 2016112900; // Requires this Moodle version $plugin->component = 'mod_data'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0;