more tests

This commit is contained in:
Shish 2009-07-16 20:20:53 +01:00
parent 96e69c4f20
commit 4abf2ac7dd
13 changed files with 233 additions and 127 deletions

View file

@ -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<strlen($string); $j++) {
@ -117,16 +101,6 @@ class FlashFileHandler extends SimpleExtension {
return $bounds;
}
private 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;
}
}
add_event_listener(new FlashFileHandler());
?>

View file

@ -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);

View file

@ -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));
}

View file

@ -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"));
}
}

View file

@ -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");

View file

@ -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);
}

View file

@ -1,5 +1,58 @@
<?php
class WordFiterTest extends ShimmieWebTestCase {}
class WordFilterTest extends ShimmieWebTestCase {
function testWordFilter() {
$this->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;

View file

@ -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);
}
?>

View file

@ -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();
}
}

View file

@ -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";

View file

@ -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);

View file

@ -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) {

View file

@ -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();
}
}