Merge branch 'MDL-76723-403' of https://github.com/junpataleta/moodle into MOODLE_403_STABLE

This commit is contained in:
Sara Arjona
2024-01-24 13:13:10 +01:00
5 changed files with 113 additions and 5 deletions
+9 -2
View File
@@ -426,10 +426,12 @@ abstract class exporter {
* @return external_format_value
*/
final protected static function get_format_structure($property, $definition, $required = VALUE_REQUIRED) {
$default = null;
if (array_key_exists('default', $definition)) {
$required = VALUE_DEFAULT;
$default = $definition['default'];
}
return new external_format_value($property, $required);
return new external_format_value($property, $required, $default);
}
/**
@@ -548,7 +550,12 @@ abstract class exporter {
if (isset($returns[$formatproperty])) {
throw new coding_exception('The format for \'' . $property . '\' is already defined.');
}
$returns[$formatproperty] = self::get_format_structure($property, $properties[$formatproperty]);
$formatpropertydef = $properties[$formatproperty];
$formatpropertyrequired = VALUE_REQUIRED;
if (!empty($formatpropertydef['optional'])) {
$formatpropertyrequired = VALUE_OPTIONAL;
}
$returns[$formatproperty] = self::get_format_structure($property, $formatpropertydef, $formatpropertyrequired);
}
}
}
+8 -1
View File
@@ -38,12 +38,19 @@ class external_format_value extends external_value {
* @param int $default Default value.
*/
public function __construct($textfieldname, $required = VALUE_REQUIRED, $default = null) {
// Make sure the default format's value is correct.
if ($default !== null && !in_array($default, [FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, FORMAT_MARKDOWN])) {
debugging("Invalid default format for $textfieldname: $default. " .
"It must be either FORMAT_MOODLE, FORMAT_HTML, FORMAT_PLAIN, or FORMAT_MARKDOWN.", DEBUG_DEVELOPER);
$default = null;
}
if ($default == null && $required == VALUE_DEFAULT) {
$default = FORMAT_HTML;
}
$desc = sprintf(
"%s format (%s = HTML, %s = MOODLE, %s = PLAIN, or %s = MARKDOWN",
"%s format (%s = HTML, %s = MOODLE, %s = PLAIN, or %s = MARKDOWN)",
$textfieldname,
FORMAT_HTML,
FORMAT_MOODLE,
+94
View File
@@ -246,6 +246,100 @@ class exporter_test extends \advanced_testcase {
$this->expectException(\coding_exception::class);
$exporter->export($output);
}
/**
* Test the processing of format properties.
*
* @covers \core\external\exporter::get_read_structure
* @return void
*/
public function test_format_properties_with_optional(): void {
$testable = new class([]) extends \core\external\exporter {
/**
* Properties definition.
*
* @return array[]
*/
public static function define_properties(): array {
return [
'content' => [
'type' => PARAM_RAW,
],
'contentformat' => [
'type' => PARAM_INT,
'optional' => true,
],
'description' => [
'type' => PARAM_RAW,
'optional' => true,
],
'descriptionformat' => [
'type' => PARAM_INT,
'default' => FORMAT_MARKDOWN,
],
'summary' => [
'type' => PARAM_RAW,
],
'summaryformat' => [
'type' => PARAM_INT,
'default' => null,
],
];
}
};
$definition = $testable::get_read_structure();
// Check content and its format.
$this->assertEquals(VALUE_REQUIRED, $definition->keys['content']->required);
$this->assertEquals(VALUE_OPTIONAL, $definition->keys['contentformat']->required);
$this->assertEquals(null, $definition->keys['contentformat']->default);
// Check description and its format.
$this->assertEquals(VALUE_OPTIONAL, $definition->keys['description']->required);
$this->assertEquals(VALUE_DEFAULT, $definition->keys['descriptionformat']->required);
$this->assertEquals(FORMAT_MARKDOWN, $definition->keys['descriptionformat']->default);
// Check summary and its format.
$this->assertEquals(VALUE_REQUIRED, $definition->keys['summary']->required);
$this->assertEquals(null, $definition->keys['summary']->default);
$this->assertEquals(VALUE_DEFAULT, $definition->keys['summaryformat']->required);
$this->assertEquals(FORMAT_HTML, $definition->keys['summaryformat']->default);
}
/**
* Test the processing of format properties when an invalid default format is passed.
*
* @covers \core\external\exporter::get_read_structure
* @return void
*/
public function test_optional_format_property_with_invalid_default(): void {
$testable = new class([]) extends \core\external\exporter {
/**
* Properties definition.
*
* @return array[]
*/
public static function define_properties(): array {
return [
'description' => [
'type' => PARAM_RAW,
],
'descriptionformat' => [
'type' => PARAM_INT,
'default' => 999,
],
];
}
};
$definition = $testable::get_read_structure();
$this->assertDebuggingCalled(null, DEBUG_DEVELOPER);
// Check description and its format.
$this->assertEquals(VALUE_REQUIRED, $definition->keys['description']->required);
$this->assertEquals(VALUE_DEFAULT, $definition->keys['descriptionformat']->required);
$this->assertEquals(FORMAT_HTML, $definition->keys['descriptionformat']->default);
}
}
/**
+1 -1
View File
@@ -193,7 +193,7 @@ class mod_page_external extends external_api {
helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
[
'content' => new external_value(PARAM_RAW, 'Page content'),
'contentformat' => new external_format_value('content', VALUE_REQUIRED, 'Content format'),
'contentformat' => new external_format_value('content'),
'contentfiles' => new external_files('Files in the content'),
'legacyfiles' => new external_value(PARAM_INT, 'Legacy files flag'),
'legacyfileslast' => new external_value(PARAM_INT, 'Legacy files last control flag'),
+1 -1
View File
@@ -242,7 +242,7 @@ class core_tag_external extends external_api {
'name' => new external_value(PARAM_TAG, 'name'),
'rawname' => new external_value(PARAM_RAW, 'tag raw name (may contain capital letters)'),
'description' => new external_value(PARAM_RAW, 'tag description'),
'descriptionformat' => new external_format_value(PARAM_INT, VALUE_REQUIRED, 'tag description format'),
'descriptionformat' => new external_format_value('description'),
'flag' => new external_value(PARAM_INT, 'flag', VALUE_OPTIONAL),
'official' => new external_value(PARAM_INT,
'whether this flag is standard (deprecated, use isstandard)', VALUE_OPTIONAL),