summaryrefslogtreecommitdiff
path: root/modules/gallery/tests
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gallery/tests')
-rw-r--r--modules/gallery/tests/Access_Helper_Test.php110
-rw-r--r--modules/gallery/tests/Gallery_Rest_Helper_Test.php3
-rw-r--r--modules/gallery/tests/Item_Model_Test.php134
-rw-r--r--modules/gallery/tests/Kohana_Bug_Test.php38
4 files changed, 149 insertions, 136 deletions
diff --git a/modules/gallery/tests/Access_Helper_Test.php b/modules/gallery/tests/Access_Helper_Test.php
index b2244766..da72f12f 100644
--- a/modules/gallery/tests/Access_Helper_Test.php
+++ b/modules/gallery/tests/Access_Helper_Test.php
@@ -40,8 +40,7 @@ class Access_Helper_Test extends Unit_Test_Case {
} catch (Exception $e) { }
// Reset some permissions that we mangle below
- $root = ORM::factory("item", 1);
- access::allow(identity::everybody(), "view", $root);
+ access::allow(identity::everybody(), "view", item::root());
}
public function setup() {
@@ -67,16 +66,15 @@ class Access_Helper_Test extends Unit_Test_Case {
public function user_can_access_test() {
$access_test = identity::create_group("access_test");
- $root = ORM::factory("item", 1);
- access::allow($access_test, "view", $root);
+ access::allow($access_test, "view", item::root());
- $item = album::create($root, rand(), "test album");
+ $item = test::random_album();
access::deny(identity::everybody(), "view", $item);
access::deny(identity::registered_users(), "view", $item);
$item->reload();
- $user = identity::create_user("access_test", "Access Test", "");
+ $user = identity::create_user("access_test", "Access Test", "*****", "user@user.com");
foreach ($user->groups() as $group) {
$user->remove($group);
}
@@ -87,13 +85,12 @@ class Access_Helper_Test extends Unit_Test_Case {
}
public function user_can_no_access_test() {
- $root = ORM::factory("item", 1);
- $item = album::create($root, rand(), "test album");
+ $item = test::random_album();
access::deny(identity::everybody(), "view", $item);
access::deny(identity::registered_users(), "view", $item);
- $user = identity::create_user("access_test", "Access Test", "");
+ $user = identity::create_user("access_test", "Access Test", "*****", "user@user.com");
foreach ($user->groups() as $group) {
$user->remove($group);
}
@@ -103,8 +100,7 @@ class Access_Helper_Test extends Unit_Test_Case {
}
public function adding_and_removing_items_adds_ands_removes_rows_test() {
- $root = ORM::factory("item", 1);
- $item = album::create($root, rand(), "test album");
+ $item = test::random_album();
// New rows exist
$this->assert_true(ORM::factory("access_cache")->where("item_id", "=", $item->id)->find()->loaded());
@@ -119,19 +115,16 @@ class Access_Helper_Test extends Unit_Test_Case {
}
public function new_photos_inherit_parent_permissions_test() {
- $root = ORM::factory("item", 1);
-
- $album = album::create($root, rand(), "test album");
+ $album = test::random_album();
access::allow(identity::everybody(), "view", $album);
- $photo = photo::create($album, MODPATH . "gallery/images/gallery.png", "", "");
+ $photo = test::random_photo($album);
$this->assert_true($photo->__get("view_" . identity::everybody()->id));
}
public function can_allow_deny_and_reset_intent_test() {
- $root = ORM::factory("item", 1);
- $album = album::create($root, rand(), "test album");
+ $album = test::random_album();
$intent = ORM::factory("access_intent")->where("item_id", "=", $album->id)->find();
// Allow
@@ -167,23 +160,21 @@ class Access_Helper_Test extends Unit_Test_Case {
}
public function can_view_item_test() {
- $root = ORM::factory("item", 1);
- access::allow(identity::everybody(), "view", $root);
- $this->assert_true(access::group_can(identity::everybody(), "view", $root));
+ access::allow(identity::everybody(), "view", item::root());
+ $this->assert_true(access::group_can(identity::everybody(), "view", item::root()));
}
public function can_always_fails_on_unloaded_items_test() {
- $root = ORM::factory("item", 1);
- access::allow(identity::everybody(), "view", $root);
- $this->assert_true(access::group_can(identity::everybody(), "view", $root));
+ access::allow(identity::everybody(), "view", item::root());
+ $this->assert_true(access::group_can(identity::everybody(), "view", item::root()));
$bogus = ORM::factory("item", -1);
$this->assert_false(access::group_can(identity::everybody(), "view", $bogus));
}
public function cant_view_child_of_hidden_parent_test() {
- $root = ORM::factory("item", 1);
- $album = album::create($root, rand(), "test album");
+ $root = item::root();
+ $album = test::random_album();
$root->reload();
access::deny(identity::everybody(), "view", $root);
@@ -194,28 +185,26 @@ class Access_Helper_Test extends Unit_Test_Case {
}
public function view_permissions_propagate_down_test() {
- $root = ORM::factory("item", 1);
- $album = album::create($root, rand(), "test album");
+ $album = test::random_album();
- access::allow(identity::everybody(), "view", $root);
+ access::allow(identity::everybody(), "view", item::root());
access::reset(identity::everybody(), "view", $album);
$album->reload();
$this->assert_true(access::group_can(identity::everybody(), "view", $album));
}
public function can_toggle_view_permissions_propagate_down_test() {
- $root = ORM::factory("item", 1);
- $album1 = album::create($root, rand(), "test album");
- $album2 = album::create($album1, rand(), "test album");
- $album3 = album::create($album2, rand(), "test album");
- $album4 = album::create($album3, rand(), "test album");
+ $album1 = test::random_album(item::root());
+ $album2 = test::random_album($album1);
+ $album3 = test::random_album($album2);
+ $album4 = test::random_album($album3);
$album1->reload();
$album2->reload();
$album3->reload();
$album4->reload();
- access::allow(identity::everybody(), "view", $root);
+ access::allow(identity::everybody(), "view", item::root());
access::deny(identity::everybody(), "view", $album1);
access::reset(identity::everybody(), "view", $album2);
access::reset(identity::everybody(), "view", $album3);
@@ -230,9 +219,9 @@ class Access_Helper_Test extends Unit_Test_Case {
}
public function revoked_view_permissions_cant_be_allowed_lower_down_test() {
- $root = ORM::factory("item", 1);
- $album1 = album::create($root, rand(), "test album");
- $album2 = album::create($album1, rand(), "test album");
+ $root = item::root();
+ $album1 = test::random_album($root);
+ $album2 = test::random_album($album1);
$root->reload();
access::deny(identity::everybody(), "view", $root);
@@ -246,38 +235,30 @@ class Access_Helper_Test extends Unit_Test_Case {
}
public function can_edit_item_test() {
- $root = ORM::factory("item", 1);
+ $root = item::root();
access::allow(identity::everybody(), "edit", $root);
$this->assert_true(access::group_can(identity::everybody(), "edit", $root));
}
public function non_view_permissions_propagate_down_test() {
- $root = ORM::factory("item", 1);
- $album = album::create($root, rand(), "test album");
+ $album = test::random_album();
- access::allow(identity::everybody(), "edit", $root);
+ access::allow(identity::everybody(), "edit", item::root());
access::reset(identity::everybody(), "edit", $album);
$this->assert_true(access::group_can(identity::everybody(), "edit", $album));
}
public function non_view_permissions_can_be_revoked_lower_down_test() {
- $root = ORM::factory("item", 1);
- $outer = album::create($root, rand(), "test album");
- $outer_photo = ORM::factory("item");
- $outer_photo->type = "photo";
- $outer_photo->add_to_parent($outer);
- access::add_item($outer_photo);
-
- $inner = album::create($outer, rand(), "test album");
- $inner_photo = ORM::factory("item");
- $inner_photo->type = "photo";
- $inner_photo->add_to_parent($inner);
- access::add_item($inner_photo);
+ $outer = test::random_album();
+ $outer_photo = test::random_photo($outer);
+
+ $inner = test::random_album($outer);
+ $inner_photo = test::random_photo($inner);
$outer->reload();
$inner->reload();
- access::allow(identity::everybody(), "edit", $root);
+ access::allow(identity::everybody(), "edit", item::root());
access::deny(identity::everybody(), "edit", $outer);
access::allow(identity::everybody(), "edit", $inner);
@@ -288,7 +269,7 @@ class Access_Helper_Test extends Unit_Test_Case {
public function i_can_edit_test() {
// Create a new user that belongs to no groups
- $user = identity::create_user("access_test", "Access Test", "");
+ $user = identity::create_user("access_test", "Access Test", "*****", "user@user.com");
foreach ($user->groups() as $group) {
$user->remove($group);
}
@@ -296,7 +277,7 @@ class Access_Helper_Test extends Unit_Test_Case {
identity::set_active_user($user);
// This user can't edit anything
- $root = ORM::factory("item", 1);
+ $root = item::root();
$this->assert_false(access::can("edit", $root));
// Now add them to a group that has edit permission
@@ -313,8 +294,7 @@ class Access_Helper_Test extends Unit_Test_Case {
}
public function everybody_view_permission_maintains_htaccess_files_test() {
- $root = ORM::factory("item", 1);
- $album = album::create($root, rand(), "test album");
+ $album = test::random_album();
$this->assert_false(file_exists($album->file_path() . "/.htaccess"));
@@ -332,8 +312,7 @@ class Access_Helper_Test extends Unit_Test_Case {
}
public function everybody_view_full_permission_maintains_htaccess_files_test() {
- $root = ORM::factory("item", 1);
- $album = album::create($root, rand(), "test album");
+ $album = test::random_album();
$this->assert_false(file_exists($album->file_path() . "/.htaccess"));
$this->assert_false(file_exists($album->resize_path() . "/.htaccess"));
@@ -363,16 +342,15 @@ class Access_Helper_Test extends Unit_Test_Case {
public function moved_items_inherit_new_permissions_test() {
identity::set_active_user(identity::lookup_user_by_name("admin"));
- $root = ORM::factory("item", 1);
- $public_album = album::create($root, rand(), "public album");
- $public_photo = photo::create($public_album, MODPATH . "gallery/images/gallery.png", "", "");
+ $public_album = test::random_album();
+ $public_photo = test::random_photo($public_album);
access::allow(identity::everybody(), "view", $public_album);
- $root->reload(); // Account for MPTT changes
+ item::root()->reload(); // Account for MPTT changes
- $private_album = album::create($root, rand(), "private album");
+ $private_album = test::random_album();
access::deny(identity::everybody(), "view", $private_album);
- $private_photo = photo::create($private_album, MODPATH . "gallery/images/gallery.png", "", "");
+ $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));
diff --git a/modules/gallery/tests/Gallery_Rest_Helper_Test.php b/modules/gallery/tests/Gallery_Rest_Helper_Test.php
index f8cf6190..dac221b3 100644
--- a/modules/gallery/tests/Gallery_Rest_Helper_Test.php
+++ b/modules/gallery/tests/Gallery_Rest_Helper_Test.php
@@ -136,7 +136,8 @@ class Gallery_Rest_Helper_Test extends Unit_Test_Case {
try {
gallery_rest::put($request);
} catch (Rest_Exception $e) {
- $this->assert_equal("400 Bad request", $e->getMessage());
+ $this->assert_equal("Bad request", $e->getMessage());
+ $this->assert_equal(400, $e->getCode());
} catch (Exception $e) {
$this->assert_false(true, $e->__toString());
}
diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php
index d03a03f4..b41740d6 100644
--- a/modules/gallery/tests/Item_Model_Test.php
+++ b/modules/gallery/tests/Item_Model_Test.php
@@ -19,20 +19,13 @@
*/
class Item_Model_Test extends Unit_Test_Case {
public function saving_sets_created_and_updated_dates_test() {
- $item = self::_create_random_item();
+ $item = test::random_photo();
$this->assert_true(!empty($item->created));
$this->assert_true(!empty($item->updated));
}
- private static function _create_random_item($root=null, $rand=null) {
- $root = $root ? $root : ORM::factory("item", 1);
- $rand = $rand ? $rand : rand();
- $item = photo::create($root, MODPATH . "gallery/tests/test.jpg", "$rand.jpg", $rand, $rand);
- return $item;
- }
-
public function updating_doesnt_change_created_date_test() {
- $item = self::_create_random_item();
+ $item = test::random_photo();
// Force the creation date to something well known
db::build()
@@ -50,7 +43,7 @@ class Item_Model_Test extends Unit_Test_Case {
}
public function updating_view_count_only_doesnt_change_updated_date_test() {
- $item = self::_create_random_item();
+ $item = test::random_photo();
$item->reload();
$this->assert_equal(0, $item->view_count);
@@ -69,8 +62,7 @@ class Item_Model_Test extends Unit_Test_Case {
}
public function rename_photo_test() {
- // Create a test photo
- $item = self::_create_random_item();
+ $item = test::random_photo();
file_put_contents($item->thumb_path(), "thumb");
file_put_contents($item->resize_path(), "resize");
@@ -80,7 +72,8 @@ class Item_Model_Test extends Unit_Test_Case {
$new_name = rand();
// Now rename it
- $item->rename($new_name)->save();
+ $item->name = $new_name;
+ $item->save();
// Expected: the name changed, the name is now baked into all paths, and all files were moved.
$this->assert_equal($new_name, $item->name);
@@ -93,10 +86,9 @@ class Item_Model_Test extends Unit_Test_Case {
}
public function rename_album_test() {
- // Create an album with a photo in it
- $root = ORM::factory("item", 1);
- $album = album::create($root, rand(), rand(), rand());
- $photo = self::_create_random_item($album);
+ $album = test::random_album();
+ $photo = test::random_photo($album);
+ $album->reload();
file_put_contents($photo->thumb_path(), "thumb");
file_put_contents($photo->resize_path(), "resize");
@@ -107,7 +99,8 @@ class Item_Model_Test extends Unit_Test_Case {
$new_album_name = rand();
// Now rename the album
- $album->rename($new_album_name)->save();
+ $album->name = $new_album_name;
+ $album->save();
$photo->reload();
// Expected:
@@ -130,8 +123,7 @@ class Item_Model_Test extends Unit_Test_Case {
}
public function item_rename_wont_accept_slash_test() {
- // Create a test photo
- $item = self::_create_random_item();
+ $item = test::random_photo();
$new_name = rand() . "/";
@@ -146,24 +138,23 @@ class Item_Model_Test extends Unit_Test_Case {
public function item_rename_fails_with_existing_name_test() {
// Create a test photo
- $item = self::_create_random_item();
- $item2 = self::_create_random_item();
-
- $new_name = $item2->name;
+ $item = test::random_photo();
+ $item2 = test::random_photo();
try {
- $item->rename($new_name)->save();
- } catch (Exception $e) {
- // pass
- $this->assert_true(strpos($e->getMessage(), "INVALID_RENAME_FILE_EXISTS") !== false,
- "incorrect exception.");
+ $item->name = $item2->name;
+ $item->validate(); // @todo: switch this to save() once
+ // http://dev.kohanaphp.com/issues/2504 is fixed.
+ } catch (ORM_Validation_Exception $e) {
+ $this->assert_true(in_array("conflict", $e->validation->errors()));
return;
}
- $this->assert_false(true, "Item_Model::rename should fail.");
+
+ $this->assert_false(true, "rename should conflict");
}
public function save_original_values_test() {
- $item = self::_create_random_item();
+ $item = test::random_photo_unsaved();
$item->title = "ORIGINAL_VALUE";
$item->save();
$item->title = "NEW_VALUE";
@@ -172,29 +163,17 @@ class Item_Model_Test extends Unit_Test_Case {
$this->assert_same("NEW_VALUE", $item->title);
}
- public function urls_are_rawurlencoded_test() {
- $item = self::_create_random_item();
- $item->slug = "foo bar";
- $item->name = "foo bar.jpg";
- $item->save();
-
- $this->assert_equal("foo%20bar", $item->relative_url());
- $this->assert_equal("foo%20bar.jpg", $item->relative_path());
- }
-
public function move_album_test() {
- // Create an album with a photo in it
- $root = ORM::factory("item", 1);
- $album2 = album::create($root, rand(), rand(), rand());
- $album = album::create($album2, rand(), rand(), rand());
- $photo = self::_create_random_item($album);
+ $album2 = test::random_album();
+ $album = test::random_album($album2);
+ $photo = test::random_photo($album);
file_put_contents($photo->thumb_path(), "thumb");
file_put_contents($photo->resize_path(), "resize");
file_put_contents($photo->file_path(), "file");
// Now move the album
- $album->move_to($root);
+ $album->move_to(item::root());
$photo->reload();
// Expected:
@@ -212,11 +191,9 @@ class Item_Model_Test extends Unit_Test_Case {
}
public function move_photo_test() {
- // Create an album with a photo in it
- $root = ORM::factory("item", 1);
- $album2 = album::create($root, rand(), rand(), rand());
- $album = album::create($album2, rand(), rand(), rand());
- $photo = self::_create_random_item($album);
+ $album2 = test::random_album();
+ $album = test::random_album($album2);
+ $photo = test::random_photo($album);
file_put_contents($photo->thumb_path(), "thumb");
file_put_contents($photo->resize_path(), "resize");
@@ -241,42 +218,61 @@ class Item_Model_Test extends Unit_Test_Case {
}
public function move_album_fails_invalid_target_test() {
- // Create an album with a photo in it
- $root = ORM::factory("item", 1);
- $name = rand();
- $album = album::create($root, $name, $name, $name);
- $source = album::create($album, $name, $name, $name);
+ $album = test::random_album();
+ $source = test::random_album($album);
try {
- $source->move_to($root);
+ $source->move_to(item::root());
} catch (Exception $e) {
// pass
$this->assert_true(strpos($e->getMessage(), "INVALID_MOVE_TARGET_EXISTS") !== false,
"incorrect exception.");
return;
}
-
- $this->assert_false(true, "Item_Model::rename should not accept / characters");
}
public function move_photo_fails_invalid_target_test() {
- // Create an album with a photo in it
- $root = ORM::factory("item", 1);
- $photo_name = rand();
- $photo1 = self::_create_random_item($root, $photo_name);
- $name = rand();
- $album = album::create($root, $name, $name, $name);
- $photo2 = self::_create_random_item($album, $photo_name);
+ $photo1 = test::random_photo();
+ $album = test::random_album();
+ $photo2 = test::random_photo($album);
try {
- $photo2->move_to($root);
+ $photo2->move_to(item::root());
} catch (Exception $e) {
// pass
$this->assert_true(strpos($e->getMessage(), "INVALID_MOVE_TARGET_EXISTS") !== false,
"incorrect exception.");
return;
}
+ }
- $this->assert_false(true, "Item_Model::rename should not accept / characters");
+ public function basic_validation_test() {
+ $item = ORM::factory("item");
+ $item->album_cover_item_id = rand(); // invalid
+ $item->description = str_repeat("x", 70000); // invalid
+ $item->name = null;
+ $item->parent_id = rand();
+ $item->slug = null;
+ $item->sort_column = "bogus";
+ $item->sort_order = "bogus";
+ $item->title = null;
+ $item->type = "bogus";
+ try {
+ $item->save();
+ } catch (ORM_Validation_Exception $e) {
+ $this->assert_same(array("description" => "length",
+ "name" => "required",
+ "slug" => "required",
+ "title" => "required",
+ "album_cover_item_id" => "invalid_item",
+ "parent_id" => "invalid",
+ "sort_column" => "invalid",
+ "sort_order" => "invalid",
+ "type" => "invalid"),
+ $e->validation->errors());
+ return;
+ }
+
+ $this->assert_false(true, "Shouldn't get here");
}
}
diff --git a/modules/gallery/tests/Kohana_Bug_Test.php b/modules/gallery/tests/Kohana_Bug_Test.php
new file mode 100644
index 00000000..61f5d69e
--- /dev/null
+++ b/modules/gallery/tests/Kohana_Bug_Test.php
@@ -0,0 +1,38 @@
+<?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 Kohana_Bug_Test extends Unit_Test_Case {
+ function double_save_test_should_fail_test() {
+ // http://dev.kohanaframework.org/issues/2504
+ $group = ORM::factory("group");
+ $group->name = rand();
+ $group->save(); // this save works
+
+ try {
+ $group->name = null; // now I change to an illegal value
+ $group->save(); // this passes, but it shouldn't. My model is broken!
+
+ // This is the normal state when the bug is not fixed.
+ } catch (ORM_Validation_Exception $e) {
+ // When this triggers, the bug is fixed. Find any references to ticket #2504 in the code
+ // and update those accordingly
+ $this->assert_true(false, "Bug #2504 has been fixed");
+ }
+ }
+}