// Prepare comment for database unset($comment->id); $comment->userid = $USER->id; $comment->postid = $postid; // Special behaviour for moderated users if ($moderated) { // Check IP address if (oublog_too_many_comments_from_ip()) { print_error('error_toomanycomments', 'oublog'); } // Set the confirmed cookie if they haven't got it yet if (!$confirmed) { setcookie(OUBLOG_CONFIRMED_COOKIE, $comment->confirm, time() + 365 * 24 * 3600); // Expire in 1 year } if (!oublog_add_comment_moderated($oublog, $oubloginstance, $post, $comment)) { print_error('couldnotaddcomment', 'oublog'); } $approvaltime = oublog_get_typical_approval_time($post->userid); oublog_build_navigation($oublog, $oubloginstance, isset($oubloguser) ? $oubloguser : null); oublog_get_post_extranav($post); $PAGE->navbar->add(get_string('moderated_submitted', 'oublog')); echo $OUTPUT->header(); notice(get_string('moderated_addedcomment', 'oublog') . ($approvaltime ? ' ' . get_string('moderated_typicaltime', 'oublog', $approvaltime) : ''), 'viewpost.php?post=' . $postid, $course); // does not continue } $comment->userid = $USER->id; if (!oublog_add_comment($course, $cm, $oublog, $comment)) { print_error('couldnotaddcomment', 'oublog'); } add_to_log($course->id, "oublog", "add comment", $viewurl, $oublog->id, $cm->id);
public function test_oublog_add_comment() { global $SITE, $USER, $DB; $this->resetAfterTest(true); $this->setAdminUser(); // Test comment using Personal blog. $oublog = $this->get_new_oublog($SITE->id, array('global' => 1, 'visibility' => OUBLOG_VISIBILITY_PUBLIC)); $cm = get_coursemodule_from_id('oublog', $oublog->cmid); $post = $this->get_post_stub($oublog->id); $postid = oublog_add_post($post, $cm, $oublog, $SITE); $comment = new stdClass(); $comment->title = 'Test Comment'; $comment->messagecomment = array(); $comment->messagecomment['text'] = 'Message for test comment'; $comment->postid = $postid; $comment->userid = $USER->id; $commentid = oublog_add_comment($SITE, $cm, $oublog, $comment); $this->assertTrue(is_int($commentid)); // Get post with comments to check created correctly. $post = oublog_get_post($postid); $this->assertNotEmpty($post->comments); $this->assertTrue(array_key_exists($commentid, $post->comments)); $this->assertEquals($comment->message, $post->comments[$commentid]->message); $this->assertEquals($comment->title, $post->comments[$commentid]->title); $this->assertEquals(fullname($USER), fullname($post->comments[$commentid])); // Check $canaudit sees deleted comments (and other users don't). $DB->update_record('oublog_comments', (object) array('id' => $commentid, 'deletedby' => $USER->id)); $post = oublog_get_post($postid, true); $this->assertNotEmpty($post->comments); $post = oublog_get_post($postid); $this->assertFalse(isset($post->comments)); // Check moderated (not logged-in comments). $bloginstance = $DB->get_record('oublog_instances', array('id' => $post->oubloginstancesid)); $adminid = $USER->id; $this->setGuestUser(); $this->assertFalse(oublog_too_many_comments_from_ip()); $modcomment = new stdClass(); $modcomment->messagecomment = 'TEST'; $modcomment->title = 'TITLE'; $modcomment->postid = $postid; $modcomment->authorname = 'Unittest'; // Catch email sent. unset_config('noemailever'); $sink = $this->redirectEmails(); // Update our admin user email as default is blank. $DB->update_record('user', (object) array('id' => $adminid, 'email' => '*****@*****.**')); $result = oublog_add_comment_moderated($oublog, $bloginstance, $post, $modcomment); $messages = $sink->get_messages(); $this->assertTrue($result); $this->assertEquals(1, count($messages)); $modcomment = $DB->get_record('oublog_comments_moderated', array('postid' => $postid)); $this->assertInstanceOf('stdClass', $modcomment); $id = oublog_approve_comment($modcomment, true); $this->assertTrue(is_int($id)); $saved = $DB->get_record('oublog_comments', array('authorname' => $modcomment->authorname)); $this->assertInstanceOf('stdClass', $saved); // Check post without allowcomments returns no comments (even if added already). $DB->update_record('oublog_posts', (object) array('id' => $postid, 'allowcomments' => 0)); $post = oublog_get_post($postid); $this->assertFalse(isset($post->comments)); }