PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/gallery/tests/ORM_MPTT_Test.php

https://github.com/ShakeNBake/gallery3
PHP | 234 lines | 169 code | 45 blank | 20 comment | 0 complexity | 143bd880cd03027877c6d8c00c63502d MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2009 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. class ORM_MPTT_Test extends Unit_Test_Case {
  21. private function create_item_and_add_to_parent($parent) {
  22. $album = album::create($parent, rand(), "test album");
  23. return $album;
  24. }
  25. public function add_to_parent_test() {
  26. $root = ORM::factory("item", 1);
  27. $album = ORM::factory("item");
  28. $album->type = "album";
  29. $album->rand_key = ((float)mt_rand()) / (float)mt_getrandmax();
  30. $album->sort_column = "weight";
  31. $album->sort_order = "ASC";
  32. $album->add_to_parent($root);
  33. $this->assert_equal($album->parent()->right_ptr - 2, $album->left_ptr);
  34. $this->assert_equal($album->parent()->right_ptr - 1, $album->right_ptr);
  35. $this->assert_equal($album->parent()->level + 1, $album->level);
  36. $this->assert_equal($album->parent()->id, $album->parent_id);
  37. }
  38. public function add_hierarchy_test() {
  39. $root = ORM::factory("item", 1);
  40. $album1 = self::create_item_and_add_to_parent($root);
  41. $album1_1 = self::create_item_and_add_to_parent($album1);
  42. $album1_2 = self::create_item_and_add_to_parent($album1);
  43. $album1_1_1 = self::create_item_and_add_to_parent($album1_1);
  44. $album1_1_2 = self::create_item_and_add_to_parent($album1_1);
  45. $album1->reload();
  46. $this->assert_equal(9, $album1->right_ptr - $album1->left_ptr);
  47. $album1_1->reload();
  48. $this->assert_equal(5, $album1_1->right_ptr - $album1_1->left_ptr);
  49. }
  50. public function delete_hierarchy_test() {
  51. $root = ORM::factory("item", 1);
  52. $album1 = self::create_item_and_add_to_parent($root);
  53. $album1_1 = self::create_item_and_add_to_parent($album1);
  54. $album1_2 = self::create_item_and_add_to_parent($album1);
  55. $album1_1_1 = self::create_item_and_add_to_parent($album1_1);
  56. $album1_1_2 = self::create_item_and_add_to_parent($album1_1);
  57. $album1_1->delete();
  58. $album1->reload();
  59. // Now album1 contains only album1_2
  60. $this->assert_equal(3, $album1->right_ptr - $album1->left_ptr);
  61. }
  62. public function move_to_test() {
  63. $root = ORM::factory("item", 1);
  64. $album1 = album::create($root, "move_to_test_1", "move_to_test_1");
  65. $album1_1 = album::create($album1, "move_to_test_1_1", "move_to_test_1_1");
  66. $album1_2 = album::create($album1, "move_to_test_1_2", "move_to_test_1_2");
  67. $album1_1_1 = album::create($album1_1, "move_to_test_1_1_1", "move_to_test_1_1_1");
  68. $album1_1_2 = album::create($album1_1, "move_to_test_1_1_2", "move_to_test_1_1_2");
  69. $album1_2->reload();
  70. $album1_1_1->reload();
  71. $album1_1_1->move_to($album1_2);
  72. $album1_1->reload();
  73. $album1_2->reload();
  74. $this->assert_equal(3, $album1_1->right_ptr - $album1_1->left_ptr);
  75. $this->assert_equal(3, $album1_2->right_ptr - $album1_2->left_ptr);
  76. $this->assert_equal(
  77. array($album1_1_2->id => "move_to_test_1_1_2"),
  78. $album1_1->children()->select_list());
  79. $this->assert_equal(
  80. array($album1_1_1->id => "move_to_test_1_1_1"),
  81. $album1_2->children()->select_list());
  82. }
  83. public function cant_move_parent_into_own_subtree_test() {
  84. $album1 = album::create(item::root(), "move_to_test", "move_to_test");
  85. $album2 = album::create($album1, "move_to_test", "move_to_test");
  86. $album3 = album::create($album2, "move_to_test", "move_to_test");
  87. try {
  88. $album1->move_to($album3);
  89. $self->assert_true(false, "We should be unable to move an item inside its own hierarchy");
  90. } catch (Exception $e) {
  91. // pass
  92. }
  93. }
  94. public function parent_test() {
  95. $root = ORM::factory("item", 1);
  96. $album = self::create_item_and_add_to_parent($root);
  97. $parent = ORM::factory("item", 1);
  98. $this->assert_equal($parent->id, $album->parent()->id);
  99. }
  100. public function parents_test() {
  101. $root = ORM::factory("item", 1);
  102. $outer = self::create_item_and_add_to_parent($root);
  103. $inner = self::create_item_and_add_to_parent($outer);
  104. $parent_ids = array();
  105. foreach ($inner->parents() as $parent) {
  106. $parent_ids[] = $parent->id;
  107. }
  108. $this->assert_equal(array(1, $outer->id), $parent_ids);
  109. }
  110. public function children_test() {
  111. $root = ORM::factory("item", 1);
  112. $outer = self::create_item_and_add_to_parent($root);
  113. $inner1 = self::create_item_and_add_to_parent($outer);
  114. $inner2 = self::create_item_and_add_to_parent($outer);
  115. $child_ids = array();
  116. foreach ($outer->children() as $child) {
  117. $child_ids[] = $child->id;
  118. }
  119. $this->assert_equal(array($inner1->id, $inner2->id), $child_ids);
  120. }
  121. public function children_limit_test() {
  122. $root = ORM::factory("item", 1);
  123. $outer = self::create_item_and_add_to_parent($root);
  124. $inner1 = self::create_item_and_add_to_parent($outer);
  125. $inner2 = self::create_item_and_add_to_parent($outer);
  126. $this->assert_equal(array($inner2->id => $inner2->name),
  127. $outer->children(1, 1)->select_list('id'));
  128. }
  129. public function children_count_test() {
  130. $root = ORM::factory("item", 1);
  131. $outer = self::create_item_and_add_to_parent($root);
  132. $inner1 = self::create_item_and_add_to_parent($outer);
  133. $inner2 = self::create_item_and_add_to_parent($outer);
  134. $this->assert_equal(2, $outer->children_count());
  135. }
  136. public function descendant_test() {
  137. $root = ORM::factory("item", 1);
  138. $parent = ORM::factory("item");
  139. $parent->type = "album";
  140. $parent->rand_key = ((float)mt_rand()) / (float)mt_getrandmax();
  141. $parent->sort_column = "weight";
  142. $parent->sort_order = "ASC";
  143. $parent->add_to_parent($root);
  144. $photo = ORM::factory("item");
  145. $photo->type = "photo";
  146. $photo->add_to_parent($parent);
  147. $album1 = ORM::factory("item");
  148. $album1->type = "album";
  149. $album1->rand_key = ((float)mt_rand()) / (float)mt_getrandmax();
  150. $album1->sort_column = "weight";
  151. $album1->sort_order = "ASC";
  152. $album1->add_to_parent($parent);
  153. $photo1 = ORM::factory("item");
  154. $photo1->type = "photo";
  155. $photo1->add_to_parent($album1);
  156. $parent->reload();
  157. $this->assert_equal(3, $parent->descendants()->count());
  158. $this->assert_equal(2, $parent->descendants(null, 0, array("type" => "photo"))->count());
  159. $this->assert_equal(1, $parent->descendants(null, 0, array("type" => "album"))->count());
  160. }
  161. public function descendant_limit_test() {
  162. $root = ORM::factory("item", 1);
  163. $parent = self::create_item_and_add_to_parent($root);
  164. $album1 = self::create_item_and_add_to_parent($parent);
  165. $album2 = self::create_item_and_add_to_parent($parent);
  166. $album3 = self::create_item_and_add_to_parent($parent);
  167. $parent->reload();
  168. $this->assert_equal(2, $parent->descendants(2)->count());
  169. }
  170. public function descendant_count_test() {
  171. $root = ORM::factory("item", 1);
  172. $parent = ORM::factory("item");
  173. $parent->type = "album";
  174. $parent->add_to_parent($root);
  175. $photo = ORM::factory("item");
  176. $photo->type = "photo";
  177. $photo->add_to_parent($parent);
  178. $album1 = ORM::factory("item");
  179. $album1->type = "album";
  180. $album1->add_to_parent($parent);
  181. $photo1 = ORM::factory("item");
  182. $photo1->type = "photo";
  183. $photo1->add_to_parent($album1);
  184. $parent->reload();
  185. $this->assert_equal(3, $parent->descendants_count());
  186. $this->assert_equal(2, $parent->descendants_count(array("type" => "photo")));
  187. $this->assert_equal(1, $parent->descendants_count(array("type" => "album")));
  188. }
  189. }