[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/lti/tests/ -> locallib_test.php (source)

   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  
  50  defined('MOODLE_INTERNAL') || die;
  51  
  52  global $CFG;
  53  require_once($CFG->dirroot . '/mod/lti/locallib.php');
  54  require_once($CFG->dirroot . '/mod/lti/servicelib.php');
  55  
  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   */
  63  class mod_lti_locallib_testcase extends advanced_testcase {
  64  
  65      public function test_split_custom_parameters() {
  66          $this->resetAfterTest();
  67  
  68          $tool = new stdClass();
  69          $tool->enabledcapability = '';
  70          $tool->parameter = '';
  71          $this->assertEquals(lti_split_custom_parameters(null, $tool, array(), "x=1\ny=2", false),
  72              array('custom_x' => '1', 'custom_y' => '2'));
  73  
  74          // Removed repeat of previous test with a semicolon separator.
  75  
  76          $this->assertEquals(lti_split_custom_parameters(null, $tool, array(), 'Review:Chapter=1.2.56', false),
  77              array('custom_review_chapter' => '1.2.56'));
  78  
  79          $this->assertEquals(lti_split_custom_parameters(null, $tool, array(),
  80              'Complex!@#$^*(){}[]KEY=Complex!@#$^*;(){}[]½Value', false),
  81              array('custom_complex____________key' => 'Complex!@#$^*;(){}[]½Value'));
  82  
  83          // Test custom parameter that returns $USER property.
  84          $user = $this->getDataGenerator()->create_user(array('middlename' => 'SOMETHING'));
  85          $this->setUser($user);
  86          $this->assertEquals(array('custom_x' => '1', 'custom_y' => 'SOMETHING'),
  87              lti_split_custom_parameters(null, $tool, array(), "x=1\ny=\$Person.name.middle", false));
  88      }
  89  
  90      /**
  91       * This test has been disabled because the test-tool is
  92       * being moved and probably it won't work anymore for this.
  93       * We should be testing here local stuff only and leave
  94       * outside-checks to the conformance tests. MDL-30347
  95       */
  96      public function disabled_test_sign_parameters() {
  97          $correct = array ( 'context_id' => '12345', 'context_label' => 'SI124', 'context_title' => 'Social Computing',
  98              'ext_submit' => 'Click Me', 'lti_message_type' => 'basic-lti-launch-request', 'lti_version' => 'LTI-1p0',
  99              'oauth_consumer_key' => 'lmsng.school.edu', 'oauth_nonce' => '47458148e33a8f9dafb888c3684cf476',
 100              'oauth_signature' => 'qWgaBIezihCbeHgcwUy14tZcyDQ=', 'oauth_signature_method' => 'HMAC-SHA1',
 101              'oauth_timestamp' => '1307141660', 'oauth_version' => '1.0', 'resource_link_id' => '123',
 102              'resource_link_title' => 'Weekly Blog', 'roles' => 'Learner', 'tool_consumer_instance_guid' => 'lmsng.school.edu',
 103              'user_id' => '789');
 104  
 105          $requestparams = array('resource_link_id' => '123', 'resource_link_title' => 'Weekly Blog', 'user_id' => '789',
 106              'roles' => 'Learner', 'context_id' => '12345', 'context_label' => 'SI124', 'context_title' => 'Social Computing');
 107  
 108          $parms = lti_sign_parameters($requestparams, 'http://www.imsglobal.org/developer/LTI/tool.php', 'POST',
 109              'lmsng.school.edu', 'secret', 'Click Me', 'lmsng.school.edu' /*, $org_desc*/);
 110          $this->assertTrue(isset($parms['oauth_nonce']));
 111          $this->assertTrue(isset($parms['oauth_signature']));
 112          $this->assertTrue(isset($parms['oauth_timestamp']));
 113  
 114          // Those things that are hard to mock.
 115          $correct['oauth_nonce'] = $parms['oauth_nonce'];
 116          $correct['oauth_signature'] = $parms['oauth_signature'];
 117          $correct['oauth_timestamp'] = $parms['oauth_timestamp'];
 118          ksort($parms);
 119          ksort($correct);
 120          $this->assertEquals($parms, $correct);
 121      }
 122  
 123      /**
 124       * This test has been disabled because, since its creation,
 125       * the sourceId generation has changed and surely this is outdated.
 126       * Some day these should be replaced by proper tests, but until then
 127       * conformance tests say this is working. MDL-30347
 128       */
 129      public function disabled_test_parse_grade_replace_message() {
 130          $message = '
 131              <imsx_POXEnvelopeRequest xmlns = "http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0">
 132                <imsx_POXHeader>
 133                  <imsx_POXRequestHeaderInfo>
 134                    <imsx_version>V1.0</imsx_version>
 135                    <imsx_messageIdentifier>999998123</imsx_messageIdentifier>
 136                  </imsx_POXRequestHeaderInfo>
 137                </imsx_POXHeader>
 138                <imsx_POXBody>
 139                  <replaceResultRequest>
 140                    <resultRecord>
 141                      <sourcedGUID>
 142                        <sourcedId>' .
 143              '{&quot;data&quot;:{&quot;instanceid&quot;:&quot;2&quot;,&quot;userid&quot;:&quot;2&quot;},&quot;hash&quot;:' .
 144              '&quot;0b5078feab59b9938c333ceaae21d8e003a7b295e43cdf55338445254421076b&quot;}' .
 145                        '</sourcedId>
 146                      </sourcedGUID>
 147                      <result>
 148                        <resultScore>
 149                          <language>en-us</language>
 150                          <textString>0.92</textString>
 151                        </resultScore>
 152                      </result>
 153                    </resultRecord>
 154                  </replaceResultRequest>
 155                </imsx_POXBody>
 156              </imsx_POXEnvelopeRequest>
 157  ';
 158  
 159          $parsed = lti_parse_grade_replace_message(new SimpleXMLElement($message));
 160  
 161          $this->assertEquals($parsed->userid, '2');
 162          $this->assertEquals($parsed->instanceid, '2');
 163          $this->assertEquals($parsed->sourcedidhash, '0b5078feab59b9938c333ceaae21d8e003a7b295e43cdf55338445254421076b');
 164  
 165          $ltiinstance = (object)array('servicesalt' => '4e5fcc06de1d58.44963230');
 166  
 167          lti_verify_sourcedid($ltiinstance, $parsed);
 168      }
 169  
 170      public function test_lti_ensure_url_is_https() {
 171          $this->assertEquals('https://moodle.org', lti_ensure_url_is_https('http://moodle.org'));
 172          $this->assertEquals('https://moodle.org', lti_ensure_url_is_https('moodle.org'));
 173          $this->assertEquals('https://moodle.org', lti_ensure_url_is_https('https://moodle.org'));
 174      }
 175  
 176      /**
 177       * Test lti_get_url_thumbprint against various URLs
 178       */
 179      public function test_lti_get_url_thumbprint() {
 180          // Note: trailing and double slash are expected right now.  Must evaluate if it must be removed at some point.
 181          $this->assertEquals('moodle.org/', lti_get_url_thumbprint('http://MOODLE.ORG'));
 182          $this->assertEquals('moodle.org/', lti_get_url_thumbprint('http://www.moodle.org'));
 183          $this->assertEquals('moodle.org/', lti_get_url_thumbprint('https://www.moodle.org'));
 184          $this->assertEquals('moodle.org/', lti_get_url_thumbprint('moodle.org'));
 185          $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('http://moodle.org/this/is/moodle'));
 186          $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('https://moodle.org/this/is/moodle'));
 187          $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('moodle.org/this/is/moodle'));
 188          $this->assertEquals('moodle.org//this/is/moodle', lti_get_url_thumbprint('moodle.org/this/is/moodle?foo=bar'));
 189      }
 190  
 191      /*
 192       * Verify that lti_build_request does handle resource_link_id as expected
 193       */
 194      public function test_lti_buid_request_resource_link_id() {
 195          $this->resetAfterTest();
 196  
 197          self::setUser($this->getDataGenerator()->create_user());
 198          $course   = $this->getDataGenerator()->create_course();
 199          $instance = $this->getDataGenerator()->create_module('lti', array(
 200              'intro'       => "<p>This</p>\nhas\r\n<p>some</p>\nnew\n\rlines",
 201              'introformat' => FORMAT_HTML,
 202              'course'      => $course->id,
 203          ));
 204  
 205          $typeconfig = array(
 206              'acceptgrades'     => 1,
 207              'forcessl'         => 0,
 208              'sendname'         => 2,
 209              'sendemailaddr'    => 2,
 210              'customparameters' => '',
 211          );
 212  
 213          // Normal call, we expect $instance->id to be used as resource_link_id.
 214          $params = lti_build_request($instance, $typeconfig, $course, null);
 215          $this->assertSame($instance->id, $params['resource_link_id']);
 216  
 217          // If there is a resource_link_id set, it gets precedence.
 218          $instance->resource_link_id = $instance->id + 99;
 219          $params = lti_build_request($instance, $typeconfig, $course, null);
 220          $this->assertSame($instance->resource_link_id, $params['resource_link_id']);
 221  
 222          // With none set, resource_link_id is not set either.
 223          unset($instance->id);
 224          unset($instance->resource_link_id);
 225          $params = lti_build_request($instance, $typeconfig, $course, null);
 226          $this->assertArrayNotHasKey('resource_link_id', $params);
 227      }
 228  
 229      /**
 230       * Test lti_build_request's resource_link_description and ensure
 231       * that the newlines in the description are correct.
 232       */
 233      public function test_lti_build_request_description() {
 234          $this->resetAfterTest();
 235  
 236          self::setUser($this->getDataGenerator()->create_user());
 237          $course   = $this->getDataGenerator()->create_course();
 238          $instance = $this->getDataGenerator()->create_module('lti', array(
 239              'intro'       => "<p>This</p>\nhas\r\n<p>some</p>\nnew\n\rlines",
 240              'introformat' => FORMAT_HTML,
 241              'course'      => $course->id,
 242          ));
 243  
 244          $typeconfig = array(
 245              'acceptgrades'     => 1,
 246              'forcessl'         => 0,
 247              'sendname'         => 2,
 248              'sendemailaddr'    => 2,
 249              'customparameters' => '',
 250          );
 251  
 252          $params = lti_build_request($instance, $typeconfig, $course, null);
 253  
 254          $ncount = substr_count($params['resource_link_description'], "\n");
 255          $this->assertGreaterThan(0, $ncount);
 256  
 257          $rcount = substr_count($params['resource_link_description'], "\r");
 258          $this->assertGreaterThan(0, $rcount);
 259  
 260          $this->assertEquals($ncount, $rcount, 'The number of \n characters should be the same as the number of \r characters');
 261  
 262          $rncount = substr_count($params['resource_link_description'], "\r\n");
 263          $this->assertGreaterThan(0, $rncount);
 264  
 265          $this->assertEquals($ncount, $rncount, 'All newline characters should be a combination of \r\n');
 266      }
 267  
 268      /**
 269       * Tests lti_prepare_type_for_save's handling of the "Force SSL" configuration.
 270       */
 271      public function test_lti_prepare_type_for_save_forcessl() {
 272          $type = new stdClass();
 273          $config = new stdClass();
 274  
 275          // Try when the forcessl config property is not set.
 276          lti_prepare_type_for_save($type, $config);
 277          $this->assertObjectHasAttribute('lti_forcessl', $config);
 278          $this->assertEquals(0, $config->lti_forcessl);
 279          $this->assertEquals(0, $type->forcessl);
 280  
 281          // Try when forcessl config property is set.
 282          $config->lti_forcessl = 1;
 283          lti_prepare_type_for_save($type, $config);
 284          $this->assertObjectHasAttribute('lti_forcessl', $config);
 285          $this->assertEquals(1, $config->lti_forcessl);
 286          $this->assertEquals(1, $type->forcessl);
 287  
 288          // Try when forcessl config property is set to 0.
 289          $config->lti_forcessl = 0;
 290          lti_prepare_type_for_save($type, $config);
 291          $this->assertObjectHasAttribute('lti_forcessl', $config);
 292          $this->assertEquals(0, $config->lti_forcessl);
 293          $this->assertEquals(0, $type->forcessl);
 294      }
 295  
 296      /**
 297       * Tests lti_load_type_from_cartridge and lti_load_type_if_cartridge
 298       */
 299      public function test_lti_load_type_from_cartridge() {
 300          $type = new stdClass();
 301          $type->lti_toolurl = $this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml');
 302  
 303          lti_load_type_if_cartridge($type);
 304  
 305          $this->assertEquals('Example tool', $type->lti_typename);
 306          $this->assertEquals('Example tool description', $type->lti_description);
 307          $this->assertEquals('http://www.example.com/lti/provider.php', $type->lti_toolurl);
 308          $this->assertEquals('http://download.moodle.org/unittest/test.jpg', $type->lti_icon);
 309          $this->assertEquals('https://download.moodle.org/unittest/test.jpg', $type->lti_secureicon);
 310      }
 311  
 312      /**
 313       * Tests lti_load_tool_from_cartridge and lti_load_tool_if_cartridge
 314       */
 315      public function test_lti_load_tool_from_cartridge() {
 316          $lti = new stdClass();
 317          $lti->toolurl = $this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml');
 318  
 319          lti_load_tool_if_cartridge($lti);
 320  
 321          $this->assertEquals('Example tool', $lti->name);
 322          $this->assertEquals('Example tool description', $lti->intro);
 323          $this->assertEquals('http://www.example.com/lti/provider.php', $lti->toolurl);
 324          $this->assertEquals('https://www.example.com/lti/provider.php', $lti->securetoolurl);
 325          $this->assertEquals('http://download.moodle.org/unittest/test.jpg', $lti->icon);
 326          $this->assertEquals('https://download.moodle.org/unittest/test.jpg', $lti->secureicon);
 327      }
 328  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1