diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/libraries/ORM_MPTT.php | 10 | ||||
-rw-r--r-- | core/models/item.php | 11 | ||||
-rw-r--r-- | core/tests/ORM_MPTT_Test.php | 78 |
3 files changed, 85 insertions, 14 deletions
diff --git a/core/libraries/ORM_MPTT.php b/core/libraries/ORM_MPTT.php index d7e8d7b7..2cdc617d 100644 --- a/core/libraries/ORM_MPTT.php +++ b/core/libraries/ORM_MPTT.php @@ -73,6 +73,14 @@ class ORM_MPTT_Core extends ORM { } /** + * Deleted this node and adjust the left and right pointers + */ + public function delete() { + parent::delete(); + $this->_grow(-1); + } + + /** * Return the parent of this node * * @return ORM @@ -194,7 +202,7 @@ class ORM_MPTT_Core extends ORM { * * @param integer $count the number of new nodes to add */ - private function _grow($count=1) { + public function _grow($count=1) { $size = $count * 2; $this->db->query( "UPDATE `{$this->table_name}` SET `left` = `left` + $size WHERE `left` >= {$this->right}"); diff --git a/core/models/item.php b/core/models/item.php index 0f6e16e8..fbaca0e7 100644 --- a/core/models/item.php +++ b/core/models/item.php @@ -42,10 +42,13 @@ class Item_Model extends ORM_MPTT { public function delete() { $path = $this->file_path(); parent::delete(); - if ($this->is_album()) { - dir::unlink($path); - } else { - unlink($path); + // If there is no name, the path is invalid so don't try to delete + if (!empty($this->name)) { + if ($this->is_album()) { + dir::unlink($path); + } else { + unlink($path); + } } } diff --git a/core/tests/ORM_MPTT_Test.php b/core/tests/ORM_MPTT_Test.php index 2b334243..34a6ed9d 100644 --- a/core/tests/ORM_MPTT_Test.php +++ b/core/tests/ORM_MPTT_Test.php @@ -97,15 +97,15 @@ class ORM_MPTT_Test extends Unit_Test_Case { public function descendant_test() { $parent = album::create(1, "parent album", "parent album title"); - photo::create($parent->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", + photo::create($parent->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", "photo1", "parent album photo"); $album1 = album::create($parent->id, "album1", "album1 title"); - photo::create($album1->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", + photo::create($album1->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", "photo1", "album1 photo"); $album2 = album::create($parent->id, "album2", "album2 title"); - photo::create($album2->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo2", + photo::create($album2->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo2", "photo2", "album2 photo"); $parent->reload(); @@ -116,15 +116,15 @@ class ORM_MPTT_Test extends Unit_Test_Case { public function descendant_limit_test() { $parent = album::create(1, "parent album", "parent album title"); - photo::create($parent->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", + photo::create($parent->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", "photo1", "parent album photo"); $album1 = album::create($parent->id, "album1", "album1 title"); - photo::create($album1->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", + photo::create($album1->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", "photo1", "album1 photo"); $album2 = album::create($parent->id, "album2", "album2 title"); - photo::create($album2->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo2", + photo::create($album2->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo2", "photo2", "album2 photo"); $parent->reload(); @@ -133,15 +133,15 @@ class ORM_MPTT_Test extends Unit_Test_Case { public function descendant_count_test() { $parent = album::create(1, "parent album", "parent album title"); - photo::create($parent->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", + photo::create($parent->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", "photo1", "parent album photo"); $album1 = album::create($parent->id, "album1", "album1 title"); - photo::create($album1->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", + photo::create($album1->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo1", "photo1", "album1 photo"); $album2 = album::create($parent->id, "album2", "album2 title"); - photo::create($album2->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo2", + photo::create($album2->id, DOCROOT . "themes/default/images/thumbnail.jpg", "photo2", "photo2", "album2 photo"); $parent->reload(); @@ -149,4 +149,64 @@ class ORM_MPTT_Test extends Unit_Test_Case { $this->assert_equal(3, $parent->descendants_count("photo")); $this->assert_equal(2, $parent->descendants_count("album")); } + + public function grow_test() { + $parent = ORM::factory("item", 1); + + $outer = ORM::factory("item"); + $outer->add_to_parent(1); + + $inner1 = ORM::factory("item"); + $inner1->add_to_parent($outer->id); + + $inner2 = ORM::factory("item"); + $inner2->add_to_parent($outer->id); + + $parent->reload(); + $original_parent_right = $parent->right; + $original_right = $outer->right; + + $outer->_grow(2); + + $parent->reload(); + $this->assert_equal($original_right + 2, $outer->right); + $this->assert_equal($original_parent_right + 4, $parent->right); + } + + public function contract_test() { + $parent = ORM::factory("item", 1); + $parent->reload(); + Kohana::log("debug", "original parent->right: " . $parent->right); + + $outer = ORM::factory("item"); + $outer->add_to_parent(1); + + $inner1 = ORM::factory("item"); + $inner1->add_to_parent($outer->id); + + $inner2 = ORM::factory("item"); + $inner2->add_to_parent($outer->id); + + $outer->reload(); + $parent->reload(); + $original_parent_right = $parent->right; + $original_right = $outer->right; + + $inner3 = ORM::factory("item"); + $inner3->add_to_parent($outer->id); + + $inner4 = ORM::factory("item"); + $inner4->add_to_parent($outer->id); + + + $inner2->delete(); + $inner4->delete(); + + $outer->_grow(-2); + + $outer->reload(); + $parent->reload(); + $this->assert_equal($original_right, $outer->right); + $this->assert_equal($original_parent_right, $parent->right); + } } |