TreeBehaviorNumberTest.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  1. <?php
  2. /**
  3. * TreeBehaviorNumberTest file
  4. *
  5. * This is the basic Tree behavior test
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  10. * Copyright 2005-2012, 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-2012, 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. * TreeBehaviorNumberTest class
  26. *
  27. * @package Cake.Test.Case.Model.Behavior
  28. */
  29. class TreeBehaviorNumberTest 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' => 'NumberTree',
  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.number_tree', 'core.person');
  53. /**
  54. * testInitialize method
  55. *
  56. * @return void
  57. */
  58. public function testInitialize() {
  59. extract($this->settings);
  60. $this->Tree = new $modelClass();
  61. $this->Tree->initialize(2, 2);
  62. $result = $this->Tree->find('count');
  63. $this->assertEquals($result, 7);
  64. $validTree = $this->Tree->verify();
  65. $this->assertSame($validTree, true);
  66. }
  67. /**
  68. * testDetectInvalidLeft method
  69. *
  70. * @return void
  71. */
  72. public function testDetectInvalidLeft() {
  73. extract($this->settings);
  74. $this->Tree = new $modelClass();
  75. $this->Tree->initialize(2, 2);
  76. $result = $this->Tree->findByName('1.1');
  77. $save[$modelClass]['id'] = $result[$modelClass]['id'];
  78. $save[$modelClass][$leftField] = 0;
  79. $this->Tree->save($save);
  80. $result = $this->Tree->verify();
  81. $this->assertNotSame($result, true);
  82. $result = $this->Tree->recover();
  83. $this->assertSame($result, true);
  84. $result = $this->Tree->verify();
  85. $this->assertSame($result, true);
  86. }
  87. /**
  88. * testDetectInvalidRight method
  89. *
  90. * @return void
  91. */
  92. public function testDetectInvalidRight() {
  93. extract($this->settings);
  94. $this->Tree = new $modelClass();
  95. $this->Tree->initialize(2, 2);
  96. $result = $this->Tree->findByName('1.1');
  97. $save[$modelClass]['id'] = $result[$modelClass]['id'];
  98. $save[$modelClass][$rightField] = 0;
  99. $this->Tree->save($save);
  100. $result = $this->Tree->verify();
  101. $this->assertNotSame($result, true);
  102. $result = $this->Tree->recover();
  103. $this->assertSame($result, true);
  104. $result = $this->Tree->verify();
  105. $this->assertSame($result, true);
  106. }
  107. /**
  108. * testDetectInvalidParent method
  109. *
  110. * @return void
  111. */
  112. public function testDetectInvalidParent() {
  113. extract($this->settings);
  114. $this->Tree = new $modelClass();
  115. $this->Tree->initialize(2, 2);
  116. $result = $this->Tree->findByName('1.1');
  117. // Bypass behavior and any other logic
  118. $this->Tree->updateAll(array($parentField => null), array('id' => $result[$modelClass]['id']));
  119. $result = $this->Tree->verify();
  120. $this->assertNotSame($result, true);
  121. $result = $this->Tree->recover();
  122. $this->assertSame($result, true);
  123. $result = $this->Tree->verify();
  124. $this->assertSame($result, true);
  125. }
  126. /**
  127. * testDetectNoneExistentParent method
  128. *
  129. * @return void
  130. */
  131. public function testDetectNoneExistentParent() {
  132. extract($this->settings);
  133. $this->Tree = new $modelClass();
  134. $this->Tree->initialize(2, 2);
  135. $result = $this->Tree->findByName('1.1');
  136. $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
  137. $result = $this->Tree->verify();
  138. $this->assertNotSame($result, true);
  139. $result = $this->Tree->recover('MPTT');
  140. $this->assertSame($result, true);
  141. $result = $this->Tree->verify();
  142. $this->assertSame($result, true);
  143. }
  144. /**
  145. * testRecoverUsingParentMode method
  146. *
  147. * @return void
  148. */
  149. public function testRecoverUsingParentMode() {
  150. extract($this->settings);
  151. $this->Tree = new $modelClass();
  152. $this->Tree->Behaviors->disable('Tree');
  153. $this->Tree->save(array('parent_id' => null, 'name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0));
  154. $node1 = $this->Tree->id;
  155. $this->Tree->create();
  156. $this->Tree->save(array('parent_id' => null, 'name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0));
  157. $node11 = $this->Tree->id;
  158. $this->Tree->create();
  159. $this->Tree->save(array('parent_id' => null, 'name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0));
  160. $node12 = $this->Tree->id;
  161. $this->Tree->create();
  162. $this->Tree->save(array('parent_id' => null, 'name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0));
  163. $this->Tree->create();
  164. $this->Tree->save(array('parent_id' => null, 'name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0));
  165. $this->Tree->Behaviors->enable('Tree');
  166. $result = $this->Tree->verify();
  167. $this->assertNotSame($result, true);
  168. $result = $this->Tree->recover();
  169. $this->assertTrue($result);
  170. $result = $this->Tree->verify();
  171. $this->assertTrue($result);
  172. $result = $this->Tree->find('first', array(
  173. 'fields' => array('name', $parentField, $leftField, $rightField),
  174. 'conditions' => array('name' => 'Main'),
  175. 'recursive' => -1
  176. ));
  177. $expected = array(
  178. $modelClass => array(
  179. 'name' => 'Main',
  180. $parentField => null,
  181. $leftField => 1,
  182. $rightField => 10
  183. )
  184. );
  185. $this->assertEquals($expected, $result);
  186. }
  187. /**
  188. * testRecoverFromMissingParent method
  189. *
  190. * @return void
  191. */
  192. public function testRecoverFromMissingParent() {
  193. extract($this->settings);
  194. $this->Tree = new $modelClass();
  195. $this->Tree->initialize(2, 2);
  196. $result = $this->Tree->findByName('1.1');
  197. $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
  198. $result = $this->Tree->verify();
  199. $this->assertNotSame($result, true);
  200. $result = $this->Tree->recover();
  201. $this->assertSame($result, true);
  202. $result = $this->Tree->verify();
  203. $this->assertSame($result, true);
  204. }
  205. /**
  206. * testDetectInvalidParents method
  207. *
  208. * @return void
  209. */
  210. public function testDetectInvalidParents() {
  211. extract($this->settings);
  212. $this->Tree = new $modelClass();
  213. $this->Tree->initialize(2, 2);
  214. $this->Tree->updateAll(array($parentField => null));
  215. $result = $this->Tree->verify();
  216. $this->assertNotSame($result, true);
  217. $result = $this->Tree->recover();
  218. $this->assertSame($result, true);
  219. $result = $this->Tree->verify();
  220. $this->assertSame($result, true);
  221. }
  222. /**
  223. * testDetectInvalidLftsRghts method
  224. *
  225. * @return void
  226. */
  227. public function testDetectInvalidLftsRghts() {
  228. extract($this->settings);
  229. $this->Tree = new $modelClass();
  230. $this->Tree->initialize(2, 2);
  231. $this->Tree->updateAll(array($leftField => 0, $rightField => 0));
  232. $result = $this->Tree->verify();
  233. $this->assertNotSame($result, true);
  234. $this->Tree->recover();
  235. $result = $this->Tree->verify();
  236. $this->assertSame($result, true);
  237. }
  238. /**
  239. * Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
  240. *
  241. * @return void
  242. */
  243. public function testDetectEqualLftsRghts() {
  244. extract($this->settings);
  245. $this->Tree = new $modelClass();
  246. $this->Tree->initialize(1, 3);
  247. $result = $this->Tree->findByName('1.1');
  248. $this->Tree->updateAll(array($rightField => $result[$modelClass][$leftField]), array('id' => $result[$modelClass]['id']));
  249. $this->Tree->updateAll(array($leftField => $this->Tree->escapeField($leftField) . ' -1'),
  250. array($leftField . ' >' => $result[$modelClass][$leftField]));
  251. $this->Tree->updateAll(array($rightField => $this->Tree->escapeField($rightField) . ' -1'),
  252. array($rightField . ' >' => $result[$modelClass][$leftField]));
  253. $result = $this->Tree->verify();
  254. $this->assertNotSame($result, true);
  255. $result = $this->Tree->recover();
  256. $this->assertTrue($result);
  257. $result = $this->Tree->verify();
  258. $this->assertTrue($result);
  259. }
  260. /**
  261. * testAddOrphan method
  262. *
  263. * @return void
  264. */
  265. public function testAddOrphan() {
  266. extract($this->settings);
  267. $this->Tree = new $modelClass();
  268. $this->Tree->initialize(2, 2);
  269. $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
  270. $result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
  271. $expected = array($modelClass => array('name' => 'testAddOrphan', $parentField => null));
  272. $this->assertEquals($expected, $result);
  273. $validTree = $this->Tree->verify();
  274. $this->assertSame($validTree, true);
  275. }
  276. /**
  277. * testAddMiddle method
  278. *
  279. * @return void
  280. */
  281. public function testAddMiddle() {
  282. extract($this->settings);
  283. $this->Tree = new $modelClass();
  284. $this->Tree->initialize(2, 2);
  285. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  286. $initialCount = $this->Tree->find('count');
  287. $this->Tree->create();
  288. $result = $this->Tree->save(array($modelClass => array('name' => 'testAddMiddle', $parentField => $data[$modelClass]['id'])));
  289. $expected = array_merge(array($modelClass => array('name' => 'testAddMiddle', $parentField => '2')), $result);
  290. $this->assertSame($expected, $result);
  291. $laterCount = $this->Tree->find('count');
  292. $laterCount = $this->Tree->find('count');
  293. $this->assertEquals($initialCount + 1, $laterCount);
  294. $children = $this->Tree->children($data[$modelClass]['id'], true, array('name'));
  295. $expects = array(array($modelClass => array('name' => '1.1.1')),
  296. array($modelClass => array('name' => '1.1.2')),
  297. array($modelClass => array('name' => 'testAddMiddle')));
  298. $this->assertSame($children, $expects);
  299. $validTree = $this->Tree->verify();
  300. $this->assertSame($validTree, true);
  301. }
  302. /**
  303. * testAddInvalid method
  304. *
  305. * @return void
  306. */
  307. public function testAddInvalid() {
  308. extract($this->settings);
  309. $this->Tree = new $modelClass();
  310. $this->Tree->initialize(2, 2);
  311. $this->Tree->id = null;
  312. $initialCount = $this->Tree->find('count');
  313. //$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
  314. $saveSuccess = $this->Tree->save(array($modelClass => array('name' => 'testAddInvalid', $parentField => 99999)));
  315. $this->assertSame($saveSuccess, false);
  316. $laterCount = $this->Tree->find('count');
  317. $this->assertSame($initialCount, $laterCount);
  318. $validTree = $this->Tree->verify();
  319. $this->assertSame($validTree, true);
  320. }
  321. /**
  322. * testAddNotIndexedByModel method
  323. *
  324. * @return void
  325. */
  326. public function testAddNotIndexedByModel() {
  327. extract($this->settings);
  328. $this->Tree = new $modelClass();
  329. $this->Tree->initialize(2, 2);
  330. $this->Tree->save(array('name' => 'testAddNotIndexed', $parentField => null));
  331. $result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
  332. $expected = array($modelClass => array('name' => 'testAddNotIndexed', $parentField => null));
  333. $this->assertEquals($expected, $result);
  334. $validTree = $this->Tree->verify();
  335. $this->assertSame($validTree, true);
  336. }
  337. /**
  338. * testMovePromote method
  339. *
  340. * @return void
  341. */
  342. public function testMovePromote() {
  343. extract($this->settings);
  344. $this->Tree = new $modelClass();
  345. $this->Tree->initialize(2, 2);
  346. $this->Tree->id = null;
  347. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  348. $parentId = $parent[$modelClass]['id'];
  349. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  350. $this->Tree->id = $data[$modelClass]['id'];
  351. $this->Tree->saveField($parentField, $parentId);
  352. $direct = $this->Tree->children($parentId, true, array('id', 'name', $parentField, $leftField, $rightField));
  353. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
  354. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
  355. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
  356. $this->assertEquals($direct, $expects);
  357. $validTree = $this->Tree->verify();
  358. $this->assertSame($validTree, true);
  359. }
  360. /**
  361. * testMoveWithWhitelist method
  362. *
  363. * @return void
  364. */
  365. public function testMoveWithWhitelist() {
  366. extract($this->settings);
  367. $this->Tree = new $modelClass();
  368. $this->Tree->initialize(2, 2);
  369. $this->Tree->id = null;
  370. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  371. $parentId = $parent[$modelClass]['id'];
  372. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  373. $this->Tree->id = $data[$modelClass]['id'];
  374. $this->Tree->whitelist = array($parentField, 'name', 'description');
  375. $this->Tree->saveField($parentField, $parentId);
  376. $result = $this->Tree->children($parentId, true, array('id', 'name', $parentField, $leftField, $rightField));
  377. $expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
  378. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
  379. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
  380. $this->assertEquals($expected, $result);
  381. $this->assertTrue($this->Tree->verify());
  382. }
  383. /**
  384. * testInsertWithWhitelist method
  385. *
  386. * @return void
  387. */
  388. public function testInsertWithWhitelist() {
  389. extract($this->settings);
  390. $this->Tree = new $modelClass();
  391. $this->Tree->initialize(2, 2);
  392. $this->Tree->whitelist = array('name', $parentField);
  393. $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
  394. $result = $this->Tree->findByName('testAddOrphan', array('name', $parentField, $leftField, $rightField));
  395. $expected = array('name' => 'testAddOrphan', $parentField => null, $leftField => '15', $rightField => 16);
  396. $this->assertEquals($result[$modelClass], $expected);
  397. $this->assertSame($this->Tree->verify(), true);
  398. }
  399. /**
  400. * testMoveBefore method
  401. *
  402. * @return void
  403. */
  404. public function testMoveBefore() {
  405. extract($this->settings);
  406. $this->Tree = new $modelClass();
  407. $this->Tree->initialize(2, 2);
  408. $this->Tree->id = null;
  409. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
  410. $parentId = $parent[$modelClass]['id'];
  411. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
  412. $this->Tree->id = $data[$modelClass]['id'];
  413. $this->Tree->saveField($parentField, $parentId);
  414. $result = $this->Tree->children($parentId, true, array('name'));
  415. $expects = array(array($modelClass => array('name' => '1.1.1')),
  416. array($modelClass => array('name' => '1.1.2')),
  417. array($modelClass => array('name' => '1.2')));
  418. $this->assertEquals($result, $expects);
  419. $validTree = $this->Tree->verify();
  420. $this->assertSame($validTree, true);
  421. }
  422. /**
  423. * testMoveAfter method
  424. *
  425. * @return void
  426. */
  427. public function testMoveAfter() {
  428. extract($this->settings);
  429. $this->Tree = new $modelClass();
  430. $this->Tree->initialize(2, 2);
  431. $this->Tree->id = null;
  432. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
  433. $parentId = $parent[$modelClass]['id'];
  434. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  435. $this->Tree->id = $data[$modelClass]['id'];
  436. $this->Tree->saveField($parentField, $parentId);
  437. $result = $this->Tree->children($parentId, true, array('name'));
  438. $expects = array(array($modelClass => array('name' => '1.2.1')),
  439. array($modelClass => array('name' => '1.2.2')),
  440. array($modelClass => array('name' => '1.1')));
  441. $this->assertEquals($result, $expects);
  442. $validTree = $this->Tree->verify();
  443. $this->assertSame($validTree, true);
  444. }
  445. /**
  446. * testMoveDemoteInvalid method
  447. *
  448. * @return void
  449. */
  450. public function testMoveDemoteInvalid() {
  451. extract($this->settings);
  452. $this->Tree = new $modelClass();
  453. $this->Tree->initialize(2, 2);
  454. $this->Tree->id = null;
  455. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  456. $parentId = $parent[$modelClass]['id'];
  457. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  458. $expects = $this->Tree->find('all');
  459. $before = $this->Tree->read(null, $data[$modelClass]['id']);
  460. $this->Tree->id = $parentId;
  461. $this->Tree->saveField($parentField, $data[$modelClass]['id']);
  462. $results = $this->Tree->find('all');
  463. $after = $this->Tree->read(null, $data[$modelClass]['id']);
  464. $this->assertEquals($results, $expects);
  465. $this->assertEquals($before, $after);
  466. $validTree = $this->Tree->verify();
  467. $this->assertSame($validTree, true);
  468. }
  469. /**
  470. * testMoveInvalid method
  471. *
  472. * @return void
  473. */
  474. public function testMoveInvalid() {
  475. extract($this->settings);
  476. $this->Tree = new $modelClass();
  477. $this->Tree->initialize(2, 2);
  478. $this->Tree->id = null;
  479. $initialCount = $this->Tree->find('count');
  480. $data = $this->Tree->findByName('1.1');
  481. $this->Tree->id = $data[$modelClass]['id'];
  482. $this->Tree->saveField($parentField, 999999);
  483. $laterCount = $this->Tree->find('count');
  484. $this->assertSame($initialCount, $laterCount);
  485. $validTree = $this->Tree->verify();
  486. $this->assertSame($validTree, true);
  487. }
  488. /**
  489. * testMoveSelfInvalid method
  490. *
  491. * @return void
  492. */
  493. public function testMoveSelfInvalid() {
  494. extract($this->settings);
  495. $this->Tree = new $modelClass();
  496. $this->Tree->initialize(2, 2);
  497. $this->Tree->id = null;
  498. $initialCount = $this->Tree->find('count');
  499. $data = $this->Tree->findByName('1.1');
  500. $this->Tree->id = $data[$modelClass]['id'];
  501. $saveSuccess = $this->Tree->saveField($parentField, $this->Tree->id);
  502. $this->assertSame($saveSuccess, false);
  503. $laterCount = $this->Tree->find('count');
  504. $this->assertSame($initialCount, $laterCount);
  505. $validTree = $this->Tree->verify();
  506. $this->assertSame($validTree, true);
  507. }
  508. /**
  509. * testMoveUpSuccess method
  510. *
  511. * @return void
  512. */
  513. public function testMoveUpSuccess() {
  514. extract($this->settings);
  515. $this->Tree = new $modelClass();
  516. $this->Tree->initialize(2, 2);
  517. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
  518. $this->Tree->moveUp($data[$modelClass]['id']);
  519. $parent = $this->Tree->findByName('1. Root', array('id'));
  520. $this->Tree->id = $parent[$modelClass]['id'];
  521. $result = $this->Tree->children(null, true, array('name'));
  522. $expected = array(array($modelClass => array('name' => '1.2', )),
  523. array($modelClass => array('name' => '1.1', )));
  524. $this->assertSame($expected, $result);
  525. }
  526. /**
  527. * testMoveUpFail method
  528. *
  529. * @return void
  530. */
  531. public function testMoveUpFail() {
  532. extract($this->settings);
  533. $this->Tree = new $modelClass();
  534. $this->Tree->initialize(2, 2);
  535. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
  536. $this->Tree->moveUp($data[$modelClass]['id']);
  537. $parent = $this->Tree->findByName('1. Root', array('id'));
  538. $this->Tree->id = $parent[$modelClass]['id'];
  539. $result = $this->Tree->children(null, true, array('name'));
  540. $expected = array(array($modelClass => array('name' => '1.1', )),
  541. array($modelClass => array('name' => '1.2', )));
  542. $this->assertSame($expected, $result);
  543. }
  544. /**
  545. * testMoveUp2 method
  546. *
  547. * @return void
  548. */
  549. public function testMoveUp2() {
  550. extract($this->settings);
  551. $this->Tree = new $modelClass();
  552. $this->Tree->initialize(1, 10);
  553. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  554. $this->Tree->moveUp($data[$modelClass]['id'], 2);
  555. $parent = $this->Tree->findByName('1. Root', array('id'));
  556. $this->Tree->id = $parent[$modelClass]['id'];
  557. $result = $this->Tree->children(null, true, array('name'));
  558. $expected = array(
  559. array($modelClass => array('name' => '1.1', )),
  560. array($modelClass => array('name' => '1.2', )),
  561. array($modelClass => array('name' => '1.5', )),
  562. array($modelClass => array('name' => '1.3', )),
  563. array($modelClass => array('name' => '1.4', )),
  564. array($modelClass => array('name' => '1.6', )),
  565. array($modelClass => array('name' => '1.7', )),
  566. array($modelClass => array('name' => '1.8', )),
  567. array($modelClass => array('name' => '1.9', )),
  568. array($modelClass => array('name' => '1.10', )));
  569. $this->assertSame($expected, $result);
  570. }
  571. /**
  572. * testMoveUpFirst method
  573. *
  574. * @return void
  575. */
  576. public function testMoveUpFirst() {
  577. extract($this->settings);
  578. $this->Tree = new $modelClass();
  579. $this->Tree->initialize(1, 10);
  580. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  581. $this->Tree->moveUp($data[$modelClass]['id'], true);
  582. $parent = $this->Tree->findByName('1. Root', array('id'));
  583. $this->Tree->id = $parent[$modelClass]['id'];
  584. $result = $this->Tree->children(null, true, array('name'));
  585. $expected = array(
  586. array($modelClass => array('name' => '1.5', )),
  587. array($modelClass => array('name' => '1.1', )),
  588. array($modelClass => array('name' => '1.2', )),
  589. array($modelClass => array('name' => '1.3', )),
  590. array($modelClass => array('name' => '1.4', )),
  591. array($modelClass => array('name' => '1.6', )),
  592. array($modelClass => array('name' => '1.7', )),
  593. array($modelClass => array('name' => '1.8', )),
  594. array($modelClass => array('name' => '1.9', )),
  595. array($modelClass => array('name' => '1.10', )));
  596. $this->assertSame($expected, $result);
  597. }
  598. /**
  599. * testMoveDownSuccess method
  600. *
  601. * @return void
  602. */
  603. public function testMoveDownSuccess() {
  604. extract($this->settings);
  605. $this->Tree = new $modelClass();
  606. $this->Tree->initialize(2, 2);
  607. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  608. $this->Tree->moveDown($data[$modelClass]['id']);
  609. $parent = $this->Tree->findByName('1. Root', array('id'));
  610. $this->Tree->id = $parent[$modelClass]['id'];
  611. $result = $this->Tree->children(null, true, array('name'));
  612. $expected = array(array($modelClass => array('name' => '1.2', )),
  613. array($modelClass => array('name' => '1.1', )));
  614. $this->assertSame($expected, $result);
  615. }
  616. /**
  617. * testMoveDownFail method
  618. *
  619. * @return void
  620. */
  621. public function testMoveDownFail() {
  622. extract($this->settings);
  623. $this->Tree = new $modelClass();
  624. $this->Tree->initialize(2, 2);
  625. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
  626. $this->Tree->moveDown($data[$modelClass]['id']);
  627. $parent = $this->Tree->findByName('1. Root', array('id'));
  628. $this->Tree->id = $parent[$modelClass]['id'];
  629. $result = $this->Tree->children(null, true, array('name'));
  630. $expected = array(array($modelClass => array('name' => '1.1', )),
  631. array($modelClass => array('name' => '1.2', )));
  632. $this->assertSame($expected, $result);
  633. }
  634. /**
  635. * testMoveDownLast method
  636. *
  637. * @return void
  638. */
  639. public function testMoveDownLast() {
  640. extract($this->settings);
  641. $this->Tree = new $modelClass();
  642. $this->Tree->initialize(1, 10);
  643. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  644. $this->Tree->moveDown($data[$modelClass]['id'], true);
  645. $parent = $this->Tree->findByName('1. Root', array('id'));
  646. $this->Tree->id = $parent[$modelClass]['id'];
  647. $result = $this->Tree->children(null, true, array('name'));
  648. $expected = array(
  649. array($modelClass => array('name' => '1.1', )),
  650. array($modelClass => array('name' => '1.2', )),
  651. array($modelClass => array('name' => '1.3', )),
  652. array($modelClass => array('name' => '1.4', )),
  653. array($modelClass => array('name' => '1.6', )),
  654. array($modelClass => array('name' => '1.7', )),
  655. array($modelClass => array('name' => '1.8', )),
  656. array($modelClass => array('name' => '1.9', )),
  657. array($modelClass => array('name' => '1.10', )),
  658. array($modelClass => array('name' => '1.5', )));
  659. $this->assertSame($expected, $result);
  660. }
  661. /**
  662. * testMoveDown2 method
  663. *
  664. * @return void
  665. */
  666. public function testMoveDown2() {
  667. extract($this->settings);
  668. $this->Tree = new $modelClass();
  669. $this->Tree->initialize(1, 10);
  670. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  671. $this->Tree->moveDown($data[$modelClass]['id'], 2);
  672. $parent = $this->Tree->findByName('1. Root', array('id'));
  673. $this->Tree->id = $parent[$modelClass]['id'];
  674. $result = $this->Tree->children(null, true, array('name'));
  675. $expected = array(
  676. array($modelClass => array('name' => '1.1', )),
  677. array($modelClass => array('name' => '1.2', )),
  678. array($modelClass => array('name' => '1.3', )),
  679. array($modelClass => array('name' => '1.4', )),
  680. array($modelClass => array('name' => '1.6', )),
  681. array($modelClass => array('name' => '1.7', )),
  682. array($modelClass => array('name' => '1.5', )),
  683. array($modelClass => array('name' => '1.8', )),
  684. array($modelClass => array('name' => '1.9', )),
  685. array($modelClass => array('name' => '1.10', )));
  686. $this->assertSame($expected, $result);
  687. }
  688. /**
  689. * testSaveNoMove method
  690. *
  691. * @return void
  692. */
  693. public function testSaveNoMove() {
  694. extract($this->settings);
  695. $this->Tree = new $modelClass();
  696. $this->Tree->initialize(1, 10);
  697. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  698. $this->Tree->id = $data[$modelClass]['id'];
  699. $this->Tree->saveField('name', 'renamed');
  700. $parent = $this->Tree->findByName('1. Root', array('id'));
  701. $this->Tree->id = $parent[$modelClass]['id'];
  702. $result = $this->Tree->children(null, true, array('name'));
  703. $expected = array(
  704. array($modelClass => array('name' => '1.1', )),
  705. array($modelClass => array('name' => '1.2', )),
  706. array($modelClass => array('name' => '1.3', )),
  707. array($modelClass => array('name' => '1.4', )),
  708. array($modelClass => array('name' => 'renamed', )),
  709. array($modelClass => array('name' => '1.6', )),
  710. array($modelClass => array('name' => '1.7', )),
  711. array($modelClass => array('name' => '1.8', )),
  712. array($modelClass => array('name' => '1.9', )),
  713. array($modelClass => array('name' => '1.10', )));
  714. $this->assertSame($expected, $result);
  715. }
  716. /**
  717. * testMoveToRootAndMoveUp method
  718. *
  719. * @return void
  720. */
  721. public function testMoveToRootAndMoveUp() {
  722. extract($this->settings);
  723. $this->Tree = new $modelClass();
  724. $this->Tree->initialize(1, 1);
  725. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  726. $this->Tree->id = $data[$modelClass]['id'];
  727. $this->Tree->save(array($parentField => null));
  728. $result = $this->Tree->verify();
  729. $this->assertSame($result, true);
  730. $this->Tree->moveUp();
  731. $result = $this->Tree->find('all', array('fields' => 'name', 'order' => $modelClass . '.' . $leftField . ' ASC'));
  732. $expected = array(array($modelClass => array('name' => '1.1')),
  733. array($modelClass => array('name' => '1. Root')));
  734. $this->assertSame($expected, $result);
  735. }
  736. /**
  737. * testDelete method
  738. *
  739. * @return void
  740. */
  741. public function testDelete() {
  742. extract($this->settings);
  743. $this->Tree = new $modelClass();
  744. $this->Tree->initialize(2, 2);
  745. $initialCount = $this->Tree->find('count');
  746. $result = $this->Tree->findByName('1.1.1');
  747. $return = $this->Tree->delete($result[$modelClass]['id']);
  748. $this->assertEquals($return, true);
  749. $laterCount = $this->Tree->find('count');
  750. $this->assertEquals($initialCount - 1, $laterCount);
  751. $validTree = $this->Tree->verify();
  752. $this->assertSame($validTree, true);
  753. $initialCount = $this->Tree->find('count');
  754. $result = $this->Tree->findByName('1.1');
  755. $return = $this->Tree->delete($result[$modelClass]['id']);
  756. $this->assertEquals($return, true);
  757. $laterCount = $this->Tree->find('count');
  758. $this->assertEquals($initialCount - 2, $laterCount);
  759. $validTree = $this->Tree->verify();
  760. $this->assertSame($validTree, true);
  761. }
  762. /**
  763. * testRemove method
  764. *
  765. * @return void
  766. */
  767. public function testRemove() {
  768. extract($this->settings);
  769. $this->Tree = new $modelClass();
  770. $this->Tree->initialize(2, 2);
  771. $initialCount = $this->Tree->find('count');
  772. $result = $this->Tree->findByName('1.1');
  773. $this->Tree->removeFromTree($result[$modelClass]['id']);
  774. $laterCount = $this->Tree->find('count');
  775. $this->assertEquals($initialCount, $laterCount);
  776. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'));
  777. $expects = array(array($modelClass => array('name' => '1.1.1')),
  778. array($modelClass => array('name' => '1.1.2')),
  779. array($modelClass => array('name' => '1.2')));
  780. $this->assertEquals($children, $expects);
  781. $topNodes = $this->Tree->children(false, true,array('name'));
  782. $expects = array(array($modelClass => array('name' => '1. Root')),
  783. array($modelClass => array('name' => '1.1')));
  784. $this->assertEquals($topNodes, $expects);
  785. $validTree = $this->Tree->verify();
  786. $this->assertSame($validTree, true);
  787. }
  788. /**
  789. * testRemoveLastTopParent method
  790. *
  791. * @return void
  792. */
  793. public function testRemoveLastTopParent() {
  794. extract($this->settings);
  795. $this->Tree = new $modelClass();
  796. $this->Tree->initialize(2, 2);
  797. $initialCount = $this->Tree->find('count');
  798. $initialTopNodes = $this->Tree->childCount(false);
  799. $result = $this->Tree->findByName('1. Root');
  800. $this->Tree->removeFromTree($result[$modelClass]['id']);
  801. $laterCount = $this->Tree->find('count');
  802. $laterTopNodes = $this->Tree->childCount(false);
  803. $this->assertEquals($initialCount, $laterCount);
  804. $this->assertEquals($initialTopNodes, $laterTopNodes);
  805. $topNodes = $this->Tree->children(false, true,array('name'));
  806. $expects = array(array($modelClass => array('name' => '1.1')),
  807. array($modelClass => array('name' => '1.2')),
  808. array($modelClass => array('name' => '1. Root')));
  809. $this->assertEquals($topNodes, $expects);
  810. $validTree = $this->Tree->verify();
  811. $this->assertSame($validTree, true);
  812. }
  813. /**
  814. * testRemoveNoChildren method
  815. *
  816. * @return void
  817. */
  818. public function testRemoveNoChildren() {
  819. extract($this->settings);
  820. $this->Tree = new $modelClass();
  821. $this->Tree->initialize(2, 2);
  822. $initialCount = $this->Tree->find('count');
  823. $result = $this->Tree->findByName('1.1.1');
  824. $this->Tree->removeFromTree($result[$modelClass]['id']);
  825. $laterCount = $this->Tree->find('count');
  826. $this->assertEquals($initialCount, $laterCount);
  827. $nodes = $this->Tree->find('list', array('order' => $leftField));
  828. $expects = array(
  829. 1 => '1. Root',
  830. 2 => '1.1',
  831. 4 => '1.1.2',
  832. 5 => '1.2',
  833. 6 => '1.2.1',
  834. 7 => '1.2.2',
  835. 3 => '1.1.1',
  836. );
  837. $this->assertEquals($nodes, $expects);
  838. $validTree = $this->Tree->verify();
  839. $this->assertSame($validTree, true);
  840. }
  841. /**
  842. * testRemoveAndDelete method
  843. *
  844. * @return void
  845. */
  846. public function testRemoveAndDelete() {
  847. extract($this->settings);
  848. $this->Tree = new $modelClass();
  849. $this->Tree->initialize(2, 2);
  850. $initialCount = $this->Tree->find('count');
  851. $result = $this->Tree->findByName('1.1');
  852. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  853. $laterCount = $this->Tree->find('count');
  854. $this->assertEquals($initialCount - 1, $laterCount);
  855. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'), $leftField . ' asc');
  856. $expects = array(
  857. array($modelClass => array('name' => '1.1.1')),
  858. array($modelClass => array('name' => '1.1.2')),
  859. array($modelClass => array('name' => '1.2'))
  860. );
  861. $this->assertEquals($children, $expects);
  862. $topNodes = $this->Tree->children(false, true,array('name'));
  863. $expects = array(array($modelClass => array('name' => '1. Root')));
  864. $this->assertEquals($topNodes, $expects);
  865. $validTree = $this->Tree->verify();
  866. $this->assertSame($validTree, true);
  867. }
  868. /**
  869. * testRemoveAndDeleteNoChildren method
  870. *
  871. * @return void
  872. */
  873. public function testRemoveAndDeleteNoChildren() {
  874. extract($this->settings);
  875. $this->Tree = new $modelClass();
  876. $this->Tree->initialize(2, 2);
  877. $initialCount = $this->Tree->find('count');
  878. $result = $this->Tree->findByName('1.1.1');
  879. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  880. $laterCount = $this->Tree->find('count');
  881. $this->assertEquals($initialCount - 1, $laterCount);
  882. $nodes = $this->Tree->find('list', array('order' => $leftField));
  883. $expects = array(
  884. 1 => '1. Root',
  885. 2 => '1.1',
  886. 4 => '1.1.2',
  887. 5 => '1.2',
  888. 6 => '1.2.1',
  889. 7 => '1.2.2',
  890. );
  891. $this->assertEquals($nodes, $expects);
  892. $validTree = $this->Tree->verify();
  893. $this->assertSame($validTree, true);
  894. }
  895. /**
  896. * testChildren method
  897. *
  898. * @return void
  899. */
  900. public function testChildren() {
  901. extract($this->settings);
  902. $this->Tree = new $modelClass();
  903. $this->Tree->initialize(2, 2);
  904. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  905. $this->Tree->id = $data[$modelClass]['id'];
  906. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  907. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  908. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  909. $this->assertEquals($direct, $expects);
  910. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  911. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  912. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  913. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  914. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  915. array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  916. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12)));
  917. $this->assertEquals($total, $expects);
  918. $this->assertEquals(array(), $this->Tree->children(10000));
  919. }
  920. /**
  921. * testCountChildren method
  922. *
  923. * @return void
  924. */
  925. public function testCountChildren() {
  926. extract($this->settings);
  927. $this->Tree = new $modelClass();
  928. $this->Tree->initialize(2, 2);
  929. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  930. $this->Tree->id = $data[$modelClass]['id'];
  931. $direct = $this->Tree->childCount(null, true);
  932. $this->assertEquals($direct, 2);
  933. $total = $this->Tree->childCount();
  934. $this->assertEquals($total, 6);
  935. $this->Tree->read(null, $data[$modelClass]['id']);
  936. $id = $this->Tree->field('id', array($modelClass . '.name' => '1.2'));
  937. $total = $this->Tree->childCount($id);
  938. $this->assertEquals($total, 2);
  939. }
  940. /**
  941. * testGetParentNode method
  942. *
  943. * @return void
  944. */
  945. public function testGetParentNode() {
  946. extract($this->settings);
  947. $this->Tree = new $modelClass();
  948. $this->Tree->initialize(2, 2);
  949. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  950. $this->Tree->id = $data[$modelClass]['id'];
  951. $result = $this->Tree->getParentNode(null, array('name'));
  952. $expects = array($modelClass => array('name' => '1.2'));
  953. $this->assertSame($result, $expects);
  954. }
  955. /**
  956. * testGetPath method
  957. *
  958. * @return void
  959. */
  960. public function testGetPath() {
  961. extract($this->settings);
  962. $this->Tree = new $modelClass();
  963. $this->Tree->initialize(2, 2);
  964. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  965. $this->Tree->id = $data[$modelClass]['id'];
  966. $result = $this->Tree->getPath(null, array('name'));
  967. $expects = array(array($modelClass => array('name' => '1. Root')),
  968. array($modelClass => array('name' => '1.2')),
  969. array($modelClass => array('name' => '1.2.2')));
  970. $this->assertSame($result, $expects);
  971. }
  972. /**
  973. * testNoAmbiguousColumn method
  974. *
  975. * @return void
  976. */
  977. public function testNoAmbiguousColumn() {
  978. extract($this->settings);
  979. $this->Tree = new $modelClass();
  980. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  981. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  982. $this->Tree->initialize(2, 2);
  983. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  984. $this->Tree->id = $data[$modelClass]['id'];
  985. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  986. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  987. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  988. $this->assertEquals($direct, $expects);
  989. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  990. $expects = array(
  991. array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  992. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  993. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  994. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  995. array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  996. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12))
  997. );
  998. $this->assertEquals($total, $expects);
  999. }
  1000. /**
  1001. * testReorderTree method
  1002. *
  1003. * @return void
  1004. */
  1005. public function testReorderTree() {
  1006. extract($this->settings);
  1007. $this->Tree = new $modelClass();
  1008. $this->Tree->initialize(3, 3);
  1009. $nodes = $this->Tree->find('list', array('order' => $leftField));
  1010. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  1011. $this->Tree->moveDown($data[$modelClass]['id']);
  1012. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2.1')));
  1013. $this->Tree->moveDown($data[$modelClass]['id']);
  1014. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.3.2.2')));
  1015. $this->Tree->moveDown($data[$modelClass]['id']);
  1016. $unsortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1017. $this->assertEquals($nodes, $unsortedNodes);
  1018. $this->assertNotEquals(array_keys($nodes), array_keys($unsortedNodes));
  1019. $this->Tree->reorder();
  1020. $sortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1021. $this->assertSame($nodes, $sortedNodes);
  1022. }
  1023. /**
  1024. * test reordering large-ish trees with cacheQueries = true.
  1025. * This caused infinite loops when moving down elements as stale data is returned
  1026. * from the memory cache
  1027. *
  1028. * @return void
  1029. */
  1030. public function testReorderBigTreeWithQueryCaching() {
  1031. extract($this->settings);
  1032. $this->Tree = new $modelClass();
  1033. $this->Tree->initialize(2, 10);
  1034. $original = $this->Tree->cacheQueries;
  1035. $this->Tree->cacheQueries = true;
  1036. $this->Tree->reorder(array('field' => 'name', 'direction' => 'DESC'));
  1037. $this->assertTrue($this->Tree->cacheQueries, 'cacheQueries was not restored after reorder(). %s');
  1038. $this->Tree->cacheQueries = $original;
  1039. }
  1040. /**
  1041. * testGenerateTreeListWithSelfJoin method
  1042. *
  1043. * @return void
  1044. */
  1045. public function testGenerateTreeListWithSelfJoin() {
  1046. extract($this->settings);
  1047. $this->Tree = new $modelClass();
  1048. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  1049. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  1050. $this->Tree->initialize(2, 2);
  1051. $result = $this->Tree->generateTreeList();
  1052. $expected = array(1 => '1. Root', 2 => '_1.1', 3 => '__1.1.1', 4 => '__1.1.2', 5 => '_1.2', 6 => '__1.2.1', 7 => '__1.2.2');
  1053. $this->assertSame($expected, $result);
  1054. }
  1055. /**
  1056. * testArraySyntax method
  1057. *
  1058. * @return void
  1059. */
  1060. public function testArraySyntax() {
  1061. extract($this->settings);
  1062. $this->Tree = new $modelClass();
  1063. $this->Tree->initialize(3, 3);
  1064. $this->assertSame($this->Tree->childCount(2), $this->Tree->childCount(array('id' => 2)));
  1065. $this->assertSame($this->Tree->getParentNode(2), $this->Tree->getParentNode(array('id' => 2)));
  1066. $this->assertSame($this->Tree->getPath(4), $this->Tree->getPath(array('id' => 4)));
  1067. }
  1068. /**
  1069. * testFindThreaded method
  1070. *
  1071. * @return void
  1072. */
  1073. public function testFindThreaded() {
  1074. $Model = new Person();
  1075. $Model->recursive = -1;
  1076. $Model->Behaviors->attach('Tree', array('parent' => 'mother_id'));
  1077. $result = $Model->find('threaded');
  1078. $expected = array(
  1079. array(
  1080. 'Person' => array(
  1081. 'id' => '4',
  1082. 'name' => 'mother - grand mother',
  1083. 'mother_id' => '0',
  1084. 'father_id' => '0'
  1085. ),
  1086. 'children' => array(
  1087. array(
  1088. 'Person' => array(
  1089. 'id' => '2',
  1090. 'name' => 'mother',
  1091. 'mother_id' => '4',
  1092. 'father_id' => '5'
  1093. ),
  1094. 'children' => array(
  1095. array(
  1096. 'Person' => array(
  1097. 'id' => '1',
  1098. 'name' => 'person',
  1099. 'mother_id' => '2',
  1100. 'father_id' => '3'
  1101. ),
  1102. 'children' => array()
  1103. )
  1104. )
  1105. )
  1106. )
  1107. ),
  1108. array(
  1109. 'Person' => array(
  1110. 'id' => '5',
  1111. 'name' => 'mother - grand father',
  1112. 'mother_id' => '0',
  1113. 'father_id' => '0'
  1114. ),
  1115. 'children' => array()
  1116. ),
  1117. array(
  1118. 'Person' => array(
  1119. 'id' => '6',
  1120. 'name' => 'father - grand mother',
  1121. 'mother_id' => '0',
  1122. 'father_id' => '0'
  1123. ),
  1124. 'children' => array(
  1125. array(
  1126. 'Person' => array(
  1127. 'id' => '3',
  1128. 'name' => 'father',
  1129. 'mother_id' => '6',
  1130. 'father_id' => '7'
  1131. ),
  1132. 'children' => array()
  1133. )
  1134. )
  1135. ),
  1136. array(
  1137. 'Person' => array(
  1138. 'id' => '7',
  1139. 'name' => 'father - grand father',
  1140. 'mother_id' => '0',
  1141. 'father_id' => '0'
  1142. ),
  1143. 'children' => array()
  1144. )
  1145. );
  1146. $this->assertEquals($expected, $result);
  1147. }
  1148. }