Exemplo n.º 1
0
 /**
  * Run forum cron.
  */
 public function execute()
 {
     global $CFG;
     require_once $CFG->dirroot . '/mod/forum/lib.php';
     forum_cron();
 }
Exemplo n.º 2
0
 public function test_forum_message_inbound_multiple_posts()
 {
     $this->resetAfterTest(true);
     // Create a course, with a forum.
     $course = $this->getDataGenerator()->create_course();
     $options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE);
     $forum = $this->getDataGenerator()->create_module('forum', $options);
     // Create a user enrolled in the course as a student.
     list($author) = $this->helper_create_users($course, 1);
     $expectedmessages = array();
     // Post a discussion to the forum.
     list($discussion, $post) = $this->helper_post_to_forum($forum, $author);
     $this->helper_update_post_time($post, -90);
     $expectedmessages[] = array('id' => $post->id, 'subject' => $post->subject, 'count' => 0);
     // Then post a reply to the first discussion.
     $reply = $this->helper_post_to_discussion($forum, $discussion, $author);
     $this->helper_update_post_time($reply, -60);
     $expectedmessages[] = array('id' => $reply->id, 'subject' => $reply->subject, 'count' => 1);
     $expectedcount = 2;
     // Ensure that messageinbound is enabled and configured for the forum handler.
     $this->helper_spoof_message_inbound_setup();
     $author->emailstop = '0';
     set_user_preference('message_provider_mod_forum_posts_loggedoff', 'email', $author);
     set_user_preference('message_provider_mod_forum_posts_loggedin', 'email', $author);
     // Run cron and check that the expected number of users received the notification.
     // Clear the mailsink, and close the messagesink.
     $this->helper->mailsink->clear();
     $this->helper->messagesink->close();
     // Cron daily uses mtrace, turn on buffering to silence output.
     foreach ($expectedmessages as $post) {
         $this->expectOutputRegex("/{$post['count']} users were sent post {$post['id']}, '{$post['subject']}'/");
     }
     forum_cron();
     $messages = $this->helper->mailsink->get_messages();
     // There should be the expected number of messages.
     $this->assertEquals($expectedcount, count($messages));
     foreach ($messages as $message) {
         $this->assertRegExp('/Reply-To: moodlemoodle123\\+[^@]*@example.com/', $message->header);
     }
 }
Exemplo n.º 3
0
 /**
  * Verify forum emails body using templates to generate the expected results.
  *
  * @dataProvider forum_post_email_templates_provider
  * @param array $data provider samples.
  */
 public function test_forum_post_email_templates($data)
 {
     global $DB;
     $this->resetAfterTest();
     // Create the course, with the specified options.
     $options = array();
     foreach ($data['course'] as $option => $value) {
         $options[$option] = $value;
     }
     $course = $this->getDataGenerator()->create_course($options);
     // Create the user, with the specified options and enrol in the course.
     $options = array();
     foreach ($data['user'] as $option => $value) {
         $options[$option] = $value;
     }
     $user = $this->getDataGenerator()->create_user($options);
     $this->getDataGenerator()->enrol_user($user->id, $course->id);
     // Create forums, always force susbscribed (for easy), with the specified options.
     $posts = array();
     foreach ($data['forums'] as $dataforum) {
         $forumposts = isset($dataforum['forumposts']) ? $dataforum['forumposts'] : array();
         unset($dataforum['forumposts']);
         $options = array('course' => $course->id, 'forcesubscribe' => FORUM_FORCESUBSCRIBE);
         foreach ($dataforum as $option => $value) {
             $options[$option] = $value;
         }
         $forum = $this->getDataGenerator()->create_module('forum', $options);
         // Create posts, always for immediate delivery (for easy), with the specified options.
         foreach ($forumposts as $forumpost) {
             $attachments = isset($forumpost['attachments']) ? $forumpost['attachments'] : array();
             unset($forumpost['attachments']);
             $postoptions = array('course' => $course->id, 'forum' => $forum->id, 'userid' => $user->id, 'mailnow' => 1, 'attachment' => !empty($attachments));
             foreach ($forumpost as $option => $value) {
                 $postoptions[$option] = $value;
             }
             list($discussion, $post) = $this->helper_post_to_forum($forum, $user, $postoptions);
             $posts[$post->subject] = $post;
             // Need this to verify cron output.
             // Add the attachments to the post.
             if ($attachments) {
                 $fs = get_file_storage();
                 foreach ($attachments as $attachment) {
                     $filerecord = array('contextid' => context_module::instance($forum->cmid)->id, 'component' => 'mod_forum', 'filearea' => 'attachment', 'itemid' => $post->id, 'filepath' => '/', 'filename' => $attachment['filename']);
                     $fs->create_file_from_string($filerecord, $attachment['filecontents']);
                 }
                 $DB->set_field('forum_posts', 'attachment', '1', array('id' => $post->id));
             }
         }
     }
     // Clear the mailsink and close the messagesink.
     // (surely setup should provide us this cleared but...)
     $this->helper->mailsink->clear();
     $this->helper->messagesink->close();
     // Capture and silence cron output, verifying contents.
     foreach ($posts as $post) {
         $this->expectOutputRegex("/1 users were sent post {$post->id}, '{$post->subject}'/");
     }
     forum_cron();
     // It's really annoying that we have to run cron to test this.
     // Get the mails.
     $mails = $this->helper->mailsink->get_messages();
     // Start testing the expectations.
     $expectations = $data['expectations'];
     // Assert the number is the expected.
     $this->assertSame(count($expectations), count($mails));
     // Start processing mails, first localizing its expectations, then checking them.
     foreach ($mails as $mail) {
         // Find the corresponding expectation.
         $foundexpectation = null;
         foreach ($expectations as $key => $expectation) {
             // All expectations must have a subject for matching.
             if (!isset($expectation['subject'])) {
                 $this->fail('Provider expectation missing mandatory subject');
             }
             if (preg_match('!' . $expectation['subject'] . '!', $mail->subject)) {
                 // If we already had found the expectation, there are non-unique subjects. Fail.
                 if (isset($foundexpectation)) {
                     $this->fail('Multiple expectations found (by subject matching). Please make them unique.');
                 }
                 $foundexpectation = $expectation;
                 unset($expectations[$key]);
             }
         }
         // Arrived here, we should have found the expectations.
         $this->assertNotEmpty($foundexpectation, 'Expectation not found for the mail');
         // If we have found the expectation and have contents to match, let's do it.
         if (isset($foundexpectation) and isset($foundexpectation['contents'])) {
             $mail->body = quoted_printable_decode($mail->body);
             if (!is_array($foundexpectation['contents'])) {
                 // Accept both string and array.
                 $foundexpectation['contents'] = array($foundexpectation['contents']);
             }
             foreach ($foundexpectation['contents'] as $content) {
                 if (strpos($content, '~') !== 0) {
                     $this->assertRegexp('#' . $content . '#m', $mail->body);
                 } else {
                     preg_match('#' . substr($content, 1) . '#m', $mail->body, $matches);
                     $this->assertNotRegexp('#' . substr($content, 1) . '#m', $mail->body);
                 }
             }
         }
     }
     // Finished, there should not be remaining expectations.
     $this->assertCount(0, $expectations);
 }
Exemplo n.º 4
0
 /**
  * Run the forum cron, and check that the specified post was sent the
  * specified number of times.
  *
  * @param integer $expected The number of times that the post should have been sent
  * @return array An array of the messages caught by the message sink
  */
 protected function helper_run_cron_check_count($expected, $messagecount, $mailcount)
 {
     if ($expected === 0) {
         $this->expectOutputRegex('/(Email digests successfully sent to .* users.){0}/');
     } else {
         $this->expectOutputRegex("/Email digests successfully sent to {$expected} users/");
     }
     forum_cron();
     // Now check the results in the message sink.
     $messages = $this->helper->messagesink->get_messages();
     // There should be the expected number of messages.
     $this->assertEquals($messagecount, count($messages));
     // Now check the results in the mail sink.
     $messages = $this->helper->mailsink->get_messages();
     // There should be the expected number of messages.
     $this->assertEquals($mailcount, count($messages));
     return $messages;
 }
Exemplo n.º 5
0
 function test_forum_cron()
 {
     forum_cron();
     $this->assertTrue(true);
 }
Exemplo n.º 6
0
 /**
  * Run the forum cron, and check that the specified post was sent the
  * specified number of times.
  *
  * @param stdClass $post The forum post object
  * @param integer $expected The number of times that the post should have been sent
  * @return array An array of the messages caught by the message sink
  */
 protected function helper_run_cron_check_count($post, $expected)
 {
     // Clear the sinks before running cron.
     $this->helper->messagesink->clear();
     $this->helper->mailsink->clear();
     // Cron daily uses mtrace, turn on buffering to silence output.
     $this->expectOutputRegex("/{$expected} users were sent post {$post->id}, '{$post->subject}'/");
     forum_cron();
     // Now check the results in the message sink.
     $messages = $this->helper->messagesink->get_messages();
     // There should be the expected number of messages.
     $this->assertEquals($expected, count($messages));
     return $messages;
 }
Exemplo n.º 7
0
 /**
  * Run the forum cron, and check that the specified post was sent the
  * specified number of times.
  *
  * @param integer $expected The number of times that the post should have been sent
  * @param integer $individualcount The number of individual messages sent
  * @param integer $digestcount The number of digest messages sent
  */
 protected function helper_run_cron_check_count($expected, $individualcount, $digestcount)
 {
     if ($expected === 0) {
         $this->expectOutputRegex('/(Email digests successfully sent to .* users.){0}/');
     } else {
         $this->expectOutputRegex("/Email digests successfully sent to {$expected} users/");
     }
     forum_cron();
     // Now check the results in the message sink.
     $messages = $this->helper->messagesink->get_messages();
     $counts = (object) array('digest' => 0, 'individual' => 0);
     foreach ($messages as $message) {
         if (strpos($message->subject, 'forum digest') !== false) {
             $counts->digest++;
         } else {
             $counts->individual++;
         }
     }
     $this->assertEquals($digestcount, $counts->digest);
     $this->assertEquals($individualcount, $counts->individual);
 }