TreeBehaviorUuidTest.php 9.5 KB

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