TreeBehaviorUuidTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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->order = null;
  64. $this->Tree->initialize(2, 2);
  65. $data = $this->Tree->find('first', array(
  66. 'fields' => array('id'),
  67. 'conditions' => array($modelClass . '.name' => '1.1')
  68. ));
  69. $id = String::uuid();
  70. $this->Tree->create();
  71. $result = $this->Tree->save(array($modelClass => array(
  72. 'id' => $id,
  73. 'name' => 'testAddMiddle',
  74. $parentField => $data[$modelClass]['id'])
  75. ));
  76. $expected = array_merge(
  77. array($modelClass => array('id' => $id, 'name' => 'testAddMiddle', $parentField => '2')),
  78. $result
  79. );
  80. $this->assertSame($expected, $result);
  81. $this->assertTrue($this->Tree->verify());
  82. }
  83. /**
  84. * testMovePromote method
  85. *
  86. * @return void
  87. */
  88. public function testMovePromote() {
  89. extract($this->settings);
  90. $this->Tree = new $modelClass();
  91. $this->Tree->order = null;
  92. $this->Tree->initialize(2, 2);
  93. $this->Tree->id = null;
  94. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  95. $parentId = $parent[$modelClass]['id'];
  96. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  97. $this->Tree->id = $data[$modelClass]['id'];
  98. $this->Tree->saveField($parentField, $parentId);
  99. $direct = $this->Tree->children($parentId, true, array('name', $leftField, $rightField));
  100. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
  101. array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
  102. array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
  103. $this->assertEquals($direct, $expects);
  104. $validTree = $this->Tree->verify();
  105. $this->assertSame($validTree, true);
  106. }
  107. /**
  108. * testMoveWithWhitelist method
  109. *
  110. * @return void
  111. */
  112. public function testMoveWithWhitelist() {
  113. extract($this->settings);
  114. $this->Tree = new $modelClass();
  115. $this->Tree->order = null;
  116. $this->Tree->initialize(2, 2);
  117. $this->Tree->id = null;
  118. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  119. $parentId = $parent[$modelClass]['id'];
  120. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  121. $this->Tree->id = $data[$modelClass]['id'];
  122. $this->Tree->whitelist = array($parentField, 'name', 'description');
  123. $this->Tree->saveField($parentField, $parentId);
  124. $result = $this->Tree->children($parentId, true, array('name', $leftField, $rightField));
  125. $expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
  126. array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
  127. array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
  128. $this->assertEquals($expected, $result);
  129. $this->assertTrue($this->Tree->verify());
  130. }
  131. /**
  132. * testRemoveNoChildren method
  133. *
  134. * @return void
  135. */
  136. public function testRemoveNoChildren() {
  137. extract($this->settings);
  138. $this->Tree = new $modelClass();
  139. $this->Tree->order = null;
  140. $this->Tree->initialize(2, 2);
  141. $initialCount = $this->Tree->find('count');
  142. $result = $this->Tree->findByName('1.1.1');
  143. $this->Tree->removeFromTree($result[$modelClass]['id']);
  144. $laterCount = $this->Tree->find('count');
  145. $this->assertEquals($initialCount, $laterCount);
  146. $nodes = $this->Tree->find('list', array('order' => $leftField));
  147. $expects = array(
  148. '1. Root',
  149. '1.1',
  150. '1.1.2',
  151. '1.2',
  152. '1.2.1',
  153. '1.2.2',
  154. '1.1.1',
  155. );
  156. $this->assertEquals(array_values($nodes), $expects);
  157. $validTree = $this->Tree->verify();
  158. $this->assertSame($validTree, true);
  159. }
  160. /**
  161. * testRemoveAndDeleteNoChildren method
  162. *
  163. * @return void
  164. */
  165. public function testRemoveAndDeleteNoChildren() {
  166. extract($this->settings);
  167. $this->Tree = new $modelClass();
  168. $this->Tree->order = null;
  169. $this->Tree->initialize(2, 2);
  170. $initialCount = $this->Tree->find('count');
  171. $result = $this->Tree->findByName('1.1.1');
  172. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  173. $laterCount = $this->Tree->find('count');
  174. $this->assertEquals($initialCount - 1, $laterCount);
  175. $nodes = $this->Tree->find('list', array('order' => $leftField));
  176. $expects = array(
  177. '1. Root',
  178. '1.1',
  179. '1.1.2',
  180. '1.2',
  181. '1.2.1',
  182. '1.2.2',
  183. );
  184. $this->assertEquals(array_values($nodes), $expects);
  185. $validTree = $this->Tree->verify();
  186. $this->assertSame($validTree, true);
  187. }
  188. /**
  189. * testChildren method
  190. *
  191. * @return void
  192. */
  193. public function testChildren() {
  194. extract($this->settings);
  195. $this->Tree = new $modelClass();
  196. $this->Tree->order = null;
  197. $this->Tree->initialize(2, 2);
  198. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  199. $this->Tree->id = $data[$modelClass]['id'];
  200. $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
  201. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  202. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
  203. $this->assertEquals($direct, $expects);
  204. $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
  205. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  206. array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
  207. array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
  208. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
  209. array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
  210. array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12)));
  211. $this->assertEquals($total, $expects);
  212. }
  213. /**
  214. * testNoAmbiguousColumn method
  215. *
  216. * @return void
  217. */
  218. public function testNoAmbiguousColumn() {
  219. extract($this->settings);
  220. $this->Tree = new $modelClass();
  221. $this->Tree->order = null;
  222. $this->Tree->initialize(2, 2);
  223. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  224. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  225. $data = $this->Tree->find('first', array(
  226. 'conditions' => array($modelClass . '.name' => '1. Root'),
  227. 'recursive' => -1
  228. ));
  229. $this->Tree->id = $data[$modelClass]['id'];
  230. $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
  231. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  232. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
  233. $this->assertEquals($direct, $expects);
  234. $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
  235. $expects = array(
  236. array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  237. array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
  238. array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
  239. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
  240. array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
  241. array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12))
  242. );
  243. $this->assertEquals($total, $expects);
  244. }
  245. /**
  246. * testGenerateTreeListWithSelfJoin method
  247. *
  248. * @return void
  249. */
  250. public function testGenerateTreeListWithSelfJoin() {
  251. extract($this->settings);
  252. $this->Tree = new $modelClass();
  253. $this->Tree->order = null;
  254. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  255. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  256. $this->Tree->initialize(2, 2);
  257. $result = $this->Tree->generateTreeList();
  258. $expected = array('1. Root', '_1.1', '__1.1.1', '__1.1.2', '_1.2', '__1.2.1', '__1.2.2');
  259. $this->assertSame(array_values($result), $expected);
  260. }
  261. }