function test_everything() { // Add a bunch of comments ouwiki_add_comment(13, 'frog', 'Frogs', 'My happy comment', 'This comment has a title'); ouwiki_add_comment(13, 'frog', 'Frogs', null, 'This one does not'); ouwiki_add_comment(13, null, null, null, 'This comment is on the main page section'); ouwiki_add_comment(13, 'zombie', 'Zombies', null, 'This one is on a different section'); ouwiki_add_comment(66, null, null, null, 'This one is on another page'); ouwiki_add_comment(66, 'frog', 'Frogs', null, 'This one is on another page but same section'); ouwiki_add_comment(13, null, null, null, 'Main 2', 2); ouwiki_add_comment(13, null, null, null, 'Main 3', OUWIKI_SYSTEMUSER); ouwiki_add_comment(13, null, null, null, 'Main 4'); ouwiki_add_comment(13, 'frog', 'Frogs', null, 'Frog 3'); ouwiki_add_comment(13, 'frog', 'Frogs', null, 'Frog 4'); ouwiki_add_comment(13, 'frog', 'Frogs', null, 'Frog 5'); // Check sections $sections = get_records('ouwiki_sections'); $this->assertEqual(count($sections), 5); $this->assertEqual((array) array_shift($sections), array('pageid' => 13, 'xhtmlid' => 'frog', 'title' => 'Frogs', 'id' => 1)); $this->assertEqual((array) array_shift($sections), array('pageid' => 13, 'xhtmlid' => null, 'title' => null, 'id' => 2)); $this->assertEqual((array) array_shift($sections), array('pageid' => 13, 'xhtmlid' => 'zombie', 'title' => 'Zombies', 'id' => 3)); $this->assertEqual((array) array_shift($sections), array('pageid' => 66, 'xhtmlid' => null, 'title' => null, 'id' => 4)); $this->assertEqual((array) array_shift($sections), array('pageid' => 66, 'xhtmlid' => 'frog', 'title' => 'Frogs', 'id' => 5)); $comments = get_records('ouwiki_comments'); $this->assertEqual(count($comments), 12); // Check timeposted once then chuck it because it makes comparison harder $this->assertTrue(time() - $comments[1]->timeposted < 5 && time() - $comments[1]->timeposted >= 0); for ($i = 1; $i <= 12; $i++) { unset($comments[$i]->timeposted); } $this->assertEqual((array) array_shift($comments), array('sectionid' => 1, 'title' => 'My happy comment', 'xhtml' => 'This comment has a title', 'userid' => 1, 'deleted' => 0, 'id' => 1)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 1, 'title' => null, 'xhtml' => 'This one does not', 'userid' => 1, 'deleted' => 0, 'id' => 2)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 2, 'title' => null, 'xhtml' => 'This comment is on the main page section', 'userid' => 1, 'deleted' => 0, 'id' => 3)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 3, 'title' => null, 'xhtml' => 'This one is on a different section', 'userid' => 1, 'deleted' => 0, 'id' => 4)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 4, 'title' => null, 'xhtml' => 'This one is on another page', 'userid' => 1, 'deleted' => 0, 'id' => 5)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 5, 'title' => null, 'xhtml' => 'This one is on another page but same section', 'userid' => 1, 'deleted' => 0, 'id' => 6)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 2, 'title' => null, 'xhtml' => 'Main 2', 'userid' => 2, 'deleted' => 0, 'id' => 7)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 2, 'title' => null, 'xhtml' => 'Main 3', 'userid' => null, 'deleted' => 0, 'id' => 8)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 2, 'title' => null, 'xhtml' => 'Main 4', 'userid' => 1, 'deleted' => 0, 'id' => 9)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 1, 'title' => null, 'xhtml' => 'Frog 3', 'userid' => 1, 'deleted' => 0, 'id' => 10)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 1, 'title' => null, 'xhtml' => 'Frog 4', 'userid' => 1, 'deleted' => 0, 'id' => 11)); $this->assertEqual((array) array_shift($comments), array('sectionid' => 1, 'title' => null, 'xhtml' => 'Frog 5', 'userid' => 1, 'deleted' => 0, 'id' => 12)); // Now delete Frog 3 ouwiki_delete_comment(13, 10, 1); $comment = get_record('ouwiki_comments', 'id', 10); unset($comment->timeposted); $this->assertEqual((array) $comment, array('sectionid' => 1, 'title' => null, 'xhtml' => 'Frog 3', 'userid' => 1, 'deleted' => 1, 'id' => 10)); // Check query for all content... $comments = ouwiki_get_all_comments(13, 'frog'); $comment = $comments[1]; unset($comment->timeposted); // Ensure comments have all expected details (just taking one example) $this->assertEqual((array) $comment, array('sectionid' => 1, 'title' => 'My happy comment', 'xhtml' => 'This comment has a title', 'userid' => 1, 'deleted' => 0, 'sectiontitle' => 'Frogs', 'section' => 'frog', 'firstname' => 'user', 'lastname' => 'one', 'username' => 'u1', 'id' => 1)); // Now check correct comments are included $this->assertEqual(self::get_id_array($comments), array(1, 2, 11, 12)); // Check it works to include deleted ones if asked $comments = ouwiki_get_all_comments(13, 'frog', true); $this->assertEqual(self::get_id_array($comments), array(1, 2, 10, 11, 12)); // Ask for comments from main section with everything else valid... $comments = ouwiki_get_all_comments(13, null, false, array('frog' => 0, 'zombie' => 0)); $this->assertEqual(self::get_id_array($comments), array(3, 7, 8, 9)); // ...and with nothing else valid $comments = ouwiki_get_all_comments(13, null, false, array()); $this->assertEqual(self::get_id_array($comments), array(1, 2, 3, 4, 7, 8, 9, 11, 12)); // Check query for page-display content $comments = ouwiki_get_recent_comments(13, array('frog' => 0, 'zombie' => 0)); $this->assertEqual($comments['']->count, 4); $this->assertEqual(self::get_id_array($comments['']->comments), array(7, 8, 9)); $this->assertEqual($comments['frog']->count, 4); $this->assertEqual(self::get_id_array($comments['frog']->comments), array(2, 11, 12)); $this->assertEqual($comments['zombie']->count, 1); $this->assertEqual(self::get_id_array($comments['zombie']->comments), array(4)); }
if (!preg_match('/^[0-9]+_[0-9]+$/', $section)) { $section = null; } if ($section) { if (!array_key_exists($section, $knownsections)) { error("Unknown section {$section}"); } $sectiontitle = $knownsections[$section]; } // Get other parameters $fromphp = required_param('fromphp', PARAM_RAW); $title = stripslashes(required_param('title', PARAM_RAW)); $xhtml = stripslashes(required_param('xhtml', PARAM_CLEAN)); // Work out redirect url if ($fromphp == 'view') { $url = 'view.php?' . ouwiki_display_wiki_parameters($pagename, $subwiki, $cm, OUWIKI_PARAMS_URL) . '&showcomments' . ($section ? '#ouw_s' . $section : ''); } else { $url = 'comments.php?' . ouwiki_display_wiki_parameters($pagename, $subwiki, $cm, OUWIKI_PARAMS_URL) . ($section ? '§ion=' . $section : ''); } if ('' == preg_replace('/(<.*?>)|( )|\\s/', '', $xhtml)) { error(get_string('commentblank', 'ouwiki'), $url); } // Check post permission require_capability('mod/ouwiki:comment', $context); // Add post ouwiki_add_comment($pageversion->pageid, $section, $section ? $sectiontitle : null, $title, $xhtml); // Redirect redirect($url); ?>