From e4f7cfe19f9dcc2d552d80ede0e2cb75b3d87456 Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Tue, 29 Aug 2017 10:31:04 +0530 Subject: [PATCH] MDL-59198 lti: Make params LTI2 compatible LTI2 recommends to include both upper case and lower case of custom params. This patch makes our implementation confer to that standard. --- mod/lti/locallib.php | 15 +++++++++------ mod/lti/tests/locallib_test.php | 10 ++++++++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/mod/lti/locallib.php b/mod/lti/locallib.php index 81e4a4452c34b..f1e57625a3b21 100644 --- a/mod/lti/locallib.php +++ b/mod/lti/locallib.php @@ -1165,7 +1165,7 @@ function lti_get_enabled_capabilities($tool) { * @param string $customstr String containing the parameters * @param boolean $islti2 True if an LTI 2 tool is being launched * - * @return Array of custom parameters + * @return array of custom parameters */ function lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $islti2 = false) { $customstr = str_replace("\r\n", "\n", $customstr); @@ -1179,11 +1179,12 @@ function lti_split_custom_parameters($toolproxy, $tool, $params, $customstr, $is continue; } $key = trim(core_text::substr($line, 0, $pos)); + $key = lti_map_keyname($key, false); $val = trim(core_text::substr($line, $pos + 1, strlen($line))); $val = lti_parse_custom_parameter($toolproxy, $tool, $params, $val, $islti2); $key2 = lti_map_keyname($key); $retval['custom_'.$key2] = $val; - if ($islti2 && ($key != $key2)) { + if ($key != $key2) { $retval['custom_'.$key] = $val; } } @@ -1269,14 +1270,16 @@ function lti_parse_custom_parameter($toolproxy, $tool, $params, $value, $islti2) * Used for building the names of the different custom parameters * * @param string $key Parameter name - * + * @param bool $tolower Do we want to convert the key into lower case? * @return string Processed name */ -function lti_map_keyname($key) { +function lti_map_keyname($key, $tolower = true) { $newkey = ""; - $key = core_text::strtolower(trim($key)); + if ($tolower) { + $key = core_text::strtolower(trim($key)); + } foreach (str_split($key) as $ch) { - if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') ) { + if ( ($ch >= 'a' && $ch <= 'z') || ($ch >= '0' && $ch <= '9') || (!$tolower && ($ch >= 'A' && $ch <= 'Z'))) { $newkey .= $ch; } else { $newkey .= '_'; diff --git a/mod/lti/tests/locallib_test.php b/mod/lti/tests/locallib_test.php index 5a471c01d478d..cdc3160335556 100644 --- a/mod/lti/tests/locallib_test.php +++ b/mod/lti/tests/locallib_test.php @@ -71,14 +71,20 @@ public function test_split_custom_parameters() { $this->assertEquals(lti_split_custom_parameters(null, $tool, array(), "x=1\ny=2", false), array('custom_x' => '1', 'custom_y' => '2')); + // Check params with caps. + $this->assertEquals(lti_split_custom_parameters(null, $tool, array(), "X=1", false), + array('custom_x' => '1', 'custom_X' => '1')); + // Removed repeat of previous test with a semicolon separator. $this->assertEquals(lti_split_custom_parameters(null, $tool, array(), 'Review:Chapter=1.2.56', false), - array('custom_review_chapter' => '1.2.56')); + array('custom_review_chapter' => '1.2.56', 'custom_Review_Chapter' => '1.2.56')); $this->assertEquals(lti_split_custom_parameters(null, $tool, array(), 'Complex!@#$^*(){}[]KEY=Complex!@#$^*;(){}[]½Value', false), - array('custom_complex____________key' => 'Complex!@#$^*;(){}[]½Value')); + array( + 'custom_complex____________key' => 'Complex!@#$^*;(){}[]½Value', + 'custom_Complex____________KEY' => 'Complex!@#$^*;(){}[]½Value')); // Test custom parameter that returns $USER property. $user = $this->getDataGenerator()->create_user(array('middlename' => 'SOMETHING'));