Skip to content
Permalink
Browse files

MDL-53345 forum: Add item id as parameter to support inline attachments

  • Loading branch information...
Ben Kelada
Ben Kelada committed Mar 4, 2016
1 parent 4f33514 commit 41182118ae3ea8c10f99e275d3b69bdf2d27baac
Showing with 113 additions and 15 deletions.
  1. +14 −4 mod/forum/externallib.php
  2. +99 −11 mod/forum/tests/externallib_test.php
@@ -961,6 +961,7 @@ public static function add_discussion_post_parameters() {
'name' => new external_value(PARAM_ALPHANUM,
'The allowed keys (value format) are:
discussionsubscribe (bool); subscribe to the discussion?, default to true
itemid (int); the draft file area id for inline attachments
'),
'value' => new external_value(PARAM_RAW, 'the value of the option,
this param is validated in the external function.'
@@ -995,14 +996,18 @@ public static function add_discussion_post($postid, $subject, $message, $options
));
// Validate options.
$options = array(
'discussionsubscribe' => true
'discussionsubscribe' => true,
'itemid' => 0
);
foreach ($params['options'] as $option) {
$name = trim($option['name']);
switch ($name) {
case 'discussionsubscribe':
$value = clean_param($option['value'], PARAM_BOOL);
break;
case 'itemid':
$value = clean_param($option['value'], PARAM_INT);
break;
default:
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
@@ -1041,7 +1046,7 @@ public static function add_discussion_post($postid, $subject, $message, $options
$post->message = $params['message'];
$post->messageformat = FORMAT_HTML; // Force formatting for now.
$post->messagetrust = trusttext_trusted($context);
$post->itemid = 0;
$post->itemid = $options['itemid'];
if ($postid = forum_add_new_post($post, null)) {
@@ -1117,6 +1122,7 @@ public static function add_discussion_parameters() {
'The allowed keys (value format) are:
discussionsubscribe (bool); subscribe to the discussion?, default to true
discussionpinned (bool); is the discussion pinned, default to false
itemid (int); the draft file area id for inline attachments
'),
'value' => new external_value(PARAM_RAW, 'The value of the option,
This param is validated in the external function.'
@@ -1154,7 +1160,8 @@ public static function add_discussion($forumid, $subject, $message, $groupid = -
// Validate options.
$options = array(
'discussionsubscribe' => true,
'discussionpinned' => false
'discussionpinned' => false,
'itemid' => 0
);
foreach ($params['options'] as $option) {
$name = trim($option['name']);
@@ -1165,6 +1172,9 @@ public static function add_discussion($forumid, $subject, $message, $groupid = -
case 'discussionpinned':
$value = clean_param($option['value'], PARAM_BOOL);
break;
case 'itemid':
$value = clean_param($option['value'], PARAM_INT);
break;
default:
throw new moodle_exception('errorinvalidparam', 'webservice', '', $name);
}
@@ -1209,7 +1219,7 @@ public static function add_discussion($forumid, $subject, $message, $groupid = -
$discussion->message = $params['message'];
$discussion->messageformat = FORMAT_HTML; // Force formatting for now.
$discussion->messagetrust = trusttext_trusted($context);
$discussion->itemid = 0;
$discussion->itemid = $options['itemid'];
$discussion->groupid = $groupid;
$discussion->mailnow = 0;
$discussion->subject = $params['subject'];
@@ -782,7 +782,7 @@ public function test_mod_forum_get_forum_discussions_paginated_qanda() {
* Test add_discussion_post
*/
public function test_add_discussion_post() {
global $CFG;
global $CFG, $USER;
$this->resetAfterTest(true);
@@ -820,24 +820,68 @@ public function test_add_discussion_post() {
$this->getDataGenerator()->enrol_user($user->id, $course->id);
$this->getDataGenerator()->enrol_user($otheruser->id, $course->id);
$post = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
$post = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $post);
$createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...');
$createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
$posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
$posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
// We receive the discussion and the post.
$this->assertEquals(2, count($posts['posts']));
$tested = false;
foreach ($posts['posts'] as $postel) {
if ($post['postid'] == $postel['id']) {
$this->assertEquals('some subject', $postel['subject']);
$this->assertEquals('some text here...', $postel['message']);
foreach ($posts['posts'] as $thispost) {
if ($createdpost['postid'] == $thispost['id']) {
$this->assertEquals('some subject', $thispost['subject']);
$this->assertEquals('some text here...', $thispost['message']);
$tested = true;
}
}
$this->assertTrue($tested);
// Test inline attachment in post
// Create a file in a draft area for inline attachments.
$draftidinlineattach = file_get_unused_draft_itemid();
self::setUser($user);
$usercontext = context_user::instance($user->id);
$filepath = '/';
$filearea = 'draft';
$component = 'user';
$filenameimg = 'shouldbeanimage.txt';
$filerecord = array(
'contextid' => $usercontext->id,
'component' => $component,
'filearea' => $filearea,
'itemid' => $draftidinlineattach,
'filepath' => $filepath,
'filename' => $filenameimg,
);
$fs = get_file_storage();
$fs->create_file_from_string($filerecord, 'image contents (not really)');
$options = array(array('name' => 'itemid', 'value' => $draftidinlineattach));
$dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot
. "/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}"
. '" alt="inlineimage">.';
$createdpost = mod_forum_external::add_discussion_post($discussion->firstpost, 'new post inline attachment',
$dummytext, $options);
$createdpost = external_api::clean_returnvalue(mod_forum_external::add_discussion_post_returns(), $createdpost);
$posts = mod_forum_external::get_forum_discussion_posts($discussion->id);
$posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts);
// We receive the discussion and the post.
// Can't guarantee order of posts during tests.
$postfound = false;
foreach ($posts['posts'] as $thispost) {
if ($createdpost['postid'] == $thispost['id']) {
$this->assertEquals($createdpost['postid'], $thispost['id']);
$this->assertNotContains('draftfile.php', $thispost['message']);
$this->assertContains('pluginfile.php', $thispost['message']);
$postfound = true;
}
}
$this->assertTrue($postfound);
// Check not posting in groups the user is not member of.
$group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
groups_add_member($group->id, $otheruser->id);
@@ -861,7 +905,7 @@ public function test_add_discussion_post() {
* Test add_discussion. A basic test since all the API functions are already covered by unit tests.
*/
public function test_add_discussion() {
global $CFG, $USER;
$this->resetAfterTest(true);
// Create courses to add the modules.
@@ -887,16 +931,16 @@ public function test_add_discussion() {
}
self::setAdminUser();
$discussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
$discussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $discussion);
$createddiscussion = mod_forum_external::add_discussion($forum->id, 'the subject', 'some text here...');
$createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion);
$discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
$this->assertCount(1, $discussions['discussions']);
$this->assertCount(0, $discussions['warnings']);
$this->assertEquals($discussion['discussionid'], $discussions['discussions'][0]['discussion']);
$this->assertEquals($createddiscussion['discussionid'], $discussions['discussions'][0]['discussion']);
$this->assertEquals(-1, $discussions['discussions'][0]['groupid']);
$this->assertEquals('the subject', $discussions['discussions'][0]['subject']);
$this->assertEquals('some text here...', $discussions['discussions'][0]['message']);
@@ -909,6 +953,50 @@ public function test_add_discussion() {
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
$this->assertCount(3, $discussions['discussions']);
$this->assertEquals($discussion2pinned['discussionid'], $discussions['discussions'][0]['discussion']);
// Test inline attachment in new discussion
// Create a file in a draft area for inline attachments.
$draftidinlineattach = file_get_unused_draft_itemid();
$usercontext = context_user::instance($USER->id);
$filepath = '/';
$filearea = 'draft';
$component = 'user';
$filenameimg = 'shouldbeanimage.txt';
$filerecord = array(
'contextid' => $usercontext->id,
'component' => $component,
'filearea' => $filearea,
'itemid' => $draftidinlineattach,
'filepath' => $filepath,
'filename' => $filenameimg,
);
$fs = get_file_storage();
$fs->create_file_from_string($filerecord, 'image contents (not really)');
$dummytext = 'Here is an inline image: <img src="' . $CFG->wwwroot .
"/draftfile.php/{$usercontext->id}/user/draft/{$draftidinlineattach}/{$filenameimg}" .
'" alt="inlineimage">.';
$options = array(array('name' => 'itemid', 'value' => $draftidinlineattach));
$createddiscussion = mod_forum_external::add_discussion($forum->id, 'the inline attachment subject',
$dummytext, -1, $options);
$createddiscussion = external_api::clean_returnvalue(mod_forum_external::add_discussion_returns(), $createddiscussion);
$discussions = mod_forum_external::get_forum_discussions_paginated($forum->id);
$discussions = external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_paginated_returns(), $discussions);
$this->assertCount(4, $discussions['discussions']);
$this->assertCount(0, $createddiscussion['warnings']);
// Can't guarantee order of posts during tests.
$postfound = false;
foreach ($discussions['discussions'] as $thisdiscussion) {
if ($createddiscussion['discussionid'] == $thisdiscussion['discussion']) {
$this->assertNotContains('draftfile.php', $thisdiscussion['message']);
$this->assertContains('pluginfile.php', $thisdiscussion['message']);
$postfound = true;
}
}
$this->assertTrue($postfound);
}
/**

0 comments on commit 4118211

Please sign in to comment.
You can’t perform that action at this time.