Merge branch 'MDL-46588-master' of git://github.com/jleyva/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 15 Sep 2014 22:23:09 +0000 (00:23 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 15 Sep 2014 22:23:09 +0000 (00:23 +0200)
lib/externallib.php
lib/tests/externallib_test.php

index 2ce6de6..56144ef 100644 (file)
@@ -377,9 +377,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']) && isset($param['instanceid'])) {
             $contextlevel = "context_".$param['contextlevel'];
             if (!array_search($contextlevel, $levels)) {
                 throw new invalid_parameter_exception('Invalid context level = '.$param['contextlevel']);
index f0a2a04..1012a6d 100644 (file)
@@ -147,6 +147,33 @@ 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);
+        }
+
+        // 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));