From 4abf2ac7dd75614021fad5b44940dbd76ebdc41a Mon Sep 17 00:00:00 2001 From: Shish Date: Thu, 16 Jul 2009 20:20:53 +0100 Subject: [PATCH] more tests --- contrib/handle_flash/main.php | 64 +++++++++++------------------------ contrib/handle_ico/main.php | 9 +++-- contrib/handle_mp3/main.php | 39 +++++---------------- contrib/handle_svg/main.php | 16 +++++---- contrib/rss_images/test.php | 14 +++++--- contrib/word_filter/main.php | 2 ++ contrib/word_filter/test.php | 55 +++++++++++++++++++++++++++++- core/extension.class.php | 36 ++++++++++++++++++++ ext/alias_editor/test.php | 56 ++++++++++++++++++++++++++++++ ext/handle_pixel/main.php | 40 +++------------------- ext/index/test.php | 8 +++++ ext/upload/main.php | 3 ++ ext/upload/test.php | 18 ++++++++++ 13 files changed, 233 insertions(+), 127 deletions(-) diff --git a/contrib/handle_flash/main.php b/contrib/handle_flash/main.php index cd2671c4..be3ac459 100644 --- a/contrib/handle_flash/main.php +++ b/contrib/handle_flash/main.php @@ -5,46 +5,19 @@ * Description: Handle Flash files */ -class FlashFileHandler extends SimpleExtension { - public function onDataUpload($event) { - if($this->supported_ext($event->type) && $this->check_contents($event->tmpname)) { - $hash = $event->hash; - $ha = substr($hash, 0, 2); - if(!move_upload_to_archive($event)) return; - send_event(new ThumbnailGenerationEvent($event->hash, $event->type)); - $image = $this->create_image_from_data("images/$ha/$hash", $event->metadata); - if(is_null($image)) { - throw new UploadException( - "Flash Handler failed to create image object from data. ". - "Note: compressed flash files are currently unsupported"); - } - send_event(new ImageAdditionEvent($event->user, $image)); - } +class FlashFileHandler extends DataHandlerExtension { + protected function create_thumb($hash) { + $ha = substr($hash, 0, 2); + // FIXME: scale image, as not all boards use 192x192 + copy("ext/handle_flash/thumb.jpg", "thumbs/$ha/$hash"); } - public function onThumbnailGeneration($event) { - if($this->supported_ext($event->type)) { - $hash = $event->hash; - $ha = substr($hash, 0, 2); - // FIXME: scale image, as not all boards use 192x192 - copy("ext/handle_flash/thumb.jpg", "thumbs/$ha/$hash"); - } - } - - public function onDisplayingImage($event) { - global $page; - if($this->supported_ext($event->image->ext)) { - $this->theme->display_image($event->page, $event->image); - } - } - - - private function supported_ext($ext) { + protected function supported_ext($ext) { $exts = array("swf"); return in_array(strtolower($ext), $exts); } - private function create_image_from_data($filename, $metadata) { + protected function create_image_from_data($filename, $metadata) { global $config; $image = new Image(); @@ -72,6 +45,17 @@ class FlashFileHandler extends SimpleExtension { return $image; } + protected function check_contents($file) { + if(!file_exists($file)) return false; + + $fp = fopen($file, "r"); + $head = fread($fp, 3); + fclose($fp); + if(!in_array($head, array("CWS", "FWS"))) return false; + + return true; + } + private function str_to_binarray($string) { $binary = array(); for($j=0; $j diff --git a/contrib/handle_ico/main.php b/contrib/handle_ico/main.php index 678f2579..45f2940f 100644 --- a/contrib/handle_ico/main.php +++ b/contrib/handle_ico/main.php @@ -16,7 +16,9 @@ class IcoFileHandler extends SimpleExtension { if(is_null($image)) { throw new UploadException("Icon handler failed to create image object from data"); } - send_event(new ImageAdditionEvent($event->user, $image)); + $iae = new ImageAdditionEvent($event->user, $image); + send_event($iae); + $event->image_id = $iae->image->id; } } @@ -27,13 +29,14 @@ class IcoFileHandler extends SimpleExtension { } public function onDisplayingImage($event) { + global $page; if($this->supported_ext($event->image->ext)) { - $this->theme->display_image($event->page, $event->image); + $this->theme->display_image($page, $event->image); } } public function onPageRequest($event) { - global $config, $database; + global $config, $database, $page; if($event->page_matches("get_ico")) { $id = int_escape($event->get_arg(0)); $image = Image::by_id($id); diff --git a/contrib/handle_mp3/main.php b/contrib/handle_mp3/main.php index dc771a93..91255f5a 100644 --- a/contrib/handle_mp3/main.php +++ b/contrib/handle_mp3/main.php @@ -5,42 +5,19 @@ * Description: Handle MP3 files */ -class MP3FileHandler implements Extension { - var $theme; - - public function receive_event(Event $event) { - if(is_null($this->theme)) $this->theme = get_theme_object($this); - - if(($event instanceof DataUploadEvent) && $this->supported_ext($event->type) && $this->check_contents($event->tmpname)) { - $hash = $event->hash; - $ha = substr($hash, 0, 2); - if(!move_upload_to_archive($event)) return; - send_event(new ThumbnailGenerationEvent($event->hash, $event->type)); - $image = $this->create_image_from_data("images/$ha/$hash", $event->metadata); - if(is_null($image)) { - throw new UploadException("MP3 handler failed to create image object from data"); - } - send_event(new ImageAdditionEvent($event->user, $image)); - } - - if(($event instanceof ThumbnailGenerationEvent) && $this->supported_ext($event->type)) { - $hash = $event->hash; - $ha = substr($hash, 0, 2); - // FIXME: scale image, as not all boards use 192x192 - copy("ext/handle_mp3/thumb.jpg", "thumbs/$ha/$hash"); - } - - if(($event instanceof DisplayingImageEvent) && $this->supported_ext($event->image->ext)) { - $this->theme->display_image($event->page, $event->image); - } +class MP3FileHandler extends DataHandlerExtension { + protected function create_thumb($hash) { + $ha = substr($hash, 0, 2); + // FIXME: scale image, as not all boards use 192x192 + copy("ext/handle_mp3/thumb.jpg", "thumbs/$ha/$hash"); } - private function supported_ext($ext) { + protected function supported_ext($ext) { $exts = array("mp3"); return in_array(strtolower($ext), $exts); } - private function create_image_from_data($filename, $metadata) { + protected function create_image_from_data($filename, $metadata) { global $config; $image = new Image(); @@ -59,7 +36,7 @@ class MP3FileHandler implements Extension { return $image; } - private function check_contents($file) { + protected function check_contents($file) { // FIXME: mp3 magic header? return (file_exists($file)); } diff --git a/contrib/handle_svg/main.php b/contrib/handle_svg/main.php index 741a3805..401898cb 100644 --- a/contrib/handle_svg/main.php +++ b/contrib/handle_svg/main.php @@ -20,7 +20,9 @@ class SVGFileHandler implements Extension { if(is_null($image)) { throw new UploadException("SVG handler failed to create image object from data"); } - send_event(new ImageAdditionEvent($event->user, $image)); + $iae = new ImageAdditionEvent($event->user, $image); + send_event($iae); + $event->image_id = $iae->image->id; } if(($event instanceof ThumbnailGenerationEvent) && $this->supported_ext($event->type)) { @@ -44,20 +46,20 @@ class SVGFileHandler implements Extension { } if(($event instanceof DisplayingImageEvent) && $this->supported_ext($event->image->ext)) { - $this->theme->display_image($event->page, $event->image); + global $page; + $this->theme->display_image($page, $event->image); } if(($event instanceof PageRequestEvent) && $event->page_matches("get_svg")) { - global $config; - global $database; + global $config, $database, $page; $id = int_escape($event->get_arg(0)); $image = Image::by_id($id); $hash = $image->hash; $ha = substr($hash, 0, 2); - $event->page->set_type("image/svg+xml"); - $event->page->set_mode("data"); - $event->page->set_data(file_get_contents("images/$ha/$hash")); + $page->set_type("image/svg+xml"); + $page->set_mode("data"); + $page->set_data(file_get_contents("images/$ha/$hash")); } } diff --git a/contrib/rss_images/test.php b/contrib/rss_images/test.php index fb27131b..9b1c4f3a 100644 --- a/contrib/rss_images/test.php +++ b/contrib/rss_images/test.php @@ -5,9 +5,6 @@ class RSSImagesTest extends ShimmieWebTestCase { $image_id = $this->post_image("ext/simpletest/data/pbx_screenshot.jpg", "pbx computer screenshot"); $this->log_out(); - $this->assertTitle("Upload Status"); - $this->assertText("already has hash"); - $this->get_page('rss/images'); $this->assertMime("application/rss+xml"); $this->assertNoText("Exception"); @@ -16,11 +13,18 @@ class RSSImagesTest extends ShimmieWebTestCase { $this->assertMime("application/rss+xml"); $this->assertNoText("Exception"); - $this->get_page('rss/images/tagme/1'); + # FIXME: test that the image is actually found + $this->get_page('rss/images/computer/1'); $this->assertMime("application/rss+xml"); $this->assertNoText("Exception"); - $this->get_page('rss/images/tagme/2'); + # valid tag, invalid page + $this->get_page('rss/images/computer/2'); + $this->assertMime("application/rss+xml"); + $this->assertNoText("Exception"); + + # not found + $this->get_page('rss/images/waffle/2'); $this->assertMime("application/rss+xml"); $this->assertNoText("Exception"); diff --git a/contrib/word_filter/main.php b/contrib/word_filter/main.php index f471b8d4..dc2a8f9a 100644 --- a/contrib/word_filter/main.php +++ b/contrib/word_filter/main.php @@ -23,6 +23,8 @@ class WordFilter implements Extension { private function filter($text) { $map = $this->get_map(); foreach($map as $search => $replace) { + $search = trim($search); + $replace = trim($replace); $search = "/\\b$search\\b/i"; $text = preg_replace($search, $replace, $text); } diff --git a/contrib/word_filter/test.php b/contrib/word_filter/test.php index e82fa0ad..e84c8882 100644 --- a/contrib/word_filter/test.php +++ b/contrib/word_filter/test.php @@ -1,5 +1,58 @@ log_in_as_admin(); + $this->get_page("setup"); + $this->setField("_config_word_filter", "whore,nice lady\na duck,a kitten\n white ,\tspace\ninvalid"); + $this->click("Save Settings"); + $this->log_out(); + + $this->log_in_as_user(); + $image_id = $this->post_image("ext/simpletest/data/pbx_screenshot.jpg", "pbx computer screenshot"); + $this->get_page("post/view/$image_id"); + + # regular + $this->setField('comment', "posted by a whore"); + $this->click("Post Comment"); + $this->assertText("posted by a nice lady"); + + # replace all instances + $this->setField('comment', "a whore is a whore is a whore"); + $this->click("Post Comment"); + $this->assertText("a nice lady is a nice lady is a nice lady"); + + # still have effect when case is changed + $this->setField('comment', "monkey WhorE"); + $this->click("Post Comment"); + $this->assertText("monkey nice lady"); + + # only do whole words + $this->setField('comment', "my name is whoretta"); + $this->click("Post Comment"); + $this->assertText("my name is whoretta"); + + # search multiple words + $this->setField('comment', "I would like a duck"); + $this->click("Post Comment"); + $this->assertText("I would like a kitten"); + + # test for a line which was entered with extra whitespace + $this->setField('comment', "A colour is white"); + $this->click("Post Comment"); + $this->assertText("A colour is space"); + + # don't do anything with invalid lines + $this->setField('comment', "The word was invalid"); + $this->click("Post Comment"); + $this->assertText("The word was invalid"); + + $this->log_out(); + + $this->log_in_as_admin(); + $this->delete_image($image_id); + $this->log_out(); + } +} if(!defined(VERSION)) return; diff --git a/core/extension.class.php b/core/extension.class.php index acffecf2..1c498170 100644 --- a/core/extension.class.php +++ b/core/extension.class.php @@ -54,4 +54,40 @@ abstract class FormatterExtension implements Extension { abstract public function format($text); abstract public function strip($text); } + +abstract class DataHandlerExtension implements Extension { + var $theme; + + public function receive_event(Event $event) { + if(is_null($this->theme)) $this->theme = get_theme_object($this); + + if(($event instanceof DataUploadEvent) && $this->supported_ext($event->type) && $this->check_contents($event->tmpname)) { + $hash = $event->hash; + $ha = substr($hash, 0, 2); + if(!move_upload_to_archive($event)) return; + send_event(new ThumbnailGenerationEvent($event->hash, $event->type)); + $image = $this->create_image_from_data("images/$ha/$hash", $event->metadata); + if(is_null($image)) { + throw new UploadException("Data handler failed to create image object from data"); + } + $iae = new ImageAdditionEvent($event->user, $image); + send_event($iae); + $event->image_id = $iae->image->id; + } + + if(($event instanceof ThumbnailGenerationEvent) && $this->supported_ext($event->type)) { + $this->create_thumb($event->hash); + } + + if(($event instanceof DisplayingImageEvent) && $this->supported_ext($event->image->ext)) { + global $page; + $this->theme->display_image($page, $event->image); + } + } + + abstract protected function supported_ext($ext); + abstract protected function check_contents($tmpname); + abstract protected function create_image_from_data($filename, $metadata); + abstract protected function create_thumb($hash); +} ?> diff --git a/ext/alias_editor/test.php b/ext/alias_editor/test.php index 5eeb6c80..8d6f469d 100644 --- a/ext/alias_editor/test.php +++ b/ext/alias_editor/test.php @@ -5,8 +5,64 @@ class AliasEditorTest extends ShimmieWebTestCase { $this->assertTitle("Alias List"); $this->log_in_as_admin(); + + # test one to one $this->get_page('alias/list'); $this->assertTitle("Alias List"); + $this->setField('oldtag', "test1"); + $this->setField('newtag', "test2"); + $this->click("Add"); + $this->assertText("test1"); + + $this->get_page("alias/export/aliases.csv"); + $this->assertText("test1,test2"); + + $image_id = $this->post_image("ext/simpletest/data/pbx_screenshot.jpg", "test1"); + $this->get_page("post/view/$image_id"); # check that the tag has been replaced + $this->assertTitle("Image $image_id: test2"); + $this->get_page("post/list/test1/1"); # searching for an alias should find the master tag + $this->assertTitle("Image $image_id: test2"); + $this->get_page("post/list/test2/1"); # check that searching for the main tag still works + $this->assertTitle("Image $image_id: test2"); + $this->delete_image($image_id); + + $this->get_page('alias/list'); + $this->click("Remove"); + $this->assertTitle("Alias List"); + $this->assertNoText("test1"); + + # test one to many + $this->get_page('alias/list'); + $this->assertTitle("Alias List"); + $this->setField('oldtag', "onetag"); + $this->setField('newtag', "multi tag"); + $this->click("Add"); + $this->assertText("multi"); + $this->assertText("tag"); + + $this->get_page("alias/export/aliases.csv"); + $this->assertText("onetag,multi tag"); + + $image_id_1 = $this->post_image("ext/simpletest/data/pbx_screenshot.jpg", "onetag"); + $image_id_2 = $this->post_image("ext/simpletest/data/bedroom_workshop.jpg", "onetag"); + // FIXME: known broken + //$this->get_page("post/list/onetag/1"); # searching for an aliased tag should find its aliases + //$this->assertTitle("onetag"); + //$this->assertNoText("No Images Found"); + $this->get_page("post/list/multi/1"); + $this->assertTitle("multi"); + $this->assertNoText("No Images Found"); + $this->get_page("post/list/multi%20tag/1"); + $this->assertTitle("multi tag"); + $this->assertNoText("No Images Found"); + $this->delete_image($image_id_1); + $this->delete_image($image_id_2); + + $this->get_page('alias/list'); + $this->click("Remove"); + $this->assertTitle("Alias List"); + $this->assertNoText("test1"); + $this->log_out(); } } diff --git a/ext/handle_pixel/main.php b/ext/handle_pixel/main.php index 37f662b0..eef011f4 100644 --- a/ext/handle_pixel/main.php +++ b/ext/handle_pixel/main.php @@ -5,43 +5,13 @@ * Description: Handle JPG, PNG, GIF, etc files */ -class PixelFileHandler implements Extension { - var $theme; - - public function receive_event(Event $event) { - global $page; - if(is_null($this->theme)) $this->theme = get_theme_object($this); - - if(($event instanceof DataUploadEvent) && $this->supported_ext($event->type) && $this->check_contents($event->tmpname)) { - $hash = $event->hash; - $ha = substr($hash, 0, 2); - if(!move_upload_to_archive($event)) return; - send_event(new ThumbnailGenerationEvent($event->hash, $event->type)); - $image = $this->create_image_from_data("images/$ha/$hash", $event->metadata); - if(is_null($image)) { - throw new UploadException("Pixel Handler failed to create image object from data"); - } - - $iae = new ImageAdditionEvent($event->user, $image); - send_event($iae); // this might raise an exception, but all we'd do is re-throw it... - $event->image_id = $iae->image->id; - } - - if(($event instanceof ThumbnailGenerationEvent) && $this->supported_ext($event->type)) { - $this->create_thumb($event->hash); - } - - if(($event instanceof DisplayingImageEvent) && $this->supported_ext($event->image->ext)) { - $this->theme->display_image($page, $event->image); - } - } - - private function supported_ext($ext) { +class PixelFileHandler extends DataHandlerExtension { + protected function supported_ext($ext) { $exts = array("jpg", "jpeg", "gif", "png"); return in_array(strtolower($ext), $exts); } - private function create_image_from_data($filename, $metadata) { + protected function create_image_from_data($filename, $metadata) { global $config; $image = new Image(); @@ -62,7 +32,7 @@ class PixelFileHandler implements Extension { return $image; } - private function check_contents($file) { + protected function check_contents($file) { $valid = Array(IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG); if(!file_exists($file)) return false; $info = getimagesize($file); @@ -71,7 +41,7 @@ class PixelFileHandler implements Extension { return false; } - private function create_thumb($hash) { + protected function create_thumb($hash) { $ha = substr($hash, 0, 2); $inname = "images/$ha/$hash"; $outname = "thumbs/$ha/$hash"; diff --git a/ext/index/test.php b/ext/index/test.php index e7605f25..d2931000 100644 --- a/ext/index/test.php +++ b/ext/index/test.php @@ -48,14 +48,17 @@ class IndexTest extends ShimmieWebTestCase { # regular tag, many results $this->get_page('post/list/computer/1'); $this->assertTitle("computer"); + $this->assertNoText("No Images Found"); # meta tag, many results $this->get_page('post/list/size=640x480/1'); $this->assertTitle("size=640x480"); + $this->assertNoText("No Images Found"); # multiple tags, many results $this->get_page('post/list/computer%20size=640x480/1'); $this->assertTitle("computer size=640x480"); + $this->assertNoText("No Images Found"); # multiple tags, single result; search with one result = direct to image $this->get_page('post/list/screenshot%20computer/1'); @@ -65,6 +68,11 @@ class IndexTest extends ShimmieWebTestCase { $this->get_page('post/list/computer%20-pbx/1'); $this->assertTitle(new PatternExpectation("/^Image $image_id_2: /")); + # negative tag alone, should work + # FIXME: known broken in mysql + //$this->get_page('post/list/-pbx/1'); + //$this->assertTitle(new PatternExpectation("/^Image $image_id_2: /")); + $this->log_in_as_admin(); $this->delete_image($image_id_1); $this->delete_image($image_id_2); diff --git a/ext/upload/main.php b/ext/upload/main.php index b389a61c..d6c1990b 100644 --- a/ext/upload/main.php +++ b/ext/upload/main.php @@ -150,6 +150,9 @@ class Upload implements Extension { $event = new DataUploadEvent($user, $file['tmp_name'], $metadata); try { send_event($event); + if($event->image_id == -1) { + throw new UploadException("File type not recognised"); + } header("X-Shimmie-Image-ID: ".int_escape($event->image_id)); } catch(UploadException $ex) { diff --git a/ext/upload/test.php b/ext/upload/test.php index 1ebf096f..2f7b4386 100644 --- a/ext/upload/test.php +++ b/ext/upload/test.php @@ -7,14 +7,32 @@ class UploadTest extends ShimmieWebTestCase { $this->assertResponse(302); $image_id_2 = $this->post_image("ext/simpletest/data/pbx_screenshot.jpg", "pbx computer screenshot"); + $this->assertResponse(200); $this->assertTitle("Upload Status"); $this->assertText("already has hash"); + $image_id_3 = $this->post_image("index.php", "test"); + $this->assertResponse(200); + $this->assertTitle("Upload Status"); + $this->assertText("File type not recognised"); + + /* + // FIXME: huge.dat is rejected for other reasons; manual testing shows that this works + file_put_contents("huge.dat", file_get_contents("ext/simpletest/data/pbx_screenshot.jpg") . str_repeat("U", 1024*1024*3)); + $image_id_4 = $this->post_image("index.php", "test"); + $this->assertResponse(200); + $this->assertTitle("Upload Status"); + $this->assertText("File too large"); + unlink("huge.dat"); + */ + $this->log_out(); $this->log_in_as_admin(); $this->delete_image($image_id_1); $this->delete_image($image_id_2); + $this->delete_image($image_id_3); # if these were successfully rejected, + //$this->delete_image($image_id_4); # then delete_image is a no-op $this->log_out(); } }