Skip to content

Commit

Permalink
Merge branch 'MDL-80201-402' of https://github.com/junpataleta/moodle
Browse files Browse the repository at this point in the history
…into MOODLE_402_STABLE
  • Loading branch information
andrewnicols committed Jan 18, 2024
2 parents bac138d + 797e4fb commit f93bc84
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/classes/external/exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@ final public function export(renderer_base $output) {

// If the field is PARAM_RAW and has a format field.
if ($propertyformat = self::get_format_field($properties, $property)) {
if (!property_exists($record, $propertyformat)) {
$formatdefinition = $properties[$propertyformat];
if (!property_exists($record, $propertyformat) && !array_key_exists('default', $formatdefinition)) {
// Whoops, we got something that wasn't defined.
throw new coding_exception('Unexpected property ' . $propertyformat);
}

$formatparams = $this->get_format_parameters($property);
$format = $record->$propertyformat;
$format = $record->$propertyformat ?? $formatdefinition['default'];

list($text, $format) = \core_external\util::format_text($data->$property, $format, $formatparams['context'],
$formatparams['component'], $formatparams['filearea'], $formatparams['itemid'], $formatparams['options']);
Expand Down
44 changes: 44 additions & 0 deletions lib/tests/exporter_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use core_external\external_settings;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\util;

/**
* Exporter testcase.
Expand Down Expand Up @@ -202,6 +203,49 @@ public function test_properties_description() {
// Assert nested elements are formatted correctly.
$this->assertEquals('id', $properties['nestedarray']['type']['id']['description']);
}

/**
* Tests for the handling of the default attribute of format properties in exporters.
*
* @covers \core\external\exporter::export
* @return void
*/
public function test_export_format_no_default(): void {
global $PAGE;
$output = $PAGE->get_renderer('core');
$syscontext = \context_system::instance();
$related = [
'context' => $syscontext,
] + $this->validrelated;

// Pass a data that does not have the format property for stringA.
$data = [
'stringA' => '__Go to:__ [Moodle.org](https://moodle.org)',
'intB' => 1,
];

// Note: For testing purposes only. Never extend exporter implementation. Only extend from the base exporter class!
$testablexporterclass = new class($data, $related) extends core_testable_exporter {
/**
* Properties definition.
*/
public static function define_properties(): array {
$properties = parent::define_properties();
$properties['stringAformat']['default'] = FORMAT_MARKDOWN;
return $properties;
}
};
// For a property format with default set, it should be able to export a data even if the property format is not passed.
$result = $testablexporterclass->export($output);
$expected = '<strong>Go to:</strong> <a href="https://moodle.org">Moodle.org</a>';
$this->assertStringContainsString($expected, $result->stringA);
$this->assertEquals(FORMAT_HTML, $result->stringAformat);

// Passing data to an exporter with a required property format will throw an exception.
$exporter = new core_testable_exporter($data, $related);
$this->expectException(\coding_exception::class);
$exporter->export($output);
}
}

/**
Expand Down

0 comments on commit f93bc84

Please sign in to comment.