/** * Test validation of discussion_subscription_deleted event. */ public function test_discussion_subscription_deleted_validation() { global $CFG, $DB; require_once $CFG->dirroot . '/mod/forum/lib.php'; // Setup test data. $course = $this->getDataGenerator()->create_course(); $user = $this->getDataGenerator()->create_user(); $this->getDataGenerator()->enrol_user($user->id, $course->id); $options = array('course' => $course->id, 'forcesubscribe' => FORUM_INITIALSUBSCRIBE); $forum = $this->getDataGenerator()->create_module('forum', $options); // Add a discussion. $record = array(); $record['course'] = $course->id; $record['forum'] = $forum->id; $record['userid'] = $user->id; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // Add a post. $record = array(); $record['discussion'] = $discussion->id; $record['userid'] = $user->id; $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); // The user is not subscribed to the forum. Insert a new discussion subscription. $subscription = new \stdClass(); $subscription->userid = $user->id; $subscription->forum = $forum->id; $subscription->discussion = $discussion->id; $subscription->preference = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED; $subscription->id = $DB->insert_record('forum_discussion_subs', $subscription); $context = context_module::instance($forum->cmid); $params = array('context' => $context, 'objectid' => $subscription->id, 'relateduserid' => $user->id, 'other' => array('forumid' => $forum->id, 'discussion' => $discussion->id)); $event = \mod_forum\event\discussion_subscription_deleted::create($params); // Trigger and capturing the event. $sink = $this->redirectEvents(); $event->trigger(); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Without an invalid context. $params['context'] = \context_course::instance($course->id); $this->setExpectedException('coding_exception', 'Context level must be CONTEXT_MODULE.'); \mod_forum\event\discussion_deleted::create($params); // Without the discussion. unset($params['discussion']); $this->setExpectedException('coding_exception', 'The \'discussion\' value must be set in other.'); \mod_forum\event\discussion_deleted::create($params); // Without the forumid. unset($params['forumid']); $this->setExpectedException('coding_exception', 'The \'forumid\' value must be set in other.'); \mod_forum\event\discussion_deleted::create($params); // Without the relateduserid. unset($params['relateduserid']); $this->setExpectedException('coding_exception', 'The \'relateduserid\' value must be set in other.'); \mod_forum\event\discussion_deleted::create($params); }
print_error("cannotdeletepost", "forum", forum_go_back_to("discuss.php?d={$post->discussion}")); } if ($post->totalscore) { notice(get_string('couldnotdeleteratings', 'rating'), forum_go_back_to("discuss.php?d={$post->discussion}")); } else { if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) { print_error("couldnotdeletereplies", "forum", forum_go_back_to("discuss.php?d={$post->discussion}")); } else { if (!$post->parent) { // post is a discussion topic as well, so delete discussion if ($forum->type == 'single') { notice("Sorry, but you are not allowed to delete that discussion!", forum_go_back_to("discuss.php?d={$post->discussion}")); } forum_delete_discussion($discussion, false, $course, $cm, $forum); $params = array('objectid' => $discussion->id, 'context' => $modcontext, 'other' => array('forumid' => $forum->id)); $event = \mod_forum\event\discussion_deleted::create($params); $event->add_record_snapshot('forum_discussions', $discussion); $event->trigger(); redirect("view.php?f={$discussion->forum}"); } else { if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext), $course, $cm, $forum)) { if ($forum->type == 'single') { // Single discussion forums are an exception. We show // the forum itself since it only has one discussion // thread. $discussionurl = "view.php?f={$forum->id}"; } else { $discussionurl = "discuss.php?d={$post->discussion}"; } $params = array('context' => $modcontext, 'objectid' => $post->id, 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)); if ($post->userid !== $USER->id) {
/** * Test discussion_deleted event. */ public function test_discussion_deleted() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); // Add a discussion. $record = array(); $record['course'] = $course->id; $record['forum'] = $forum->id; $record['userid'] = $user->id; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); $context = context_module::instance($forum->cmid); $params = array('context' => $context, 'objectid' => $discussion->id, 'other' => array('forumid' => $forum->id)); $event = \mod_forum\event\discussion_deleted::create($params); // Trigger and capture the event. $sink = $this->redirectEvents(); $event->trigger(); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\discussion_deleted', $event); $this->assertEquals($context, $event->get_context()); $expected = array($course->id, 'forum', 'delete discussion', "view.php?id={$forum->cmid}", $forum->id, $forum->cmid); $this->assertEventLegacyLogData($expected, $event); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); }