diff options
-rw-r--r-- | modules/gallery/controllers/photos.php | 3 | ||||
-rw-r--r-- | modules/gallery/helpers/gallery_rest.php | 51 | ||||
-rw-r--r-- | modules/gallery/tests/Gallery_Rest_Helper_Test.php | 108 | ||||
-rw-r--r-- | modules/rest/helpers/rest.php | 21 |
4 files changed, 168 insertions, 15 deletions
diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index f2c0f5dd..455ac25c 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -102,8 +102,7 @@ class Photos_Controller extends Items_Controller { log::success("content", "Updated photo", "<a href=\"{$photo->url()}\">view</a>"); message::success( - t("Saved photo %photo_title", - array("photo_title" => html::purify($photo->title)))); + t("Saved photo %photo_title", array("photo_title" => html::purify($photo->title)))); print json_encode( array("result" => "success", diff --git a/modules/gallery/helpers/gallery_rest.php b/modules/gallery/helpers/gallery_rest.php index 043e17b5..82d1bb5b 100644 --- a/modules/gallery/helpers/gallery_rest.php +++ b/modules/gallery/helpers/gallery_rest.php @@ -46,6 +46,57 @@ class gallery_rest_Core { return rest::success(array($item->type => $response_data)); } + static function put($request) { + if (empty($request->path)) { + return rest::invalid_request(); + } + + $item = ORM::factory("item") + ->where("relative_url_cache", $request->path) + ->viewable() + ->find(); + + if (!$item->loaded) { + return rest::not_found("Resource: {$request->path} missing."); + } + + if (!access::can("edit", $item)) { + return rest::not_found("Resource: {$request->path} permission denied."); + } + + // Normalize the request + $new_values = array(); + $fields = array("title", "description", "name", "slug"); + if ($item->is_album()) { + $fields = array_merge($fields, array("sort_column", "sort_order")); + } + foreach ($fields as $field) { + $new_values[$field] = !empty($request->$field) ? $request->$field : $item->$field; + } + if ($item->id == 1) { + unset($new_values["name"]); + } + if ($item->id != 1 && + ($new_values["name"] != $item->name || $new_values["slug"] != $item->slug)) { + // Make sure that there's not a conflict + $errors = item::check_for_conflicts($item, $new_values["name"], $new_values["slug"]); + if (!empty($errors["name_conflict"])) { + return rest::fail(t("Renaming %path failed: new name exists", + array("path" => $request->path))); + } + if (!empty($errors["slug_conflict"])) { + return rest::fail(t("Renaming %path failed: new internet address exists", + array("path" => $request->path))); + } + } + + item::update($item, $new_values); + + log::success("content", "Updated $item->type", "<a href=\"{$item->type}s/$item->id\">view</a>"); + + return rest::success(); + } + 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 b874863f..9c960409 100644 --- a/modules/gallery/tests/Gallery_Rest_Helper_Test.php +++ b/modules/gallery/tests/Gallery_Rest_Helper_Test.php @@ -36,7 +36,9 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { $rand = rand(); $this->_photo = photo::create($this->_child, $filename, "$rand.jpg", $rand); - identity::set_active_user($this->_user); + $filename = MODPATH . "gallery/tests/test.jpg"; + $rand = rand(); + $this->_sibling = photo::create($this->_album, $filename, "$rand.jpg", $rand); } public function teardown() { @@ -54,11 +56,11 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { } public function gallery_rest_get_album_test() { - $request = (object)array("path" => $this->_child->relative_path()); + $request = (object)array("path" => $this->_child->relative_url()); $this->assert_equal( json_encode(array("status" => "OK", - "album" => array("path" => $this->_child->relative_url_path(), + "album" => array("path" => $this->_child->relative_url(), "title" => $this->_child->title, "thumb_url" => $this->_child->thumb_url(), "url" => $this->_child->abs_url(), @@ -67,17 +69,17 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { "children" => array(array( "type" => "photo", "has_children" => false, - "path" => $this->_photo->relative_url_path(), + "path" => $this->_photo->relative_url(), "title" => $this->_photo->title))))), gallery_rest::get($request)); } public function gallery_rest_get_photo_test() { - $request = (object)array("path" => $this->_photo->relative_path()); + $request = (object)array("path" => $this->_photo->relative_url()); $this->assert_equal( json_encode(array("status" => "OK", - "photo" => array("path" => $this->_photo->relative_path(), + "photo" => array("path" => $this->_photo->relative_url(), "title" => $this->_photo->title, "thumb_url" => $this->_photo->thumb_url(), "url" => $this->_photo->abs_url(), @@ -85,4 +87,98 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case { "internet_address" => $this->_photo->slug))), gallery_rest::get($request)); } + + public function gallery_rest_put_album_no_path_test() { + access::allow(identity::registered_users(), "edit", $this->_child); + + identity::set_active_user($this->_user); + $request = (object)array("description" => "Updated description", + "title" => "Updated Title", + "sort_order" => "DESC", + "sort_column" => "title", + "name" => "new name"); + + $this->assert_equal(json_encode(array("status" => "ERROR", "message" => "Invalid request")), + gallery_rest::put($request)); + } + + public function gallery_rest_put_album_not_found_test() { + access::allow(identity::registered_users(), "edit", $this->_child); + + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_child->relative_url() . rand(), + "description" => "Updated description", + "title" => "Updated Title", + "sort_order" => "DESC", + "sort_column" => "title", + "name" => "new name"); + + $this->assert_equal(json_encode(array("status" => "ERROR", "message" => "Resource not found")), + gallery_rest::put($request)); + } + + public function gallery_rest_put_album_no_edit_permission_test() { + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_child->relative_url(), + "description" => "Updated description", + "title" => "Updated Title", + "sort_order" => "DESC", + "sort_column" => "title", + "name" => "new name"); + + $this->assert_equal(json_encode(array("status" => "ERROR", "message" => "Resource not found")), + gallery_rest::put($request)); + } + + public function gallery_rest_put_album_rename_conflict_test() { + access::allow(identity::registered_users(), "edit", $this->_child); + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_child->relative_url(), + "description" => "Updated description", + "title" => "Updated Title", + "sort_order" => "DESC", + "sort_column" => "title", + "name" => $this->_sibling->name); + + $this->assert_equal( + json_encode(array("status" => "ERROR", + "message" => "Renaming album/child failed: new name exists")), + gallery_rest::put($request)); + } + + public function gallery_rest_put_album_test() { + access::allow(identity::registered_users(), "edit", $this->_child); + + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_child->relative_url(), + "description" => "Updated description", + "title" => "Updated Title", + "sort_order" => "DESC", + "sort_column" => "title", + "name" => "new name"); + + $this->assert_equal(json_encode(array("status" => "OK")), gallery_rest::put($request)); + $this->_child->reload(); + $this->assert_equal("Updated description", $this->_child->description); + $this->assert_equal("Updated Title", $this->_child->title); + $this->assert_equal("DESC", $this->_child->sort_order); + $this->assert_equal("title", $this->_child->sort_column); + $this->assert_equal("new name", $this->_child->name); + } + + public function gallery_rest_put_photo_test() { + access::allow(identity::registered_users(), "edit", $this->_child); + + identity::set_active_user($this->_user); + $request = (object)array("path" => $this->_photo->relative_url(), + "description" => "Updated description", + "title" => "Updated Title", + "name" => "new name"); + + $this->assert_equal(json_encode(array("status" => "OK")), gallery_rest::put($request)); + $this->_photo->reload(); + $this->assert_equal("Updated description", $this->_photo->description); + $this->assert_equal("Updated Title", $this->_photo->title); + $this->assert_equal("new name", $this->_photo->name); + } } diff --git a/modules/rest/helpers/rest.php b/modules/rest/helpers/rest.php index fbbd6733..2c653f21 100644 --- a/modules/rest/helpers/rest.php +++ b/modules/rest/helpers/rest.php @@ -21,41 +21,48 @@ class rest_Core { * Authorization Failure */ static function forbidden($log_message=null) { - return self::_format_response(t("Authorization failed"), $log_message); + return self::_format_failure_response(t("Authorization failed"), $log_message); } /** * Invalid Failure */ static function invalid_request($log_message=null) { - return self::_format_response(t("Invalid request"), $log_message); + return self::_format_failure_response(t("Invalid request"), $log_message); } /** * Not Implemented */ static function not_implemented($log_message=null) { - return self::_format_response(t("Service not implemented"), $log_message); + return self::_format_failure_response(t("Service not implemented"), $log_message); } /** * Internal Error */ static function internal_error($log_message=null) { - return self::_format_response(t("Internal error"), $log_message); + return self::_format_failure_response(t("Internal error"), $log_message); } /** * Resource Not Found */ static function not_found($log_message=null) { - return self::_format_response(t("Resource not found"), $log_message); + return self::_format_failure_response(t("Resource not found"), $log_message); + } + + /** + * Resource Not Found + */ + static function fail($log_message=null) { + return self::_format_failure_response($log_message, $log_message); } /** * Success */ - static function success($response_data, $message=null) { + static function success($response_data=null, $message=null) { $response = array("status" => "OK"); if (!empty($message)) { $response["message"] = (string)$message; @@ -68,7 +75,7 @@ class rest_Core { return json_encode($response); } - private static function _format_response($message, $log_message) { + private static function _format_failure_response($message, $log_message) { if (!empty($log_message)) { Kohana::log("info", $log_message); } |