From 9ddb961a91f99ccc043981bbe8c8a661f9646563 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 17 Jan 2010 20:53:32 -0800 Subject: Partially updated for model based validation. --- modules/gallery/tests/Item_Model_Test.php | 74 ++++++++++--------------------- 1 file changed, 24 insertions(+), 50 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index bf5fca1a..a3f590a1 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_same(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"); @@ -93,10 +85,8 @@ 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); file_put_contents($photo->thumb_path(), "thumb"); file_put_contents($photo->resize_path(), "resize"); @@ -130,8 +120,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,8 +135,8 @@ 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(); + $item = test::random_photo(); + $item2 = test::random_photo(); $new_name = $item2->name; @@ -163,7 +152,7 @@ class Item_Model_Test extends Unit_Test_Case { } 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"; @@ -173,7 +162,7 @@ class Item_Model_Test extends Unit_Test_Case { } public function urls_are_rawurlencoded_test() { - $item = self::_create_random_item(); + $item = test::random_photo_unsaved(); $item->slug = "foo bar"; $item->name = "foo bar.jpg"; $item->save(); @@ -183,18 +172,16 @@ class Item_Model_Test extends Unit_Test_Case { } 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 +199,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,32 +226,23 @@ 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); @@ -276,7 +252,5 @@ class Item_Model_Test extends Unit_Test_Case { "incorrect exception."); return; } - - $this->assert_false(true, "Item_Model::rename should not accept / characters"); } } -- cgit v1.2.3 From b35a3c8b81cc4867fc6143b3a401937b20ea82e2 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 17 Jan 2010 20:54:08 -0800 Subject: Fix rename_photo_test(). --- modules/gallery/tests/Item_Model_Test.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index a3f590a1..52e0f799 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -72,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); -- cgit v1.2.3 From 710e472edc95dd4b7a192348e11b81636ac83e52 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 17 Jan 2010 21:03:15 -0800 Subject: Fix rename_album_test() --- modules/gallery/tests/Item_Model_Test.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index 52e0f799..a2720f7d 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -88,6 +88,7 @@ class Item_Model_Test extends Unit_Test_Case { public function rename_album_test() { $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"); @@ -98,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: -- cgit v1.2.3 From 06541f0e2a5cb9ee78b6b1553dd522dcc33a1b3d Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 17 Jan 2010 21:49:48 -0800 Subject: Fix item_rename_fails_with_existing_name_test() -- broken because of http://dev.kohanaphp.com/issues/2504 --- modules/gallery/tests/Item_Model_Test.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index a2720f7d..c4e150a6 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -141,17 +141,16 @@ class Item_Model_Test extends Unit_Test_Case { $item = test::random_photo(); $item2 = test::random_photo(); - $new_name = $item2->name; - 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() { -- cgit v1.2.3 From 31ecb009bac2d9177329aafb9596fbfa65bd6a04 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 17 Jan 2010 23:45:18 -0800 Subject: Get rid of urls_are_rawurlencoded_test -- it's no longer necessary since we can't have unsafe slugs. Add basic_validation_test. --- modules/gallery/tests/Item_Model_Test.php | 40 +++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index c4e150a6..ea6c2af6 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -163,16 +163,6 @@ class Item_Model_Test extends Unit_Test_Case { $this->assert_same("NEW_VALUE", $item->title); } - public function urls_are_rawurlencoded_test() { - $item = test::random_photo_unsaved(); - $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() { $album2 = test::random_album(); $album = test::random_album($album2); @@ -255,4 +245,34 @@ class Item_Model_Test extends Unit_Test_Case { return; } } + + 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"); + } } -- cgit v1.2.3 From 53735f6b166fe823f5606e6f3cf527d98105593c Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sun, 17 Jan 2010 23:50:42 -0800 Subject: Convert a $root to item::root() to fix a test. --- modules/gallery/tests/Item_Model_Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index ea6c2af6..c1df70c6 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -237,7 +237,7 @@ class Item_Model_Test extends Unit_Test_Case { $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, -- cgit v1.2.3 From 92ed5d45e6e81b39f4d6b8621c36b597a1b315d8 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 18 Jan 2010 12:56:49 -0800 Subject: In item_rename_fails_with_existing_name_test switch validate() to save() now that Kohana ticket #2504 is resolved. --- modules/gallery/tests/Item_Model_Test.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index b41740d6..12f69a34 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -143,8 +143,7 @@ class Item_Model_Test extends Unit_Test_Case { try { $item->name = $item2->name; - $item->validate(); // @todo: switch this to save() once - // http://dev.kohanaphp.com/issues/2504 is fixed. + $item->save(); } catch (ORM_Validation_Exception $e) { $this->assert_true(in_array("conflict", $e->validation->errors())); return; -- cgit v1.2.3 From 38c13760afaae693af245e814b6bbe76c20fcae4 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 18 Jan 2010 20:14:09 -0800 Subject: Switch to using test::random_name() to avoid integer names. Fix up rename_photo_test. --- modules/gallery/tests/Item_Model_Test.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index 12f69a34..542cf371 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -63,16 +63,14 @@ class Item_Model_Test extends Unit_Test_Case { public function rename_photo_test() { $item = test::random_photo(); + $original_name = $item->name; file_put_contents($item->thumb_path(), "thumb"); file_put_contents($item->resize_path(), "resize"); file_put_contents($item->file_path(), "file"); - $original_name = $item->name; - $new_name = rand(); - // Now rename it - $item->name = $new_name; + $item->name = ($new_name = test::random_name($item)); $item->save(); // Expected: the name changed, the name is now baked into all paths, and all files were moved. @@ -96,7 +94,7 @@ class Item_Model_Test extends Unit_Test_Case { $original_album_name = $album->name; $original_photo_name = $photo->name; - $new_album_name = rand(); + $new_album_name = test::random_name(); // Now rename the album $album->name = $new_album_name; @@ -125,7 +123,7 @@ class Item_Model_Test extends Unit_Test_Case { public function item_rename_wont_accept_slash_test() { $item = test::random_photo(); - $new_name = rand() . "/"; + $new_name = test::random_name() . "/"; try { $item->rename($new_name)->save(); -- cgit v1.2.3 From 677fb080026612725d9ed317b5ff6732e00f6514 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 18 Jan 2010 21:12:26 -0800 Subject: Updated tests to match what the expected new API for moving items. Now to move an item, you just change its parent_id. --- modules/gallery/tests/Item_Model_Test.php | 82 +++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 26 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index 542cf371..b2193e90 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -127,9 +127,8 @@ class Item_Model_Test extends Unit_Test_Case { try { $item->rename($new_name)->save(); - } catch (Exception $e) { - // pass - return; + } catch (ORM_Validation_Exception $e) { + $this->assert_equals(array("name" => "no_slashes"), $e->validation->errors()); } $this->assert_false(true, "Item_Model::rename should not accept / characters"); } @@ -170,14 +169,18 @@ class Item_Model_Test extends Unit_Test_Case { file_put_contents($photo->file_path(), "file"); // Now move the album - $album->move_to(item::root()); + $album->parent_id = item::root()->id; + $album->save(); $photo->reload(); // Expected: - // * the album dirs are all moved + // * album is not inside album2 anymore // * the photo's paths are all inside the albums paths // * the photo files are all still intact and accessible + $this->assert_same(null, strpos($album->relative_path(), $album2->relative_path()), + $album2->relative_path() . " should not be in: " . $album->relative_path()); + $this->assert_same(0, strpos($photo->file_path(), $album->file_path())); $this->assert_same(0, strpos($photo->thumb_path(), dirname($album->thumb_path()))); $this->assert_same(0, strpos($photo->resize_path(), dirname($album->resize_path()))); @@ -188,26 +191,26 @@ class Item_Model_Test extends Unit_Test_Case { } public function move_photo_test() { + $album1 = test::random_album(); + $photo = test::random_photo($album1); + $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 - $photo->move_to($album2); - $photo->reload(); + // Now move the photo + $photo->parent_id = $album2->id; + $photo->save(); // Expected: - // * the album dirs are all moved - // * the photo's paths are all inside the albums paths + // * the photo's paths are inside the album2 not album1 // * the photo files are all still intact and accessible - $this->assert_same(0, strpos($photo->file_path(), $album->file_path())); - $this->assert_same(0, strpos($photo->thumb_path(), dirname($album->thumb_path()))); - $this->assert_same(0, strpos($photo->resize_path(), dirname($album->resize_path()))); + $this->assert_same(0, strpos($photo->file_path(), $album2->file_path())); + $this->assert_same(0, strpos($photo->thumb_path(), dirname($album2->thumb_path()))); + $this->assert_same(0, strpos($photo->resize_path(), dirname($album2->resize_path()))); $this->assert_equal("thumb", file_get_contents($photo->thumb_path())); $this->assert_equal("resize", file_get_contents($photo->resize_path())); @@ -216,33 +219,60 @@ class Item_Model_Test extends Unit_Test_Case { public function move_album_fails_invalid_target_test() { $album = test::random_album(); - $source = test::random_album($album); + $source = test::random_album_unsaved($album); + $source->name = $album->name; + $source->save(); + + // $source and $album have the same name, so if we move $source into the root they should + // conflict. try { - $source->move_to(item::root()); - } catch (Exception $e) { - // pass - $this->assert_true(strpos($e->getMessage(), "INVALID_MOVE_TARGET_EXISTS") !== false, - "incorrect exception."); - return; + $source->parent_id = item::root()->id; + $source->save(); + $this->assert_true(false, "Shouldn't get here"); + } catch (ORM_Validation_Exception $e) { + $this->assert_equal( + array("name" => "conflict", "slug" => "conflict"), $e->validation->errors()); } } public function move_photo_fails_invalid_target_test() { $photo1 = test::random_photo(); $album = test::random_album(); - $photo2 = test::random_photo($album); + $photo2 = test::random_photo_unsaved($album); + $photo2->name = $photo1->name; + $photo2->save(); + + // $photo1 and $photo2 have the same name, so if we move $photo1 into the root they should + // conflict. try { - $photo2->move_to(item::root()); + $photo2->parent_id = item::root()->id; + $photo2->save(); + $this->assert_true(false, "Shouldn't get here"); } catch (Exception $e) { // pass - $this->assert_true(strpos($e->getMessage(), "INVALID_MOVE_TARGET_EXISTS") !== false, - "incorrect exception."); + $this->assert_equal( + array("name" => "conflict", "slug" => "conflict"), $e->validation->errors()); return; } } + public function move_album_inside_descendent_fails_test() { + $album = test::random_album(); + $album2 = test::random_album($album); + $album3 = test::random_album($album2); + + try { + $album->parent_id = $album3->id; + $album->save(); + $this->assert_true(false, "Shouldn't get here"); + } catch (ORM_Validation_Exception $e) { + $this->assert_equal(array("parent_id" => "invalid"), $e->validation->errors()); + } + } + + public function basic_validation_test() { $item = ORM::factory("item"); $item->album_cover_item_id = rand(); // invalid -- cgit v1.2.3 From b4e6834a28e557c3143f5f3cc91144d1565c18f0 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 18 Jan 2010 21:14:43 -0800 Subject: Added move_album_fails_wrong_target_type_test() --- modules/gallery/tests/Item_Model_Test.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index b2193e90..abe872d2 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -217,7 +217,7 @@ class Item_Model_Test extends Unit_Test_Case { $this->assert_equal("file", file_get_contents($photo->file_path())); } - public function move_album_fails_invalid_target_test() { + public function move_album_fails_conflicting_target_test() { $album = test::random_album(); $source = test::random_album_unsaved($album); $source->name = $album->name; @@ -236,7 +236,23 @@ class Item_Model_Test extends Unit_Test_Case { } } - public function move_photo_fails_invalid_target_test() { + public function move_album_fails_wrong_target_type_test() { + $album = test::random_album(); + $photo = test::random_photo(); + + // $source and $album have the same name, so if we move $source into the root they should + // conflict. + + try { + $album->parent_id = $photo->id; + $album->save(); + $this->assert_true(false, "Shouldn't get here"); + } catch (ORM_Validation_Exception $e) { + $this->assert_equal(array("parent_id" => "invalid"), $e->validation->errors()); + } + } + + public function move_photo_fails_conflicting_target_test() { $photo1 = test::random_photo(); $album = test::random_album(); $photo2 = test::random_photo_unsaved($album); -- cgit v1.2.3 From cfc0f3d0c0935839e9eedd11322e35e9baaca0df Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 18 Jan 2010 22:51:40 -0800 Subject: Minor test cleanup. --- modules/gallery/tests/Item_Model_Test.php | 48 +++++++++++++++---------------- 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index abe872d2..5a59e6d0 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -122,15 +122,14 @@ class Item_Model_Test extends Unit_Test_Case { public function item_rename_wont_accept_slash_test() { $item = test::random_photo(); - - $new_name = test::random_name() . "/"; - try { - $item->rename($new_name)->save(); + $item->name = test::random_name() . "/"; + $item->save(); } catch (ORM_Validation_Exception $e) { - $this->assert_equals(array("name" => "no_slashes"), $e->validation->errors()); + $this->assert_equal(array("name" => "no_slashes"), $e->validation->errors()); + return; } - $this->assert_false(true, "Item_Model::rename should not accept / characters"); + $this->assert_true(false, "Shouldn't get here"); } public function item_rename_fails_with_existing_name_test() { @@ -161,16 +160,16 @@ class Item_Model_Test extends Unit_Test_Case { public function move_album_test() { $album2 = test::random_album(); - $album = test::random_album($album2); - $photo = test::random_photo($album); + $album1 = test::random_album($album2); + $photo = test::random_photo($album1); 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->parent_id = item::root()->id; - $album->save(); + $album1->parent_id = item::root()->id; + $album1->save(); $photo->reload(); // Expected: @@ -178,12 +177,10 @@ class Item_Model_Test extends Unit_Test_Case { // * the photo's paths are all inside the albums paths // * the photo files are all still intact and accessible - $this->assert_same(null, strpos($album->relative_path(), $album2->relative_path()), - $album2->relative_path() . " should not be in: " . $album->relative_path()); - - $this->assert_same(0, strpos($photo->file_path(), $album->file_path())); - $this->assert_same(0, strpos($photo->thumb_path(), dirname($album->thumb_path()))); - $this->assert_same(0, strpos($photo->resize_path(), dirname($album->resize_path()))); + $this->assert_false(test::starts_with($album2->file_path(), $album1->file_path())); + $this->assert_true(test::starts_with($photo->file_path(), $album1->file_path())); + $this->assert_true(test::starts_with($photo->thumb_path(), dirname($album1->thumb_path()))); + $this->assert_true(test::starts_with($photo->resize_path(), dirname($album1->resize_path()))); $this->assert_equal("thumb", file_get_contents($photo->thumb_path())); $this->assert_equal("resize", file_get_contents($photo->resize_path())); @@ -229,11 +226,12 @@ class Item_Model_Test extends Unit_Test_Case { try { $source->parent_id = item::root()->id; $source->save(); - $this->assert_true(false, "Shouldn't get here"); } catch (ORM_Validation_Exception $e) { $this->assert_equal( array("name" => "conflict", "slug" => "conflict"), $e->validation->errors()); + return; } + $this->assert_true(false, "Shouldn't get here"); } public function move_album_fails_wrong_target_type_test() { @@ -246,10 +244,11 @@ class Item_Model_Test extends Unit_Test_Case { try { $album->parent_id = $photo->id; $album->save(); - $this->assert_true(false, "Shouldn't get here"); } 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 move_photo_fails_conflicting_target_test() { @@ -265,27 +264,28 @@ class Item_Model_Test extends Unit_Test_Case { try { $photo2->parent_id = item::root()->id; $photo2->save(); - $this->assert_true(false, "Shouldn't get here"); } catch (Exception $e) { // pass $this->assert_equal( array("name" => "conflict", "slug" => "conflict"), $e->validation->errors()); return; } + $this->assert_true(false, "Shouldn't get here"); } public function move_album_inside_descendent_fails_test() { - $album = test::random_album(); - $album2 = test::random_album($album); + $album1 = test::random_album(); + $album2 = test::random_album($album1); $album3 = test::random_album($album2); try { - $album->parent_id = $album3->id; - $album->save(); - $this->assert_true(false, "Shouldn't get here"); + $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"); } -- cgit v1.2.3 From 9eedf5c2072ee508d539e3e25ce4f02eff746476 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 18 Jan 2010 22:55:16 -0800 Subject: switch to test::starts_with(). --- modules/gallery/tests/Item_Model_Test.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index 5a59e6d0..5294836d 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -111,9 +111,9 @@ class Item_Model_Test extends Unit_Test_Case { $this->assert_equal($new_album_name, basename(dirname($album->thumb_path()))); $this->assert_equal($new_album_name, basename(dirname($album->resize_path()))); - $this->assert_same(0, strpos($photo->file_path(), $album->file_path())); - $this->assert_same(0, strpos($photo->thumb_path(), dirname($album->thumb_path()))); - $this->assert_same(0, strpos($photo->resize_path(), dirname($album->resize_path()))); + $this->assert_true(test::starts_with($photo->file_path(), $album->file_path())); + $this->assert_true(test::starts_with($photo->thumb_path(), dirname($album->thumb_path()))); + $this->assert_true(test::starts_with($photo->resize_path(), dirname($album->resize_path()))); $this->assert_equal("thumb", file_get_contents($photo->thumb_path())); $this->assert_equal("resize", file_get_contents($photo->resize_path())); @@ -205,9 +205,9 @@ class Item_Model_Test extends Unit_Test_Case { // * the photo's paths are inside the album2 not album1 // * the photo files are all still intact and accessible - $this->assert_same(0, strpos($photo->file_path(), $album2->file_path())); - $this->assert_same(0, strpos($photo->thumb_path(), dirname($album2->thumb_path()))); - $this->assert_same(0, strpos($photo->resize_path(), dirname($album2->resize_path()))); + $this->assert_true(test::starts_with($photo->file_path(), $album2->file_path())); + $this->assert_true(test::starts_with($photo->thumb_path(), dirname($album2->thumb_path()))); + $this->assert_true(test::starts_with($photo->resize_path(), dirname($album2->resize_path()))); $this->assert_equal("thumb", file_get_contents($photo->thumb_path())); $this->assert_equal("resize", file_get_contents($photo->resize_path())); -- cgit v1.2.3 From 4418993db96a6d9c4444957b78588b4ce01556f4 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 18 Jan 2010 23:38:39 -0800 Subject: Add slug_is_url_safe_test() --- modules/gallery/tests/Item_Model_Test.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index 5294836d..afb131fc 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -318,4 +318,20 @@ class Item_Model_Test extends Unit_Test_Case { $this->assert_false(true, "Shouldn't get here"); } + + public function slug_is_url_safe_test() { + $album = test::random_album_unsaved(); + + try { + $album->slug = "illegal chars! !@#@#$!@~"; + $album->save(); + $this->assert_true(false, "Shouldn't be able to save"); + } catch (ORM_Validation_Exception $e) { + $this->assert_same(array("slug" => "not_url_safe"), $e->validation->errors()); + } + + // This should work + $album->slug = "the_quick_brown_fox"; + $album->save(); + } } -- cgit v1.2.3 From e39c8df19fc0dadcfe65cb8a3ed6529648c6c9cf Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 19 Jan 2010 21:20:36 -0800 Subject: Fix some validation checks to check to see if the original was loaded before deciding whether or not we changed a value. Change valid_name to be cascading, not parallel. --- modules/gallery/models/item.php | 18 +++++------------- modules/gallery/tests/Item_Model_Test.php | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 15 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index a7f73d0e..58ff86ed 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -782,21 +782,15 @@ class Item_Model extends ORM_MPTT { if (strpos($this->name, "/") !== false) { $v->add_error("name", "no_slashes"); return; - } - - if (rtrim($this->name, ".") !== $this->name) { + } else if (rtrim($this->name, ".") !== $this->name) { $v->add_error("name", "no_trailing_period"); - return; - } - - if ($this->is_movie() || $this->is_photo()) { - if ($this->loaded()) { + } else if ($this->is_movie() || $this->is_photo()) { + if ($this->original()->loaded()) { // Existing items can't change their extension $new_ext = pathinfo($this->name, PATHINFO_EXTENSION); $old_ext = pathinfo($this->original()->name, PATHINFO_EXTENSION); if (strcasecmp($new_ext, $old_ext)) { $v->add_error("name", "illegal_data_file_extension"); - return; } } else { // New items must have an extension @@ -804,9 +798,7 @@ class Item_Model extends ORM_MPTT { $v->add_error("name", "illegal_data_file_extension"); } } - } - - if (db::build() + } else if (db::build() ->from("items") ->where("parent_id", "=", $this->parent_id) ->where("name", "=", $this->name) @@ -908,7 +900,7 @@ class Item_Model extends ORM_MPTT { * This field cannot be changed after it's been set. */ public function read_only(Validation $v, $field) { - if ($this->loaded() && $this->original()->$field != $this->$field) { + if ($this->original()->loaded() && $this->original()->$field != $this->$field) { $v->add_error($field, "read_only"); } } diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index afb131fc..284491a0 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -320,9 +320,8 @@ class Item_Model_Test extends Unit_Test_Case { } public function slug_is_url_safe_test() { - $album = test::random_album_unsaved(); - try { + $album = test::random_album_unsaved(); $album->slug = "illegal chars! !@#@#$!@~"; $album->save(); $this->assert_true(false, "Shouldn't be able to save"); @@ -334,4 +333,17 @@ class Item_Model_Test extends Unit_Test_Case { $album->slug = "the_quick_brown_fox"; $album->save(); } + + public function cant_change_item_type_test() { + $photo = test::random_photo(); + try { + $photo->type = "movie"; + $photo->mime_type = "video/x-flv"; + $photo->save(); + } catch (ORM_Validation_Exception $e) { + $this->assert_same(array("type" => "read_only"), $e->validation->errors()); + return; // pass + } + $this->assert_true(false, "Shouldn't get here"); + } } -- cgit v1.2.3 From 76da85a1a08cdf065bf186c81ea444d03d6f8935 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 19 Jan 2010 22:38:19 -0800 Subject: Extend Gallery_Unit_Test_Case instead of Unit_Test_Case. --- modules/akismet/tests/Akismet_Helper_Test.php | 2 +- modules/comment/tests/Comment_Event_Test.php | 2 +- modules/comment/tests/Comment_Helper_Test.php | 2 +- modules/comment/tests/Comment_Model_Test.php | 2 +- modules/digibug/tests/Digibug_Controller_Test.php | 2 +- modules/exif/tests/Exif_Test.php | 2 +- modules/gallery/tests/Access_Helper_Test.php | 2 +- modules/gallery/tests/Albums_Controller_Test.php | 2 +- modules/gallery/tests/Cache_Test.php | 2 +- modules/gallery/tests/Controller_Auth_Test.php | 2 +- modules/gallery/tests/Database_Test.php | 2 +- modules/gallery/tests/Dir_Helper_Test.php | 2 +- modules/gallery/tests/DrawForm_Test.php | 2 +- modules/gallery/tests/File_Structure_Test.php | 2 +- modules/gallery/tests/Gallery_I18n_Test.php | 2 +- modules/gallery/tests/Gallery_Installer_Test.php | 2 +- modules/gallery/tests/Html_Helper_Test.php | 2 +- modules/gallery/tests/Item_Helper_Test.php | 2 +- modules/gallery/tests/Item_Model_Test.php | 2 +- modules/gallery/tests/Locales_Helper_Test.php | 2 +- modules/gallery/tests/Menu_Test.php | 2 +- modules/gallery/tests/ORM_MPTT_Test.php | 2 +- modules/gallery/tests/Photos_Controller_Test.php | 2 +- modules/gallery/tests/SafeString_Test.php | 2 +- modules/gallery/tests/Sendmail_Test.php | 2 +- modules/gallery/tests/Url_Security_Test.php | 2 +- modules/gallery/tests/Var_Test.php | 2 +- modules/gallery/tests/Xss_Security_Test.php | 2 +- modules/gallery_unit_test/helpers/test.php | 11 ++ modules/rest/tests/Rest_Controller_Test.php | 142 ++++------------------ modules/tag/tests/Tag_Rest_Helper_Test.php | 2 +- modules/tag/tests/Tag_Test.php | 2 +- modules/user/tests/No_Direct_ORM_Access_Test.php | 2 +- modules/user/tests/User_Groups_Test.php | 2 +- modules/user/tests/User_Installer_Test.php | 2 +- 35 files changed, 68 insertions(+), 151 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/akismet/tests/Akismet_Helper_Test.php b/modules/akismet/tests/Akismet_Helper_Test.php index b32e9a02..e185f280 100644 --- a/modules/akismet/tests/Akismet_Helper_Test.php +++ b/modules/akismet/tests/Akismet_Helper_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Akismet_Helper_Test extends Unit_Test_Case { +class Akismet_Helper_Test extends Gallery_Unit_Test_Case { private $_comment; public function setup() { diff --git a/modules/comment/tests/Comment_Event_Test.php b/modules/comment/tests/Comment_Event_Test.php index 5b7daef4..27272055 100644 --- a/modules/comment/tests/Comment_Event_Test.php +++ b/modules/comment/tests/Comment_Event_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Comment_Event_Test extends Unit_Test_Case { +class Comment_Event_Test extends Gallery_Unit_Test_Case { public function deleting_an_item_deletes_its_comments_too_test() { $album = test::random_album(); diff --git a/modules/comment/tests/Comment_Helper_Test.php b/modules/comment/tests/Comment_Helper_Test.php index d780aba6..7ba024c7 100644 --- a/modules/comment/tests/Comment_Helper_Test.php +++ b/modules/comment/tests/Comment_Helper_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Comment_Helper_Test extends Unit_Test_Case { +class Comment_Helper_Test extends Gallery_Unit_Test_Case { private $_ip_address; private $_user_agent; diff --git a/modules/comment/tests/Comment_Model_Test.php b/modules/comment/tests/Comment_Model_Test.php index c98eb63c..f0449c05 100644 --- a/modules/comment/tests/Comment_Model_Test.php +++ b/modules/comment/tests/Comment_Model_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Comment_Model_Test extends Unit_Test_Case { +class Comment_Model_Test extends Gallery_Unit_Test_Case { public function cant_view_comments_for_unviewable_items_test() { $album = test::random_album(); diff --git a/modules/digibug/tests/Digibug_Controller_Test.php b/modules/digibug/tests/Digibug_Controller_Test.php index 38dcd8e9..561dd3c9 100644 --- a/modules/digibug/tests/Digibug_Controller_Test.php +++ b/modules/digibug/tests/Digibug_Controller_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Digibug_Controller_Test extends Unit_Test_Case { +class Digibug_Controller_Test extends Gallery_Unit_Test_Case { private $_server; public function setup() { diff --git a/modules/exif/tests/Exif_Test.php b/modules/exif/tests/Exif_Test.php index 191bdb99..e4835b7f 100644 --- a/modules/exif/tests/Exif_Test.php +++ b/modules/exif/tests/Exif_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Exif_Test extends Unit_Test_Case { +class Exif_Test extends Gallery_Unit_Test_Case { public function exif_extract_test() { $photo = test::random_photo_unsaved() ->set_data_file(MODPATH . "exif/tests/data/image.jpg") diff --git a/modules/gallery/tests/Access_Helper_Test.php b/modules/gallery/tests/Access_Helper_Test.php index da72f12f..7ddd2875 100644 --- a/modules/gallery/tests/Access_Helper_Test.php +++ b/modules/gallery/tests/Access_Helper_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Access_Helper_Test extends Unit_Test_Case { +class Access_Helper_Test extends Gallery_Unit_Test_Case { private $_group; public function teardown() { diff --git a/modules/gallery/tests/Albums_Controller_Test.php b/modules/gallery/tests/Albums_Controller_Test.php index 26dc2571..76c9a628 100644 --- a/modules/gallery/tests/Albums_Controller_Test.php +++ b/modules/gallery/tests/Albums_Controller_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Albums_Controller_Test extends Unit_Test_Case { +class Albums_Controller_Test extends Gallery_Unit_Test_Case { public function setup() { $this->_save = array($_POST, $_SERVER); } diff --git a/modules/gallery/tests/Cache_Test.php b/modules/gallery/tests/Cache_Test.php index d5bf37cc..1023568b 100644 --- a/modules/gallery/tests/Cache_Test.php +++ b/modules/gallery/tests/Cache_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Cache_Test extends Unit_Test_Case { +class Cache_Test extends Gallery_Unit_Test_Case { private $_driver; public function setup() { db::build()->delete("caches")->execute(); diff --git a/modules/gallery/tests/Controller_Auth_Test.php b/modules/gallery/tests/Controller_Auth_Test.php index 124d8b4c..c27196da 100644 --- a/modules/gallery/tests/Controller_Auth_Test.php +++ b/modules/gallery/tests/Controller_Auth_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Controller_Auth_Test extends Unit_Test_Case { +class Controller_Auth_Test extends Gallery_Unit_Test_Case { public function find_missing_auth_test() { $found = array(); $controllers = explode("\n", `git ls-files '*/*/controllers/*.php'`); diff --git a/modules/gallery/tests/Database_Test.php b/modules/gallery/tests/Database_Test.php index 6aa186e5..e58f73eb 100644 --- a/modules/gallery/tests/Database_Test.php +++ b/modules/gallery/tests/Database_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Database_Test extends Unit_Test_Case { +class Database_Test extends Gallery_Unit_Test_Case { function setup() { $config = Kohana_Config::instance(); $config->set("database.mock.connection.type", "mock"); diff --git a/modules/gallery/tests/Dir_Helper_Test.php b/modules/gallery/tests/Dir_Helper_Test.php index 46bb871c..69241447 100644 --- a/modules/gallery/tests/Dir_Helper_Test.php +++ b/modules/gallery/tests/Dir_Helper_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Dir_Helper_Test extends Unit_Test_Case { +class Dir_Helper_Test extends Gallery_Unit_Test_Case { public function remove_album_test() { $dirname = (VARPATH . "albums/testdir"); mkdir($dirname, 0777, true); diff --git a/modules/gallery/tests/DrawForm_Test.php b/modules/gallery/tests/DrawForm_Test.php index da8a6b04..f7b727c0 100644 --- a/modules/gallery/tests/DrawForm_Test.php +++ b/modules/gallery/tests/DrawForm_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class DrawForm_Test extends Unit_Test_Case { +class DrawForm_Test extends Gallery_Unit_Test_Case { function no_group_test() { $form = new Forge("test/controller", "", "post", array("id" => "g-test-group-form")); $form->input("title")->label(t("Title")); diff --git a/modules/gallery/tests/File_Structure_Test.php b/modules/gallery/tests/File_Structure_Test.php index b5026188..bffdf361 100644 --- a/modules/gallery/tests/File_Structure_Test.php +++ b/modules/gallery/tests/File_Structure_Test.php @@ -19,7 +19,7 @@ */ require_once(MODPATH . "gallery/tests/Gallery_Filters.php"); -class File_Structure_Test extends Unit_Test_Case { +class File_Structure_Test extends Gallery_Unit_Test_Case { public function no_trailing_closing_php_tag_test() { $dir = new GalleryCodeFilterIterator( new RecursiveIteratorIterator(new RecursiveDirectoryIterator(DOCROOT))); diff --git a/modules/gallery/tests/Gallery_I18n_Test.php b/modules/gallery/tests/Gallery_I18n_Test.php index 5d2fd994..f6e50d71 100644 --- a/modules/gallery/tests/Gallery_I18n_Test.php +++ b/modules/gallery/tests/Gallery_I18n_Test.php @@ -18,7 +18,7 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Gallery_I18n_Test extends Unit_Test_Case { +class Gallery_I18n_Test extends Gallery_Unit_Test_Case { private $i18n; public function setup() { diff --git a/modules/gallery/tests/Gallery_Installer_Test.php b/modules/gallery/tests/Gallery_Installer_Test.php index 74a07b1a..3db434bc 100644 --- a/modules/gallery/tests/Gallery_Installer_Test.php +++ b/modules/gallery/tests/Gallery_Installer_Test.php @@ -22,7 +22,7 @@ * This test case operates under the assumption that gallery_installer::install() is called by the * test controller before it starts. */ -class Gallery_Installer_Test extends Unit_Test_Case { +class Gallery_Installer_Test extends Gallery_Unit_Test_Case { public function install_creates_dirs_test() { $this->assert_true(file_exists(VARPATH . "albums")); $this->assert_true(file_exists(VARPATH . "resizes")); diff --git a/modules/gallery/tests/Html_Helper_Test.php b/modules/gallery/tests/Html_Helper_Test.php index 1662b866..be318632 100644 --- a/modules/gallery/tests/Html_Helper_Test.php +++ b/modules/gallery/tests/Html_Helper_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Html_Helper_Test extends Unit_Test_Case { +class Html_Helper_Test extends Gallery_Unit_Test_Case { public function clean_test() { $safe_string = html::clean("hello

world

"); $this->assert_equal("hello <p >world</p>", diff --git a/modules/gallery/tests/Item_Helper_Test.php b/modules/gallery/tests/Item_Helper_Test.php index b3896c7a..5fa8d6b1 100644 --- a/modules/gallery/tests/Item_Helper_Test.php +++ b/modules/gallery/tests/Item_Helper_Test.php @@ -17,7 +17,7 @@ * 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_Helper_Test extends Unit_Test_Case { +class Item_Helper_Test extends Gallery_Unit_Test_Case { public function viewable_test() { $album = test::random_album(); diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index 284491a0..9ea74b16 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -17,7 +17,7 @@ * 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_Model_Test extends Unit_Test_Case { +class Item_Model_Test extends Gallery_Unit_Test_Case { public function saving_sets_created_and_updated_dates_test() { $item = test::random_photo(); $this->assert_true(!empty($item->created)); diff --git a/modules/gallery/tests/Locales_Helper_Test.php b/modules/gallery/tests/Locales_Helper_Test.php index 4c03d8d4..a2680928 100644 --- a/modules/gallery/tests/Locales_Helper_Test.php +++ b/modules/gallery/tests/Locales_Helper_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Locales_Helper_Test extends Unit_Test_Case { +class Locales_Helper_Test extends Gallery_Unit_Test_Case { static $installed_locales; static $default_locale; diff --git a/modules/gallery/tests/Menu_Test.php b/modules/gallery/tests/Menu_Test.php index c91aee0b..643aa727 100644 --- a/modules/gallery/tests/Menu_Test.php +++ b/modules/gallery/tests/Menu_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Menu_Test extends Unit_Test_Case { +class Menu_Test extends Gallery_Unit_Test_Case { public function find_menu_item_test() { $menu = new Menu(true); $menu diff --git a/modules/gallery/tests/ORM_MPTT_Test.php b/modules/gallery/tests/ORM_MPTT_Test.php index 30adf2a0..1ffe1c57 100644 --- a/modules/gallery/tests/ORM_MPTT_Test.php +++ b/modules/gallery/tests/ORM_MPTT_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class ORM_MPTT_Test extends Unit_Test_Case { +class ORM_MPTT_Test extends Gallery_Unit_Test_Case { public function add_to_parent_test() { $album = test::random_album(); diff --git a/modules/gallery/tests/Photos_Controller_Test.php b/modules/gallery/tests/Photos_Controller_Test.php index f548b40d..6012ed1c 100644 --- a/modules/gallery/tests/Photos_Controller_Test.php +++ b/modules/gallery/tests/Photos_Controller_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Photos_Controller_Test extends Unit_Test_Case { +class Photos_Controller_Test extends Gallery_Unit_Test_Case { public function setup() { $this->_save = array($_POST, $_SERVER); $_SERVER["HTTP_REFERER"] = "HTTP_REFERER"; diff --git a/modules/gallery/tests/SafeString_Test.php b/modules/gallery/tests/SafeString_Test.php index 2c07d934..7002a874 100644 --- a/modules/gallery/tests/SafeString_Test.php +++ b/modules/gallery/tests/SafeString_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class SafeString_Test extends Unit_Test_Case { +class SafeString_Test extends Gallery_Unit_Test_Case { public function toString_escapes_for_html_test() { $safe_string = new SafeString("hello

world

"); $this->assert_equal("hello <p>world</p>", diff --git a/modules/gallery/tests/Sendmail_Test.php b/modules/gallery/tests/Sendmail_Test.php index f3a8d897..bc57e434 100644 --- a/modules/gallery/tests/Sendmail_Test.php +++ b/modules/gallery/tests/Sendmail_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Sendmail_Test extends Unit_Test_Case { +class Sendmail_Test extends Gallery_Unit_Test_Case { public function setup() { Kohana_Config::instance()->set("sendmail.from", "from@gallery3.com"); } diff --git a/modules/gallery/tests/Url_Security_Test.php b/modules/gallery/tests/Url_Security_Test.php index de25880f..255b3909 100644 --- a/modules/gallery/tests/Url_Security_Test.php +++ b/modules/gallery/tests/Url_Security_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Url_Security_Test extends Unit_Test_Case { +class Url_Security_Test extends Gallery_Unit_Test_Case { public function setup() { $this->save = array(Router::$current_uri, Router::$complete_uri, $_GET); } diff --git a/modules/gallery/tests/Var_Test.php b/modules/gallery/tests/Var_Test.php index 355d94a7..fb19da7a 100644 --- a/modules/gallery/tests/Var_Test.php +++ b/modules/gallery/tests/Var_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Var_Test extends Unit_Test_Case { +class Var_Test extends Gallery_Unit_Test_Case { public function add_parameter_test() { module::set_var("gallery", "Parameter", "original value"); $this->assert_equal("original value", module::get_var("gallery", "Parameter")); diff --git a/modules/gallery/tests/Xss_Security_Test.php b/modules/gallery/tests/Xss_Security_Test.php index b296d97c..a39a069d 100644 --- a/modules/gallery/tests/Xss_Security_Test.php +++ b/modules/gallery/tests/Xss_Security_Test.php @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Xss_Security_Test extends Unit_Test_Case { +class Xss_Security_Test extends Gallery_Unit_Test_Case { public function find_unescaped_variables_in_views_test() { $found = array(); foreach (glob("*/*/views/*.php") as $view) { diff --git a/modules/gallery_unit_test/helpers/test.php b/modules/gallery_unit_test/helpers/test.php index 77948465..8e483c60 100644 --- a/modules/gallery_unit_test/helpers/test.php +++ b/modules/gallery_unit_test/helpers/test.php @@ -48,6 +48,11 @@ class test_Core { return test::random_photo_unsaved($parent)->save(); } + static function random_user($password="password") { + $rand = "name_" . rand(); + return identity::create_user($rand, $rand, $password, "$rand@rand.com"); + } + static function random_name($item=null) { $rand = "name_" . rand(); if ($item && $item->is_photo()) { @@ -59,4 +64,10 @@ class test_Core { static function starts_with($outer, $inner) { return strpos($outer, $inner) === 0; } + + static function call_and_capture($callback) { + ob_start(); + call_user_func($callback); + return ob_get_clean(); + } } diff --git a/modules/rest/tests/Rest_Controller_Test.php b/modules/rest/tests/Rest_Controller_Test.php index c881583c..ae5e6d48 100644 --- a/modules/rest/tests/Rest_Controller_Test.php +++ b/modules/rest/tests/Rest_Controller_Test.php @@ -17,108 +17,43 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class Rest_Controller_Test extends Unit_Test_Case { +class Rest_Controller_Test extends Gallery_Unit_Test_Case { public function setup() { $this->_save = array($_GET, $_POST, $_SERVER); } - private function _create_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) { - $filename = MODPATH . "gallery/tests/test.jpg"; - $image_name = "image_" . rand(); - if (empty($parent)) { - $parent = ORM::factory("item", 1); - } - return photo::create($parent, $filename, "$image_name.jpg", $image_name); - } - - public function rest_access_key_exists_test() { - list ($access_key, $user) = $this->_create_user(); - $_SERVER["REQUEST_METHOD"] = "GET"; - $_GET["user"] = $user->name;; - $_GET["password"] = "password"; - - $this->assert_equal( - json_encode(array("status" => "OK", "token" => $access_key)), - $this->_call_controller()); } - public function rest_access_key_generated_test() { - list ($access_key, $user) = $this->_create_user(); - ORM::factory("user_access_token") - ->where("access_key", $access_key) - ->delete(); - $_SERVER["REQUEST_METHOD"] = "GET"; - $_GET["user"] = $user->name; - $_GET["password"] = "password"; + public function login_test() { + $user = test::random_user("password"); - $results = json_decode($this->_call_controller()); - - $this->assert_equal("OK", $results->status); - $this->assert_false(empty($results->token)); - } + // There's no access key at first + $this->assert_false( + ORM::factory("user_access_token")->where("user_id", "=", $user->id)->find()->loaded()); - public function rest_access_key_no_parameters_test() { - $_SERVER["REQUEST_METHOD"] = "GET"; - - try { - $this->_call_controller(); - } catch (Rest_Exception $e) { - $this->assert_equal(403, $e->getCode()); - $this->assert_equal("Forbidden", $e->getMessage()); - } catch (Exception $e) { - $this->assert_false(true, $e->__toString()); - } - } + $_POST["user"] = $user->name; + $_POST["password"] = "password"; - public function rest_access_key_user_not_found_test() { - $_SERVER["REQUEST_METHOD"] = "POST"; - $_POST["request"] = json_encode(array("user" => "access_test2", "password" => "password")); + $response = test::call_and_capture(array(new Rest_Controller(), "index")); + $expected = + ORM::factory("user_access_token")->where("user_id", "=", $user->id)->find()->access_key; - try { - $this->_call_controller(); - } catch (Rest_Exception $e) { - $this->assert_equal(403, $e->getCode()); - $this->assert_equal("Forbidden", $e->getMessage()); - } catch (Exception $e) { - $this->assert_false(true, $e->__toString()); - } + // Now there is an access key, and it was returned + $this->assert_equal(json_encode($expected), $response); } - public function rest_access_key_invalid_password_test() { - $_SERVER["REQUEST_METHOD"] = "POST"; + public function login_failed_test() { + $user = test::random_user("password"); + $_POST["user"] = $user->name; + $_POST["password"] = "WRONG PASSWORD"; - try { - $this->_call_controller(); - } catch (Rest_Exception $e) { - $this->assert_equal(403, $e->getCode()); - $this->assert_equal("Forbidden", $e->getMessage()); - } catch (Exception $e) { - $this->assert_false(true, $e->__toString()); - } + // @todo check the http response code + $this->assert_equal(null, test::call_and_capture(array(new Rest_Controller(), "index"))); } - public function rest_get_resource_no_request_key_test() { + public function rest_get_resource_no_request_key_test_() { $_SERVER["REQUEST_METHOD"] = "GET"; $photo = $this->_create_image(); @@ -132,7 +67,7 @@ class Rest_Controller_Test extends Unit_Test_Case { $this->_call_controller("rest", explode("/", $photo->relative_url()))); } - public function rest_get_resource_invalid_key_test() { + public function rest_get_resource_invalid_key_test_() { list ($access_key, $user) = $this->_create_user(); $_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = md5($access_key); // screw up the access key; $_SERVER["REQUEST_METHOD"] = "GET"; @@ -147,7 +82,7 @@ class Rest_Controller_Test extends Unit_Test_Case { } } - public function rest_get_resource_no_user_for_key_test() { + public function rest_get_resource_no_user_for_key_test_() { list ($access_key, $user) = $this->_create_user(); $_SERVER["REQUEST_METHOD"] = "GET"; $_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = $access_key; @@ -166,7 +101,7 @@ class Rest_Controller_Test extends Unit_Test_Case { } } - public function rest_get_resource_no_handler_test() { + public function rest_get_resource_no_handler_test_() { list ($access_key, $user) = $this->_create_user(); $_SERVER["REQUEST_METHOD"] = "GET"; $_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = $access_key; @@ -183,7 +118,7 @@ class Rest_Controller_Test extends Unit_Test_Case { } } - public function rest_get_resource_test() { + public function rest_get_resource_test_() { list ($access_key, $user) = $this->_create_user(); $_SERVER["REQUEST_METHOD"] = "GET"; $_SERVER["HTTP_X_GALLERY_REQUEST_KEY"] = $access_key; @@ -198,33 +133,4 @@ class Rest_Controller_Test extends Unit_Test_Case { "internet_address" => $photo->slug))), $this->_call_controller("rest", explode("/", $photo->relative_url()))); } - - private function _call_controller($method="access_key", $arg=null) { - $controller = new Rest_Controller(); - - ob_start(); - call_user_func_array(array($controller, $method), $arg); - $results = ob_get_contents(); - ob_end_clean(); - - return $results; - } -} - -class rest_rest { - static $request = null; - - static function get($request) { - self::$request = $request; - $item = ORM::factory("item") - ->where("relative_url_cache", "=", implode("/", $request->arguments)) - ->find(); - $response["path"] = $item->relative_url(); - $response["title"] = $item->title; - $response["thumb_url"] = $item->thumb_url(); - $response["description"] = $item->description; - $response["internet_address"] = $item->slug; - return rest::reply(array($item->type => $response)); - } - } diff --git a/modules/tag/tests/Tag_Rest_Helper_Test.php b/modules/tag/tests/Tag_Rest_Helper_Test.php index 555539fd..c2d55ba4 100644 --- a/modules/tag/tests/Tag_Rest_Helper_Test.php +++ b/modules/tag/tests/Tag_Rest_Helper_Test.php @@ -17,7 +17,7 @@ * 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}"); diff --git a/modules/tag/tests/Tag_Test.php b/modules/tag/tests/Tag_Test.php index c96e7f2b..c3243145 100644 --- a/modules/tag/tests/Tag_Test.php +++ b/modules/tag/tests/Tag_Test.php @@ -17,7 +17,7 @@ * 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); diff --git a/modules/user/tests/No_Direct_ORM_Access_Test.php b/modules/user/tests/No_Direct_ORM_Access_Test.php index 440321fa..c372258e 100644 --- a/modules/user/tests/No_Direct_ORM_Access_Test.php +++ b/modules/user/tests/No_Direct_ORM_Access_Test.php @@ -19,7 +19,7 @@ */ require_once(MODPATH . "gallery/tests/Gallery_Filters.php"); -class No_Direct_ORM_Access_Test extends Unit_Test_Case { +class No_Direct_ORM_Access_Test extends Gallery_Unit_Test_Case { public function no_access_to_users_table_test() { $dir = new UserModuleFilterIterator( new PhpCodeFilterIterator( diff --git a/modules/user/tests/User_Groups_Test.php b/modules/user/tests/User_Groups_Test.php index 163b7d79..089ab9a6 100644 --- a/modules/user/tests/User_Groups_Test.php +++ b/modules/user/tests/User_Groups_Test.php @@ -18,7 +18,7 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ -class User_Groups_Test extends Unit_Test_Case { +class User_Groups_Test extends Gallery_Unit_Test_Case { public function teardown() { try { $group = ORM::factory("group")->where("name", "=", "user_groups_test")->find(); diff --git a/modules/user/tests/User_Installer_Test.php b/modules/user/tests/User_Installer_Test.php index 12a10eda..b3c5960a 100644 --- a/modules/user/tests/User_Installer_Test.php +++ b/modules/user/tests/User_Installer_Test.php @@ -22,7 +22,7 @@ * This test case operates under the assumption that user_installer::install() is called by the * test controller before it starts. */ -class User_Installer_Test extends Unit_Test_Case { +class User_Installer_Test extends Gallery_Unit_Test_Case { public function install_creates_admin_user_test() { $user = ORM::factory("user", 1); $this->assert_equal("guest", $user->name); -- cgit v1.2.3 From 995faaa27fc870589e346f7ef65e0bd7cabfe047 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 20 Jan 2010 22:45:19 -0800 Subject: Stop using MY_ORM::original(). It's got very odd semantics and we are not capturing all cases for setting and resetting $original, which leads to some weird and hard to reproduce behavior. Instead, if we need the original just reload it from the database. This may result in a somewhat excessive load in places, but we'll have to fix that in a later optimization pass. --- modules/gallery/models/item.php | 104 +++++++++++++++--------------- modules/gallery/tests/Item_Model_Test.php | 1 + 2 files changed, 54 insertions(+), 51 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index 58ff86ed..19c379ab 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -415,65 +415,58 @@ class Item_Model extends ORM_MPTT { } else { // Update an existing item - // The new values have to be valid before we do anything with them. If we make any - // other changes before we call parent::save() below, we'll have to validate those changes - // again. But, we can't take any action on these values until we know they're ok so this - // is unavoidable. - if (!$this->_valid) { - $this->validate(); - } - - $original = clone $this->original(); - - if ($original->name != $this->name || $original->parent_id != $this->parent_id) { - // Get the old relative path for when we rename or move below - if (!isset($this->relative_path_cache)) { - $this->_build_relative_caches(); // but don't call save() - } - $before_save = clone $this; + // If any significant fields have changed, load up a copy of the original item and + // keep it around. + if (array_intersect($this->changed, array("parent_id", "name", "slug"))) { + $original = ORM::factory("item")->where("id", "=", $this->id)->find(); + $original->_build_relative_caches(); $this->relative_path_cache = null; - } - - if ($original->slug != $this->slug) { $this->relative_url_cache = null; } parent::save(); - if ($original->parent_id != $this->parent_id || $original->name != $this->name) { + // Now update the filesystem and any database caches if there were significant value + // changes. If anything past this point fails, then we'll have an inconsistent database + // so this code should be as robust as we can make it. + if (isset($original)) { + // Update the MPTT pointers, if necessary. We have to do this before we generate any + // cached paths! if ($original->parent_id != $this->parent_id) { - // Move the ORM pointers around parent::move_to($this->parent()); } - // Move all of the items associated data files - @rename($before_save->file_path(), $this->file_path()); - if ($this->is_album()) { - @rename(dirname($before_save->resize_path()), dirname($this->resize_path())); - @rename(dirname($before_save->thumb_path()), dirname($this->thumb_path())); - } else { - @rename($before_save->resize_path(), $this->resize_path()); - @rename($before_save->thumb_path(), $this->thumb_path()); - } + if ($original->parent_id != $this->parent_id || $original->name != $this->name) { + // Move all of the items associated data files + @rename($original->file_path(), $this->file_path()); + if ($this->is_album()) { + @rename(dirname($original->resize_path()), dirname($this->resize_path())); + @rename(dirname($original->thumb_path()), dirname($this->thumb_path())); + } else { + @rename($original->resize_path(), $this->resize_path()); + @rename($original->thumb_path(), $this->thumb_path()); + } - if ($original->parent_id != $this->parent_id) { - // This will result in 2 events since we'll still fire the item_updated event below - module::event("item_moved", $this, $original->parent()); + + if ($original->parent_id != $this->parent_id) { + // This will result in 2 events since we'll still fire the item_updated event below + module::event("item_moved", $this, $original->parent()); + } } - } - // Changing the name, slug or parent ripples downwards - if ($this->is_album() && - ($original->name != $this->name || - $original->slug != $this->slug || - $original->parent_id != $this->parent_id)) { - db::build() - ->update("items") - ->set("relative_url_cache", null) - ->set("relative_path_cache", null) - ->where("left_ptr", ">", $this->left_ptr) - ->where("right_ptr", "<", $this->right_ptr) - ->execute(); + // Changing the name, slug or parent ripples downwards + if ($this->is_album() && + ($original->name != $this->name || + $original->slug != $this->slug || + $original->parent_id != $this->parent_id)) { + db::build() + ->update("items") + ->set("relative_url_cache", null) + ->set("relative_path_cache", null) + ->where("left_ptr", ">", $this->left_ptr) + ->where("right_ptr", "<", $this->right_ptr) + ->execute(); + } } module::event("item_updated", $original, $this); @@ -784,27 +777,36 @@ class Item_Model extends ORM_MPTT { return; } else if (rtrim($this->name, ".") !== $this->name) { $v->add_error("name", "no_trailing_period"); - } else if ($this->is_movie() || $this->is_photo()) { - if ($this->original()->loaded()) { + return; + } + + if ($this->is_movie() || $this->is_photo()) { + if ($this->loaded()) { // Existing items can't change their extension + $original = ORM::factory("item")->where("id", "=", $this->id)->find(); $new_ext = pathinfo($this->name, PATHINFO_EXTENSION); - $old_ext = pathinfo($this->original()->name, PATHINFO_EXTENSION); + $old_ext = pathinfo($original->name, PATHINFO_EXTENSION); if (strcasecmp($new_ext, $old_ext)) { $v->add_error("name", "illegal_data_file_extension"); + return; } } else { // New items must have an extension if (!pathinfo($this->name, PATHINFO_EXTENSION)) { $v->add_error("name", "illegal_data_file_extension"); + return; } } - } else if (db::build() + } + + if (db::build() ->from("items") ->where("parent_id", "=", $this->parent_id) ->where("name", "=", $this->name) ->merge_where($this->id ? array(array("id", "<>", $this->id)) : null) ->count_records()) { $v->add_error("name", "conflict"); + return; } } @@ -900,7 +902,7 @@ class Item_Model extends ORM_MPTT { * This field cannot be changed after it's been set. */ public function read_only(Validation $v, $field) { - if ($this->original()->loaded() && $this->original()->$field != $this->$field) { + if ($this->loaded() && isset($this->changed[$field])) { $v->add_error($field, "read_only"); } } diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index 9ea74b16..efad660e 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -139,6 +139,7 @@ class Item_Model_Test extends Gallery_Unit_Test_Case { try { $item->name = $item2->name; + print "SAVE\n"; $item->save(); } catch (ORM_Validation_Exception $e) { $this->assert_true(in_array("conflict", $e->validation->errors())); -- cgit v1.2.3 From feefdfd533dd4e82a2edd5c0b342322889877b79 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 20 Jan 2010 22:51:18 -0800 Subject: Stop using MY_ORM::original(). --- modules/gallery/tests/Item_Model_Test.php | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index efad660e..035ecb51 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -149,16 +149,6 @@ class Item_Model_Test extends Gallery_Unit_Test_Case { $this->assert_false(true, "rename should conflict"); } - public function save_original_values_test() { - $item = test::random_photo_unsaved(); - $item->title = "ORIGINAL_VALUE"; - $item->save(); - $item->title = "NEW_VALUE"; - - $this->assert_same("ORIGINAL_VALUE", $item->original()->title); - $this->assert_same("NEW_VALUE", $item->title); - } - public function move_album_test() { $album2 = test::random_album(); $album1 = test::random_album($album2); -- cgit v1.2.3 From 46d4d778ad9febf175380cd82e0a10da33293d36 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 20 Jan 2010 23:33:09 -0800 Subject: Remove debug code. --- modules/gallery/tests/Item_Model_Test.php | 1 - 1 file changed, 1 deletion(-) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index 035ecb51..1e77076a 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -139,7 +139,6 @@ class Item_Model_Test extends Gallery_Unit_Test_Case { try { $item->name = $item2->name; - print "SAVE\n"; $item->save(); } catch (ORM_Validation_Exception $e) { $this->assert_true(in_array("conflict", $e->validation->errors())); -- cgit v1.2.3 From 212633d05a5a8abb77a744a69c61d6e3051b73c5 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 27 Jan 2010 21:52:18 -0800 Subject: Prevent accidentally deleting the root album. --- modules/gallery/models/item.php | 6 ++++++ modules/gallery/tests/Item_Model_Test.php | 10 ++++++++++ 2 files changed, 16 insertions(+) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index e4ff0bfb..9706d61f 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -71,6 +71,12 @@ class Item_Model extends ORM_MPTT { } public function delete() { + if ($this->id == 1) { + $v = new Validation(array("id")); + $v->add_error("id", "cant_delete_root_album"); + ORM_Validation_Exception::handle_validation($this->table_name, $v); + } + $old = clone $this; module::event("item_before_delete", $this); diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index 1e77076a..eb9ecc99 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -336,4 +336,14 @@ class Item_Model_Test extends Gallery_Unit_Test_Case { } $this->assert_true(false, "Shouldn't get here"); } + + public function cant_delete_root_album_test() { + try { + item::root()->delete(); + } catch (ORM_Validation_Exception $e) { + $this->assert_same(array("id" => "cant_delete_root_album"), $e->validation->errors()); + return; // pass + } + $this->assert_true(false, "Shouldn't get here"); + } } -- cgit v1.2.3 From d29028c4ea9002ac036a89a4478fb4640c86fedb Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 30 Jan 2010 23:36:11 -0800 Subject: Add Item_Model::as_restful_array() for convenience. --- modules/gallery/models/item.php | 17 +++++++++++++++++ modules/gallery/tests/Item_Model_Test.php | 12 ++++++++++++ 2 files changed, 29 insertions(+) (limited to 'modules/gallery/tests/Item_Model_Test.php') diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index ae6e4cc9..fd121a5a 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -914,4 +914,21 @@ class Item_Model extends ORM_MPTT { $v->add_error($field, "read_only"); } } + + /** + * Same as ORM::as_array() but convert id fields into their RESTful form. + */ + public function as_restful_array() { + // Convert item ids to rest URLs for consistency + $data = $this->as_array(); + if ($tmp = $this->parent()) { + $data["parent"] = rest::url("item", $tmp); + } + unset($data["parent_id"]); + if ($tmp = $this->album_cover()) { + $data["album_cover"] = rest::url("item", $tmp); + } + unset($data["album_cover_item_id"]); + return $data; + } } diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index eb9ecc99..9f632fb5 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -346,4 +346,16 @@ class Item_Model_Test extends Gallery_Unit_Test_Case { } $this->assert_true(false, "Shouldn't get here"); } + + 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)); + } } -- cgit v1.2.3