diff options
author | Bharat Mediratta <bharat@menalto.com> | 2008-12-07 08:46:44 +0000 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2008-12-07 08:46:44 +0000 |
commit | bf7ab8904aa9ccbb201b1ef9634a8b13834e5f6d (patch) | |
tree | d62ae674a2a81696420ef293b0a033605c84da02 | |
parent | e40b54a4689f1565e0f76c536d0d4b05b23afc1e (diff) |
Change ORM_MPTT::add_to_parent() to take an ORM instead of an id so
that it's consistent with ORM_MPTT::move_to()
-rw-r--r-- | core/helpers/album.php | 7 | ||||
-rw-r--r-- | core/helpers/photo.php | 9 | ||||
-rw-r--r-- | core/libraries/ORM_MPTT.php | 3 | ||||
-rw-r--r-- | core/tests/Access_Helper_Test.php | 6 | ||||
-rw-r--r-- | core/tests/ORM_MPTT_Test.php | 84 |
5 files changed, 67 insertions, 42 deletions
diff --git a/core/helpers/album.php b/core/helpers/album.php index 3e3b6054..83bbfbab 100644 --- a/core/helpers/album.php +++ b/core/helpers/album.php @@ -47,7 +47,12 @@ class album_Core { $album->name = "{$name}-" . rand(); } - $album = $album->add_to_parent($parent_id); + $parent = ORM::factory("item", $parent_id); + if (!$parent->loaded) { + throw new Exception("@todo INVALID_PARENT_ID"); + } + + $album = $album->add_to_parent($parent); mkdir($album->file_path()); $thumbnail_dir = dirname($album->thumbnail_path()); if (!file_exists($thumbnail_dir)) { diff --git a/core/helpers/photo.php b/core/helpers/photo.php index 6d9887a4..0c098c84 100644 --- a/core/helpers/photo.php +++ b/core/helpers/photo.php @@ -69,7 +69,12 @@ class photo_Core { } // This saves the photo - $photo->add_to_parent($parent_id); + $parent = ORM::factory("item", $parent_id); + if (!$parent->loaded) { + throw new Exception("@todo INVALID_PARENT_ID"); + } + + $photo->add_to_parent($parent); copy($filename, $photo->file_path()); // @todo: parameterize these dimensions @@ -84,7 +89,7 @@ class photo_Core { } static function get_add_form($parent) { - $form = new Forge("albums/{$parent->id}", "", "post", + $form = new Forge("albums/{$parent->id}", "", "post", array("id" => "gAddPhotoForm", "enctype" => "multipart/form-data")); $group = $form->group(sprintf(_("Add Photo to %s"), $parent->title)); $group->input("name")->label(true); diff --git a/core/libraries/ORM_MPTT.php b/core/libraries/ORM_MPTT.php index c7cc5978..61c67b99 100644 --- a/core/libraries/ORM_MPTT.php +++ b/core/libraries/ORM_MPTT.php @@ -52,12 +52,11 @@ class ORM_MPTT_Core extends ORM { * @param integer $parent_id the id of the parent node * @return ORM */ - function add_to_parent($parent_id) { + function add_to_parent($parent) { $this->lock(); try { // Make a hole in the parent for this new item - $parent = ORM::factory($this->model_name, $parent_id); $this->db->query( "UPDATE `{$this->table_name}` SET `left` = `left` + 2 WHERE `left` >= {$parent->right}"); $this->db->query( diff --git a/core/tests/Access_Helper_Test.php b/core/tests/Access_Helper_Test.php index 0f453f15..c924575d 100644 --- a/core/tests/Access_Helper_Test.php +++ b/core/tests/Access_Helper_Test.php @@ -50,7 +50,8 @@ class Access_Helper_Test extends Unit_Test_Case { } public function adding_and_removing_items_adds_ands_removes_rows_test() { - $item = ORM::factory("item")->add_to_parent(1); + $root = ORM::factory("item", 1); + $item = ORM::factory("item")->add_to_parent($root); // Simulate an event access::add_item($item); @@ -70,7 +71,8 @@ class Access_Helper_Test extends Unit_Test_Case { } public function can_allow_deny_and_reset_intent_test() { - $item = ORM::factory("item")->add_to_parent(1); + $root = ORM::factory("item", 1); + $item = ORM::factory("item")->add_to_parent($root); access::add_item($item); $intent = ORM::factory("access_intent")->where("item_id", $item->id)->find(); diff --git a/core/tests/ORM_MPTT_Test.php b/core/tests/ORM_MPTT_Test.php index 03230ad9..b969e287 100644 --- a/core/tests/ORM_MPTT_Test.php +++ b/core/tests/ORM_MPTT_Test.php @@ -19,7 +19,8 @@ */ class ORM_MPTT_Test extends Unit_Test_Case { public function add_to_parent_test() { - $album = ORM::factory("item")->add_to_parent(1); + $root = ORM::factory("item", 1); + $album = ORM::factory("item")->add_to_parent($root); $this->assert_equal($album->parent()->right - 2, $album->left); $this->assert_equal($album->parent()->right - 1, $album->right); @@ -28,11 +29,12 @@ class ORM_MPTT_Test extends Unit_Test_Case { } public function add_hierarchy_test() { - $album1 = ORM::factory("item")->add_to_parent(1); - $album1_1 = ORM::factory("item")->add_to_parent($album1->id); - $album1_2 = ORM::factory("item")->add_to_parent($album1->id); - $album1_1_1 = ORM::factory("item")->add_to_parent($album1_1->id); - $album1_1_2 = ORM::factory("item")->add_to_parent($album1_1->id); + $root = ORM::factory("item", 1); + $album1 = ORM::factory("item")->add_to_parent($root); + $album1_1 = ORM::factory("item")->add_to_parent($album1); + $album1_2 = ORM::factory("item")->add_to_parent($album1); + $album1_1_1 = ORM::factory("item")->add_to_parent($album1_1); + $album1_1_2 = ORM::factory("item")->add_to_parent($album1_1); $album1->reload(); $this->assert_equal(9, $album1->right - $album1->left); @@ -42,11 +44,12 @@ class ORM_MPTT_Test extends Unit_Test_Case { } public function delete_hierarchy_test() { - $album1 = ORM::factory("item")->add_to_parent(1); - $album1_1 = ORM::factory("item")->add_to_parent($album1->id); - $album1_2 = ORM::factory("item")->add_to_parent($album1->id); - $album1_1_1 = ORM::factory("item")->add_to_parent($album1_1->id); - $album1_1_2 = ORM::factory("item")->add_to_parent($album1_1->id); + $root = ORM::factory("item", 1); + $album1 = ORM::factory("item")->add_to_parent($root); + $album1_1 = ORM::factory("item")->add_to_parent($album1); + $album1_2 = ORM::factory("item")->add_to_parent($album1); + $album1_1_1 = ORM::factory("item")->add_to_parent($album1_1); + $album1_1_2 = ORM::factory("item")->add_to_parent($album1_1); $album1_1->delete(); $album1->reload(); @@ -83,15 +86,17 @@ class ORM_MPTT_Test extends Unit_Test_Case { } public function parent_test() { - $album = ORM::factory("item")->add_to_parent(1); + $root = ORM::factory("item", 1); + $album = ORM::factory("item")->add_to_parent($root); $parent = ORM::factory("item", 1); $this->assert_equal($parent->id, $album->parent()->id); } public function parents_test() { - $outer = ORM::factory("item")->add_to_parent(1); - $inner = ORM::factory("item")->add_to_parent($outer->id); + $root = ORM::factory("item", 1); + $outer = ORM::factory("item")->add_to_parent($root); + $inner = ORM::factory("item")->add_to_parent($outer); $parent_ids = array(); foreach ($inner->parents() as $parent) { @@ -101,9 +106,10 @@ class ORM_MPTT_Test extends Unit_Test_Case { } public function children_test() { - $outer = ORM::factory("item")->add_to_parent(1); - $inner1 = ORM::factory("item")->add_to_parent($outer->id); - $inner2 = ORM::factory("item")->add_to_parent($outer->id); + $root = ORM::factory("item", 1); + $outer = ORM::factory("item")->add_to_parent($root); + $inner1 = ORM::factory("item")->add_to_parent($outer); + $inner2 = ORM::factory("item")->add_to_parent($outer); $child_ids = array(); foreach ($outer->children() as $child) { @@ -113,37 +119,41 @@ class ORM_MPTT_Test extends Unit_Test_Case { } public function children_limit_test() { - $outer = ORM::factory("item")->add_to_parent(1); - $inner1 = ORM::factory("item")->add_to_parent($outer->id); - $inner2 = ORM::factory("item")->add_to_parent($outer->id); + $root = ORM::factory("item", 1); + $outer = ORM::factory("item")->add_to_parent($root); + $inner1 = ORM::factory("item")->add_to_parent($outer); + $inner2 = ORM::factory("item")->add_to_parent($outer); $this->assert_equal(array($inner2->id => null), $outer->children(1, 1)->select_list('id')); } public function children_count_test() { - $outer = ORM::factory("item")->add_to_parent(1); - $inner1 = ORM::factory("item")->add_to_parent($outer->id); - $inner2 = ORM::factory("item")->add_to_parent($outer->id); + $root = ORM::factory("item", 1); + $outer = ORM::factory("item")->add_to_parent($root); + $inner1 = ORM::factory("item")->add_to_parent($outer); + $inner2 = ORM::factory("item")->add_to_parent($outer); $this->assert_equal(2, $outer->children_count()); } public function descendant_test() { + $root = ORM::factory("item", 1); + $parent = ORM::factory("item"); $parent->type = "album"; - $parent->add_to_parent(1); + $parent->add_to_parent($root); $photo = ORM::factory("item"); $photo->type = "photo"; - $photo->add_to_parent($parent->id); + $photo->add_to_parent($parent); $album1 = ORM::factory("item"); $album1->type = "album"; - $album1->add_to_parent($parent->id); + $album1->add_to_parent($parent); $photo1 = ORM::factory("item"); $photo1->type = "photo"; - $photo1->add_to_parent($album1->id); + $photo1->add_to_parent($album1); $parent->reload(); @@ -153,31 +163,35 @@ class ORM_MPTT_Test extends Unit_Test_Case { } public function descendant_limit_test() { - $parent = ORM::factory("item")->add_to_parent(1); - $album1 = ORM::factory("item")->add_to_parent($parent->id); - $album2 = ORM::factory("item")->add_to_parent($parent->id); - $album3 = ORM::factory("item")->add_to_parent($parent->id); + $root = ORM::factory("item", 1); + + $parent = ORM::factory("item")->add_to_parent($root); + $album1 = ORM::factory("item")->add_to_parent($parent); + $album2 = ORM::factory("item")->add_to_parent($parent); + $album3 = ORM::factory("item")->add_to_parent($parent); $parent->reload(); $this->assert_equal(2, $parent->descendants(2)->count()); } public function descendant_count_test() { + $root = ORM::factory("item", 1); + $parent = ORM::factory("item"); $parent->type = "album"; - $parent->add_to_parent(1); + $parent->add_to_parent($root); $photo = ORM::factory("item"); $photo->type = "photo"; - $photo->add_to_parent($parent->id); + $photo->add_to_parent($parent); $album1 = ORM::factory("item"); $album1->type = "album"; - $album1->add_to_parent($parent->id); + $album1->add_to_parent($parent); $photo1 = ORM::factory("item"); $photo1->type = "photo"; - $photo1->add_to_parent($album1->id); + $photo1->add_to_parent($album1); $parent->reload(); |