From 1fd0e14359a7c7164573e4aa897c07680339e713 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 26 Nov 2009 12:09:04 -0800 Subject: Convert all DB where() calls to take 3 arguments. Convert all open_paren() calls to and_open() or or_open() as appropriate. --- modules/tag/tests/Tag_Test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'modules/tag/tests') diff --git a/modules/tag/tests/Tag_Test.php b/modules/tag/tests/Tag_Test.php index c9a96286..c96e7f2b 100644 --- a/modules/tag/tests/Tag_Test.php +++ b/modules/tag/tests/Tag_Test.php @@ -25,18 +25,18 @@ class Tag_Test extends Unit_Test_Case { $tag1 = "tag1"; tag::add($album, $tag1); - $tag = ORM::factory("tag")->where("name", $tag1)->find(); + $tag = ORM::factory("tag")->where("name", "=", $tag1)->find(); $this->assert_true(1, $tag->count); // Make sure adding the tag again doesn't increase the count tag::add($album, $tag1); - $tag = ORM::factory("tag")->where("name", $tag1)->find(); + $tag = ORM::factory("tag")->where("name", "=", $tag1)->find(); $this->assert_true(1, $tag->count); $rand = rand(); $album = album::create($root, $rand, $rand, $rand); tag::add($album, $tag1); - $tag = ORM::factory("tag")->where("name", $tag1)->find(); + $tag = ORM::factory("tag")->where("name", "=", $tag1)->find(); $this->assert_true(2, $tag->count); } } \ No newline at end of file -- cgit v1.2.3 From 586f2d78b85cbe964a625b3c83896c1c8a991702 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 24 Dec 2009 19:25:14 -0800 Subject: Add the functionality to add, delete and rename tags via the Gallery3 remote interface. --- modules/tag/helpers/tag_rest.php | 104 +++++++++++-- modules/tag/tests/Tag_Rest_Helper_Test.php | 227 +++++++++++++++++++++++++++++ 2 files changed, 317 insertions(+), 14 deletions(-) create mode 100644 modules/tag/tests/Tag_Rest_Helper_Test.php (limited to 'modules/tag/tests') diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index eec90971..d62c0231 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -34,12 +34,18 @@ class tag_rest_Core { $response["tags"][] = array("name" => $row->name, "count" => $row->count); } } else { - $item = ORM::factory("item") - ->where("relative_url_cache", implode("/", $request->arguments)) - ->viewable() - ->find(); - if ($item->loaded) { - $response = array("tags" => tag::item_tags($item)); + $path = implode("/", $request->arguments); + if (strpos($path, ",") === false) { + $item = ORM::factory("item") + ->where("relative_url_cache", $path) + ->viewable() + ->find(); + // If we didn't find it and there was only one argument, retry as a tag not a path + if ($item->loaded || count($request->arguments) != 1) { + $response = array("tags" => $item->loaded ? tag::item_tags($item) : array()); + } else { + $response = array("resources" => tag_rest::_get_items($request)); + } } else { $response = array("resources" => tag_rest::_get_items($request)); } @@ -48,20 +54,90 @@ class tag_rest_Core { return rest::success($response); } + static function post($request) { + if (empty($request->arguments) || count($request->arguments) != 1 || empty($request->path)) { + return rest::invalid_request(); + } + $path = $request->path; + $tags = explode(",", $request->arguments[0]); + + $item = ORM::factory("item") + ->where("relative_url_cache", $path) + ->viewable() + ->find(); + if (!$item->loaded) { + return rest::not_found("Resource: {$path} missing."); + } + + if (!access::can("edit", $item)) { + return rest::not_found("Resource: {$path} permission denied."); + } + + foreach ($tags as $tag) { + tag::add($item, $tag); + } + return rest::success(); + } + + static function put($request) { + if (empty($request->arguments[0]) || empty($request->new_name)) { + return rest::invalid_request(); + } + + $name = $request->arguments[0]; + + $tag = ORM::factory("tag") + ->where("name", $name) + ->find(); + if (!$tag->loaded) { + return rest::not_found("Tag: {$name} not found."); + } + + $tag->name = $request->new_name; + $tag->save(); + + return rest::success(); + } + + static function delete($request) { + if (empty($request->arguments[0])) { + return rest::invalid_request(); + } + $tags = explode(",", $request->arguments[0]); + if (!empty($request->path)) { + $tag_list = ORM::factory("tag") + ->join("items_tags", "tags.id", "items_tags.tag_id") + ->join("items", "items.id", "items_tags.item_id") + ->in("tags.name", $tags) + ->where("relative_url_cache", $request->path) + ->viewable() + ->find_all(); + } else { + $tag_list = ORM::factory("tag") + ->in("name", $tags) + ->find_all(); + } + + foreach ($tag_list as $row) { + $row->delete(); + }; + + tag::compact(); + return rest::success(); + } + private static function _get_items($request) { - $tags = $request->arguments; + $tags = explode(",", $request->arguments[0]); $items = ORM::factory("item") - ->join("items_tags", "items.id", "items_tags.item_id", "left") - ->join("tags", "tags.id", "items_tags.tag_id", "left") - ->where("tags.name", array_shift($tags)); + ->select("distinct *") + ->join("items_tags", "items.id", "items_tags.item_id") + ->join("tags", "tags.id", "items_tags.tag_id") + ->in("tags.name", $tags); if (!empty($request->limit)) { $items->limit($request->limit); } if (!empty($request->offset)) { - $tags->offset($request->offset); - } - foreach ($tags as $tag) { - $items->orWhere("tags.name", $tag); + $items->offset($request->offset); } $resources = array(); foreach ($items->find_all() as $item) { diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php new file mode 100644 index 00000000..1c550366 --- /dev/null +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -0,0 +1,227 @@ +_save = array($_GET, $_POST, $_SERVER, $_FILES); + $this->_saved_active_user = identity::active_user(); + + $this->_user = identity::create_user("access_test", "Access Test", "password"); + $key = ORM::factory("user_access_token"); + $this->_access_key = $key->access_key = md5($this->_user->name . rand()); + $key->user_id = $this->_user->id; + $key->save(); + + $root = ORM::factory("item", 1); + $this->_album = album::create($root, "album", "Test Album", rand()); + tag::add($this->_album, "albums"); + tag::add($this->_album, "A1"); + tag::add($this->_album, "T1"); + $this->_child = album::create($this->_album, "child", "Test Child Album", rand()); + tag::add($this->_child, "albums"); + tag::add($this->_child, "C1"); + tag::add($this->_child, "T1"); + + $filename = MODPATH . "gallery/tests/test.jpg"; + $rand = rand(); + $this->_photo = photo::create($this->_child, $filename, "$rand.jpg", $rand); + tag::add($this->_photo, "photos"); + tag::add($this->_photo, "P1"); + tag::add($this->_photo, "T1"); + + $filename = MODPATH . "gallery/tests/test.jpg"; + $rand = rand(); + $this->_sibling = photo::create($this->_album, $filename, "$rand.jpg", $rand); + tag::add($this->_sibling, "photos"); + tag::add($this->_sibling, "P3"); + } + + public function teardown() { + list($_GET, $_POST, $_SERVER, $_FILES) = $this->_save; + identity::set_active_user($this->_saved_active_user); + + try { + if (!empty($this->_user)) { + $this->_user->delete(); + } + if (!empty($this->_album)) { + $this->_album->delete(); + } + Database::instance()->query("TRUNCATE {tags}"); + Database::instance()->query("TRUNCATE {items_tags}"); + + } catch (Exception $e) { } + } + + public function tag_rest_get_all_test() { + $request = (object)array("arguments" => array(), "limit" => 2, "offset" => 1); + + $this->assert_equal( + json_encode(array("status" => "OK", + "tags" => array(array("name" => "albums", "count" => 2), + array("name" => "photos", "count" => 2)))), + tag_rest::get($request)); + } + + public function tag_rest_get_tags_for_item_test() { + $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + + $this->assert_equal( + json_encode(array("status" => "OK", + "tags" => array("photos", "P1", "T1"))), + tag_rest::get($request)); + } + + public function tag_rest_get_items_test() { + $request = (object)array("arguments" => array("albums")); + + $resources = array(); + foreach (array($this->_album, $this->_child) as $resource) { + $resources[] = array("type" => $resource->type, + "has_children" => $resource->children_count() > 0, + "path" => $resource->relative_url(), + "thumb_url" => $resource->thumb_url(), + "thumb_dimensions" => array( + "width" => $resource->thumb_width, + "height" => $resource->thumb_height), + "has_thumb" => $resource->has_thumb(), + "title" => $resource->title); + + } + $this->assert_equal(json_encode(array("status" => "OK", "resources" => $resources)), + tag_rest::get($request)); + } + + public function tag_rest_add_tags_for_item_no_path_test() { + $request = (object)array("arguments" => array("new,one")); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::post($request)); + } + + public function tag_rest_add_tags_for_item_not_found_test() { + $request = (object)array("path" => $this->_photo->relative_url() . "b", + "arguments" => array("new,one")); + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Resource not found")), + tag_rest::post($request)); + } + + public function tag_rest_add_tags_for_item_no_access_test() { + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_photo->relative_url(), + "arguments" => array("new,one")); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Resource not found")), + tag_rest::post($request)); + } + + public function tag_rest_add_tags_for_item_test() { + access::allow(identity::registered_users(), "edit", $this->_child); + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_photo->relative_url(), + "arguments" => array("new,one")); + + $this->assert_equal( + json_encode(array("status" => "OK")), + tag_rest::post($request)); + $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + $this->assert_equal( + json_encode(array("status" => "OK", + "tags" => array("photos", "P1", "T1", "new", "one"))), + tag_rest::get($request)); + } + + public function tag_rest_update_tag_no_arguments_test() { + $request = (object)array("arguments" => array()); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::put($request)); + } + + public function tag_rest_update_tag_one_arguments_test() { + $request = (object)array("arguments" => array("photos")); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::put($request)); + + $request = (object)array("arguments" => array(), "new_name" => "valid"); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Invalid request")), + tag_rest::put($request)); + } + + public function tag_rest_update_tags_not_found_test() { + $request = (object)array("arguments" => array("not"), "new_name" => "found"); + + $this->assert_equal( + json_encode(array("status" => "ERROR", "message" => "Resource not found")), + tag_rest::put($request)); + } + + public function tag_rest_update_tags_test() { + $request = (object)array("arguments" => array("albums"), "new_name" => "new name"); + + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::put($request)); + + $request = (object)array("arguments" => array("new name")); + $resources = array(); + foreach (array($this->_album, $this->_child) as $resource) { + $resources[] = array("type" => $resource->type, + "has_children" => $resource->children_count() > 0, + "path" => $resource->relative_url(), + "thumb_url" => $resource->thumb_url(), + "thumb_dimensions" => array( + "width" => $resource->thumb_width, + "height" => $resource->thumb_height), + "has_thumb" => $resource->has_thumb(), + "title" => $resource->title); + + } + $this->assert_equal( + json_encode(array("status" => "OK", "resources" => $resources)), + tag_rest::get($request)); + } + + public function tag_rest_delete_tag_test() { + $request = (object)array("arguments" => array("T1,P1")); + + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); + + $request = (object)array("arguments" => array("T1,P1")); + $this->assert_equal(json_encode(array("status" => "OK", "resources" => array())), + tag_rest::get($request)); + } + + public function tag_rest_delete_tag_from_item_test() { + $request = (object)array("arguments" => array("T1,P1"), + $this->_photo->relative_url()); + + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); + + $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + $this->assert_equal(json_encode(array("status" => "OK", "tags" => array("photos"))), + tag_rest::get($request)); + } +} -- cgit v1.2.3 From bccb6fc02146fb07cd1b472a90092e78e2259e91 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sun, 27 Dec 2009 08:32:12 -0800 Subject: Clean up validation the check for duplicate names or slugs, finish converting the rest API to Kohana 2.4 --- modules/gallery/helpers/gallery_rest.php | 93 ++++++++++++++-------- modules/gallery/helpers/item.php | 8 +- modules/gallery/tests/Gallery_Rest_Helper_Test.php | 8 +- modules/image_block/helpers/image_block_rest.php | 12 +-- modules/rest/controllers/rest.php | 27 ++++--- modules/rest/helpers/rest.php | 2 +- modules/rest/tests/Rest_Controller_Test.php | 2 +- modules/tag/helpers/tag_rest.php | 62 ++++++++------- modules/tag/models/tag.php | 4 +- modules/tag/tests/Tag_Rest_Helper_Test.php | 4 +- 10 files changed, 129 insertions(+), 93 deletions(-) (limited to 'modules/tag/tests') diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php index 227a6f02..1d790d8c 100644 --- a/modules/gallery/helpers/gallery_rest.php +++ b/modules/gallery/helpers/gallery_rest.php @@ -22,11 +22,11 @@ class gallery_rest_Core { $path = implode("/", $request->arguments); $item = ORM::factory("item") - ->where("relative_url_cache", $path) + ->where("relative_url_cache", "=", $path) ->viewable() ->find(); - if (!$item->loaded) { + if (!$item->loaded()) { return rest::not_found("Resource: {$path} missing."); } @@ -62,11 +62,11 @@ class gallery_rest_Core { $path = implode("/", $request->arguments); $item = ORM::factory("item") - ->where("relative_url_cache", $path) + ->where("relative_url_cache", "=", $path) ->viewable() ->find(); - if (!$item->loaded) { + if (!$item->loaded()) { return rest::not_found("Resource: {$path} missing."); } @@ -75,12 +75,13 @@ class gallery_rest_Core { } // Validate the request data - $new_values = gallery_rest::_validate($request, $item); + $new_values = gallery_rest::_validate($request, $item->parent_id, $item->id); $errors = $new_values->errors(); if (empty($errors)) { item::update($item, $new_values->as_array()); - log::success("content", "Updated $item->type", "type}s/$item->id\">view"); + log::success("content", "Updated $item->type", + "type}s/$item->id\">view"); return rest::success(); } else { @@ -94,15 +95,15 @@ class gallery_rest_Core { } $path = implode("/", $request->arguments); - $components = explode("/", $path); + $components = $request->arguments; $name = urldecode(array_pop($components)); $parent = ORM::factory("item") - ->where("relative_url_cache", implode("/", $components)) + ->where("relative_url_cache", "=", implode("/", $components)) ->viewable() ->find(); - if (!$parent->loaded) { + if (!$parent->loaded()) { return rest::not_found("Resource: {$path} missing."); } @@ -111,7 +112,7 @@ class gallery_rest_Core { } // Validate the request data - $new_values = gallery_rest::_validate($request); + $new_values = gallery_rest::_validate($request, $parent->id); $errors = $new_values->errors(); if (!empty($errors)) { return rest::validation_error($errors); @@ -121,10 +122,10 @@ class gallery_rest_Core { $new_item = album::create( $parent, $name, - empty($request->title) ? $name : $request->title, - empty($request->description) ? null : $request->description, + empty($new_values["title"]) ? $name : $new_values["title"], + empty($new_values["description"]) ? null : $new_values["description"], identity::active_user()->id, - empty($request->slug) ? $name : $request->slug); + empty($new_values["slug"]) ? $name : $new_values["slug"]); $log_message = t("Added an album"); } else { $temp_filename = upload::save("image"); @@ -153,11 +154,11 @@ class gallery_rest_Core { $path = implode("/", $request->arguments); $item = ORM::factory("item") - ->where("relative_url_cache", $path) + ->where("relative_url_cache", "=", $path) ->viewable() ->find(); - if (!$item->loaded) { + if (!$item->loaded()) { return rest::success(); } @@ -193,7 +194,7 @@ class gallery_rest_Core { "path" => $child->relative_url(), "thumb_url" => $child->thumb_url(true), "thumb_dimensions" => array("width" => $child->thumb_width, - "height" => $child->thumb_height), + "height" => $child->thumb_height), "has_thumb" => $child->has_thumb(), "title" => $child->title); } @@ -201,38 +202,40 @@ class gallery_rest_Core { return $children; } - private static function _validate($request, $item=null) { + private static function _validate($request, $parent_id, $item_id=0) { $new_values = array(); - $fields = array("title", "description", "name", "slug", "image"); - if (empty($item)) { - $item = ORM::factory("item"); - $item->id = 0; - } - if ($item->id == 1) { + $fields = array("name" => "length[0,255]", + "title" => "required|length[0,255]", + "description" => "length[0,65535]", + "slug" => "required|length[0,255]"); + if ($item_id == 1) { unset($request["name"]); unset($request["slug"]); } - foreach ($fields as $field) { + foreach (array_keys($fields) as $field) { if (isset($request->$field)) { $new_values[$field] = $request->$field; } else if (isset($item->$field)) { $new_values[$field] = $item->$field; } } - - $new_values = new Validation($new_values); - foreach ($item->rules as $field => $rules) { - foreach (explode("|", $rules) as $rule) { - $new_values->add_rules($field, $rule); - } + if (!empty($request->image)) { + $new_values["image"] = $request->image; } + + $new_values = Validation::factory($new_values) + ->add_rules("name", "length[0,255]") + ->add_rules("title", "length[0,255]") + ->add_rules("description", "length[0,65535]") + ->add_rules("slug", "length[0,255]"); if (isset($new_values["image"])) { $new_values->add_rules( "image", "upload::valid", "upload::required", "upload::type[gif,jpg,jpeg,png,flv,mp4]"); } - if ($new_values->validate() && $item->id != 1) { - $errors = item::check_for_conflicts($item, $new_values["name"], $new_values["slug"]); + if ($new_values->validate() && $item_id != 1) { + $errors = gallery_rest::_check_for_conflicts($parent_id, $item_id, + $new_values["name"], $new_values["slug"]); if (!empty($errors)) { !empty($errors["name_conflict"]) OR $new_values->add_error("name", "Duplicate Name"); !empty($errors["slug_conflict"]) OR @@ -242,4 +245,30 @@ class gallery_rest_Core { return $new_values; } + + private static function _check_for_conflicts($parent_id, $item_id, $new_name, $new_slug) { + $errors = array(); + + if ($row = db::build() + ->select(array("name", "slug")) + ->from("items") + ->where("parent_id", "=", $parent_id) + ->where("id", "<>", $item_id) + ->and_open() + ->where("name", "=", $new_name) + ->or_where("slug", "=", $new_slug) + ->close() + ->execute() + ->current()) { + if ($row->name == $new_name) { + $errors["name_conflict"] = 1; + } + if ($row->slug == $new_slug) { + $errors["slug_conflict"] = 1; + } + } + + return $errors; + } + } diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index c620ba95..fc390e70 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -122,11 +122,11 @@ class item_Core { if ($row = db::build() ->select(array("name", "slug")) ->from("items") - ->where("parent_id", $item->parent_id) - ->where("id <>", $item->id) + ->where("parent_id", "=", $item->parent_id) + ->where("id", "<>", $item->id) ->and_open() - ->where("name", $new_name) - ->orwhere("slug", $new_slug) + ->where("name", "=", $new_name) + ->or_where("slug", "=", $new_slug) ->close() ->execute() ->current()) { diff --git a/modules/gallery/tests/Gallery_Rest_Helper_Test.php b/modules/gallery/tests/Gallery_Rest_Helper_Test.php index 14c73248..f36f6aaf 100644 --- a/modules/gallery/tests/Gallery_Rest_Helper_Test.php +++ b/modules/gallery/tests/Gallery_Rest_Helper_Test.php @@ -208,7 +208,7 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { "parent_path" => $this->_album->relative_url()))), gallery_rest::delete($request)); $this->_child->reload(); - $this->assert_false($this->_child->loaded); + $this->assert_false($this->_child->loaded()); } public function gallery_rest_delete_photo_test() { @@ -222,7 +222,7 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { "parent_path" => $this->_album->relative_url()))), gallery_rest::delete($request)); $this->_sibling->reload(); - $this->assert_false($this->_sibling->loaded); + $this->assert_false($this->_sibling->loaded()); } public function gallery_rest_post_album_test() { @@ -235,9 +235,9 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { $this->assert_equal(json_encode(array("status" => "OK", "path" => $new_path)), gallery_rest::post($request)); $album = ORM::factory("item") - ->where("relative_url_cache", $new_path) + ->where("relative_url_cache", "=", $new_path) ->find(); - $this->assert_true($album->loaded); + $this->assert_true($album->loaded()); $this->assert_equal("new child", $album->slug); } } diff --git a/modules/image_block/helpers/image_block_rest.php b/modules/image_block/helpers/image_block_rest.php index 45f849b1..7afd974c 100644 --- a/modules/image_block/helpers/image_block_rest.php +++ b/modules/image_block/helpers/image_block_rest.php @@ -26,18 +26,18 @@ class image_block_rest_Core { $items = ORM::factory("item") ->viewable() - ->where("type !=", "album") - ->where("rand_key < ", $random) - ->orderby(array("rand_key" => "DESC")) + ->where("type", "!=", "album") + ->where("rand_key", "<", $random) + ->order_by(array("rand_key" => "DESC")) ->find_all(1); if ($items->count() == 0) { // Try once more. If this fails, just ditch the block altogether $items = ORM::factory("item") ->viewable() - ->where("type !=", "album") - ->where("rand_key >= ", $random) - ->orderby(array("rand_key" => "DESC")) + ->where("type", "!=", "album") + ->where("rand_key", ">= ", $random) + ->order_by(array("rand_key" => "DESC")) ->find_all(1); } break; diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index 1289d62b..6715bc15 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -18,7 +18,7 @@ */ class Rest_Controller extends Controller { public function access_key() { - $request = (object)$this->input->get(); + $request = (object)Input::instance()->get(); if (empty($request->user) || empty($request->password)) { print rest::forbidden("No user or password supplied"); return; @@ -36,13 +36,13 @@ class Rest_Controller extends Controller { } $key = ORM::factory("user_access_token") - ->where("user_id", $user->id) + ->where("user_id", "=", $user->id) ->find(); - if (!$key->loaded) { + if (!$key->loaded()) { $key->user_id = $user->id; $key->access_key = md5($user->name . rand()); $key->save(); - Kohana::log("alert", Kohana::debug($key->as_array())); + Kohana_Log::add("alert", Kohana::debug($key->as_array())); } print rest::success(array("token" => $key->access_key)); } @@ -67,22 +67,23 @@ class Rest_Controller extends Controller { } private function _normalize_request($args=array()) { - $method = strtolower($this->input->server("REQUEST_METHOD")); + $input = Input::instance(); + $method = strtolower($input->server("REQUEST_METHOD")); $request = new stdClass(); - foreach (array_keys($this->input->get()) as $key) { - $request->$key = $this->input->get($key); + foreach (array_keys($input->get()) as $key) { + $request->$key = $input->get($key); } if ($method != "get") { - foreach (array_keys($this->input->post()) as $key) { - $request->$key = $this->input->post($key); + foreach (array_keys($input->post()) as $key) { + $request->$key = $input->post($key); } foreach (array_keys($_FILES) as $key) { $request->$key = $_FILES[$key]; } } - $request->method = strtolower($this->input->server("HTTP_X_GALLERY_REQUEST_METHOD", $method)); - $request->access_token = $this->input->server("HTTP_X_GALLERY_REQUEST_KEY"); + $request->method = strtolower($input->server("HTTP_X_GALLERY_REQUEST_METHOD", $method)); + $request->access_token = $input->server("HTTP_X_GALLERY_REQUEST_KEY"); $request->arguments = $args; // Let the rest handler figure out what the arguments mean return $request; @@ -93,10 +94,10 @@ class Rest_Controller extends Controller { $user = identity::guest(); } else { $key = ORM::factory("user_access_token") - ->where("access_key", $access_token) + ->where("access_key", "=", $access_token) ->find(); - if ($key->loaded) { + if ($key->loaded()) { $user = identity::lookup_user($key->user_id); if (empty($user)) { print rest::forbidden("User not found: {$key->user_id}"); diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php index ad6ca7c7..276ff0c2 100644 --- a/modules/rest/helpers/rest.php +++ b/modules/rest/helpers/rest.php @@ -88,7 +88,7 @@ class rest_Core { private static function _format_failure_response($message, $log_message) { if (!empty($log_message)) { - Kohana::log("info", $log_message); + Kohana_Log::add("info", $log_message); } // We don't need to save the session for this request Session::abort_save(); diff --git a/modules/rest/tests/Rest_Controller_Test.php b/modules/rest/tests/Rest_Controller_Test.php index b7fbd5a3..6bebc47d 100644 --- a/modules/rest/tests/Rest_Controller_Test.php +++ b/modules/rest/tests/Rest_Controller_Test.php @@ -173,7 +173,7 @@ class rest_rest { static function get($request) { self::$request = $request; $item = ORM::factory("item") - ->where("relative_url_cache", implode("/", $request->arguments)) + ->where("relative_url_cache", "=", implode("/", $request->arguments)) ->find(); $response["path"] = $item->relative_url(); $response["title"] = $item->title; diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index d62c0231..cca9a88b 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -18,40 +18,44 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class tag_rest_Core { + // If no arguments just return all the tags. If 2 or more then it is a path then + // return the tags for that item. But if its only 1, then is it a path or a tag? + // Assume a tag first, if nothing is found then try finding the item. static function get($request) { - if (empty($request->arguments)) { + $resources = array(); + switch (count($request->arguments)) { + case 0: $tags = ORM::factory("tag") ->select("name", "count") - ->orderby("count", "DESC"); + ->order_by("count", "DESC"); if (!empty($request->limit)) { $tags->limit($request->limit); } if (!empty($request->offset)) { $tags->offset($request->offset); } - $response = array("tags" => array()); + $resources = array("tags" => array()); foreach ($tags->find_all() as $row) { - $response["tags"][] = array("name" => $row->name, "count" => $row->count); + $resources["tags"][] = array("name" => $row->name, "count" => $row->count); } - } else { - $path = implode("/", $request->arguments); - if (strpos($path, ",") === false) { - $item = ORM::factory("item") - ->where("relative_url_cache", $path) - ->viewable() - ->find(); - // If we didn't find it and there was only one argument, retry as a tag not a path - if ($item->loaded || count($request->arguments) != 1) { - $response = array("tags" => $item->loaded ? tag::item_tags($item) : array()); - } else { - $response = array("resources" => tag_rest::_get_items($request)); - } - } else { - $response = array("resources" => tag_rest::_get_items($request)); + break; + case 1: + $resources = tag_rest::_get_items($request); + if (!empty($resources)) { + $resources = array("resources" =>$resources); + break; + } + default: + $item = ORM::factory("item") + ->where("relative_url_cache", "=", implode("/", $request->arguments)) + ->viewable() + ->find(); + if ($item->loaded()) { + $resources = array("tags" => tag::item_tags($item)); } } - return rest::success($response); + return rest::success($resources); } static function post($request) { @@ -62,10 +66,10 @@ class tag_rest_Core { $tags = explode(",", $request->arguments[0]); $item = ORM::factory("item") - ->where("relative_url_cache", $path) + ->where("relative_url_cache", "=", $path) ->viewable() ->find(); - if (!$item->loaded) { + if (!$item->loaded()) { return rest::not_found("Resource: {$path} missing."); } @@ -87,9 +91,9 @@ class tag_rest_Core { $name = $request->arguments[0]; $tag = ORM::factory("tag") - ->where("name", $name) + ->where("name", "=", $name) ->find(); - if (!$tag->loaded) { + if (!$tag->loaded()) { return rest::not_found("Tag: {$name} not found."); } @@ -108,13 +112,13 @@ class tag_rest_Core { $tag_list = ORM::factory("tag") ->join("items_tags", "tags.id", "items_tags.tag_id") ->join("items", "items.id", "items_tags.item_id") - ->in("tags.name", $tags) - ->where("relative_url_cache", $request->path) + ->where("tags.name", "IN", $tags) + ->where("relative_url_cache", "=", $request->path) ->viewable() ->find_all(); } else { $tag_list = ORM::factory("tag") - ->in("name", $tags) + ->where("name", "IN", $tags) ->find_all(); } @@ -129,10 +133,10 @@ class tag_rest_Core { private static function _get_items($request) { $tags = explode(",", $request->arguments[0]); $items = ORM::factory("item") - ->select("distinct *") + ->select_distinct("*") ->join("items_tags", "items.id", "items_tags.item_id") ->join("tags", "tags.id", "items_tags.tag_id") - ->in("tags.name", $tags); + ->where("tags.name", "IN", $tags); if (!empty($request->limit)) { $items->limit($request->limit); } diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index b2ce9eda..d0d2117c 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -108,7 +108,9 @@ class Tag_Model extends ORM { $result = parent::delete(); if ($related_item_ids) { - foreach (ORM::factory("item")->in("id", array_keys($related_item_ids))->find_all() as $item) { + foreach (ORM::factory("item") + ->where("id", "IN", array_keys($related_item_ids)) + ->find_all() as $item) { module::event("item_related_update", $item); } } diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php index 1c550366..6b1c9a33 100644 --- a/modules/tag/tests/Tag_Rest_Helper_Test.php +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -210,11 +210,11 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); $request = (object)array("arguments" => array("T1,P1")); - $this->assert_equal(json_encode(array("status" => "OK", "resources" => array())), + $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::get($request)); } - public function tag_rest_delete_tag_from_item_test() { + public function tag_rest_delete_tagc_from_item_test() { $request = (object)array("arguments" => array("T1,P1"), $this->_photo->relative_url()); -- cgit v1.2.3 From 11792a12bb2002a434217efabe232022dd253b67 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Wed, 30 Dec 2009 17:08:01 -0800 Subject: 1) Remove the rest::not_found method and replace it with "throw new Kohana_404_Exception 2) Don't use the input path to lookup the item via relative_path_cache. Instead use url::get_item_from_uri method. --- modules/gallery/helpers/gallery_rest.php | 72 ++++++++-------------- modules/gallery/tests/Gallery_Rest_Helper_Test.php | 16 +++-- modules/rest/helpers/rest.php | 9 +-- modules/tag/helpers/tag_rest.php | 6 +- modules/tag/tests/Tag_Rest_Helper_Test.php | 27 +++++--- 5 files changed, 59 insertions(+), 71 deletions(-) (limited to 'modules/tag/tests') diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php index 1d790d8c..94c7dc6f 100644 --- a/modules/gallery/helpers/gallery_rest.php +++ b/modules/gallery/helpers/gallery_rest.php @@ -21,14 +21,7 @@ class gallery_rest_Core { static function get($request) { $path = implode("/", $request->arguments); - $item = ORM::factory("item") - ->where("relative_url_cache", "=", $path) - ->viewable() - ->find(); - - if (!$item->loaded()) { - return rest::not_found("Resource: {$path} missing."); - } + $item = gallery_rest::_get_item($path); $parent = $item->parent(); $response_data = array("type" => $item->type, @@ -60,25 +53,19 @@ class gallery_rest_Core { return rest::invalid_request(); } $path = implode("/", $request->arguments); - - $item = ORM::factory("item") - ->where("relative_url_cache", "=", $path) - ->viewable() - ->find(); - - if (!$item->loaded()) { - return rest::not_found("Resource: {$path} missing."); - } - - if (!access::can("edit", $item)) { - return rest::not_found("Resource: {$path} permission denied."); - } + $item = gallery_rest::_get_item($path, "edit"); // Validate the request data $new_values = gallery_rest::_validate($request, $item->parent_id, $item->id); $errors = $new_values->errors(); if (empty($errors)) { - item::update($item, $new_values->as_array()); + $item->title = $new_values->title; + $item->description = $new_values->description; + if ($item->id != 1) { + $item->rename($new_values->name); + } + $item->slug = $new_values->slug; + $item->save(); log::success("content", "Updated $item->type", "type}s/$item->id\">view"); @@ -93,23 +80,11 @@ class gallery_rest_Core { if (empty($request->arguments)) { return rest::invalid_request(); } - $path = implode("/", $request->arguments); $components = $request->arguments; $name = urldecode(array_pop($components)); - $parent = ORM::factory("item") - ->where("relative_url_cache", "=", implode("/", $components)) - ->viewable() - ->find(); - - if (!$parent->loaded()) { - return rest::not_found("Resource: {$path} missing."); - } - - if (!access::can("edit", $parent)) { - return rest::not_found("Resource: {$path} permission denied."); - } + $parent = gallery_rest::_get_item(implode("/", $components), "edit"); // Validate the request data $new_values = gallery_rest::_validate($request, $parent->id); @@ -153,18 +128,7 @@ class gallery_rest_Core { } $path = implode("/", $request->arguments); - $item = ORM::factory("item") - ->where("relative_url_cache", "=", $path) - ->viewable() - ->find(); - - if (!$item->loaded()) { - return rest::success(); - } - - if (!access::can("edit", $item)) { - return rest::not_found("Resource: {$path} permission denied."); - } + $item = gallery_rest::_get_item($path, "edit"); if ($item->id == 1) { return rest::invalid_request("Attempt to delete the root album"); @@ -183,6 +147,20 @@ class gallery_rest_Core { return rest::success(array("resource" => array("parent_path" => $parent->relative_url()))); } + private static function _get_item($path, $permission="view") { + $item = url::get_item_from_uri($path); + + if (!$item->loaded()) { + throw new Kohana_404_Exception(); + } + + if (!access::can($permission, $item)) { + throw new Kohana_404_Exception(); + } + + return $item; + } + private static function _get_children($item, $request) { $children = array(); $limit = empty($request->limit) ? null : $request->limit; diff --git a/modules/gallery/tests/Gallery_Rest_Helper_Test.php b/modules/gallery/tests/Gallery_Rest_Helper_Test.php index f36f6aaf..fba83d47 100644 --- a/modules/gallery/tests/Gallery_Rest_Helper_Test.php +++ b/modules/gallery/tests/Gallery_Rest_Helper_Test.php @@ -136,8 +136,12 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { "title" => "Updated Title", "name" => "new name"); - $this->assert_equal(json_encode(array("status" => "ERROR", "message" => "Resource not found")), - gallery_rest::put($request)); + try { + gallery_rest::put($request); + } catch (Kohana_404_Exception $k404) { + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function gallery_rest_put_album_no_edit_permission_test() { @@ -147,8 +151,12 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { "title" => "Updated Title", "name" => "new name"); - $this->assert_equal(json_encode(array("status" => "ERROR", "message" => "Resource not found")), - gallery_rest::put($request)); + try { + gallery_rest::put($request); + } catch (Kohana_404_Exception $k404) { + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function gallery_rest_put_album_rename_conflict_test() { diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php index 276ff0c2..4b3166c0 100644 --- a/modules/rest/helpers/rest.php +++ b/modules/rest/helpers/rest.php @@ -46,14 +46,7 @@ class rest_Core { } /** - * Resource Not Found - */ - static function not_found($log_message=null) { - return self::_format_failure_response(t("Resource not found"), $log_message); - } - - /** - * Resource Not Found + * Request failed */ static function fail($log_message=null) { return self::_format_failure_response($log_message, $log_message); diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index ed6cfc1c..cfcf93b2 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -70,11 +70,11 @@ class tag_rest_Core { ->viewable() ->find(); if (!$item->loaded()) { - return rest::not_found("Resource: {$path} missing."); + throw new Kohana_404_Exception(); } if (!access::can("edit", $item)) { - return rest::not_found("Resource: {$path} permission denied."); + throw new Kohana_404_Exception(); } foreach ($tags as $tag) { @@ -94,7 +94,7 @@ class tag_rest_Core { ->where("name", "=", $name) ->find(); if (!$tag->loaded()) { - return rest::not_found("Tag: {$name} not found."); + throw new Kohana_404_Exception(); } $tag->name = $request->new_name; diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php index 6b1c9a33..4408bf4b 100644 --- a/modules/tag/tests/Tag_Rest_Helper_Test.php +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -119,9 +119,12 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { public function tag_rest_add_tags_for_item_not_found_test() { $request = (object)array("path" => $this->_photo->relative_url() . "b", "arguments" => array("new,one")); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => "Resource not found")), - tag_rest::post($request)); + try { + tag_rest::post($request); + } catch (Kohana_404_Exception $k404) { + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function tag_rest_add_tags_for_item_no_access_test() { @@ -129,9 +132,12 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { $request = (object)array("path" => $this->_photo->relative_url(), "arguments" => array("new,one")); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => "Resource not found")), - tag_rest::post($request)); + try { + tag_rest::post($request); + } catch (Kohana_404_Exception $k404) { + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function tag_rest_add_tags_for_item_test() { @@ -175,9 +181,12 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { public function tag_rest_update_tags_not_found_test() { $request = (object)array("arguments" => array("not"), "new_name" => "found"); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => "Resource not found")), - tag_rest::put($request)); + try { + tag_rest::put($request); + } catch (Kohana_404_Exception $k404) { + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function tag_rest_update_tags_test() { -- cgit v1.2.3 From 40d496edeef614f7f3bc55d2fb178ea581dcd9f6 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 31 Dec 2009 13:24:00 -0800 Subject: Remove extra debug statement in rest_controller::access_key() and restructure the Rest_Tag_Helper_Test to only create items as required for test in each test. --- modules/rest/controllers/rest.php | 1 - modules/tag/tests/Tag_Rest_Helper_Test.php | 164 ++++++++++++++++++----------- 2 files changed, 102 insertions(+), 63 deletions(-) (limited to 'modules/tag/tests') diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index 05935e75..446ec7cb 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -41,7 +41,6 @@ class Rest_Controller extends Controller { $key->user_id = $user->id; $key->access_key = md5($user->name . rand()); $key->save(); - Kohana_Log::add("alert", Kohana::debug($key->as_array())); } print rest::success(array("token" => $key->access_key)); } diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php index 4408bf4b..ac64470c 100644 --- a/modules/tag/tests/Tag_Rest_Helper_Test.php +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -21,35 +21,6 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { public function setup() { $this->_save = array($_GET, $_POST, $_SERVER, $_FILES); $this->_saved_active_user = identity::active_user(); - - $this->_user = identity::create_user("access_test", "Access Test", "password"); - $key = ORM::factory("user_access_token"); - $this->_access_key = $key->access_key = md5($this->_user->name . rand()); - $key->user_id = $this->_user->id; - $key->save(); - - $root = ORM::factory("item", 1); - $this->_album = album::create($root, "album", "Test Album", rand()); - tag::add($this->_album, "albums"); - tag::add($this->_album, "A1"); - tag::add($this->_album, "T1"); - $this->_child = album::create($this->_album, "child", "Test Child Album", rand()); - tag::add($this->_child, "albums"); - tag::add($this->_child, "C1"); - tag::add($this->_child, "T1"); - - $filename = MODPATH . "gallery/tests/test.jpg"; - $rand = rand(); - $this->_photo = photo::create($this->_child, $filename, "$rand.jpg", $rand); - tag::add($this->_photo, "photos"); - tag::add($this->_photo, "P1"); - tag::add($this->_photo, "T1"); - - $filename = MODPATH . "gallery/tests/test.jpg"; - $rand = rand(); - $this->_sibling = photo::create($this->_album, $filename, "$rand.jpg", $rand); - tag::add($this->_sibling, "photos"); - tag::add($this->_sibling, "P3"); } public function teardown() { @@ -57,19 +28,52 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { identity::set_active_user($this->_saved_active_user); try { - if (!empty($this->_user)) { - $this->_user->delete(); - } - if (!empty($this->_album)) { - $this->_album->delete(); - } Database::instance()->query("TRUNCATE {tags}"); Database::instance()->query("TRUNCATE {items_tags}"); } catch (Exception $e) { } } + private function _create_user() { + $user = identity::create_user("access_test" . rand(), "Access Test", "password"); + $key = ORM::factory("user_access_token"); + $key->access_key = md5($user->name . rand()); + $key->user_id = $user->id; + $key->save(); + identity::set_active_user($user); + return $user; + } + private function _create_album($tags=array(), $parent=null) { + $album_name = "album_" . rand(); + if (empty($parent)) { + $parent = ORM::factory("item", 1); + } + $album = album::create($parent, $album_name, $album_name, $album_name); + foreach ($tags as $tag) { + tag::add($album, $tag); + } + return $album; + } + + private function _create_image($tags=array(), $parent=null) { + $filename = MODPATH . "gallery/tests/test.jpg"; + $image_name = "image_" . rand(); + if (empty($parent)) { + $parent = ORM::factory("item", 1); + } + $photo = photo::create($parent, $filename, "$image_name.jpg", $image_name); + foreach ($tags as $tag) { + tag::add($photo, $tag); + } + return $photo; + } + public function tag_rest_get_all_test() { + $album = $this->_create_album(array("albums", "A1", "T1")); + $child = $this->_create_album(array("albums", "C1", "T1"), $album); + $photo = $this->_create_image(array("photos", "P1", "T1"), $child); + $sibling = $this->_create_image(array("photos", "P3"), $album); + $request = (object)array("arguments" => array(), "limit" => 2, "offset" => 1); $this->assert_equal( @@ -80,7 +84,9 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { } public function tag_rest_get_tags_for_item_test() { - $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + $photo = $this->_create_image(array("photos", "P1", "T1")); + + $request = (object)array("arguments" => explode("/", $photo->relative_url())); $this->assert_equal( json_encode(array("status" => "OK", @@ -89,10 +95,14 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { } public function tag_rest_get_items_test() { + $album = $this->_create_album(array("albums", "A1", "T1")); + $child = $this->_create_album(array("albums", "A1", "T1"), $album); + $photo = $this->_create_image(array("photos", "P1", "T1"), $child); + $sibling = $this->_create_image(array("photos", "P3"), $album); $request = (object)array("arguments" => array("albums")); $resources = array(); - foreach (array($this->_album, $this->_child) as $resource) { + foreach (array($album, $child) as $resource) { $resources[] = array("type" => $resource->type, "has_children" => $resource->children_count() > 0, "path" => $resource->relative_url(), @@ -111,13 +121,18 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { public function tag_rest_add_tags_for_item_no_path_test() { $request = (object)array("arguments" => array("new,one")); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => "Invalid request")), - tag_rest::post($request)); + try { + tag_rest::post($request); + } catch (Rest_Exception $e) { + $this->assert_equal("400 Bad request", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function tag_rest_add_tags_for_item_not_found_test() { - $request = (object)array("path" => $this->_photo->relative_url() . "b", + $photo = $this->_create_image(array("photos", "P1", "T1")); + $request = (object)array("path" => $photo->relative_url() . "b", "arguments" => array("new,one")); try { tag_rest::post($request); @@ -128,8 +143,9 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { } public function tag_rest_add_tags_for_item_no_access_test() { - identity::set_active_user($this->_user); - $request = (object)array("path" => $this->_photo->relative_url(), + $photo = $this->_create_image(array("photos", "P1", "T1")); + $this->_create_user(); + $request = (object)array("path" => $photo->relative_url(), "arguments" => array("new,one")); try { @@ -141,15 +157,19 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { } public function tag_rest_add_tags_for_item_test() { - access::allow(identity::registered_users(), "edit", $this->_child); - identity::set_active_user($this->_user); - $request = (object)array("path" => $this->_photo->relative_url(), + $album = $this->_create_album(array("albums", "A1", "T1")); + $child = $this->_create_album(array("albums", "A1", "T1"), $album); + $photo = $this->_create_image(array("photos", "P1", "T1"), $child); + $sibling = $this->_create_image(array("photos", "P3"), $album); + access::allow(identity::registered_users(), "edit", $child); + $this->_create_user(); + $request = (object)array("path" => $photo->relative_url(), "arguments" => array("new,one")); $this->assert_equal( json_encode(array("status" => "OK")), tag_rest::post($request)); - $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + $request = (object)array("arguments" => explode("/", $photo->relative_url())); $this->assert_equal( json_encode(array("status" => "OK", "tags" => array("photos", "P1", "T1", "new", "one"))), @@ -159,23 +179,33 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { public function tag_rest_update_tag_no_arguments_test() { $request = (object)array("arguments" => array()); - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => "Invalid request")), - tag_rest::put($request)); + try { + tag_rest::put($request); + } catch (Rest_Exception $e) { + $this->assert_equal("400 Bad request", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function tag_rest_update_tag_one_arguments_test() { $request = (object)array("arguments" => array("photos")); - - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => "Invalid request")), - tag_rest::put($request)); + try { + tag_rest::put($request); + } catch (Rest_Exception $e) { + $this->assert_equal("400 Bad request", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } $request = (object)array("arguments" => array(), "new_name" => "valid"); - - $this->assert_equal( - json_encode(array("status" => "ERROR", "message" => "Invalid request")), - tag_rest::put($request)); + try { + tag_rest::put($request); + } catch (Rest_Exception $e) { + $this->assert_equal("400 Bad request", $e->getMessage()); + } catch (Exception $e) { + $this->assert_false(true, $e->__toString()); + } } public function tag_rest_update_tags_not_found_test() { @@ -190,13 +220,17 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { } public function tag_rest_update_tags_test() { + $album = $this->_create_album(array("albums", "A1", "T1")); + $child = $this->_create_album(array("albums", "A1", "T1"), $album); + $photo = $this->_create_image(array("photos", "P1", "T1"), $child); + $sibling = $this->_create_image(array("photos", "P3"), $album); $request = (object)array("arguments" => array("albums"), "new_name" => "new name"); $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::put($request)); $request = (object)array("arguments" => array("new name")); $resources = array(); - foreach (array($this->_album, $this->_child) as $resource) { + foreach (array($album, $child) as $resource) { $resources[] = array("type" => $resource->type, "has_children" => $resource->children_count() > 0, "path" => $resource->relative_url(), @@ -214,8 +248,11 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { } public function tag_rest_delete_tag_test() { - $request = (object)array("arguments" => array("T1,P1")); + $album = $this->_create_album(array("albums", "A1", "T1")); + $child = $this->_create_album(array("albums", "A1", "T1"), $album); + $photo = $this->_create_image(array("photos", "P1", "T1"), $child); + $request = (object)array("arguments" => array("T1,P1")); $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); $request = (object)array("arguments" => array("T1,P1")); @@ -224,12 +261,15 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { } public function tag_rest_delete_tagc_from_item_test() { + $album = $this->_create_album(array("albums", "A1", "T1")); + $child = $this->_create_album(array("albums", "A1", "T1"), $album); + $photo = $this->_create_image(array("photos", "P1", "T1"), $child); $request = (object)array("arguments" => array("T1,P1"), - $this->_photo->relative_url()); + $photo->relative_url()); $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); - $request = (object)array("arguments" => explode("/", $this->_photo->relative_url())); + $request = (object)array("arguments" => explode("/", $photo->relative_url())); $this->assert_equal(json_encode(array("status" => "OK", "tags" => array("photos"))), tag_rest::get($request)); } -- cgit v1.2.3 From 28597ba53354537704899e7ad9eb39bbd5718b21 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sat, 2 Jan 2010 14:31:59 -0800 Subject: Correct file structure tests, Have the tests delete the userid they create so as not to impact other tests. --- modules/gallery/tests/Gallery_Rest_Helper_Test.php | 22 ++++++++++------ modules/rest/controllers/rest.php | 3 ++- modules/rest/helpers/rest.php | 3 ++- modules/rest/helpers/rest_event.php | 3 ++- modules/rest/tests/Rest_Controller_Test.php | 29 ++++++++++++++-------- modules/tag/tests/Tag_Rest_Helper_Test.php | 20 +++++++++------ 6 files changed, 52 insertions(+), 28 deletions(-) (limited to 'modules/tag/tests') diff --git a/modules/gallery/tests/Gallery_Rest_Helper_Test.php b/modules/gallery/tests/Gallery_Rest_Helper_Test.php index 4cd3f2a6..605a4f37 100644 --- a/modules/gallery/tests/Gallery_Rest_Helper_Test.php +++ b/modules/gallery/tests/Gallery_Rest_Helper_Test.php @@ -26,17 +26,25 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { public function teardown() { list($_GET, $_POST, $_SERVER, $_FILES) = $this->_save; identity::set_active_user($this->_saved_active_user); + if (!empty($this->_user)) { + try { + $this->_user->delete(); + } catch (Exception $e) { } + } } private function _create_user() { - $user = identity::create_user("access_test" . rand(), "Access Test", "password"); - $key = ORM::factory("user_access_token"); - $key->access_key = md5($user->name . rand()); - $key->user_id = $user->id; - $key->save(); - identity::set_active_user($user); - return $user; + if (empty($this->_user)) { + $this->_user = identity::create_user("access_test" . rand(), "Access Test", "password"); + $key = ORM::factory("user_access_token"); + $key->access_key = md5($this->_user->name . rand()); + $key->user_id = $this->_user->id; + $key->save(); + identity::set_active_user($this->_user); + } + return $this->_user; } + private function _create_album($parent=null) { $album_name = "album_" . rand(); if (empty($parent)) { diff --git a/modules/rest/controllers/rest.php b/modules/rest/controllers/rest.php index 446ec7cb..39ca4797 100644 --- a/modules/rest/controllers/rest.php +++ b/modules/rest/controllers/rest.php @@ -1,4 +1,5 @@ -access_key = md5($user->name . rand()); - $key->user_id = $user->id; - $key->save(); - return array($key->access_key, $user); + if (empty($this->_user)) { + $this->_user = identity::create_user("access_test" . rand(), "Access Test", "password"); + $this->_key = ORM::factory("user_access_token"); + $this->_key->access_key = md5($this->_user->name . rand()); + $this->_key->user_id = $this->_user->id; + $this->_key->save(); + identity::set_active_user($this->_user); + } + return array($this->_key->access_key, $this->_user); + } + + public function teardown() { + list($_GET, $_POST, $_SERVER) = $this->_save; + if (!empty($this->_user)) { + try { + $this->_user->delete(); + } catch (Exception $e) { } + } } private function _create_image($parent=null) { @@ -40,11 +52,6 @@ class Rest_Controller_Test extends Unit_Test_Case { return photo::create($parent, $filename, "$image_name.jpg", $image_name); } - - public function teardown() { - list($_GET, $_POST, $_SERVER) = $this->_save; - } - public function rest_access_key_exists_test() { list ($access_key, $user) = $this->_create_user(); $_SERVER["REQUEST_METHOD"] = "GET"; diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php index ac64470c..055e5cec 100644 --- a/modules/tag/tests/Tag_Rest_Helper_Test.php +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -31,18 +31,24 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { Database::instance()->query("TRUNCATE {tags}"); Database::instance()->query("TRUNCATE {items_tags}"); + if (!empty($this->_user)) { + $this->_user->delete(); + } } catch (Exception $e) { } } private function _create_user() { - $user = identity::create_user("access_test" . rand(), "Access Test", "password"); - $key = ORM::factory("user_access_token"); - $key->access_key = md5($user->name . rand()); - $key->user_id = $user->id; - $key->save(); - identity::set_active_user($user); - return $user; + if (empty($this->_user)) { + $this->_user = identity::create_user("access_test" . rand(), "Access Test", "password"); + $key = ORM::factory("user_access_token"); + $key->access_key = md5($this->_user->name . rand()); + $key->user_id = $this->_user->id; + $key->save(); + identity::set_active_user($this->_user); + } + return $this->_user; } + private function _create_album($tags=array(), $parent=null) { $album_name = "album_" . rand(); if (empty($parent)) { -- cgit v1.2.3 From ff95c4079e70cec101a214a07d2ef64ac381d5cd Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Sat, 2 Jan 2010 15:48:54 -0800 Subject: Fix the tag_rest helper tests --- modules/tag/tests/Tag_Rest_Helper_Test.php | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'modules/tag/tests') diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php index 055e5cec..4e8dd527 100644 --- a/modules/tag/tests/Tag_Rest_Helper_Test.php +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -19,6 +19,10 @@ */ class Tag_Rest_Helper_Test extends Unit_Test_Case { public function setup() { + try { + Database::instance()->query("TRUNCATE {tags}"); + Database::instance()->query("TRUNCATE {items_tags}"); + } catch (Exception $e) { } $this->_save = array($_GET, $_POST, $_SERVER, $_FILES); $this->_saved_active_user = identity::active_user(); } @@ -28,9 +32,6 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { identity::set_active_user($this->_saved_active_user); try { - Database::instance()->query("TRUNCATE {tags}"); - Database::instance()->query("TRUNCATE {items_tags}"); - if (!empty($this->_user)) { $this->_user->delete(); } @@ -50,7 +51,7 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { } private function _create_album($tags=array(), $parent=null) { - $album_name = "album_" . rand(); + $album_name = "tag_album_" . rand(); if (empty($parent)) { $parent = ORM::factory("item", 1); } @@ -63,7 +64,7 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { private function _create_image($tags=array(), $parent=null) { $filename = MODPATH . "gallery/tests/test.jpg"; - $image_name = "image_" . rand(); + $image_name = "tag_image_" . rand(); if (empty($parent)) { $parent = ORM::factory("item", 1); } @@ -105,6 +106,9 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { $child = $this->_create_album(array("albums", "A1", "T1"), $album); $photo = $this->_create_image(array("photos", "P1", "T1"), $child); $sibling = $this->_create_image(array("photos", "P3"), $album); + $child->reload(); + $album->reload(); + $request = (object)array("arguments" => array("albums")); $resources = array(); @@ -113,9 +117,8 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { "has_children" => $resource->children_count() > 0, "path" => $resource->relative_url(), "thumb_url" => $resource->thumb_url(), - "thumb_dimensions" => array( - "width" => $resource->thumb_width, - "height" => $resource->thumb_height), + "thumb_dimensions" => array("width" => $resource->thumb_width, + "height" => $resource->thumb_height), "has_thumb" => $resource->has_thumb(), "title" => $resource->title); @@ -229,7 +232,11 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { $album = $this->_create_album(array("albums", "A1", "T1")); $child = $this->_create_album(array("albums", "A1", "T1"), $album); $photo = $this->_create_image(array("photos", "P1", "T1"), $child); + $child->reload(); $sibling = $this->_create_image(array("photos", "P3"), $album); + $child->reload(); + $album->reload(); + $request = (object)array("arguments" => array("albums"), "new_name" => "new name"); $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::put($request)); @@ -241,9 +248,8 @@ class Tag_Rest_Helper_Test extends Unit_Test_Case { "has_children" => $resource->children_count() > 0, "path" => $resource->relative_url(), "thumb_url" => $resource->thumb_url(), - "thumb_dimensions" => array( - "width" => $resource->thumb_width, - "height" => $resource->thumb_height), + "thumb_dimensions" => array("width" => $resource->thumb_width, + "height" => $resource->thumb_height), "has_thumb" => $resource->has_thumb(), "title" => $resource->title); -- cgit v1.2.3