Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Packaging + tests of Bharat's find_by_path routine.

  • Loading branch information...
commit 48640005a4edac955d9087f62fed1ab5f756b686 1 parent 612ddd7
@kandsten kandsten authored bharat committed
View
25 modules/gallery/helpers/item.php
@@ -208,7 +208,30 @@ static function viewable($model) {
return $model;
}
-
+
+ static function find_by_path($path) {
+ $path = trim($path, '/');
+
+ // The root path name is NULL, not '', hence this workaround.
+ if ($path == '') {
+ return ORM::factory("item", 1);
+ }
+
+ $paths = explode("/", $path);
+ $count = count($paths);
+ foreach (ORM::factory("item")
+ ->where('name', '=', $paths[$count - 1])
+ ->where('level', '=', $count + 1)
+ ->find_all() as $item) {
+ if (urldecode($item->relative_path()) == $path) {
+ return $item;
+ }
+ }
+
+ return false;
+ }
+
+
/**
* Return the root Item_Model
* @return Item_Model
View
48 modules/gallery/tests/Item_Helper_Test.php
@@ -125,4 +125,52 @@ public function delete_cover_photo_picks_new_album_cover_test() {
$this->assert_same($photo2->id, $album->album_cover_item_id);
$this->assert_same($photo2->id, $parent->album_cover_item_id);
}
+
+ public function find_by_path_does_the_right_thing_test() {
+ $level1 = test::random_album();
+ $level2 = test::random_album($level1);
+ $level3 = test::random_photo($level2);
+ $level3->name = 'same.jpg';
+ $level3->save();
+
+ $level2b = test::random_album($level1);
+ $level3b = test::random_photo($level2b);
+ $level3b->name = 'same.jpg';
+ $level3b->save();
+
+ // Item in album
+ $this->assert_same(
+ item::find_by_path('/' . $level1->name . '/' . $level2->name . '/' . $level3->name)->id,
+ $level3->id);
+
+ // Album, ends with a slash
+ $this->assert_same(
+ item::find_by_path($level1->name . '/' . $level2->name . '/')->id,
+ $level2->id);
+
+ // Album, ends without a slash
+ $this->assert_same(
+ item::find_by_path('/' . $level1->name . '/' . $level2->name)->id,
+ $level2->id);
+
+ // Return root if '' is passed
+ $this->assert_same(
+ item::find_by_path('')->id,
+ "1");
+
+ // Verify that we don't get confused by the part names
+ $this->assert_same(
+ item::find_by_path($level1->name . '/' . $level2->name . '/' . $level3->name)->id,
+ $level3->id);
+
+ $this->assert_same(
+ item::find_by_path($level1->name . '/' . $level2b->name . '/' . $level3b->name)->id,
+ $level3b->id);
+
+ // Verify that we don't get false positives
+ $this->assert_same(
+ item::find_by_path('foo/bar/baz'),
+ false);
+
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.