Files
gallery3/modules/gallery/tests/Item_Model_Test.php

524 lines
19 KiB
PHP
Raw Normal View History

<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
2012-02-27 09:46:39 -08:00
* Copyright (C) 2000-2012 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 Item_Model_Test extends Gallery_Unit_Test_Case {
public function teardown() {
identity::set_active_user(identity::admin_user());
}
public function saving_sets_created_and_updated_dates_test() {
$item = test::random_photo();
$this->assert_true(!empty($item->created));
$this->assert_true(!empty($item->updated));
}
public function updating_doesnt_change_created_date_test() {
$item = test::random_photo();
// Force the creation date to something well known
2009-12-21 11:30:34 -08:00
db::build()
->update("items")
->set("created", 0)
->set("updated", 0)
->where("id", "=", $item->id)
->execute();
$item->reload();
$item->title = "foo"; // force a change
$item->save();
$this->assert_true(empty($item->created));
$this->assert_true(!empty($item->updated));
}
public function updating_view_count_only_doesnt_change_updated_date_test() {
$item = test::random_photo();
$item->reload();
$this->assert_equal(0, $item->view_count);
// Force the updated date to something well known
2009-12-21 11:30:34 -08:00
db::build()
->update("items")
->set("updated", 0)
->where("id", "=", $item->id)
->execute();
$item->reload();
$item->view_count++;
$item->save();
$this->assert_same(1, $item->view_count);
$this->assert_true(empty($item->updated));
}
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");
// Now rename it
$item->name = ($new_name = test::random_name($item));
2010-01-17 20:54:08 -08:00
$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);
$this->assert_equal($new_name, basename($item->file_path()));
$this->assert_equal($new_name, basename($item->thumb_path()));
$this->assert_equal($new_name, basename($item->resize_path()));
$this->assert_equal("thumb", file_get_contents($item->thumb_path()));
$this->assert_equal("resize", file_get_contents($item->resize_path()));
$this->assert_equal("file", file_get_contents($item->file_path()));
}
public function rename_album_test() {
$album = test::random_album();
$photo = test::random_photo($album);
2010-01-17 21:03:15 -08:00
$album->reload();
file_put_contents($photo->thumb_path(), "thumb");
file_put_contents($photo->resize_path(), "resize");
file_put_contents($photo->file_path(), "file");
$original_album_name = $album->name;
$original_photo_name = $photo->name;
$new_album_name = test::random_name();
// Now rename the album
2010-01-17 21:03:15 -08:00
$album->name = $new_album_name;
$album->save();
$photo->reload();
// Expected:
// * the album name changed.
// * the album dirs are all moved
// * the photo's paths are all inside the albums paths
// * the photo files are all still intact and accessible
$this->assert_equal($new_album_name, $album->name);
$this->assert_equal($new_album_name, basename($album->file_path()));
$this->assert_equal($new_album_name, basename(dirname($album->thumb_path())));
$this->assert_equal($new_album_name, basename(dirname($album->resize_path())));
2010-01-18 22:55:16 -08:00
$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()));
$this->assert_equal("file", file_get_contents($photo->file_path()));
}
public function item_rename_wont_accept_slash_test() {
$item = test::random_photo();
try {
2010-01-18 22:51:40 -08:00
$item->name = test::random_name() . "/";
$item->save();
} catch (ORM_Validation_Exception $e) {
2010-01-18 22:51:40 -08:00
$this->assert_equal(array("name" => "no_slashes"), $e->validation->errors());
return;
}
2010-01-18 22:51:40 -08:00
$this->assert_true(false, "Shouldn't get here");
}
public function item_rename_over_existing_name_gets_uniqified_test() {
// Create a test photo
$item = test::random_photo();
$item2 = test::random_photo();
$item->name = $item2->name;
$item->save();
// foo.jpg should become foo-####.jpg
$this->assert_true(
preg_match("/" . str_replace(".jpg", "", $item2->name) . "-\d+\.jpg/", $item->name));
}
public function move_album_test() {
$album2 = test::random_album();
2010-01-18 22:51:40 -08:00
$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
2010-01-18 22:51:40 -08:00
$album1->parent_id = item::root()->id;
$album1->save();
$photo->reload();
// Expected:
// * 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
2010-01-18 22:51:40 -08:00
$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()));
$this->assert_equal("file", file_get_contents($photo->file_path()));
}
public function move_photo_test() {
$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 photo
$photo->parent_id = $album2->id;
$photo->save();
// Expected:
// * the photo's paths are inside the album2 not album1
// * the photo files are all still intact and accessible
2010-01-18 22:55:16 -08:00
$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()));
}
2009-11-06 23:19:48 -08:00
public function move_album_with_conflicting_target_gets_uniqified_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 and get randomized
$source->parent_id = item::root()->id;
$source->save();
// foo should become foo-####
$this->assert_true(preg_match("/{$album->name}-\d+/", $source->name));
$this->assert_true(preg_match("/{$album->slug}-\d+/", $source->slug));
}
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();
} catch (ORM_Validation_Exception $e) {
$this->assert_equal(array("parent_id" => "invalid"), $e->validation->errors());
2010-01-18 22:51:40 -08:00
return;
}
2010-01-18 22:51:40 -08:00
$this->assert_true(false, "Shouldn't get here");
}
public function move_photo_with_conflicting_target_gets_uniqified_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 and get uniqified.
$photo2->parent_id = item::root()->id;
$photo2->save();
// foo.jpg should become foo-####.jpg
$this->assert_true(
preg_match("/" . str_replace(".jpg", "", $photo1->name) . "-\d+\.jpg/", $photo2->name));
// foo should become foo
$this->assert_true(preg_match("/{$photo1->slug}/", $photo2->name));
}
public function move_album_inside_descendent_fails_test() {
2010-01-18 22:51:40 -08:00
$album1 = test::random_album();
$album2 = test::random_album($album1);
$album3 = test::random_album($album2);
try {
2010-01-18 22:51:40 -08:00
$album1->parent_id = $album3->id;
$album1->save();
} catch (ORM_Validation_Exception $e) {
$this->assert_equal(array("parent_id" => "invalid"), $e->validation->errors());
2010-01-18 22:51:40 -08:00
return;
}
2010-01-18 22:51:40 -08:00
$this->assert_true(false, "Shouldn't get here");
}
public function basic_validation_test() {
$item = ORM::factory("item");
$item->album_cover_item_id = random::int(); // invalid
$item->description = str_repeat("x", 70000); // invalid
$item->name = null;
$item->parent_id = random::int();
$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",
"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");
}
2010-01-18 23:38:39 -08:00
public function slug_is_url_safe_test() {
try {
$album = test::random_album_unsaved();
2010-01-18 23:38:39 -08:00
$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 name_with_only_invalid_chars_is_still_valid_test() {
$album = test::random_album_unsaved();
$album->name = "[]";
$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) {
2012-05-05 18:52:44 -07:00
$this->assert_same(
array("name" => "illegal_data_file_extension", "type" => "read_only"),
$e->validation->errors());
return; // pass
}
$this->assert_true(false, "Shouldn't get here");
}
public function photo_files_must_have_an_extension_test() {
try {
$photo = test::random_photo_unsaved();
$photo->mime_type = "image/jpeg";
$photo->name = "no_extension";
$photo->save();
} catch (ORM_Validation_Exception $e) {
$this->assert_same(array("name" => "illegal_data_file_extension"), $e->validation->errors());
return; // pass
}
$this->assert_true(false, "Shouldn't get here");
}
public function movie_files_must_have_an_extension_test() {
try {
$movie = test::random_photo_unsaved();
$movie->type = "movie";
$movie->mime_type = "video/x-flv";
$movie->name = "no_extension";
$movie->save();
} catch (ORM_Validation_Exception $e) {
$this->assert_same(array("name" => "illegal_data_file_extension"), $e->validation->errors());
return; // pass
}
$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");
}
public function as_restful_array_test() {
$album = test::random_album();
$photo = test::random_photo($album);
$album->reload();
$result = $album->as_restful_array();
$this->assert_same(rest::url("item", item::root()), $result["parent"]);
$this->assert_same(rest::url("item", $photo), $result["album_cover"]);
$this->assert_true(!array_key_exists("parent_id", $result));
$this->assert_true(!array_key_exists("album_cover_item_id", $result));
}
public function as_restful_array_with_edit_bit_test() {
$response = item::root()->as_restful_array();
$this->assert_true($response["can_edit"]);
identity::set_active_user(identity::guest());
$response = item::root()->as_restful_array();
$this->assert_false($response["can_edit"]);
}
public function first_photo_becomes_album_cover() {
$album = test::random_album();
$photo = test::random_photo($album);
$album->reload();
$this->assert_same($photo->id, $album->album_cover_item_id);
}
public function replace_data_file_test() {
// Random photo is modules/gallery/tests/test.jpg which is 1024x768 and 6232 bytes.
$photo = test::random_photo();
$this->assert_equal(1024, $photo->width);
$this->assert_equal(768, $photo->height);
$this->assert_equal(6232, filesize($photo->file_path()));
// Random photo is gallery/images/imagemagick.jpg is 114x118 and 20337 bytes
$photo->set_data_file(MODPATH . "gallery/images/imagemagick.jpg");
$photo->save();
$this->assert_equal(114, $photo->width);
$this->assert_equal(118, $photo->height);
$this->assert_equal(20337, filesize($photo->file_path()));
}
Polishing the rawphoto changes, including adding some tests. Squashed commit of the following: commit 945316a8c220b12adb687c896bcc5e86f99f46a4 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 01:11:13 2011 -0600 Add a test for the sunny-day scenario where a rule changes a data file's MIME type. commit 4ee1ee000c8f4d8ebaae66f637bc71080486fd73 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:49:47 2011 -0600 Ensure that a third-party cannot swap out a legitimate photo with an unsafe file type. commit 7dd0105bfc59c150e5640e693778f51bbaa44eab Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:48:19 2011 -0600 Update the MIME type and other meta-data when a new data file is provided. commit 5a8844c7947b21cf658f22cc61f20ffa9e8f07f2 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:30:01 2011 -0600 Remove a unit test that no longer applies. Replacement data files are allowed to have different MIME types. commit 0de9c6283ce4f5773cad8e92b6785d6a1f7b5e46 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:27:45 2011 -0600 If one rule fails, then abort processing, rather than trying to proceed to subsequent rules. commit 41d379c2b777ae7b3a11f528971228e234f8976f Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:10:10 2011 -0600 Replace an overly-complicated regular expression with a simple in_array, at Bharat's suggestion. commit 1b3f7111d4c2607baaa2da0aab3b501f2d9a1426 Merge: 8f7904a 403f64b Author: Chad Parry <github@chad.parry.org> Date: Wed Jul 20 21:02:56 2011 -0600 Merge branch 'master' into rawphoto commit 8f7904ab62c71a7e4ee68762f936030b4dcb4ea1 Merge: e950573 771de0a Author: Chad Parry <github@chad.parry.org> Date: Sat Jun 25 14:12:39 2011 -0600 Merge branches 'master' and 'rawphoto' into rawphoto commit e95057337996351e49915d9f85d007d50103a4be Author: Chad Parry <github@chad.parry.org> Date: Wed Jun 15 20:24:18 2011 -0600 Merge branches 'rawphoto-squash' and 'rawphoto' into rawphoto
2011-07-21 01:12:26 -06:00
public function replace_data_file_type_test() {
// Random photo is modules/gallery/tests/test.jpg
$photo = test::random_photo();
Polishing the rawphoto changes, including adding some tests. Squashed commit of the following: commit 945316a8c220b12adb687c896bcc5e86f99f46a4 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 01:11:13 2011 -0600 Add a test for the sunny-day scenario where a rule changes a data file's MIME type. commit 4ee1ee000c8f4d8ebaae66f637bc71080486fd73 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:49:47 2011 -0600 Ensure that a third-party cannot swap out a legitimate photo with an unsafe file type. commit 7dd0105bfc59c150e5640e693778f51bbaa44eab Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:48:19 2011 -0600 Update the MIME type and other meta-data when a new data file is provided. commit 5a8844c7947b21cf658f22cc61f20ffa9e8f07f2 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:30:01 2011 -0600 Remove a unit test that no longer applies. Replacement data files are allowed to have different MIME types. commit 0de9c6283ce4f5773cad8e92b6785d6a1f7b5e46 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:27:45 2011 -0600 If one rule fails, then abort processing, rather than trying to proceed to subsequent rules. commit 41d379c2b777ae7b3a11f528971228e234f8976f Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:10:10 2011 -0600 Replace an overly-complicated regular expression with a simple in_array, at Bharat's suggestion. commit 1b3f7111d4c2607baaa2da0aab3b501f2d9a1426 Merge: 8f7904a 403f64b Author: Chad Parry <github@chad.parry.org> Date: Wed Jul 20 21:02:56 2011 -0600 Merge branch 'master' into rawphoto commit 8f7904ab62c71a7e4ee68762f936030b4dcb4ea1 Merge: e950573 771de0a Author: Chad Parry <github@chad.parry.org> Date: Sat Jun 25 14:12:39 2011 -0600 Merge branches 'master' and 'rawphoto' into rawphoto commit e95057337996351e49915d9f85d007d50103a4be Author: Chad Parry <github@chad.parry.org> Date: Wed Jun 15 20:24:18 2011 -0600 Merge branches 'rawphoto-squash' and 'rawphoto' into rawphoto
2011-07-21 01:12:26 -06:00
$this->assert_equal(1024, $photo->width);
$this->assert_equal(768, $photo->height);
$this->assert_equal(6232, filesize($photo->file_path()));
$this->assert_equal("image/jpeg", $photo->mime_type);
$orig_name = $photo->name;
// Random photo is gallery/images/graphicsmagick.png is 104x76 and 1486 bytes
$photo->set_data_file(MODPATH . "gallery/images/graphicsmagick.png");
Polishing the rawphoto changes, including adding some tests. Squashed commit of the following: commit 945316a8c220b12adb687c896bcc5e86f99f46a4 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 01:11:13 2011 -0600 Add a test for the sunny-day scenario where a rule changes a data file's MIME type. commit 4ee1ee000c8f4d8ebaae66f637bc71080486fd73 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:49:47 2011 -0600 Ensure that a third-party cannot swap out a legitimate photo with an unsafe file type. commit 7dd0105bfc59c150e5640e693778f51bbaa44eab Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:48:19 2011 -0600 Update the MIME type and other meta-data when a new data file is provided. commit 5a8844c7947b21cf658f22cc61f20ffa9e8f07f2 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:30:01 2011 -0600 Remove a unit test that no longer applies. Replacement data files are allowed to have different MIME types. commit 0de9c6283ce4f5773cad8e92b6785d6a1f7b5e46 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:27:45 2011 -0600 If one rule fails, then abort processing, rather than trying to proceed to subsequent rules. commit 41d379c2b777ae7b3a11f528971228e234f8976f Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:10:10 2011 -0600 Replace an overly-complicated regular expression with a simple in_array, at Bharat's suggestion. commit 1b3f7111d4c2607baaa2da0aab3b501f2d9a1426 Merge: 8f7904a 403f64b Author: Chad Parry <github@chad.parry.org> Date: Wed Jul 20 21:02:56 2011 -0600 Merge branch 'master' into rawphoto commit 8f7904ab62c71a7e4ee68762f936030b4dcb4ea1 Merge: e950573 771de0a Author: Chad Parry <github@chad.parry.org> Date: Sat Jun 25 14:12:39 2011 -0600 Merge branches 'master' and 'rawphoto' into rawphoto commit e95057337996351e49915d9f85d007d50103a4be Author: Chad Parry <github@chad.parry.org> Date: Wed Jun 15 20:24:18 2011 -0600 Merge branches 'rawphoto-squash' and 'rawphoto' into rawphoto
2011-07-21 01:12:26 -06:00
$photo->save();
$this->assert_equal(104, $photo->width);
$this->assert_equal(76, $photo->height);
$this->assert_equal(1486, filesize($photo->file_path()));
$this->assert_equal("image/png", $photo->mime_type);
$this->assert_equal("png", pathinfo($photo->name, PATHINFO_EXTENSION));
$this->assert_equal(pathinfo($orig_name, PATHINFO_FILENAME), pathinfo($photo->name, PATHINFO_FILENAME));
}
Polishing the rawphoto changes, including adding some tests. Squashed commit of the following: commit 945316a8c220b12adb687c896bcc5e86f99f46a4 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 01:11:13 2011 -0600 Add a test for the sunny-day scenario where a rule changes a data file's MIME type. commit 4ee1ee000c8f4d8ebaae66f637bc71080486fd73 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:49:47 2011 -0600 Ensure that a third-party cannot swap out a legitimate photo with an unsafe file type. commit 7dd0105bfc59c150e5640e693778f51bbaa44eab Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:48:19 2011 -0600 Update the MIME type and other meta-data when a new data file is provided. commit 5a8844c7947b21cf658f22cc61f20ffa9e8f07f2 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:30:01 2011 -0600 Remove a unit test that no longer applies. Replacement data files are allowed to have different MIME types. commit 0de9c6283ce4f5773cad8e92b6785d6a1f7b5e46 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:27:45 2011 -0600 If one rule fails, then abort processing, rather than trying to proceed to subsequent rules. commit 41d379c2b777ae7b3a11f528971228e234f8976f Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:10:10 2011 -0600 Replace an overly-complicated regular expression with a simple in_array, at Bharat's suggestion. commit 1b3f7111d4c2607baaa2da0aab3b501f2d9a1426 Merge: 8f7904a 403f64b Author: Chad Parry <github@chad.parry.org> Date: Wed Jul 20 21:02:56 2011 -0600 Merge branch 'master' into rawphoto commit 8f7904ab62c71a7e4ee68762f936030b4dcb4ea1 Merge: e950573 771de0a Author: Chad Parry <github@chad.parry.org> Date: Sat Jun 25 14:12:39 2011 -0600 Merge branches 'master' and 'rawphoto' into rawphoto commit e95057337996351e49915d9f85d007d50103a4be Author: Chad Parry <github@chad.parry.org> Date: Wed Jun 15 20:24:18 2011 -0600 Merge branches 'rawphoto-squash' and 'rawphoto' into rawphoto
2011-07-21 01:12:26 -06:00
public function unsafe_data_file_replacement_test() {
try {
Polishing the rawphoto changes, including adding some tests. Squashed commit of the following: commit 945316a8c220b12adb687c896bcc5e86f99f46a4 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 01:11:13 2011 -0600 Add a test for the sunny-day scenario where a rule changes a data file's MIME type. commit 4ee1ee000c8f4d8ebaae66f637bc71080486fd73 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:49:47 2011 -0600 Ensure that a third-party cannot swap out a legitimate photo with an unsafe file type. commit 7dd0105bfc59c150e5640e693778f51bbaa44eab Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:48:19 2011 -0600 Update the MIME type and other meta-data when a new data file is provided. commit 5a8844c7947b21cf658f22cc61f20ffa9e8f07f2 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:30:01 2011 -0600 Remove a unit test that no longer applies. Replacement data files are allowed to have different MIME types. commit 0de9c6283ce4f5773cad8e92b6785d6a1f7b5e46 Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:27:45 2011 -0600 If one rule fails, then abort processing, rather than trying to proceed to subsequent rules. commit 41d379c2b777ae7b3a11f528971228e234f8976f Author: Chad Parry <github@chad.parry.org> Date: Thu Jul 21 00:10:10 2011 -0600 Replace an overly-complicated regular expression with a simple in_array, at Bharat's suggestion. commit 1b3f7111d4c2607baaa2da0aab3b501f2d9a1426 Merge: 8f7904a 403f64b Author: Chad Parry <github@chad.parry.org> Date: Wed Jul 20 21:02:56 2011 -0600 Merge branch 'master' into rawphoto commit 8f7904ab62c71a7e4ee68762f936030b4dcb4ea1 Merge: e950573 771de0a Author: Chad Parry <github@chad.parry.org> Date: Sat Jun 25 14:12:39 2011 -0600 Merge branches 'master' and 'rawphoto' into rawphoto commit e95057337996351e49915d9f85d007d50103a4be Author: Chad Parry <github@chad.parry.org> Date: Wed Jun 15 20:24:18 2011 -0600 Merge branches 'rawphoto-squash' and 'rawphoto' into rawphoto
2011-07-21 01:12:26 -06:00
$photo = test::random_photo();
$photo->set_data_file(MODPATH . "gallery/tests/Item_Model_Test.php");
$photo->save();
} catch (ORM_Validation_Exception $e) {
2012-05-05 18:52:44 -07:00
$this->assert_same(array("mime_type" => "invalid", "name" => "illegal_data_file_extension"),
$e->validation->errors());
return; // pass
}
$this->assert_true(false, "Shouldn't get here");
}
public function urls_test() {
$photo = test::random_photo();
$this->assert_true(
preg_match("|http://./var/resizes/name_\w+\.jpg\?m=\d+|", $photo->resize_url()),
$photo->resize_url() . " is malformed");
$this->assert_true(
preg_match("|http://./var/thumbs/name_\w+\.jpg\?m=\d+|", $photo->thumb_url()),
$photo->thumb_url() . " is malformed");
$this->assert_true(
preg_match("|http://./var/albums/name_\w+\.jpg\?m=\d+|", $photo->file_url()),
$photo->file_url() . " is malformed");
// Albums have special thumbnails. Empty album has cachebuster of 0 since it has no thumbnail
$album = test::random_album();
$this->assert_true(
preg_match("|http://./var/thumbs/name_\w+/\.album\.jpg\?m=0|", $album->thumb_url()),
$album->thumb_url() . " is malformed");
$photo = test::random_photo($album);
$this->assert_true(
preg_match("|http://./var/thumbs/name_\w+/\.album\.jpg\?m=\d+|", $album->thumb_url()),
$album->thumb_url() . " is malformed");
}
public function legal_extension_test() {
foreach (array("test.gif", "test.GIF", "test.Gif", "test.jpeg", "test.JPG") as $name) {
$photo = test::random_photo_unsaved(item::root());
$photo->name = $name;
$photo->save();
}
}
public function illegal_extension_test() {
foreach (array("test.php", "test.PHP", "test.php5", "test.php4",
"test.pl", "test.php.png") as $name) {
try {
$photo = test::random_photo_unsaved(item::root());
$photo->name = $name;
$photo->save();
} catch (ORM_Validation_Exception $e) {
$this->assert_equal(array("name" => "illegal_data_file_extension"),
$e->validation->errors());
continue;
}
$this->assert_true(false, "Shouldn't get here");
}
}
2012-05-05 18:52:44 -07:00
public function cant_rename_to_illegal_extension_test() {
foreach (array("test.php.test", "test.php", "test.PHP",
"test.php5", "test.php4", "test.pl") as $name) {
try {
$photo = test::random_photo(item::root());
$photo->name = $name;
$photo->save();
} catch (ORM_Validation_Exception $e) {
$this->assert_equal(array("name" => "illegal_data_file_extension"),
$e->validation->errors());
continue;
}
$this->assert_true(false, "Shouldn't get here");
}
}
}