/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorUuidTest.php

https://github.com/masihnewbie/cakephp · PHP · 255 lines · 147 code · 34 blank · 74 comment · 0 complexity · 74471717f767dab59df7acc476b7bf3c MD5 · raw file

  1. <?php
  2. /**
  3. * TreeBehaviorUuidTest file
  4. *
  5. * Tree test using UUIDs
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  10. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice
  14. *
  15. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  17. * @package Cake.Test.Case.Model.Behavior
  18. * @since CakePHP(tm) v 1.2.0.5330
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Model', 'Model');
  22. App::uses('AppModel', 'Model');
  23. require_once(dirname(dirname(__FILE__)) . DS . 'models.php');
  24. /**
  25. * TreeBehaviorUuidTest class
  26. *
  27. * @package Cake.Test.Case.Model.Behavior
  28. */
  29. class TreeBehaviorUuidTest extends CakeTestCase {
  30. /**
  31. * Whether backup global state for each test method or not
  32. *
  33. * @var bool false
  34. */
  35. public $backupGlobals = false;
  36. /**
  37. * settings property
  38. *
  39. * @var array
  40. */
  41. public $settings = array(
  42. 'modelClass' => 'UuidTree',
  43. 'leftField' => 'lft',
  44. 'rightField' => 'rght',
  45. 'parentField' => 'parent_id'
  46. );
  47. /**
  48. * fixtures property
  49. *
  50. * @var array
  51. */
  52. public $fixtures = array('core.uuid_tree');
  53. /**
  54. * testMovePromote method
  55. *
  56. * @return void
  57. */
  58. public function testMovePromote() {
  59. extract($this->settings);
  60. $this->Tree = new $modelClass();
  61. $this->Tree->initialize(2, 2);
  62. $this->Tree->id = null;
  63. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  64. $parent_id = $parent[$modelClass]['id'];
  65. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  66. $this->Tree->id= $data[$modelClass]['id'];
  67. $this->Tree->saveField($parentField, $parent_id);
  68. $direct = $this->Tree->children($parent_id, true, array('name', $leftField, $rightField));
  69. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
  70. array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
  71. array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
  72. $this->assertEqual($direct, $expects);
  73. $validTree = $this->Tree->verify();
  74. $this->assertIdentical($validTree, true);
  75. }
  76. /**
  77. * testMoveWithWhitelist method
  78. *
  79. * @return void
  80. */
  81. public function testMoveWithWhitelist() {
  82. extract($this->settings);
  83. $this->Tree = new $modelClass();
  84. $this->Tree->initialize(2, 2);
  85. $this->Tree->id = null;
  86. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  87. $parent_id = $parent[$modelClass]['id'];
  88. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  89. $this->Tree->id = $data[$modelClass]['id'];
  90. $this->Tree->whitelist = array($parentField, 'name', 'description');
  91. $this->Tree->saveField($parentField, $parent_id);
  92. $result = $this->Tree->children($parent_id, true, array('name', $leftField, $rightField));
  93. $expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
  94. array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
  95. array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
  96. $this->assertEqual($expected, $result);
  97. $this->assertTrue($this->Tree->verify());
  98. }
  99. /**
  100. * testRemoveNoChildren method
  101. *
  102. * @return void
  103. */
  104. public function testRemoveNoChildren() {
  105. extract($this->settings);
  106. $this->Tree = new $modelClass();
  107. $this->Tree->initialize(2, 2);
  108. $initialCount = $this->Tree->find('count');
  109. $result = $this->Tree->findByName('1.1.1');
  110. $this->Tree->removeFromTree($result[$modelClass]['id']);
  111. $laterCount = $this->Tree->find('count');
  112. $this->assertEqual($initialCount, $laterCount);
  113. $nodes = $this->Tree->find('list', array('order' => $leftField));
  114. $expects = array(
  115. '1. Root',
  116. '1.1',
  117. '1.1.2',
  118. '1.2',
  119. '1.2.1',
  120. '1.2.2',
  121. '1.1.1',
  122. );
  123. $this->assertEqual(array_values($nodes), $expects);
  124. $validTree = $this->Tree->verify();
  125. $this->assertIdentical($validTree, true);
  126. }
  127. /**
  128. * testRemoveAndDeleteNoChildren method
  129. *
  130. * @return void
  131. */
  132. public function testRemoveAndDeleteNoChildren() {
  133. extract($this->settings);
  134. $this->Tree = new $modelClass();
  135. $this->Tree->initialize(2, 2);
  136. $initialCount = $this->Tree->find('count');
  137. $result = $this->Tree->findByName('1.1.1');
  138. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  139. $laterCount = $this->Tree->find('count');
  140. $this->assertEqual($initialCount - 1, $laterCount);
  141. $nodes = $this->Tree->find('list', array('order' => $leftField));
  142. $expects = array(
  143. '1. Root',
  144. '1.1',
  145. '1.1.2',
  146. '1.2',
  147. '1.2.1',
  148. '1.2.2',
  149. );
  150. $this->assertEqual(array_values($nodes), $expects);
  151. $validTree = $this->Tree->verify();
  152. $this->assertIdentical($validTree, true);
  153. }
  154. /**
  155. * testChildren method
  156. *
  157. * @return void
  158. */
  159. public function testChildren() {
  160. extract($this->settings);
  161. $this->Tree = new $modelClass();
  162. $this->Tree->initialize(2, 2);
  163. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  164. $this->Tree->id = $data[$modelClass]['id'];
  165. $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
  166. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  167. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
  168. $this->assertEqual($direct, $expects);
  169. $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
  170. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  171. array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
  172. array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
  173. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
  174. array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
  175. array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12)));
  176. $this->assertEqual($total, $expects);
  177. }
  178. /**
  179. * testNoAmbiguousColumn method
  180. *
  181. * @return void
  182. */
  183. public function testNoAmbiguousColumn() {
  184. extract($this->settings);
  185. $this->Tree = new $modelClass();
  186. $this->Tree->initialize(2, 2);
  187. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  188. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  189. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  190. $this->Tree->id = $data[$modelClass]['id'];
  191. $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
  192. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  193. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
  194. $this->assertEqual($direct, $expects);
  195. $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
  196. $expects = array(
  197. array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  198. array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
  199. array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
  200. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
  201. array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
  202. array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12))
  203. );
  204. $this->assertEqual($total, $expects);
  205. }
  206. /**
  207. * testGenerateTreeListWithSelfJoin method
  208. *
  209. * @return void
  210. */
  211. public function testGenerateTreeListWithSelfJoin() {
  212. extract($this->settings);
  213. $this->Tree = new $modelClass();
  214. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  215. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  216. $this->Tree->initialize(2, 2);
  217. $result = $this->Tree->generateTreeList();
  218. $expected = array('1. Root', '_1.1', '__1.1.1', '__1.1.2', '_1.2', '__1.2.1', '__1.2.2');
  219. $this->assertIdentical(array_values($result), $expected);
  220. }
  221. }