diff options
author | Nathan Kinkade <nkinkade@nkinka.de> | 2010-02-02 02:59:20 +0000 |
---|---|---|
committer | Nathan Kinkade <nkinkade@nkinka.de> | 2010-02-02 02:59:20 +0000 |
commit | 9d0927dda936756f1f5003813f437d714fe481f8 (patch) | |
tree | fe1b887345b37387ab0ddcfd78bf344f6150b6cc /modules/tag | |
parent | a6f794c20dc3592bcaef17c622413c1b670a20d8 (diff) | |
parent | 43985ea2fb137aa7d532617271e37d7c20def3c5 (diff) |
Merge branch 'master' of git://github.com/gallery/gallery3
Diffstat (limited to 'modules/tag')
-rw-r--r-- | modules/tag/controllers/admin_tags.php | 7 | ||||
-rw-r--r-- | modules/tag/controllers/tags.php | 6 | ||||
-rw-r--r-- | modules/tag/helpers/item_tags_rest.php | 64 | ||||
-rw-r--r-- | modules/tag/helpers/tag.php | 29 | ||||
-rw-r--r-- | modules/tag/helpers/tag_event.php | 12 | ||||
-rw-r--r-- | modules/tag/helpers/tag_item_rest.php | 50 | ||||
-rw-r--r-- | modules/tag/helpers/tag_items_rest.php | 65 | ||||
-rw-r--r-- | modules/tag/helpers/tag_rest.php | 161 | ||||
-rw-r--r-- | modules/tag/helpers/tag_rss.php | 2 | ||||
-rw-r--r-- | modules/tag/helpers/tags_rest.php | 60 | ||||
-rw-r--r-- | modules/tag/models/tag.php | 2 | ||||
-rw-r--r-- | modules/tag/tests/Tag_Item_Rest_Helper_Test.php | 70 | ||||
-rw-r--r-- | modules/tag/tests/Tag_Rest_Helper_Test.php | 300 | ||||
-rw-r--r-- | modules/tag/tests/Tag_Test.php | 23 | ||||
-rw-r--r-- | modules/tag/tests/Tags_Rest_Helper_Test.php | 71 |
15 files changed, 530 insertions, 392 deletions
diff --git a/modules/tag/controllers/admin_tags.php b/modules/tag/controllers/admin_tags.php index e20b8ac8..9092ec68 100644 --- a/modules/tag/controllers/admin_tags.php +++ b/modules/tag/controllers/admin_tags.php @@ -60,9 +60,7 @@ class Admin_Tags_Controller extends Admin_Controller { array("result" => "success", "location" => url::site("admin/tags"))); } else { - print json_encode( - array("result" => "error", - "form" => $form->__toString())); + print json_encode(array("result" => "error", "form" => (string) $form)); } } @@ -99,7 +97,8 @@ class Admin_Tags_Controller extends Admin_Controller { message::success($message); log::success("tags", $message); - print json_encode(array("result" => "success")); + print json_encode(array("result" => "success", + "location" => url::site("admin/tags"))); } else { print json_encode(array("result" => "error", "form" => $in_place_edit->render())); } diff --git a/modules/tag/controllers/tags.php b/modules/tag/controllers/tags.php index 992c7411..1eede907 100644 --- a/modules/tag/controllers/tags.php +++ b/modules/tag/controllers/tags.php @@ -69,11 +69,9 @@ class Tags_Controller extends Controller { print json_encode( array("result" => "success", - "cloud" => tag::cloud(30)->__toString())); + "cloud" => (string)tag::cloud(30))); } else { - print json_encode( - array("result" => "error", - "form" => $form->__toString())); + print json_encode(array("result" => "error", "form" => (string) $form)); } } diff --git a/modules/tag/helpers/item_tags_rest.php b/modules/tag/helpers/item_tags_rest.php new file mode 100644 index 00000000..43e2cef0 --- /dev/null +++ b/modules/tag/helpers/item_tags_rest.php @@ -0,0 +1,64 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class item_tags_rest_Core { + static function get($request) { + $item = rest::resolve($request->url); + $tags = array(); + foreach (tag::item_tags($item) as $tag) { + $tags[] = rest::url("tag_item", $tag, $item); + } + + return array( + "url" => $request->url, + "members" => $tags); + } + + static function post($request) { + $tag = rest::resolve($request->params->tag); + $item = rest::resolve($request->params->item); + access::required("view", $item); + + tag::add($item, $tag->name); + return array( + "url" => rest::url("tag_item", $tag, $item), + "members" => array( + rest::url("tag", $tag), + rest::url("item", $item))); + } + + static function delete($request) { + list ($tag, $item) = rest::resolve($request->url); + $tag->remove($item); + $tag->save(); + } + + static function resolve($id) { + $item = ORM::factory("item", $id); + if (!access::can("view", $item)) { + throw new Kohana_404_Exception(); + } + + return $item; + } + + static function url($item) { + return url::abs_site("rest/item_tags/{$item->id}"); + } +} diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index 8075afe4..a500be58 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -37,17 +37,11 @@ class tag_Core { if (!$tag->loaded()) { $tag->name = $tag_name; $tag->count = 0; - $tag->save(); } - if (!$tag->has($item)) { - if (!$tag->add($item, $tag)) { - throw new Exception("@todo {$tag->name} WAS_NOT_ADDED_TO {$item->id}"); - } - $tag->count++; - $tag->save(); - } - return $tag; + $tag->add($item); + $tag->count++; + return $tag->save(); } /** @@ -73,6 +67,9 @@ class tag_Core { if ($tags) { $cloud = new View("tag_cloud.html"); $cloud->max_count = $tags[0]->count; + if (!$cloud->max_count) { + return; + } usort($tags, array("tag", "sort_by_name")); $cloud->tags = $tags; return $cloud; @@ -88,16 +85,10 @@ class tag_Core { * @return array */ static function item_tags($item) { - $tags = array(); - foreach (db::build() - ->select("name") - ->from("tags") - ->join("items_tags", "tags.id", "items_tags.tag_id", "left") - ->where("items_tags.item_id", "=", $item->id) - ->execute() as $row) { - $tags[] = $row->name; - } - return $tags; + return ORM::factory("tag") + ->join("items_tags", "tags.id", "items_tags.tag_id", "left") + ->where("items_tags.item_id", "=", $item->id) + ->find_all(); } static function get_add_form($item) { diff --git a/modules/tag/helpers/tag_event.php b/modules/tag/helpers/tag_event.php index 6ee8e708..403ccd52 100644 --- a/modules/tag/helpers/tag_event.php +++ b/modules/tag/helpers/tag_event.php @@ -71,9 +71,13 @@ class tag_event_Core { $('form input[id=tags]').autocomplete( '$url', {max: 30, multiple: true, multipleSeparator: ',', cacheLength: 1}); });"); - $tag_value = implode(", ", tag::item_tags($item)); + + $tag_names = array(); + foreach (tag::item_tags($item) as $tag) { + $tag_names[] = $tag->name; + } $form->edit_item->input("tags")->label(t("Tags (comma separated)")) - ->value($tag_value); + ->value(implode(", ", $tag_names)); } static function item_edit_form_completed($item, $form) { @@ -95,7 +99,9 @@ class tag_event_Core { } static function item_index_data($item, $data) { - $data[] = join(" ", tag::item_tags($item)); + foreach (tag::item_tags($item) as $tag) { + $data[] = $tag->name; + } } static function add_photos_form($album, $form) { diff --git a/modules/tag/helpers/tag_item_rest.php b/modules/tag/helpers/tag_item_rest.php new file mode 100644 index 00000000..672cec53 --- /dev/null +++ b/modules/tag/helpers/tag_item_rest.php @@ -0,0 +1,50 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class tag_item_rest_Core { + static function get($request) { + list ($tag, $item) = rest::resolve($request->url); + return array( + "url" => $request->url, + "members" => array( + "tag" => rest::url("tag", $tag), + "item" => rest::url("item", $item))); + } + + static function delete($request) { + list ($tag, $item) = rest::resolve($request->url); + $tag->remove($item); + $tag->save(); + } + + static function resolve($tuple) { + list ($tag_id, $item_id) = split(",", $tuple); + $tag = ORM::factory("tag", $tag_id); + $item = ORM::factory("item", $item_id); + if (!$tag->loaded() || !$item->loaded() || !$tag->has($item) || !access::can("view", $item)) { + throw new Kohana_404_Exception(); + } + + return array($tag, $item); + } + + static function url($tag, $item) { + return url::abs_site("rest/tag_item/{$tag->id},{$item->id}"); + } +} diff --git a/modules/tag/helpers/tag_items_rest.php b/modules/tag/helpers/tag_items_rest.php new file mode 100644 index 00000000..18973ebb --- /dev/null +++ b/modules/tag/helpers/tag_items_rest.php @@ -0,0 +1,65 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class tag_items_rest_Core { + static function get($request) { + $tag = rest::resolve($request->url); + $items = array(); + foreach ($tag->items() as $item) { + if (access::can("view", $item)) { + $items[] = rest::url("tag_item", $tag, $item); + } + } + + return array( + "url" => $request->url, + "members" => $items); + } + + static function post($request) { + $tag = rest::resolve($request->params->tag); + $item = rest::resolve($request->params->item); + access::required("view", $item); + + if (!$tag->loaded()) { + throw new Kohana_404_Exception(); + } + + tag::add($item, $tag->name); + return array( + "url" => rest::url("tag_item", $tag, $item), + "members" => array( + "tag" => rest::url("tag", $tag), + "item" => rest::url("item", $item))); + } + + static function delete($request) { + list ($tag, $item) = rest::resolve($request->url); + $tag->remove($item); + $tag->save(); + } + + static function resolve($id) { + return ORM::factory("tag", $id); + } + + static function url($tag) { + return url::abs_site("rest/tag_items/{$tag->id}"); + } +} diff --git a/modules/tag/helpers/tag_rest.php b/modules/tag/helpers/tag_rest.php index cd1ca6c6..4879cf63 100644 --- a/modules/tag/helpers/tag_rest.php +++ b/modules/tag/helpers/tag_rest.php @@ -18,143 +18,74 @@ * 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) { - $resources = array(); - switch (count($request->arguments)) { - case 0: - $tags = ORM::factory("tag") - ->select("name", "count") - ->order_by("count", "DESC"); - if (!empty($request->limit)) { - $tags->limit($request->limit); - } - if (!empty($request->offset)) { - $tags->offset($request->offset); - } - $resources = array("tags" => array()); - foreach ($tags->find_all() as $row) { - $resources["tags"][] = array("name" => $row->name, "count" => $row->count); - } - 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)); + $tag = rest::resolve($request->url); + $tag_items = array(); + foreach ($tag->items() as $item) { + if (access::can("view", $item)) { + $tag_items[] = rest::url("tag_item", $tag, $item); } } - return rest::success($resources); + return array( + "url" => $request->url, + "entity" => $tag->as_array(), + "relationships" => array( + "items" => array( + "url" => rest::url("tag_items", $tag), + "members" => $tag_items))); } static function post($request) { - if (empty($request->arguments) || count($request->arguments) != 1 || empty($request->path)) { - throw new Rest_Exception(400, "Bad request"); - } - $path = $request->path; - $tags = explode(",", $request->arguments[0]); - - $item = ORM::factory("item") - ->where("relative_url_cache", "=", $path) - ->viewable() - ->find(); - if (!$item->loaded()) { - throw new Kohana_404_Exception(); + if (empty($request->params->url)) { + throw new Rest_Exception("Bad request", 400); } - if (!access::can("edit", $item)) { - throw new Kohana_404_Exception(); - } + $tag = rest::resolve($request->url); + $item = rest::resolve($request->params->url); + access::required("edit", $item); - foreach ($tags as $tag) { - tag::add($item, $tag); - } - return rest::success(); + tag::add($item, $tag->name); + return array("url" => rest::url("tag_item", $tag, $item)); } static function put($request) { - if (empty($request->arguments[0]) || empty($request->new_name)) { - throw new Rest_Exception(400, "Bad request"); + $tag = rest::resolve($request->url); + if (isset($request->params->name)) { + $tag->name = $request->params->name; + $tag->save(); } + } - $name = $request->arguments[0]; + static function delete($request) { + $tag = rest::resolve($request->url); + $tag->delete(); + } - $tag = ORM::factory("tag") - ->where("name", "=", $name) - ->find(); - if (!$tag->loaded()) { - throw new Kohana_404_Exception(); + static function relationships($resource_type, $resource) { + switch ($resource_type) { + case "item": + $tags = array(); + foreach (tag::item_tags($resource) as $tag) { + $tags[] = rest::url("tag_item", $tag, $resource); + } + return array( + "tags" => array( + "url" => rest::url("item_tags", $resource), + "members" => $tags)); } - - $tag->name = $request->new_name; - $tag->save(); - - return rest::success(); } - static function delete($request) { - if (empty($request->arguments[0])) { - throw new Rest_Exception(400, "Bad 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") - ->where("tags.name", "IN", $tags) - ->where("relative_url_cache", "=", $request->path) - ->viewable() - ->find_all(); - } else { - $tag_list = ORM::factory("tag") - ->where("name", "IN", $tags) - ->find_all(); + static function resolve($id) { + $tag = ORM::factory("tag", $id); + if (!$tag->loaded()) { + throw new Kohana_404_Exception(); } - foreach ($tag_list as $row) { - $row->delete(); - }; - - tag::compact(); - return rest::success(); + return $tag; } - private static function _get_items($request) { - $tags = explode(",", $request->arguments[0]); - $items = ORM::factory("item") - ->select_distinct("*") - ->join("items_tags", "items.id", "items_tags.item_id") - ->join("tags", "tags.id", "items_tags.tag_id") - ->where("tags.name", "IN", $tags); - if (!empty($request->limit)) { - $items->limit($request->limit); - } - if (!empty($request->offset)) { - $items->offset($request->offset); - } - $resources = array(); - foreach ($items->find_all() as $item) { - $resources[] = array("type" => $item->type, - "has_children" => $item->children_count() > 0, - "path" => $item->relative_url(), - "thumb_url" => $item->thumb_url(true), - "thumb_dimensions" => array("width" => $item->thumb_width, - "height" => $item->thumb_height), - "has_thumb" => $item->has_thumb(), - "title" => $item->title); - } - - return $resources; + static function url($tag) { + return url::abs_site("rest/tag/{$tag->id}"); } } diff --git a/modules/tag/helpers/tag_rss.php b/modules/tag/helpers/tag_rss.php index f09a4530..5d42caab 100644 --- a/modules/tag/helpers/tag_rss.php +++ b/modules/tag/helpers/tag_rss.php @@ -34,6 +34,8 @@ class tag_rss_Core { if (!$tag->loaded()) { throw new Kohana_404_Exception(); } + + $feed = new stdClass(); $feed->children = $tag->items($limit, $offset, "photo"); $feed->max_pages = ceil($tag->count / $limit); $feed->title = $tag->name; diff --git a/modules/tag/helpers/tags_rest.php b/modules/tag/helpers/tags_rest.php new file mode 100644 index 00000000..f28be7b5 --- /dev/null +++ b/modules/tag/helpers/tags_rest.php @@ -0,0 +1,60 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class tags_rest_Core { + static function get($request) { + $tags = array(); + foreach (ORM::factory("tag")->find_all() as $tag) { + $tags[] = rest::url("tag", $tag); + } + return array("url" => rest::url("tags"), + "members" => $tags); + } + + static function post($request) { + // The user must have some edit permission somewhere to create a tag. + if (!identity::active_user()->admin) { + $query = db::build()->from("access_caches")->and_open(); + foreach (identity::active_user()->groups() as $group) { + $query->or_where("edit_{$group->id}", "=", access::ALLOW); + } + $has_any_edit_perm = $query->close()->count_records(); + if (!$has_any_edit_perm) { + access::forbidden(); + } + } + + if (empty($request->params->name)) { + throw new Rest_Exception("Bad Request", 400); + } + + $tag = ORM::factory("tag")->where("name", "=", $request->params->name)->find(); + if (!$tag->loaded()) { + $tag->name = $request->params->name; + $tag->count = 0; + $tag->save(); + } + + return array("url" => rest::url("tag", $tag)); + } + + static function url() { + return url::abs_site("rest/tags"); + } +} diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index 2b33c30d..38a8ed69 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -95,7 +95,7 @@ class Tag_Model extends ORM { * Overload ORM::delete() to trigger an item_related_update event for all items that are * related to this tag. */ - public function delete() { + public function delete($ignored_id=null) { $related_item_ids = array(); foreach (db::build() ->select("item_id") diff --git a/modules/tag/tests/Tag_Item_Rest_Helper_Test.php b/modules/tag/tests/Tag_Item_Rest_Helper_Test.php new file mode 100644 index 00000000..cb368790 --- /dev/null +++ b/modules/tag/tests/Tag_Item_Rest_Helper_Test.php @@ -0,0 +1,70 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Tag_Item_Rest_Helper_Test extends Gallery_Unit_Test_Case { + public function setup() { + try { + Database::instance()->query("TRUNCATE {tags}"); + Database::instance()->query("TRUNCATE {items_tags}"); + } catch (Exception $e) { } + } + + public function get_test() { + $tag = tag::add(item::root(), "tag1")->reload(); + + $request = new stdClass(); + $request->url = rest::url("tag_item", $tag, item::root()); + $this->assert_equal_array( + array("url" => rest::url("tag_item", $tag, item::root()), + "members" => array( + "tag" => rest::url("tag", $tag), + "item" => rest::url("item", item::root()))), + tag_item_rest::get($request)); + } + + public function get_with_invalid_url_test() { + $request = new stdClass(); + $request->url = "bogus"; + try { + tag_item_rest::get($request); + } catch (Kohana_404_Exception $e) { + return; // pass + } + $this->assert_true(false, "Shouldn't get here"); + } + + public function delete_test() { + $tag = tag::add(item::root(), "tag1")->reload(); + + $request = new stdClass(); + $request->url = rest::url("tag_item", $tag, item::root()); + tag_item_rest::delete($request); + + $this->assert_false($tag->reload()->has(item::root())); + } + + public function resolve_test() { + $album = test::random_album(); + $tag = tag::add($album, "tag1")->reload(); + + $tuple = rest::resolve(rest::url("tag_item", $tag, $album)); + $this->assert_equal_array($tag->as_array(), $tuple[0]->as_array()); + $this->assert_equal_array($album->as_array(), $tuple[1]->as_array()); + } +} diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php index 514538d4..838de975 100644 --- a/modules/tag/tests/Tag_Rest_Helper_Test.php +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -17,272 +17,110 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Tag_Rest_Helper_Test extends Unit_Test_Case { +class Tag_Rest_Helper_Test extends Gallery_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(); } - public function teardown() { - list($_GET, $_POST, $_SERVER, $_FILES) = $this->_save; - identity::set_active_user($this->_saved_active_user); + public function get_test() { + $tag = tag::add(item::root(), "tag1")->reload(); - try { - if (!empty($this->_user)) { - $this->_user->delete(); - } - } catch (Exception $e) { } - } - - private function _create_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 = "tag_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; + $request = new stdClass(); + $request->url = rest::url("tag", $tag); + $this->assert_equal_array( + array("url" => rest::url("tag", $tag), + "entity" => $tag->as_array(), + "relationships" => array( + "items" => array( + "url" => rest::url("tag_items", $tag), + "members" => array( + rest::url("tag_item", $tag, item::root()))))), + tag_rest::get($request)); } - private function _create_image($tags=array(), $parent=null) { - $filename = MODPATH . "gallery/tests/test.jpg"; - $image_name = "tag_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); + public function get_with_invalid_url_test() { + $request = new stdClass(); + $request->url = "bogus"; + try { + tag_rest::get($request); + } catch (Kohana_404_Exception $e) { + return; // pass } - 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( - json_encode(array("status" => "OK", - "tags" => array(array("name" => "albums", "count" => "2"), - array("name" => "photos", "count" => "2")))), - tag_rest::get($request)); + $this->assert_true(false, "Shouldn't get here"); } - public function tag_rest_get_tags_for_item_test() { - $photo = $this->_create_image(array("photos", "P1", "T1")); - - $request = (object)array("arguments" => explode("/", $photo->relative_url())); + public function get_with_no_relationships_test() { + $tag = test::random_tag(); - $this->assert_equal( - json_encode(array("status" => "OK", - "tags" => array("photos", "P1", "T1"))), + $request = new stdClass(); + $request->url = rest::url("tag", $tag); + $this->assert_equal_array( + array("url" => rest::url("tag", $tag), + "entity" => $tag->as_array(), + "relationships" => array( + "items" => array( + "url" => rest::url("tag_items", $tag), + "members" => array()))), tag_rest::get($request)); } - 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); - $child->reload(); - $album->reload(); + public function post_test() { + $tag = test::random_tag(); - $request = (object)array("arguments" => array("albums")); + // Create an editable item to be tagged + $album = test::random_album(); + access::allow(identity::everybody(), "edit", $album); - $resources = array(); - foreach (array($album, $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)); + // Add the album to the tag + $request = new stdClass(); + $request->url = rest::url("tag", $tag); + $request->params = new stdClass(); + $request->params->url = rest::url("item", $album); + $this->assert_equal_array( + array("url" => rest::url("tag_item", $tag, $album)), + tag_rest::post($request)); } - public function tag_rest_add_tags_for_item_no_path_test() { - $request = (object)array("arguments" => array("new,one")); - + public function post_with_no_item_url_test() { + $request = new stdClass(); 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() { - $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); - } 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() { - $photo = $this->_create_image(array("photos", "P1", "T1")); - $this->_create_user(); - $request = (object)array("path" => $photo->relative_url(), - "arguments" => array("new,one")); - - try { - tag_rest::post($request); - } catch (Kohana_404_Exception $k404) { - } catch (Exception $e) { - $this->assert_false(true, $e->__toString()); + $this->assert_equal(400, $e->getCode()); + return; } - } - - public function tag_rest_add_tags_for_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); - $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("/", $photo->relative_url())); - $this->assert_equal( - json_encode(array("status" => "OK", - "tags" => array("photos", "P1", "T1", "new", "one"))), - tag_rest::get($request)); + $this->assert_true(false, "Shouldn't get here"); } - public function tag_rest_update_tag_no_arguments_test() { - $request = (object)array("arguments" => array()); + public function put_test() { + $tag = test::random_tag(); + $request = new stdClass(); + $request->url = rest::url("tag", $tag); + $request->params = new stdClass(); + $request->params->name = "new name"; - 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")); - 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"); - 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()); - } + tag_rest::put($request); + $this->assert_equal("new name", $tag->reload()->name); } - public function tag_rest_update_tags_not_found_test() { - $request = (object)array("arguments" => array("not"), "new_name" => "found"); + public function delete_tag_test() { + $tag = test::random_tag(); + $request = new stdClass(); + $request->url = rest::url("tag", $tag); + tag_rest::delete($request); - try { - tag_rest::put($request); - } catch (Kohana_404_Exception $k404) { - } catch (Exception $e) { - $this->assert_false(true, $e->__toString()); - } + $this->assert_false($tag->reload()->loaded()); } - 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); - $child->reload(); - $sibling = $this->_create_image(array("photos", "P3"), $album); - $child->reload(); - $album->reload(); - - $request = (object)array("arguments" => array("albums"), "new_name" => "new name"); + public function resolve_test() { + $tag = test::random_tag(); - $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::put($request)); - - $request = (object)array("arguments" => array("new name")); - $resources = array(); - foreach (array($album, $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() { - $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")); - $this->assert_equal(json_encode(array("status" => "OK")), - tag_rest::get($request)); - } - - 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"), - $photo->relative_url()); - - $this->assert_equal(json_encode(array("status" => "OK")), tag_rest::delete($request)); - - $request = (object)array("arguments" => explode("/", $photo->relative_url())); - $this->assert_equal(json_encode(array("status" => "OK", "tags" => array("photos"))), - tag_rest::get($request)); + $tag->as_array(), + rest::resolve(rest::url("tag", $tag))->as_array()); } } diff --git a/modules/tag/tests/Tag_Test.php b/modules/tag/tests/Tag_Test.php index c96e7f2b..defc4e89 100644 --- a/modules/tag/tests/Tag_Test.php +++ b/modules/tag/tests/Tag_Test.php @@ -17,26 +17,19 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Tag_Test extends Unit_Test_Case { +class Tag_Test extends Gallery_Unit_Test_Case { public function create_tag_test() { - $rand = rand(); - $root = ORM::factory("item", 1); - $album = album::create($root, $rand, $rand, $rand); - $tag1 = "tag1"; + $album = test::random_album(); - tag::add($album, $tag1); - $tag = ORM::factory("tag")->where("name", "=", $tag1)->find(); + tag::add($album, "tag1"); + $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(); - $this->assert_true(1, $tag->count); + tag::add($album, "tag1"); + $this->assert_true(1, $tag->reload()->count); - $rand = rand(); - $album = album::create($root, $rand, $rand, $rand); - tag::add($album, $tag1); - $tag = ORM::factory("tag")->where("name", "=", $tag1)->find(); - $this->assert_true(2, $tag->count); + tag::add(test::random_album(), "tag1"); + $this->assert_true(2, $tag->reload()->count); } }
\ No newline at end of file diff --git a/modules/tag/tests/Tags_Rest_Helper_Test.php b/modules/tag/tests/Tags_Rest_Helper_Test.php new file mode 100644 index 00000000..cdf7bfdf --- /dev/null +++ b/modules/tag/tests/Tags_Rest_Helper_Test.php @@ -0,0 +1,71 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Tags_Rest_Helper_Test extends Gallery_Unit_Test_Case { + public function setup() { + try { + Database::instance()->query("TRUNCATE {tags}"); + Database::instance()->query("TRUNCATE {items_tags}"); + } catch (Exception $e) { + } + } + + public function get_test() { + $t1 = tag::add(item::root(), "t1"); + $t2 = tag::add(item::root(), "t2"); + + $request = new stdClass(); + $this->assert_equal_array( + array( + "url" => rest::url("tags"), + "members" => array( + rest::url("tag", $t1), + rest::url("tag", $t2))), + tags_rest::get($request)); + } + + public function post_test() { + access::allow(identity::everybody(), "edit", item::root()); + + $request = new stdClass(); + $request->params = new stdClass(); + $request->params->name = "test tag"; + $this->assert_equal( + array("url" => url::site("rest/tag/1")), + tags_rest::post($request)); + } + + public function post_fails_without_permissions_test() { + // We have to remove edit permissions from everywhere + Database::instance()->query("UPDATE {access_caches} SET edit_1=0"); + identity::set_active_user(identity::guest()); + + try { + $request = new stdClass(); + $request->params = new stdClass(); + $request->params->name = "test tag"; + tags_rest::post($request); + } catch (Exception $e) { + $this->assert_equal(403, $e->getCode()); + return; + } + $this->assert_true(false, "Shouldnt get here"); + } + +} |