From 924eb982385025ff7f5f723aedd263bcd61576de Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 10 Mar 2012 09:52:52 +0000 Subject: [PATCH] API tests --- contrib/shimmie_api/main.php | 39 +++++++++++++++--------------------- contrib/shimmie_api/test.php | 29 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 contrib/shimmie_api/test.php diff --git a/contrib/shimmie_api/main.php b/contrib/shimmie_api/main.php index a3150372..decacbfa 100644 --- a/contrib/shimmie_api/main.php +++ b/contrib/shimmie_api/main.php @@ -37,7 +37,7 @@ class _SafeImage { class ShimmieApi extends Extension { public function onPageRequest(PageRequestEvent $event) { - global $database, $page; + global $database, $page, $user; if($event->page_matches("api/shimmie")) { $page->set_mode("data"); @@ -55,7 +55,7 @@ class ShimmieApi extends Extension { "SELECT tag FROM tags WHERE tag LIKE ?", array($arg."%")); } - elseif(isset($_GET['id'])){ + elseif(isset($_GET['tag'])){ $all = $database->get_all( "SELECT tag FROM tags WHERE tag LIKE ?", array($_GET['tag']."%")); @@ -76,9 +76,7 @@ class ShimmieApi extends Extension { elseif(isset($_GET['id'])){ $image = Image::by_id(int_escape($_GET['id'])); } - else{ - $image = Image::by_id(int_escape("1")); //Default to id=1 - } + // FIXME: handle null image $image->get_tag_array(); // tag data isn't loaded into the object until necessary $safe_image = new _SafeImage($image); $page->set_data(json_encode($safe_image)); @@ -98,29 +96,24 @@ class ShimmieApi extends Extension { } if($event->page_matches("api/shimmie/get_user")) { - if(isset($_GET['name'])){ - $all = $database->get_all( - "SELECT id,name,joindate,class FROM users WHERE name=?", - array($_GET['name'])); + $query = $user->id; + if($event->count_args() == 1) { + $query = $event->get_arg(0); + } + if(isset($_GET['name'])) { + $query = $_GET['name']; + } + if(isset($_GET['id'])) { + $query = $_GET['id']; } - if(isset($_GET['id'])){ - $all = $database->get_all( - "SELECT id,name,joindate,class FROM users WHERE id=?", - array($_GET['id'])); - } + $all = $database->get_row( + "SELECT id,name,joindate,class FROM users WHERE name=? OR id=?", + array($_GET['name'], int_escape($_GET['id']))); - if(!isset($_GET['id']) && !isset($_GET['name'])){ - $all = $database->get_all( - "SELECT id,name,joindate,class FROM users WHERE id=?", - array("2")); //In 99% of cases, this will be the admin. - } - - $all = $all[0]; //FIXME?: For some weird reason, get_all seems to return twice. Unsetting second value to make things look nice.. + // - it returns data as eg array(0=>1234, 'id'=>1234, 1=>'bob', 'name'=>bob, ...); for($i=0; $i<4; $i++) unset($all[$i]); - /*TODO: Might be worth making it possible just to get a certain stat (Using &stat=uploadcount or something) - This would lessen strain on DB? */ $all['uploadcount'] = Image::count_images(array("user_id=".$all['id'])); $all['uploadperday'] = sprintf("%.1f", ($all['uploadcount'] / (((time() - strtotime($all['joindate'])) / 86400) + 1))); $all['commentcount'] = $database->get_one( diff --git a/contrib/shimmie_api/test.php b/contrib/shimmie_api/test.php new file mode 100644 index 00000000..961afa0a --- /dev/null +++ b/contrib/shimmie_api/test.php @@ -0,0 +1,29 @@ +log_in_as_user(); + $image_id = $this->post_image("ext/simpletest/data/pbx_screenshot.jpg", "pbx"); + + $this->get_page("api/shimmie/get_tags"); + $this->get_page("api/shimmie/get_tags/pb"); + $this->get_page("api/shimmie/get_tags?tag=pb"); + $this->get_page("api/shimmie/get_image/$image_id"); + $this->get_page("api/shimmie/get_image?id=$image_id"); + $this->get_page("api/shimmie/find_images"); + $this->get_page("api/shimmie/find_images/pbx"); + $this->get_page("api/shimmie/find_images/pbx/1"); + $this->get_page("api/shimmie/get_user/demo"); + $this->get_page("api/shimmie/get_user?name=demo"); + $this->get_page("api/shimmie/get_user?id=2"); + + // FIXME: test unspecified / bad values + // FIXME: test that json is encoded properly + + $this->log_out(); + + $this->log_in_as_admin(); + $this->delete_image($image_id); + $this->log_out(); + } +} +?>