/** * Test get forum posts */ public function test_mod_forum_get_forum_discussion_posts() { global $CFG; $this->resetAfterTest(true); // Set the CFG variable to allow track forums. $CFG->forum_trackreadposts = true; // Create a user who can track forums. $record = new stdClass(); $record->trackforums = true; $user1 = self::getDataGenerator()->create_user($record); // Create a bunch of other users to post. $user2 = self::getDataGenerator()->create_user(); $user3 = self::getDataGenerator()->create_user(); // Set the first created user to the test user. self::setUser($user1); // Create course to add the module. $course1 = self::getDataGenerator()->create_course(); // Forum with tracking off. $record = new stdClass(); $record->course = $course1->id; $record->trackingtype = FORUM_TRACKING_OFF; $forum1 = self::getDataGenerator()->create_module('forum', $record); $forum1context = context_module::instance($forum1->cmid); // Add discussions to the forums. $record = new stdClass(); $record->course = $course1->id; $record->userid = $user1->id; $record->forum = $forum1->id; $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $record = new stdClass(); $record->course = $course1->id; $record->userid = $user2->id; $record->forum = $forum1->id; $discussion2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Add 2 replies to the discussion 1 from different users. $record = new stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $record->parent = $discussion1reply1->id; $record->userid = $user3->id; $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); // Enrol the user in the course. $enrol = enrol_get_plugin('manual'); // Following line enrol and assign default role id to the user. // So the user automatically gets mod/forum:viewdiscussion on all forums of the course. $this->getDataGenerator()->enrol_user($user1->id, $course1->id); $this->getDataGenerator()->enrol_user($user2->id, $course1->id); // Delete one user, to test that we still receive posts by this user. delete_user($user3); // Create what we expect to be returned when querying the discussion. $expectedposts = array('posts' => array(), 'warnings' => array()); // Empty picture since it's a user deleted (user3). $userpictureurl = ''; $expectedposts['posts'][] = array('id' => $discussion1reply2->id, 'discussion' => $discussion1reply2->discussion, 'parent' => $discussion1reply2->parent, 'userid' => (int) $discussion1reply2->userid, 'created' => $discussion1reply2->created, 'modified' => $discussion1reply2->modified, 'mailed' => $discussion1reply2->mailed, 'subject' => $discussion1reply2->subject, 'message' => file_rewrite_pluginfile_urls($discussion1reply2->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion1reply2->id), 'messageformat' => 1, 'messagetrust' => $discussion1reply2->messagetrust, 'attachment' => $discussion1reply2->attachment, 'totalscore' => $discussion1reply2->totalscore, 'mailnow' => $discussion1reply2->mailnow, 'children' => array(), 'canreply' => true, 'postread' => false, 'userfullname' => fullname($user3), 'userpictureurl' => $userpictureurl); $userpictureurl = moodle_url::make_webservice_pluginfile_url(context_user::instance($discussion1reply1->userid)->id, 'user', 'icon', null, '/', 'f1')->out(false); $expectedposts['posts'][] = array('id' => $discussion1reply1->id, 'discussion' => $discussion1reply1->discussion, 'parent' => $discussion1reply1->parent, 'userid' => (int) $discussion1reply1->userid, 'created' => $discussion1reply1->created, 'modified' => $discussion1reply1->modified, 'mailed' => $discussion1reply1->mailed, 'subject' => $discussion1reply1->subject, 'message' => file_rewrite_pluginfile_urls($discussion1reply1->message, 'pluginfile.php', $forum1context->id, 'mod_forum', 'post', $discussion1reply1->id), 'messageformat' => 1, 'messagetrust' => $discussion1reply1->messagetrust, 'attachment' => $discussion1reply1->attachment, 'totalscore' => $discussion1reply1->totalscore, 'mailnow' => $discussion1reply1->mailnow, 'children' => array($discussion1reply2->id), 'canreply' => true, 'postread' => false, 'userfullname' => fullname($user2), 'userpictureurl' => $userpictureurl); // Test a discussion with two additional posts (total 3 posts). $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC'); $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts); $this->assertEquals(3, count($posts['posts'])); // Unset the initial discussion post. array_pop($posts['posts']); $this->assertEquals($expectedposts, $posts); // Test discussion without additional posts. There should be only one post (the one created by the discussion). $posts = mod_forum_external::get_forum_discussion_posts($discussion2->id, 'modified', 'DESC'); $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts); $this->assertEquals(1, count($posts['posts'])); }
/** * Test get forum posts (qanda forum) */ public function test_mod_forum_get_forum_discussion_posts_qanda() { global $CFG, $DB; $this->resetAfterTest(true); $record = new stdClass(); $user1 = self::getDataGenerator()->create_user($record); $user2 = self::getDataGenerator()->create_user(); // Set the first created user to the test user. self::setUser($user1); // Create course to add the module. $course1 = self::getDataGenerator()->create_course(); $this->getDataGenerator()->enrol_user($user1->id, $course1->id); $this->getDataGenerator()->enrol_user($user2->id, $course1->id); // Forum with tracking off. $record = new stdClass(); $record->course = $course1->id; $record->type = 'qanda'; $forum1 = self::getDataGenerator()->create_module('forum', $record); $forum1context = context_module::instance($forum1->cmid); // Add discussions to the forums. $record = new stdClass(); $record->course = $course1->id; $record->userid = $user2->id; $record->forum = $forum1->id; $discussion1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Add 1 reply (not the actual user). $record = new stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user2->id; $discussion1reply1 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); // We still see only the original post. $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC'); $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts); $this->assertEquals(1, count($posts['posts'])); // Add a new reply, the user is going to be able to see only the original post and their new post. $record = new stdClass(); $record->discussion = $discussion1->id; $record->parent = $discussion1->firstpost; $record->userid = $user1->id; $discussion1reply2 = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC'); $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts); $this->assertEquals(2, count($posts['posts'])); // Now, we can fake the time of the user post, so he can se the rest of the discussion posts. $discussion1reply2->created -= $CFG->maxeditingtime * 2; $DB->update_record('forum_posts', $discussion1reply2); $posts = mod_forum_external::get_forum_discussion_posts($discussion1->id, 'modified', 'DESC'); $posts = external_api::clean_returnvalue(mod_forum_external::get_forum_discussion_posts_returns(), $posts); $this->assertEquals(3, count($posts['posts'])); }
/** * Test add_discussion_post */ public function test_add_discussion_post() { global $CFG; $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); $otheruser = self::getDataGenerator()->create_user(); self::setAdminUser(); // Create course to add the module. $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0)); // Forum with tracking off. $record = new stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record); $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST); $forumcontext = context_module::instance($forum->cmid); // Add discussions to the forums. $record = new stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Try to post (user not enrolled). self::setUser($user); try { mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...'); $this->fail('Exception expected due to being unenrolled from the course.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } $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); $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']); $tested = true; } } $this->assertTrue($tested); // 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); $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS)); $record->forum = $forum->id; $record->userid = $otheruser->id; $record->groupid = $group->id; $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); try { mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...'); $this->fail('Exception expected due to invalid permissions for posting.'); } catch (moodle_exception $e) { // Expect debugging since we are switching context, and this is something WS_SERVER mode don't like. $this->assertDebuggingCalled(); $this->assertEquals('nopostforum', $e->errorcode); } }
/** * Test add_discussion_post */ public function test_add_discussion_post() { global $CFG; $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); $otheruser = self::getDataGenerator()->create_user(); self::setAdminUser(); // Create course to add the module. $course = self::getDataGenerator()->create_course(array('groupmode' => VISIBLEGROUPS, 'groupmodeforce' => 0)); // Forum with tracking off. $record = new stdClass(); $record->course = $course->id; $forum = self::getDataGenerator()->create_module('forum', $record); $cm = get_coursemodule_from_id('forum', $forum->cmid, 0, false, MUST_EXIST); $forumcontext = context_module::instance($forum->cmid); // Add discussions to the forums. $record = new stdClass(); $record->course = $course->id; $record->userid = $user->id; $record->forum = $forum->id; $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Try to post (user not enrolled). self::setUser($user); try { mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...'); $this->fail('Exception expected due to being unenrolled from the course.'); } catch (moodle_exception $e) { $this->assertEquals('requireloginerror', $e->errorcode); } $this->getDataGenerator()->enrol_user($user->id, $course->id); $this->getDataGenerator()->enrol_user($otheruser->id, $course->id); $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 $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 and regular attachment in post // Create a file in a draft area for inline attachments. $draftidinlineattach = file_get_unused_draft_itemid(); $draftidattach = file_get_unused_draft_itemid(); self::setUser($user); $usercontext = context_user::instance($user->id); $filepath = '/'; $filearea = 'draft'; $component = 'user'; $filenameimg = 'shouldbeanimage.txt'; $filerecordinline = array('contextid' => $usercontext->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => $draftidinlineattach, 'filepath' => $filepath, 'filename' => $filenameimg); $fs = get_file_storage(); // Create a file in a draft area for regular attachments. $filerecordattach = $filerecordinline; $attachfilename = 'attachment.txt'; $filerecordattach['filename'] = $attachfilename; $filerecordattach['itemid'] = $draftidattach; $fs->create_file_from_string($filerecordinline, 'image contents (not really)'); $fs->create_file_from_string($filerecordattach, 'simple text attachment'); $options = array(array('name' => 'inlineattachmentsid', 'value' => $draftidinlineattach), array('name' => 'attachmentsid', 'value' => $draftidattach)); $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->assertEquals($thispost['attachment'], 1, "There should be a non-inline attachment"); $this->assertCount(1, $thispost['attachments'], "There should be 1 attachment"); $this->assertEquals($thispost['attachments'][0]['filename'], $attachfilename, "There should be 1 attachment"); $this->assertContains('pluginfile.php', $thispost['message']); $postfound = true; break; } } $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); $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS)); $record->forum = $forum->id; $record->userid = $otheruser->id; $record->groupid = $group->id; $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); try { mod_forum_external::add_discussion_post($discussion->firstpost, 'some subject', 'some text here...'); $this->fail('Exception expected due to invalid permissions for posting.'); } catch (moodle_exception $e) { $this->assertEquals('nopostforum', $e->errorcode); } }