Skip to content

Commit

Permalink
Merge branch 'MDL-51756-28' of git://github.com/damyon/moodle into MO…
Browse files Browse the repository at this point in the history
…ODLE_28_STABLE
  • Loading branch information
andrewnicols committed Oct 15, 2015
2 parents d058095 + 9a9bfb4 commit a6e1c0a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/externallib.php
Expand Up @@ -48,7 +48,7 @@ function external_function_info($function, $strictness=MUST_EXIST) {
// Fallback to explicit include of externallib.php.
$function->classpath = empty($function->classpath) ? core_component::get_component_directory($function->component).'/externallib.php' : $CFG->dirroot.'/'.$function->classpath;
if (!file_exists($function->classpath)) {
throw new coding_exception('Cannot find file with external function implementation');
throw new coding_exception('Cannot find file with external function implementation: ' . $function->classname);
}
require_once($function->classpath);
if (!class_exists($function->classname)) {
Expand Down Expand Up @@ -526,6 +526,30 @@ public function __construct(external_description $content, $desc='',
* @since Moodle 2.0
*/
class external_function_parameters extends external_single_structure {

/**
* Constructor - does extra checking to prevent top level optional parameters.
*
* @param array $keys
* @param string $desc
* @param bool $required
* @param array $default
*/
public function __construct(array $keys, $desc='', $required=VALUE_REQUIRED, $default=null) {
global $CFG;

if ($CFG->debugdeveloper) {
foreach ($keys as $key => $value) {
if ($value instanceof external_value) {
if ($value->required == VALUE_OPTIONAL) {
debugging('External function parameters: invalid OPTIONAL value specified.', DEBUG_DEVELOPER);
break;
}
}
}
}
parent::__construct($keys, $desc, $required, $default);
}
}

/**
Expand Down
29 changes: 29 additions & 0 deletions lib/tests/externallib_test.php
Expand Up @@ -213,6 +213,35 @@ public function test_get_context_params3() {
$this->setExpectedException('invalid_parameter_exception');
test_exernal_api::get_context_wrapper(array('roleid' => 3, 'userid' => $USER->id, 'instanceid' => $course->id));
}

public function all_external_info_provider() {
global $DB;

// We are testing here that all the external function descriptions can be generated without
// producing warnings. E.g. misusing optional params will generate a debugging message which
// will fail this test.
$functions = $DB->get_records('external_functions', array(), 'name');
$return = array();
foreach ($functions as $f) {
$return[$f->name] = array($f);
}
return $return;
}

/**
* @dataProvider all_external_info_provider
*/
public function test_all_external_info($f) {
$desc = external_function_info($f);
$this->assertNotEmpty($desc->name);
$this->assertNotEmpty($desc->classname);
$this->assertNotEmpty($desc->methodname);
$this->assertEquals($desc->component, clean_param($desc->component, PARAM_COMPONENT));
$this->assertInstanceOf('external_function_parameters', $desc->parameters_desc);
if ($desc->returns_desc != null) {
$this->assertInstanceOf('external_description', $desc->returns_desc);
}
}
}

/*
Expand Down

0 comments on commit a6e1c0a

Please sign in to comment.