Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement item::find_by_relative_url with tests.

  • Loading branch information...
commit 98fd1e9957ff0d65d1bbb0eaa2df6c1e59487b25 1 parent c5ede58
@bharat bharat authored
View
26 modules/gallery/helpers/item.php
@@ -253,6 +253,32 @@ static function find_by_path($path) {
/**
+ * Locate an item using the URL. We assume that the url is in the form /a/b/c where each
+ * component matches up with an item slug. If there's no match, return an empty Item_Model
+ * NOTE: the caller is responsible for performing security checks on the resulting item.
+ * @param string $url the relative url fragment
+ * @return Item_Model
+ */
+ static function find_by_relative_url($relative_url) {
+ // In most cases, we'll have an exact match in the relative_url_cache item field.
+ // but failing that, walk down the tree until we find it. The fallback code will fix caches
+ // as it goes, so it'll never be run frequently.
+ $item = ORM::factory("item")->where("relative_url_cache", "=", $relative_url)->find();
+ if (!$item->loaded()) {
+ $segments = explode("/", $relative_url);
+ foreach (ORM::factory("item")
+ ->where("slug", "=", end($segments))
+ ->where("level", "=", count($segments) + 1)
+ ->find_all() as $match) {
+ if ($match->relative_url() == $relative_url) {
+ $item = $match;
+ }
+ }
+ }
+ return $item;
+ }
+
+ /**
* Return the root Item_Model
* @return Item_Model
*/
View
70 modules/gallery/tests/Item_Helper_Test.php
@@ -128,23 +128,19 @@ public function delete_cover_photo_picks_new_album_cover_test() {
public function find_by_path_test() {
$level1 = test::random_album();
- $level2 = test::random_album($level1);
+ $level2 = test::random_album_unsaved($level1);
+ $level2->name = "plus + space";
+ $level2->save()->reload();
+
$level3 = test::random_photo_unsaved($level2);
$level3->name = "same.jpg";
$level3->save()->reload();
$level2b = test::random_album($level1);
$level3b = test::random_photo_unsaved($level2b);
- $level3b->name = "has spaces+plusses.jpg";
+ $level3b->name = "same.jpg";
$level3b->save()->reload();
- // Make sure that some of the calls below use the fallback code.
- db::build()
- ->update("items")
- ->set(array("relative_url_cache" => null, "relative_path_cache" => null))
- ->where("id", "IN", array($level3->id, $level3b->id))
- ->execute();
-
// Item in album
$this->assert_same(
$level3->id,
@@ -163,7 +159,12 @@ public function find_by_path_test() {
// Return root if "" is passed
$this->assert_same(item::root()->id, item::find_by_path("")->id);
- // Verify that we don't get confused by the part names
+ // Verify that we don't get confused by the part names, using the fallback code.
+ db::build()
+ ->update("items")
+ ->set(array("relative_path_cache" => null))
+ ->where("id", "IN", array($level3->id, $level3b->id))
+ ->execute();
$this->assert_same(
$level3->id,
item::find_by_path("{$level1->name}/{$level2->name}/{$level3->name}")->id);
@@ -181,4 +182,53 @@ public function find_by_path_test() {
$level3b->id,
item::find_by_path("{$level1->name}/{$level2b->name}/{$level3b->name}")->id);
}
+
+ public function find_by_relative_url_test() {
+ $level1 = test::random_album();
+ $level2 = test::random_album($level1);
+ $level3 = test::random_photo_unsaved($level2);
+ $level3->slug = "same";
+ $level3->save()->reload();
+
+ $level2b = test::random_album($level1);
+ $level3b = test::random_photo_unsaved($level2b);
+ $level3b->slug = "same";
+ $level3b->save()->reload();
+
+ // Item in album
+ $this->assert_same(
+ $level3->id,
+ item::find_by_relative_url("{$level1->slug}/{$level2->slug}/{$level3->slug}")->id);
+
+ // Album, ends without a slash
+ $this->assert_same(
+ $level2->id,
+ item::find_by_relative_url("{$level1->slug}/{$level2->slug}")->id);
+
+ // Return root if "" is passed
+ $this->assert_same(item::root()->id, item::find_by_relative_url("")->id);
+
+ // Verify that we don't get confused by the part slugs, using the fallback code.
+ db::build()
+ ->update("items")
+ ->set(array("relative_url_cache" => null))
+ ->where("id", "IN", array($level3->id, $level3b->id))
+ ->execute();
+ $this->assert_same(
+ $level3->id,
+ item::find_by_relative_url("{$level1->slug}/{$level2->slug}/{$level3->slug}")->id);
+
+ $this->assert_same(
+ $level3b->id,
+ item::find_by_relative_url("{$level1->slug}/{$level2b->slug}/{$level3b->slug}")->id);
+
+ // Verify that we don't get false positives
+ $this->assert_false(
+ item::find_by_relative_url("foo/bar/baz")->loaded());
+
+ // Verify that the fallback code works
+ $this->assert_same(
+ $level3b->id,
+ item::find_by_relative_url("{$level1->slug}/{$level2b->slug}/{$level3b->slug}")->id);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.