Skip to content

Commit

Permalink
MDL-44257 message: added test to make sure the stored file still exists
Browse files Browse the repository at this point in the history
  • Loading branch information
lameze committed Sep 16, 2014
1 parent bd2519c commit 1780c99
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5884,7 +5884,16 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '',
} else {
require_once($CFG->libdir.'/filelib.php');
$mimetype = mimeinfo('type', $attachname);
$mail->addAttachment($CFG->dataroot .'/'. $attachment, $attachname, 'base64', $mimetype);

$attachmentpath = $attachment;

// If the attachment is a full path to a file in the tempdir, use it as is,
// otherwise assume it is a relative path from the dataroot (for backwards compatibility reasons).
if (strpos($attachmentpath, $CFG->tempdir) !== 0) {
$attachmentpath = $CFG->dataroot . '/' . $attachmentpath;
}

$mail->addAttachment($attachmentpath, $attachname, 'base64', $mimetype);
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/tests/messagelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ public function test_message_attachment_send() {
$email = reset($emails);
$this->assertTrue(strpos($email->body, 'Content-Disposition: attachment;') !== false);
$this->assertTrue(strpos($email->body, 'emailtest.txt') !== false);

// Check if the stored file still exists after remove the temporary attachment.
$storedfileexists = $fs->file_exists($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'],
$filerecord['itemid'], $filerecord['filepath'], $filerecord['filename']);
$this->assertTrue($storedfileexists);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions message/output/email/message_output_email.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ function send_message($eventdata) {
} else {
// Copy attachment file to a temporary directory and get the file path.
$attachment = $eventdata->attachment->copy_content_to_temp();
// Function email_to_user() adds $CFG->dataroot to file path, so removing it here.
$attachment = str_replace($CFG->dataroot, '', $attachment);

// Get attachment file name.
$attachname = clean_filename($eventdata->attachname);
}
Expand All @@ -88,8 +87,8 @@ function send_message($eventdata) {
$eventdata->fullmessagehtml, $attachment, $attachname);

// Remove an attachment file if any.
if (!empty($attachment) && file_exists($CFG->dataroot.'/'.$attachment)) {
unlink($CFG->dataroot.'/'.$attachment);
if (!empty($attachment) && file_exists($attachment)) {
unlink($attachment);
}

return $result;
Expand Down

0 comments on commit 1780c99

Please sign in to comment.