Skip to content

Commit

Permalink
MDL-54049 core_message: Add missing external format text
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyva committed May 4, 2016
1 parent fc1ef59 commit 871988b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions calendar/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ public static function get_calendar_events($events = array(), $options = array()

foreach ($eventlist as $eventid => $eventobj) {
$event = (array) $eventobj;
// Description formatting.
$calendareventobj = new calendar_event($event);
list($event['description'], $event['format']) = $calendareventobj->format_external_text();

if ($hassystemcap) {
// User can see everything, no further check is needed.
Expand Down
30 changes: 30 additions & 0 deletions calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2706,6 +2706,36 @@ public static function create($properties, $checkcapability = true) {
return false;
}
}

/**
* Format the text using the external API.
* This function should we used when text formatting is required in external functions.
*
* @return array an array containing the text formatted and the text format
*/
public function format_external_text() {

if ($this->editorcontext === null) {
// Switch on the event type to decide upon the appropriate context to use for this event.
$this->editorcontext = $this->properties->context;

if ($this->properties->eventtype != 'user' && $this->properties->eventtype != 'course'
&& $this->properties->eventtype != 'site' && $this->properties->eventtype != 'group') {
// We don't have a context here, do a normal format_text.
return array(format_text($this->properties->description, $this->properties->format), $this->properties->format);
}
}

// Work out the item id for the editor, if this is a repeated event then the files will be associated with the original.
if (!empty($this->properties->repeatid) && $this->properties->repeatid > 0) {
$itemid = $this->properties->repeatid;
} else {
$itemid = $this->properties->id;
}

return external_format_text($this->properties->description, $this->properties->format, $this->editorcontext->id,
'calendar', 'event_description', $itemid);
}
}

/**
Expand Down
32 changes: 32 additions & 0 deletions calendar/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,28 @@ public function test_get_calendar_events() {

// Let's create a few events.
$siteevent = $this->create_calendar_event('site', $USER->id, 'site');

// This event will have description with an inline fake image.
$draftidfile = file_get_unused_draft_itemid();
$usercontext = context_course::instance($course->id);
$filerecord = array(
'contextid' => $usercontext->id,
'component' => 'user',
'filearea' => 'draft',
'itemid' => $draftidfile,
'filepath' => '/',
'filename' => 'fakeimage.png',
);
$fs = get_file_storage();
$fs->create_file_from_string($filerecord, 'img contents');

$record = new stdClass();
$record->courseid = $course->id;
$record->description = array(
'format' => FORMAT_HTML,
'text' => 'Text with img <img src="@@PLUGINFILE@@/fakeimage.png">',
'itemid' => $draftidfile
);
$courseevent = $this->create_calendar_event('course', $USER->id, 'course', 2, time(), $record);
$userevent = $this->create_calendar_event('user', $USER->id);
$record = new stdClass();
Expand All @@ -300,6 +320,18 @@ public function test_get_calendar_events() {
$this->assertEquals(5, count($events['events']));
$this->assertEquals(0, count($events['warnings']));

// Expect the same URL in the description of two different events (because they are repeated).
$coursecontext = context_course::instance($course->id);
$expectedurl = "webservice/pluginfile.php/$coursecontext->id/calendar/event_description/$courseevent->id/fakeimage.png";
$withdescription = 0;
foreach ($events['events'] as $event) {
if (!empty($event['description'])) {
$withdescription++;
$this->assertContains($expectedurl, $event['description']);
}
}
$this->assertEquals(2, $withdescription);

// Let's play around with caps.
$this->setUser($user);
$events = core_calendar_external::get_calendar_events($paramevents, $options);
Expand Down

0 comments on commit 871988b

Please sign in to comment.