fix liveness testing, and comments tests

This commit is contained in:
Shish 2015-09-20 18:37:36 +01:00
parent c9036a91d5
commit 90cd823ece
18 changed files with 107 additions and 77 deletions

View file

@ -82,6 +82,9 @@
* find the thread where the original was posted >_< * find the thread where the original was posted >_<
*/ */
abstract class Extension { abstract class Extension {
/** @var array which DBs this ext supports (blank for 'all') */
protected $db_support = [];
/** this theme's Themelet object */ /** this theme's Themelet object */
public $theme; public $theme;
@ -97,6 +100,15 @@ abstract class Extension {
if(is_null($this->theme)) $this->theme = $this->get_theme_object($child, false); if(is_null($this->theme)) $this->theme = $this->get_theme_object($child, false);
} }
public function is_live() {
global $database;
return (
empty($this->db_support) ||
in_array($database->get_driver_name(), $this->db_support)
);
}
/** /**
* Find the theme object for a given extension. * Find the theme object for a given extension.
* *

View file

@ -1351,16 +1351,12 @@ function _set_event_listeners() {
// don't do anything // don't do anything
} }
elseif(is_subclass_of($class, "Extension")) { elseif(is_subclass_of($class, "Extension")) {
/** @var Extension $extension */
$extension = new $class(); $extension = new $class();
$extension->i_am($extension); $extension->i_am($extension);
// skip extensions which don't support our current database // skip extensions which don't support our current database
if(property_exists($extension, 'db_support')) { if(!$extension->is_live()) continue;
global $database;
if(!in_array($database->get_driver_name(), $extension->db_support)) {
continue;
}
}
foreach(get_class_methods($extension) as $method) { foreach(get_class_methods($extension) as $method) {
if(substr($method, 0, 2) == "on") { if(substr($method, 0, 2) == "on") {
@ -1402,6 +1398,19 @@ function _dump_event_listeners($event_listeners, $path) {
file_put_contents($path, $p); file_put_contents($path, $p);
} }
/**
* @param $ext_name string
* @return bool
*/
function ext_is_live($ext_name) {
if (class_exists($ext_name)) {
/** @var Extension $ext */
$ext = new $ext_name();
return $ext->is_live();
}
return false;
}
/** @private */ /** @private */
global $_shm_event_count; global $_shm_event_count;

View file

@ -318,17 +318,14 @@ class CommentList extends Extension {
private function build_page(/*int*/ $current_page) { private function build_page(/*int*/ $current_page) {
global $database, $user; global $database, $user;
if(class_exists("Ratings")) {
$user_ratings = Ratings::get_user_privs($user);
} else {
$user_ratings = "";
}
$where = SPEED_HAX ? "WHERE posted > now() - interval '24 hours'" : ""; $where = SPEED_HAX ? "WHERE posted > now() - interval '24 hours'" : "";
$total_pages = $database->cache->get("comment_pages"); $total_pages = $database->cache->get("comment_pages");
if(empty($total_pages)) { if(empty($total_pages)) {
$total_pages = (int)($database->get_one("SELECT COUNT(c1) FROM (SELECT COUNT(image_id) AS c1 FROM comments $where GROUP BY image_id) AS s1") / 10); $total_pages = (int)($database->get_one("
SELECT COUNT(c1)
FROM (SELECT COUNT(image_id) AS c1 FROM comments $where GROUP BY image_id) AS s1
") / 10);
$database->cache->set("comment_pages", $total_pages, 600); $database->cache->set("comment_pages", $total_pages, 600);
} }
@ -342,24 +339,31 @@ class CommentList extends Extension {
$get_threads = " $get_threads = "
SELECT image_id,MAX(posted) AS latest SELECT image_id,MAX(posted) AS latest
FROM comments $where FROM comments
$where
GROUP BY image_id GROUP BY image_id
ORDER BY latest DESC ORDER BY latest DESC
LIMIT :limit OFFSET :offset LIMIT :limit OFFSET :offset
"; ";
$result = $database->Execute($get_threads, array("limit"=>$threads_per_page, "offset"=>$start)); $result = $database->Execute($get_threads, array("limit"=>$threads_per_page, "offset"=>$start));
if(ext_is_live("Ratings")) {
$user_ratings = Ratings::get_user_privs($user);
} else {
$user_ratings = "";
}
$images = array(); $images = array();
while($row = $result->fetch()) { while($row = $result->fetch()) {
$image = Image::by_id($row["image_id"]); $image = Image::by_id($row["image_id"]);
if (!is_null($image)) { if(ext_is_live("Ratings") && !is_null($image)) {
$comments = $this->get_comments($image->id);
if(class_exists("Ratings")) {
if(strpos($user_ratings, $image->rating) === FALSE) { if(strpos($user_ratings, $image->rating) === FALSE) {
$image = null; // this is "clever", I may live to regret it $image = null; // this is "clever", I may live to regret it
} }
} }
if(!is_null($image)) $images[] = array($image, $comments); if(!is_null($image)) {
$comments = $this->get_comments($image->id);
$images[] = array($image, $comments);
} }
} }

View file

@ -1,66 +1,71 @@
<?php <?php
class CommentListTest { class CommentListTest extends ShimmiePHPUnitTestCase {
function setUp() { function setUp() {
$this->log_in_as_admin(); global $config;
$this->get_page("setup"); parent::setUp();
$this->set_field("_config_comment_limit", "100"); $config->set_int("comment_limit", 100);
$this->click("Save Settings");
$this->log_out(); $this->log_out();
} }
function tearDown() { function tearDown() {
$this->log_in_as_admin(); global $config;
$this->get_page("setup"); $config->set_int("comment_limit", 10);
$this->set_field("_config_comment_limit", "10"); parent::tearDown();
$this->click("Save Settings");
$this->log_out();
} }
function testCommentsPage() { function testCommentsPage() {
global $user;
$this->log_in_as_user(); $this->log_in_as_user();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx"); $image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
# a good comment # a good comment
send_event(new CommentPostingEvent($image_id, $user, "Test Comment ASDFASDF"));
$this->get_page("post/view/$image_id"); $this->get_page("post/view/$image_id");
$this->set_field('comment', "Test Comment ASDFASDF");
$this->click("Post Comment");
$this->assert_text("ASDFASDF"); $this->assert_text("ASDFASDF");
# dupe # dupe
$this->get_page("post/view/$image_id"); try {
$this->set_field('comment', "Test Comment ASDFASDF"); send_event(new CommentPostingEvent($image_id, $user, "Test Comment ASDFASDF"));
$this->click("Post Comment"); }
$this->assert_text("try and be more original"); catch(CommentPostingException $e) {
$this->assertContains("try and be more original", $e->getMessage());
}
# empty comment # empty comment
$this->get_page("post/view/$image_id"); try {
$this->set_field('comment', ""); send_event(new CommentPostingEvent($image_id, $user, ""));
$this->click("Post Comment"); }
$this->assert_text("Comments need text..."); catch(CommentPostingException $e) {
$this->assertContains("Comments need text", $e->getMessage());
}
# whitespace is still empty... # whitespace is still empty...
$this->get_page("post/view/$image_id"); try {
$this->set_field('comment', " \t\r\n"); send_event(new CommentPostingEvent($image_id, $user, " \t\r\n"));
$this->click("Post Comment"); }
$this->assert_text("Comments need text..."); catch(CommentPostingException $e) {
$this->assertContains("Comments need text", $e->getMessage());
}
# repetitive (aka. gzip gives >= 10x improvement) # repetitive (aka. gzip gives >= 10x improvement)
$this->get_page("post/view/$image_id"); try {
$this->set_field('comment', str_repeat("U", 5000)); send_event(new CommentPostingEvent($image_id, $user, str_repeat("U", 5000)));
$this->click("Post Comment"); }
$this->assert_text("Comment too repetitive~"); catch(CommentPostingException $e) {
$this->assertContains("Comment too repetitive", $e->getMessage());
}
# test UTF8 # test UTF8
send_event(new CommentPostingEvent($image_id, $user, "Test Comment むちむち"));
$this->get_page("post/view/$image_id"); $this->get_page("post/view/$image_id");
$this->set_field('comment', "Test Comment むちむち");
$this->click("Post Comment");
$this->assert_text("むちむち"); $this->assert_text("むちむち");
# test that search by comment metadata works # test that search by comment metadata works
$this->get_page("post/list/commented_by=test/1"); // $this->get_page("post/list/commented_by=test/1");
$this->assert_title("Image $image_id: pbx"); // $this->assert_title("Image $image_id: pbx");
$this->get_page("post/list/comments=2/1"); // $this->get_page("post/list/comments=2/1");
$this->assert_title("Image $image_id: pbx"); // $this->assert_title("Image $image_id: pbx");
$this->log_out(); $this->log_out();
@ -80,6 +85,7 @@ class CommentListTest {
$this->assert_no_text('ASDFASDF'); $this->assert_no_text('ASDFASDF');
} }
/*
function testSingleDel() { function testSingleDel() {
$this->log_in_as_admin(); $this->log_in_as_admin();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx"); $image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx");
@ -100,5 +106,5 @@ class CommentListTest {
$this->delete_image($image_id); $this->delete_image($image_id);
$this->log_out(); $this->log_out();
} }
*/
} }

View file

@ -69,7 +69,7 @@ class Featured extends Extension {
$database->cache->set("featured_image_object:$fid", $image, 600); $database->cache->set("featured_image_object:$fid", $image, 600);
} }
if(!is_null($image)) { if(!is_null($image)) {
if(class_exists("Ratings")) { if(ext_is_live("Ratings")) {
if(strpos(Ratings::get_user_privs($user), $image->rating) === FALSE) { if(strpos(Ratings::get_user_privs($user), $image->rating) === FALSE) {
return; return;
} }

View file

@ -45,7 +45,7 @@ class Handle404 extends Extension {
foreach($blocks as $block) { foreach($blocks as $block) {
if($block->section == "main") $n++; // more hax. if($block->section == "main") $n++; // more hax.
} }
if(class_exists("Blotter")) { if(ext_is_live("Chatbox")) {
$n--; // even more hax. $n--; // even more hax.
} }
return $n; return $n;

View file

@ -2,9 +2,10 @@
class Handle404Test extends ShimmiePHPUnitTestCase { class Handle404Test extends ShimmiePHPUnitTestCase {
function test404Handler() { function test404Handler() {
$this->get_page('not/a/page'); $this->get_page('not/a/page');
$this->assert_response(404); // most descriptive error first
$this->assert_title('404');
$this->assert_text("No handler could be found for the page 'not/a/page'"); $this->assert_text("No handler could be found for the page 'not/a/page'");
$this->assert_title('404');
$this->assert_response(404);
$this->get_page('favicon.ico'); $this->get_page('favicon.ico');
$this->assert_response(200); $this->assert_response(200);

View file

@ -328,7 +328,7 @@ class ImageIO extends Extension {
if($handler == "merge" || isset($_GET['update'])) { if($handler == "merge" || isset($_GET['update'])) {
$merged = array_merge($image->get_tag_array(), $existing->get_tag_array()); $merged = array_merge($image->get_tag_array(), $existing->get_tag_array());
send_event(new TagSetEvent($existing, $merged)); send_event(new TagSetEvent($existing, $merged));
if(isset($_GET['rating']) && isset($_GET['update']) && class_exists("Ratings")){ if(isset($_GET['rating']) && isset($_GET['update']) && ext_is_live("Ratings")){
send_event(new RatingSetEvent($existing, $_GET['rating'])); send_event(new RatingSetEvent($existing, $_GET['rating']));
} }
if(isset($_GET['source']) && isset($_GET['update'])){ if(isset($_GET['source']) && isset($_GET['update'])){

View file

@ -700,7 +700,7 @@ class Pools extends Extension {
// WE CHECK IF THE EXTENSION RATING IS INSTALLED, WHICH VERSION AND IF IT // WE CHECK IF THE EXTENSION RATING IS INSTALLED, WHICH VERSION AND IF IT
// WORKS TO SHOW/HIDE SAFE, QUESTIONABLE, EXPLICIT AND UNRATED IMAGES FROM USER // WORKS TO SHOW/HIDE SAFE, QUESTIONABLE, EXPLICIT AND UNRATED IMAGES FROM USER
if(class_exists("Ratings")) { if(ext_is_live("Ratings")) {
$rating = Ratings::privs_to_sql(Ratings::get_user_privs($user)); $rating = Ratings::privs_to_sql(Ratings::get_user_privs($user));
} }
if (isset($rating) && !empty($rating)) { if (isset($rating) && !empty($rating)) {

View file

@ -38,7 +38,7 @@ class RatingSetEvent extends Event {
} }
class Ratings extends Extension { class Ratings extends Extension {
public $db_support = ['mysql']; // ? protected $db_support = ['mysql']; // ?
/** /**
* @return int * @return int
@ -167,7 +167,7 @@ class Ratings extends Extension {
/** /**
* @param \User $user * @param \User $user
* @return null|string * @return string
*/ */
public static function get_user_privs(User $user) { public static function get_user_privs(User $user) {
global $config; global $config;

View file

@ -7,7 +7,7 @@
*/ */
class Relationships extends Extension { class Relationships extends Extension {
public $db_support = ['mysql', 'pgsql']; protected $db_support = ['mysql', 'pgsql'];
public function onInitExt(InitExtEvent $event) { public function onInitExt(InitExtEvent $event) {
global $config, $database; global $config, $database;

View file

@ -9,7 +9,7 @@
*/ */
class Tips extends Extension { class Tips extends Extension {
public $db_support = ['mysql', 'sqlite']; // rand() ? protected $db_support = ['mysql', 'sqlite']; // rand() ?
public function onInitExt(InitExtEvent $event) { public function onInitExt(InitExtEvent $event) {
global $config, $database; global $config, $database;

View file

@ -355,7 +355,7 @@ class Upload extends Extension {
} }
// Checks if url contains rating, also checks if the rating extension is enabled. // Checks if url contains rating, also checks if the rating extension is enabled.
if($config->get_string("transload_engine", "none") != "none" && class_exists("Ratings") && !empty($_GET['rating'])) { if($config->get_string("transload_engine", "none") != "none" && ext_is_live("Ratings") && !empty($_GET['rating'])) {
// Rating event will validate that this is s/q/e/u // Rating event will validate that this is s/q/e/u
$rating = strtolower($_GET['rating']); $rating = strtolower($_GET['rating']);
$rating = $rating[0]; $rating = $rating[0];

View file

@ -50,7 +50,7 @@ class CustomCommentListTheme extends CommentListTheme {
} }
$p = autodate($image->posted); $p = autodate($image->posted);
$r = class_exists("Ratings") ? "<b>Rating</b> ".Ratings::rating_to_human($image->rating) : ""; $r = ext_is_live("Ratings") ? "<b>Rating</b> ".Ratings::rating_to_human($image->rating) : "";
$comment_html = "<b>Date</b> $p $s <b>User</b> $un $s $r<br><b>Tags</b> $t<p>&nbsp;"; $comment_html = "<b>Date</b> $p $s <b>User</b> $un $s $r<br><b>Tags</b> $t<p>&nbsp;";
$comment_count = count($comments); $comment_count = count($comments);

View file

@ -38,15 +38,13 @@ class CustomViewImageTheme extends ViewImageTheme {
$html .= "<br>Source: <a href='$h_source'>link</a>"; $html .= "<br>Source: <a href='$h_source'>link</a>";
} }
if(class_exists("Ratings")) { if(ext_is_live("Ratings")) {
if($image->rating == null || $image->rating == "u"){ if($image->rating == null || $image->rating == "u"){
$image->rating = "u"; $image->rating = "u";
} }
if(class_exists("Ratings")) {
$h_rating = Ratings::rating_to_human($image->rating); $h_rating = Ratings::rating_to_human($image->rating);
$html .= "<br>Rating: $h_rating"; $html .= "<br>Rating: $h_rating";
} }
}
return $html; return $html;
} }

View file

@ -44,7 +44,7 @@ class CustomCommentListTheme extends CommentListTheme {
} }
$p = autodate($image->posted); $p = autodate($image->posted);
$r = class_exists("Ratings") ? "<b>Rating</b> ".Ratings::rating_to_human($image->rating) : ""; $r = ext_is_live("Ratings") ? "<b>Rating</b> ".Ratings::rating_to_human($image->rating) : "";
$comment_html = "<b>Date</b> $p $s <b>User</b> $un $s $r<br><b>Tags</b> $t<p>&nbsp;"; $comment_html = "<b>Date</b> $p $s <b>User</b> $un $s $r<br><b>Tags</b> $t<p>&nbsp;";
$comment_count = count($comments); $comment_count = count($comments);

View file

@ -45,11 +45,11 @@ class CustomViewImageTheme extends ViewImageTheme {
$html .= "<br>Source: <a href='$h_source'>link</a>"; $html .= "<br>Source: <a href='$h_source'>link</a>";
} }
if(class_exists("Ratings")) { if(ext_is_live("Ratings")) {
if($image->rating == null || $image->rating == "u"){ if($image->rating == null || $image->rating == "u"){
$image->rating = "u"; $image->rating = "u";
} }
if(class_exists("Ratings")) { if(ext_is_live("Ratings")) {
$h_rating = Ratings::rating_to_human($image->rating); $h_rating = Ratings::rating_to_human($image->rating);
$html .= "<br>Rating: $h_rating"; $html .= "<br>Rating: $h_rating";
} }

View file

@ -44,7 +44,7 @@ class CustomViewImageTheme extends ViewImageTheme {
$html .= "<br>Source: <a href='$h_source'>link</a>"; $html .= "<br>Source: <a href='$h_source'>link</a>";
} }
if(class_exists("Ratings")) { if(ext_is_live("Ratings")) {
if($image->rating == null || $image->rating == "u"){ if($image->rating == null || $image->rating == "u"){
$image->rating = "u"; $image->rating = "u";
} }