Merge branch 'MDL-47576-master' of git://github.com/jethac/moodle
[moodle.git] / mod / lti / tests / locallib_test.php
CommitLineData
ba0c4843
PS
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// This file is part of BasicLTI4Moodle
18//
19// BasicLTI4Moodle is an IMS BasicLTI (Basic Learning Tools for Interoperability)
20// consumer for Moodle 1.9 and Moodle 2.0. BasicLTI is a IMS Standard that allows web
21// based learning tools to be easily integrated in LMS as native ones. The IMS BasicLTI
22// specification is part of the IMS standard Common Cartridge 1.1 Sakai and other main LMS
23// are already supporting or going to support BasicLTI. This project Implements the consumer
24// for Moodle. Moodle is a Free Open source Learning Management System by Martin Dougiamas.
25// BasicLTI4Moodle is a project iniciated and leaded by Ludo(Marc Alier) and Jordi Piguillem
26// at the GESSI research group at UPC.
27// SimpleLTI consumer for Moodle is an implementation of the early specification of LTI
28// by Charles Severance (Dr Chuck) htp://dr-chuck.com , developed by Jordi Piguillem in a
29// Google Summer of Code 2008 project co-mentored by Charles Severance and Marc Alier.
30//
31// BasicLTI4Moodle is copyright 2009 by Marc Alier Forment, Jordi Piguillem and Nikolas Galanis
32// of the Universitat Politecnica de Catalunya http://www.upc.edu
33// Contact info: Marc Alier Forment granludo @ gmail.com or marc.alier @ upc.edu
34
35/**
36 * This file contains unit tests for (some of) lti/locallib.php
37 *
38 * @package mod_lti
39 * @category phpunit
40 * @copyright 2009 Marc Alier, Jordi Piguillem, Nikolas Galanis
41 * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
42 * @author Charles Severance csev@unmich.edu
43 * @author Marc Alier (marc.alier@upc.edu)
44 * @author Jordi Piguillem
45 * @author Nikolas Galanis
46 * @author Chris Scribner
47 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48 */
49
50defined('MOODLE_INTERNAL') || die;
51
52global $CFG;
53require_once($CFG->dirroot . '/mod/lti/locallib.php');
54require_once($CFG->dirroot . '/mod/lti/servicelib.php');
55
ff9d3d81
MN
56/**
57 * Local library tests
58 *
59 * @package mod_lti
60 * @copyright Copyright (c) 2012 Moodlerooms Inc. (http://www.moodlerooms.com)
61 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
62 */
63class mod_lti_locallib_testcase extends advanced_testcase {
ba0c4843
PS
64
65 public function test_split_custom_parameters() {
66 $this->assertEquals(lti_split_custom_parameters("x=1\ny=2"),
67 array('custom_x' => '1', 'custom_y'=> '2'));
68
69 $this->assertEquals(lti_split_custom_parameters('x=1;y=2'),
70 array('custom_x' => '1', 'custom_y'=> '2'));
71
72 $this->assertEquals(lti_split_custom_parameters('Review:Chapter=1.2.56'),
73 array('custom_review_chapter' => '1.2.56'));
74
75 $this->assertEquals(lti_split_custom_parameters('Complex!@#$^*(){}[]KEY=Complex!@#$^*(){}[]Value'),
76 array('custom_complex____________key' => 'Complex!@#$^*(){}[]Value'));
77 }
78
79 /**
80 * This test has been disabled because the test-tool is
81 * being moved and probably it won't work anymore for this.
82 * We should be testing here local stuff only and leave
83 * outside-checks to the conformance tests. MDL-30347
84 */
85 public function disabled_test_sign_parameters() {
86 $correct = array ( 'context_id' => '12345', 'context_label' => 'SI124', 'context_title' => 'Social Computing', 'ext_submit' => 'Click Me', 'lti_message_type' => 'basic-lti-launch-request', 'lti_version' => 'LTI-1p0', 'oauth_consumer_key' => 'lmsng.school.edu', 'oauth_nonce' => '47458148e33a8f9dafb888c3684cf476', 'oauth_signature' => 'qWgaBIezihCbeHgcwUy14tZcyDQ=', 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => '1307141660', 'oauth_version' => '1.0', 'resource_link_id' => '123', 'resource_link_title' => 'Weekly Blog', 'roles' => 'Learner', 'tool_consumer_instance_guid' => 'lmsng.school.edu', 'user_id' => '789');
87
88 $requestparams = array('resource_link_id' => '123', 'resource_link_title' => 'Weekly Blog', 'user_id' => '789', 'roles' => 'Learner', 'context_id' => '12345', 'context_label' => 'SI124', 'context_title' => 'Social Computing');
89
90 $parms = lti_sign_parameters($requestparams, 'http://www.imsglobal.org/developer/LTI/tool.php', 'POST',
91 'lmsng.school.edu', 'secret', 'Click Me', 'lmsng.school.edu' /*, $org_desc*/);
92 $this->assertTrue(isset($parms['oauth_nonce']));
93 $this->assertTrue(isset($parms['oauth_signature']));
94 $this->assertTrue(isset($parms['oauth_timestamp']));
95
96 // Those things that are hard to mock
97 $correct['oauth_nonce'] = $parms['oauth_nonce'];
98 $correct['oauth_signature'] = $parms['oauth_signature'];
99 $correct['oauth_timestamp'] = $parms['oauth_timestamp'];
100 ksort($parms);
101 ksort($correct);
102 $this->assertEquals($parms, $correct);
103 }
104
105 /**
106 * This test has been disabled because, since its creation,
107 * the sourceId generation has changed and surely this is outdated.
108 * Some day these should be replaced by proper tests, but until then
109 * conformance tests say this is working. MDL-30347
110 */
111 public function disabled_test_parse_grade_replace_message() {
112 $message = '
33ad01a2 113 <imsx_POXEnvelopeRequest xmlns = "http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0">
ba0c4843
PS
114 <imsx_POXHeader>
115 <imsx_POXRequestHeaderInfo>
116 <imsx_version>V1.0</imsx_version>
117 <imsx_messageIdentifier>999998123</imsx_messageIdentifier>
118 </imsx_POXRequestHeaderInfo>
119 </imsx_POXHeader>
120 <imsx_POXBody>
121 <replaceResultRequest>
122 <resultRecord>
123 <sourcedGUID>
124 <sourcedId>{&quot;data&quot;:{&quot;instanceid&quot;:&quot;2&quot;,&quot;userid&quot;:&quot;2&quot;},&quot;hash&quot;:&quot;0b5078feab59b9938c333ceaae21d8e003a7b295e43cdf55338445254421076b&quot;}</sourcedId>
125 </sourcedGUID>
126 <result>
127 <resultScore>
128 <language>en-us</language>
129 <textString>0.92</textString>
130 </resultScore>
131 </result>
132 </resultRecord>
133 </replaceResultRequest>
134 </imsx_POXBody>
135 </imsx_POXEnvelopeRequest>
136';
137
138 $parsed = lti_parse_grade_replace_message(new SimpleXMLElement($message));
139
140 $this->assertEquals($parsed->userid, '2');
141 $this->assertEquals($parsed->instanceid, '2');
142 $this->assertEquals($parsed->sourcedidhash, '0b5078feab59b9938c333ceaae21d8e003a7b295e43cdf55338445254421076b');
143
144 $ltiinstance = (object)array('servicesalt' => '4e5fcc06de1d58.44963230');
145
146 lti_verify_sourcedid($ltiinstance, $parsed);
147 }
adc23cc9
MN
148
149 public function test_lti_ensure_url_is_https() {
150 $this->assertEquals('https://moodle.org', lti_ensure_url_is_https('http://moodle.org'));
151 $this->assertEquals('https://moodle.org', lti_ensure_url_is_https('moodle.org'));
152 $this->assertEquals('https://moodle.org', lti_ensure_url_is_https('https://moodle.org'));
153 }
8fa50fdd
MN
154
155 /**
156 * Test lti_get_url_thumbprint against various URLs
157 */
158 public function test_lti_get_url_thumbprint() {
159 // Note: trailing and double slash are expected right now. Must evaluate if it must be removed at some point.
160 $this->assertEquals('moodle.org/', lti_get_url_thumbprint('http://MOODLE.ORG'));
161 $this->assertEquals('moodle.org/', lti_get_url_thumbprint('http://www.moodle.org'));
162 $this->assertEquals('moodle.org/', lti_get_url_thumbprint('https://www.moodle.org'));
163 $this->assertEquals('moodle.org/', lti_get_url_thumbprint('moodle.org'));
164 $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('http://moodle.org/this/is/moodle'));
165 $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('https://moodle.org/this/is/moodle'));
166 $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('moodle.org/this/is/moodle'));
167 $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('moodle.org/this/is/moodle?foo=bar'));
168 }
ff9d3d81
MN
169
170 /**
171 * Test lti_build_request's resource_link_description and ensure
172 * that the newlines in the description are correct.
173 */
174 public function test_lti_build_request_description() {
175 $this->resetAfterTest();
176
177 self::setUser($this->getDataGenerator()->create_user());
178 $course = $this->getDataGenerator()->create_course();
179 $instance = $this->getDataGenerator()->create_module('lti', array(
180 'intro' => "<p>This</p>\nhas\r\n<p>some</p>\nnew\n\rlines",
181 'introformat' => FORMAT_HTML,
182 'course' => $course->id,
183 ));
184
185 $typeconfig = array(
186 'acceptgrades' => 1,
187 'forcessl' => 0,
188 'sendname' => 2,
189 'sendemailaddr' => 2,
190 'customparameters' => '',
191 );
192
193 $params = lti_build_request($instance, $typeconfig, $course, null);
194
195 $ncount = substr_count($params['resource_link_description'], "\n");
196 $this->assertGreaterThan(0, $ncount);
197
198 $rcount = substr_count($params['resource_link_description'], "\r");
199 $this->assertGreaterThan(0, $rcount);
200
201 $this->assertEquals($ncount, $rcount, 'The number of \n characters should be the same as the number of \r characters');
202
203 $rncount = substr_count($params['resource_link_description'], "\r\n");
204 $this->assertGreaterThan(0, $rncount);
205
206 $this->assertEquals($ncount, $rncount, 'All newline characters should be a combination of \r\n');
207 }
ba0c4843 208}