TreeBehaviorUuidTest.php 9.2 KB

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