diff options
Diffstat (limited to 'modules/gallery/tests/Item_Model_Test.php')
-rw-r--r-- | modules/gallery/tests/Item_Model_Test.php | 279 |
1 files changed, 168 insertions, 111 deletions
diff --git a/modules/gallery/tests/Item_Model_Test.php b/modules/gallery/tests/Item_Model_Test.php index d03a03f4..1e77076a 100644 --- a/modules/gallery/tests/Item_Model_Test.php +++ b/modules/gallery/tests/Item_Model_Test.php @@ -17,22 +17,15 @@ * 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 = 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,18 +62,16 @@ 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(); + $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->rename($new_name)->save(); + $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. $this->assert_equal($new_name, $item->name); @@ -93,10 +84,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"); @@ -104,10 +94,11 @@ 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->rename($new_album_name)->save(); + $album->name = $new_album_name; + $album->save(); $photo->reload(); // Expected: @@ -120,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())); @@ -130,81 +121,56 @@ 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(); - - $new_name = rand() . "/"; - + $item = test::random_photo(); try { - $item->rename($new_name)->save(); - } catch (Exception $e) { - // pass + $item->name = test::random_name() . "/"; + $item->save(); + } catch (ORM_Validation_Exception $e) { + $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() { // 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->save(); + } catch (ORM_Validation_Exception $e) { + $this->assert_true(in_array("conflict", $e->validation->errors())); return; } - $this->assert_false(true, "Item_Model::rename should fail."); - } - - public function save_original_values_test() { - $item = self::_create_random_item(); - $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 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()); + $this->assert_false(true, "rename should conflict"); } 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(); + $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->move_to($root); + $album1->parent_id = item::root()->id; + $album1->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(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())); @@ -212,71 +178,162 @@ 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); + $album1 = test::random_album(); + $photo = test::random_photo($album1); + + $album2 = test::random_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_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())); $this->assert_equal("file", file_get_contents($photo->file_path())); } - 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); + public function move_album_fails_conflicting_target_test() { + $album = test::random_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($root); - } catch (Exception $e) { - // pass - $this->assert_true(strpos($e->getMessage(), "INVALID_MOVE_TARGET_EXISTS") !== false, - "incorrect exception."); + $source->parent_id = item::root()->id; + $source->save(); + } 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() { + $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. - $this->assert_false(true, "Item_Model::rename should not accept / characters"); + try { + $album->parent_id = $photo->id; + $album->save(); + } catch (ORM_Validation_Exception $e) { + $this->assert_equal(array("parent_id" => "invalid"), $e->validation->errors()); + return; + } + $this->assert_true(false, "Shouldn't get here"); } - public function 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); + public function move_photo_fails_conflicting_target_test() { + $photo1 = test::random_photo(); + $album = test::random_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($root); + $photo2->parent_id = item::root()->id; + $photo2->save(); } 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; } + $this->assert_true(false, "Shouldn't get here"); + } + + public function move_album_inside_descendent_fails_test() { + $album1 = test::random_album(); + $album2 = test::random_album($album1); + $album3 = test::random_album($album2); + + try { + $album1->parent_id = $album3->id; + $album1->save(); + } catch (ORM_Validation_Exception $e) { + $this->assert_equal(array("parent_id" => "invalid"), $e->validation->errors()); + return; + } + $this->assert_true(false, "Shouldn't get here"); + } - $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"); + } + + public function slug_is_url_safe_test() { + try { + $album = test::random_album_unsaved(); + $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(); + } + + 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"); } } |