From 6933d61c682e84afaaeaf5b1a19929c499be436a Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 3 Sep 2014 22:26:40 +0200 Subject: [PATCH 1/3] MDL-46588 web services: Fixed invalid check of empty contextid in function get_context_from_params --- lib/externallib.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/externallib.php b/lib/externallib.php index 698a9efc12c..4f9e00cbeb3 100644 --- a/lib/externallib.php +++ b/lib/externallib.php @@ -371,9 +371,9 @@ class external_api { */ protected static function get_context_from_params($param) { $levels = context_helper::get_all_levels(); - if (isset($param['contextid'])) { + if (!empty($param['contextid'])) { return context::instance_by_id($param['contextid'], IGNORE_MISSING); - } else if (isset($param['contextlevel']) && isset($param['instanceid'])) { + } else if (!empty($param['contextlevel']) && !empty($param['instanceid'])) { $contextlevel = "context_".$param['contextlevel']; if (!array_search($contextlevel, $levels)) { throw new invalid_parameter_exception('Invalid context level = '.$param['contextlevel']); From 2a8ec1ce68b2bae35bdf153183959661f75be941 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 4 Sep 2014 11:06:48 +0200 Subject: [PATCH 2/3] MDL-46588 web services: Unit tests added --- lib/tests/externallib_test.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/tests/externallib_test.php b/lib/tests/externallib_test.php index f0a2a04cee4..369c342b466 100644 --- a/lib/tests/externallib_test.php +++ b/lib/tests/externallib_test.php @@ -147,6 +147,28 @@ class core_externallib_testcase extends advanced_testcase { $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "course", "instanceid" => $course->id)); $this->assertEquals($realcontext, $fetchedcontext); + // Passing empty values. + try { + $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => 0)); + $this->fail('Exception expected from get_context_wrapper()'); + } catch (moodle_exception $e) { + $this->assertInstanceOf('invalid_parameter_exception', $e); + } + + try { + $fetchedcontext = test_exernal_api::get_context_wrapper(array("instanceid" => 0)); + $this->fail('Exception expected from get_context_wrapper()'); + } catch (moodle_exception $e) { + $this->assertInstanceOf('invalid_parameter_exception', $e); + } + + try { + $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextid" => null)); + $this->fail('Exception expected from get_context_wrapper()'); + } catch (moodle_exception $e) { + $this->assertInstanceOf('invalid_parameter_exception', $e); + } + // Passing wrong level. $this->setExpectedException('invalid_parameter_exception'); $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "random", "instanceid" => $course->id)); From 4b8a9f2f6a29b4c26c82e50f60962b8a3fff8646 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Tue, 9 Sep 2014 09:22:20 +0200 Subject: [PATCH 3/3] MDL-46588 web services: Fixed system context instanceid eq 0 case --- lib/externallib.php | 2 +- lib/tests/externallib_test.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/externallib.php b/lib/externallib.php index 4f9e00cbeb3..2c32071481d 100644 --- a/lib/externallib.php +++ b/lib/externallib.php @@ -373,7 +373,7 @@ class external_api { $levels = context_helper::get_all_levels(); if (!empty($param['contextid'])) { return context::instance_by_id($param['contextid'], IGNORE_MISSING); - } else if (!empty($param['contextlevel']) && !empty($param['instanceid'])) { + } else if (!empty($param['contextlevel']) && isset($param['instanceid'])) { $contextlevel = "context_".$param['contextlevel']; if (!array_search($contextlevel, $levels)) { throw new invalid_parameter_exception('Invalid context level = '.$param['contextlevel']); diff --git a/lib/tests/externallib_test.php b/lib/tests/externallib_test.php index 369c342b466..1012a6d6053 100644 --- a/lib/tests/externallib_test.php +++ b/lib/tests/externallib_test.php @@ -169,6 +169,11 @@ class core_externallib_testcase extends advanced_testcase { $this->assertInstanceOf('invalid_parameter_exception', $e); } + // Tests for context with instanceid equal to 0 (System context). + $realcontext = context_system::instance(); + $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "system", "instanceid" => 0)); + $this->assertEquals($realcontext, $fetchedcontext); + // Passing wrong level. $this->setExpectedException('invalid_parameter_exception'); $fetchedcontext = test_exernal_api::get_context_wrapper(array("contextlevel" => "random", "instanceid" => $course->id));