Skip to content

Commit

Permalink
Merge branch 'MDL-76723-main' of https://github.com/junpataleta/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Jan 24, 2024
2 parents eb03b66 + 99596c7 commit 634e359
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 5 deletions.
11 changes: 9 additions & 2 deletions lib/classes/external/exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,12 @@ final protected static function get_format_field($definitions, $property) {
* @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);
}

/**
Expand Down Expand Up @@ -548,7 +550,12 @@ final protected static function get_read_structure_from_properties($properties,
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);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion lib/external/classes/external_format_value.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
94 changes: 94 additions & 0 deletions lib/tests/exporter_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,100 @@ public static function define_properties(): array {
$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);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion mod/page/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public static function get_pages_by_courses_returns() {
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'),
Expand Down
2 changes: 1 addition & 1 deletion tag/classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public static function get_tags_returns() {
'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),
Expand Down

0 comments on commit 634e359

Please sign in to comment.