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()); }
public function build_breadcrumbs_for_item_test() { $album = test::random_album(); $item = test::random_photo($album); $expected = array(); $expected[] = Breadcrumb::instance(item::root()->title, item::root()->url("show={$album->id}"))->set_first(); $expected[] = Breadcrumb::instance($album->title, $album->url("show={$item->id}")); $expected[] = Breadcrumb::instance($item->title, $item->url())->set_last(); $this->assert_equal($expected, Breadcrumb::array_from_item_parents($item)); }
public function post_test() { $tag = test::random_tag(); // Create an editable item to be tagged $album = test::random_album(); access::allow(identity::everybody(), "edit", $album); // Add the album to the tag $request->url = rest::url("tag", $tag); $request->params->url = rest::url("item", $album); $this->assert_equal_array(array("url" => rest::url("tag_item", $tag, $album)), tag_rest::post($request)); }
private function _get_proxy() { $album = test::random_album(); $photo = test::random_photo($album); access::deny(identity::everybody(), "view_full", $album); access::deny(identity::registered_users(), "view_full", $album); $proxy = ORM::factory("digibug_proxy"); $proxy->uuid = random::hash(); $proxy->item_id = $photo->id; return $proxy->save(); }
public function create_tag_test() { $album = test::random_album(); 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"); $this->assert_true(1, $tag->reload()->count); tag::add(test::random_album(), "tag1"); $this->assert_true(2, $tag->reload()->count); }
public function deleting_an_item_deletes_its_comments_too_test() { $album = test::random_album(); $comment = ORM::factory("comment"); $comment->item_id = $album->id; $comment->author_id = identity::guest()->id; $comment->guest_name = "test"; $comment->text = "text"; $comment->save(); $album->delete(); $this->assert_false(ORM::factory("comment", $comment->id)->loaded()); }
public function viewable_test() { $album = test::random_album(); $item = test::random_photo($album); $album->reload(); identity::set_active_user(identity::guest()); // We can see the item when permissions are granted access::allow(identity::everybody(), "view", $album); $this->assert_equal(1, ORM::factory("item")->viewable()->where("id", "=", $item->id)->count_all()); // We can't see the item when permissions are denied access::deny(identity::everybody(), "view", $album); $this->assert_equal(0, ORM::factory("item")->viewable()->where("id", "=", $item->id)->count_all()); }
public function rename_merge_tag_with_same_items_test() { $album = test::random_album(); tag::add($album, "tag1"); tag::add($album, "tag2"); $tag1 = ORM::factory("tag")->where("name", "=", "tag1")->find(); $tag1->name = "tag2"; $tag1->save(); // Tags should be merged $tag1->reload(); $this->assert_equal(1, $tag1->count); $this->assert_true($tag1->has($album)); $this->assert_equal(1, ORM::factory("tag")->count_all()); }
public function cant_view_comments_for_unviewable_items_test() { $album = test::random_album(); $comment = ORM::factory("comment"); $comment->item_id = $album->id; $comment->author_id = identity::admin_user()->id; $comment->text = "text"; $comment->save(); identity::set_active_user(identity::guest()); // We can see the comment when permissions are granted on the album access::allow(identity::everybody(), "view", $album); $this->assert_true(ORM::factory("comment")->viewable()->where("comments.id", "=", $comment->id)->count_all()); // We can't see the comment when permissions are denied on the album access::deny(identity::everybody(), "view", $album); $this->assert_false(ORM::factory("comment")->viewable()->where("comments.id", "=", $comment->id)->count_all()); }
public function change_album_no_csrf_fails_test() { $controller = new Albums_Controller(); $album = test::random_album(); $_POST["name"] = "new name"; $_POST["title"] = "new title"; $_POST["description"] = "new description"; access::allow(identity::everybody(), "edit", item::root()); try { $controller->update($album->id); $this->assert_true(false, "This should fail"); } catch (Exception $e) { // pass $this->assert_same("@todo FORBIDDEN", $e->getMessage()); } }
public function move_conflicts_result_in_a_rename_test() { $rand = rand(); $photo1 = test::random_photo_unsaved(item::root()); $photo1->name = "{$rand}.jpg"; $photo1->slug = (string) $rand; $photo1->save(); $src_album = test::random_album(); $photo2 = test::random_photo_unsaved($src_album); $photo2->name = "{$rand}.jpg"; $photo2->slug = (string) $rand; $photo2->save(); item::move($photo2, item::root()); $this->assert_same(item::root()->id, $photo2->parent_id); $this->assert_not_same("{$rand}.jpg", $photo2->name); $this->assert_not_same($rand, $photo2->slug); }
public function illegal_access_test() { $album = test::random_album(); $photo = test::random_photo($album); $album->reload(); access::deny(identity::everybody(), "view", $album); identity::set_active_user(identity::guest()); $request = new stdClass(); $request->url = rest::url("data", $photo, "thumb"); $request->params = new stdClass(); $request->params->size = "thumb"; try { data_rest::get($request); $this->assert_true(false); } catch (Kohana_404_Exception $e) { // pass } }
public function get_ancestors_test() { $album1 = test::random_album(); $photo1 = test::random_photo($album1); $album2 = test::random_album($album1); $photo2 = test::random_photo($album2); $album1->reload(); $album2->reload(); $root = ORM::factory("item", 1); $restful_root = array("url" => rest::url("item", $root), "entity" => $root->as_restful_array(), "relationships" => rest::relationships("item", $root)); $restful_root["members"] = array(); foreach ($root->children() as $child) { $restful_root["members"][] = rest::url("item", $child); } $request = new stdClass(); $request->params = new stdClass(); $request->params->ancestors_for = rest::url("item", $photo2); $this->assert_equal_array(array($restful_root, array("url" => rest::url("item", $album1), "entity" => $album1->as_restful_array(), "relationships" => array("comments" => array("url" => rest::url("item_comments", $album1)), "tags" => array("url" => rest::url("item_tags", $album1), "members" => array())), "members" => array(rest::url("item", $photo1), rest::url("item", $album2))), array("url" => rest::url("item", $album2), "entity" => $album2->as_restful_array(), "relationships" => array("comments" => array("url" => rest::url("item_comments", $album2)), "tags" => array("url" => rest::url("item_tags", $album2), "members" => array())), "members" => array(rest::url("item", $photo2))), array("url" => rest::url("item", $photo2), "entity" => $photo2->as_restful_array(), "relationships" => array("comments" => array("url" => rest::url("item_comments", $photo2)), "tags" => array("url" => rest::url("item_tags", $photo2), "members" => array())))), items_rest::get($request)); }
public function descendant_count_test() { $parent = test::random_album(); $photo = test::random_photo($parent); $album1 = test::random_album($parent); $photo1 = test::random_photo($album1); $parent->reload(); $this->assert_equal(3, $parent->descendants_count()); $this->assert_equal(2, $parent->descendants_count(array(array("type", "=", "photo")))); $this->assert_equal(1, $parent->descendants_count(array(array("type", "=", "album")))); }
public function delete_album_fails_without_permission_test() { $album1 = test::random_album(); access::deny(identity::everybody(), "edit", $album1); identity::set_active_user(identity::guest()); $request->url = rest::url("item", $album1); try { item_rest::delete($request); } catch (Exception $e) { $this->assert_equal("@todo FORBIDDEN", $e->getMessage()); return; } $this->assert_true(false, "Shouldn't get here"); }
public function urls_test() { $photo = test::random_photo(); $this->assert_true(preg_match("|http://./var/resizes/name_\\w+\\.jpg\\?m=\\d+|", $photo->resize_url()), $photo->resize_url() . " is malformed"); $this->assert_true(preg_match("|http://./var/thumbs/name_\\w+\\.jpg\\?m=\\d+|", $photo->thumb_url()), $photo->thumb_url() . " is malformed"); $this->assert_true(preg_match("|http://./var/albums/name_\\w+\\.jpg\\?m=\\d+|", $photo->file_url()), $photo->file_url() . " is malformed"); // Albums have special thumbnails. Empty album has cachebuster of 0 since it has no thumbnail $album = test::random_album(); $this->assert_true(preg_match("|http://./var/thumbs/name_\\w+/\\.album\\.jpg\\?m=0|", $album->thumb_url()), $album->thumb_url() . " is malformed"); $photo = test::random_photo($album); $this->assert_true(preg_match("|http://./var/thumbs/name_\\w+/\\.album\\.jpg\\?m=\\d+|", $album->thumb_url()), $album->thumb_url() . " is malformed"); }
public function moved_items_inherit_new_permissions_test() { identity::set_active_user(identity::lookup_user_by_name("admin")); $public_album = test::random_album(); $public_photo = test::random_photo($public_album); access::allow(identity::everybody(), "view", $public_album); access::allow(identity::everybody(), "edit", $public_album); item::root()->reload(); // Account for MPTT changes $private_album = test::random_album(); access::deny(identity::everybody(), "view", $private_album); access::deny(identity::everybody(), "edit", $private_album); $private_photo = test::random_photo($private_album); // Make sure that we now have a public photo and private photo. $this->assert_true(access::group_can(identity::everybody(), "view", $public_photo)); $this->assert_false(access::group_can(identity::everybody(), "view", $private_photo)); // Swap the photos item::move($public_photo, $private_album); $private_album->reload(); // Reload to get new MPTT pointers and cached perms. $public_album->reload(); $private_photo->reload(); $public_photo->reload(); item::move($private_photo, $public_album); $private_album->reload(); // Reload to get new MPTT pointers and cached perms. $public_album->reload(); $private_photo->reload(); $public_photo->reload(); // Make sure that the public_photo is now private, and the private_photo is now public. $this->assert_false(access::group_can(identity::everybody(), "view", $public_photo)); $this->assert_false(access::group_can(identity::everybody(), "edit", $public_photo)); $this->assert_true(access::group_can(identity::everybody(), "view", $private_photo)); $this->assert_true(access::group_can(identity::everybody(), "edit", $private_photo)); }
public function find_by_relative_url_test() { $level1 = test::random_album(); $level2 = test::random_album($level1); $level3 = test::random_photo_unsaved($level2); $level3->slug = "same"; $level3->save()->reload(); $level2b = test::random_album($level1); $level3b = test::random_photo_unsaved($level2b); $level3b->slug = "same"; $level3b->save()->reload(); // Item in album $this->assert_same($level3->id, item::find_by_relative_url("{$level1->slug}/{$level2->slug}/{$level3->slug}")->id); // Album, ends without a slash $this->assert_same($level2->id, item::find_by_relative_url("{$level1->slug}/{$level2->slug}")->id); // Return root if "" is passed $this->assert_same(item::root()->id, item::find_by_relative_url("")->id); // Verify that we don't get confused by the part slugs, using the fallback code. db::build()->update("items")->set(array("relative_url_cache" => null))->where("id", "IN", array($level3->id, $level3b->id))->execute(); $this->assert_same($level3->id, item::find_by_relative_url("{$level1->slug}/{$level2->slug}/{$level3->slug}")->id); $this->assert_same($level3b->id, item::find_by_relative_url("{$level1->slug}/{$level2b->slug}/{$level3b->slug}")->id); // Verify that we don't get false positives $this->assert_false(item::find_by_relative_url("foo/bar/baz")->loaded()); // Verify that the fallback code works $this->assert_same($level3b->id, item::find_by_relative_url("{$level1->slug}/{$level2b->slug}/{$level3b->slug}")->id); }
public function as_restful_array_test() { $album = test::random_album(); $photo = test::random_photo($album); $album->reload(); $result = $album->as_restful_array(); $this->assert_same(rest::url("item", item::root()), $result["parent"]); $this->assert_same(rest::url("item", $photo), $result["album_cover"]); $this->assert_true(!array_key_exists("parent_id", $result)); $this->assert_true(!array_key_exists("album_cover_item_id", $result)); }
public function cant_proxy_an_album_test() { $album = test::random_album(); $_SERVER["REQUEST_URI"] = url::file("var/albums/{$album->name}"); $controller = new File_Proxy_Controller(); try { $controller->__call("", array()); $this->assert_true(false); } catch (Kohana_404_Exception $e) { $this->assert_same(6, $e->test_fail_code); } }
public function generate_album_cover_from_bad_photo_test() { $album = test::random_album(); $photo = test::random_photo($album); $album->reload(); // At this point, the photo is valid and has a valid resize and thumb. Make it garble. file_put_contents($photo->file_path(), test::lorem_ipsum(200)); // Regenerate album from garbled photo. $photo->thumb_dirty = 1; $photo->save(); $album->thumb_dirty = 1; try { graphics::generate($album); $this->assert_true(false, "Shouldn't get here"); } catch (Exception $e) { // Exception expected } // Check that the image got replaced with a missing image placeholder $this->assert_same(file_get_contents(MODPATH . "gallery/images/missing_photo.jpg"), file_get_contents($album->thumb_path())); // Check that the items table got updated with new metadata $this->assert_equal(array(200, 200), array($album->thumb_width, $album->thumb_height)); // Check that the images are marked as dirty $this->assert_equal(1, $album->thumb_dirty); }
public function move_album_inside_descendent_fails_test() { $album1 = test::random_album(); $album2 = test::random_album($album1); $album3 = test::random_album($album2); try { $album1->parent_id = $album3->id; $album1->save(); } catch (ORM_Validation_Exception $e) { $this->assert_equal(array("parent_id" => "invalid"), $e->validation->errors()); return; } $this->assert_true(false, "Shouldn't get here"); }
public function fix_conflict_when_base_names_identical_between_jpg_png_flv_test() { $parent = test::random_album(); $item1 = test::random_photo($parent); $item2 = test::random_photo($parent); $item3 = test::random_movie($parent); $item1_orig_base = pathinfo($item1->name, PATHINFO_FILENAME); $item2_orig_slug = $item2->slug; $item3_orig_slug = $item3->slug; $item2->set_data_file(MODPATH . "gallery/images/graphicsmagick.png"); $item2->name = "{$item1_orig_base}.png"; $item2->save(); $item3->name = "{$item1_orig_base}.flv"; $item3->save(); // item2 and item3 have same base name as item1 - conflict resolved by renaming with -01 and -02. $this->assert_same("{$item1_orig_base}-01.png", $item2->name); $this->assert_same("{$item2_orig_slug}-01", $item2->slug); $this->assert_same("{$item1_orig_base}-02.flv", $item3->name); $this->assert_same("{$item3_orig_slug}-02", $item3->slug); }