MDL-51615 mod_lti: New Web Service mod_lti_get_tool_launch_data
[moodle.git] / mod / lti / tests / externallib_test.php
CommitLineData
d5ded9e7
JL
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 tool module external functions tests
19 *
20 * @package mod_lti
21 * @category external
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.0
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32require_once($CFG->dirroot . '/mod/lti/lib.php');
33
34/**
35 * External tool module external functions tests
36 *
37 * @package mod_lti
38 * @category external
39 * @copyright 2015 Juan Leyva <juan@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 * @since Moodle 3.0
42 */
43class mod_lti_external_testcase extends externallib_advanced_testcase {
44
45 /**
46 * Set up for every test
47 */
48 public function setUp() {
49 global $DB;
50 $this->resetAfterTest();
51 $this->setAdminUser();
52
53 // Setup test data.
54 $this->course = $this->getDataGenerator()->create_course();
55 $this->lti = $this->getDataGenerator()->create_module('lti', array('course' => $this->course->id));
56 $this->context = context_module::instance($this->lti->cmid);
57 $this->cm = get_coursemodule_from_instance('lti', $this->lti->id);
58
59 // Create users.
60 $this->student = self::getDataGenerator()->create_user();
61 $this->teacher = self::getDataGenerator()->create_user();
62
63 // Users enrolments.
64 $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
65 $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
66 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
67 $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
68 }
69
70 /**
71 * Test view_lti
72 */
73 public function test_get_tool_launch_data() {
74 global $USER;
75
76 $result = mod_lti_external::get_tool_launch_data($this->lti->id);
77 $result = external_api::clean_returnvalue(mod_lti_external::get_tool_launch_data_returns(), $result);
78
79 // Basic test, the function returns what it's expected.
80 self::assertEquals($this->lti->toolurl, $result['endpoint']);
81 self::assertCount(35, $result['parameters']);
82
83 // Check some parameters.
84 $parameters = array();
85 foreach ($result['parameters'] as $param) {
86 $parameters[$param['name']] = $param['value'];
87 }
88 self::assertEquals($this->lti->resourcekey, $parameters['oauth_consumer_key']);
89 self::assertEquals($this->course->fullname, $parameters['context_title']);
90 self::assertEquals($this->course->shortname, $parameters['context_label']);
91 self::assertEquals($USER->id, $parameters['user_id']);
92 self::assertEquals($USER->firstname, $parameters['lis_person_name_given']);
93 self::assertEquals($USER->lastname, $parameters['lis_person_name_family']);
94 self::assertEquals(fullname($USER), $parameters['lis_person_name_full']);
95 self::assertEquals($USER->username, $parameters['ext_user_username']);
96
97 }
98
99}