TreeBehaviorNumberTest.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  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/2.0/en/development/testing.html>
  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/2.0/en/development/testing.html 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(7, $result);
  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. * testAddWithPreSpecifiedId method
  304. *
  305. * @return void
  306. */
  307. public function testAddWithPreSpecifiedId() {
  308. extract($this->settings);
  309. $this->Tree = new $modelClass();
  310. $this->Tree->initialize(2, 2);
  311. $data = $this->Tree->find('first', array(
  312. 'fields' => array('id'),
  313. 'conditions' => array($modelClass . '.name' => '1.1')
  314. ));
  315. $this->Tree->create();
  316. $result = $this->Tree->save(array($modelClass => array(
  317. 'id' => 100,
  318. 'name' => 'testAddMiddle',
  319. $parentField => $data[$modelClass]['id'])
  320. ));
  321. $expected = array_merge(
  322. array($modelClass => array('id' => 100, 'name' => 'testAddMiddle', $parentField => '2')),
  323. $result
  324. );
  325. $this->assertSame($expected, $result);
  326. $this->assertTrue($this->Tree->verify());
  327. }
  328. /**
  329. * testAddInvalid method
  330. *
  331. * @return void
  332. */
  333. public function testAddInvalid() {
  334. extract($this->settings);
  335. $this->Tree = new $modelClass();
  336. $this->Tree->initialize(2, 2);
  337. $this->Tree->id = null;
  338. $initialCount = $this->Tree->find('count');
  339. //$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
  340. $saveSuccess = $this->Tree->save(array($modelClass => array('name' => 'testAddInvalid', $parentField => 99999)));
  341. $this->assertSame($saveSuccess, false);
  342. $laterCount = $this->Tree->find('count');
  343. $this->assertSame($initialCount, $laterCount);
  344. $validTree = $this->Tree->verify();
  345. $this->assertSame($validTree, true);
  346. }
  347. /**
  348. * testAddNotIndexedByModel method
  349. *
  350. * @return void
  351. */
  352. public function testAddNotIndexedByModel() {
  353. extract($this->settings);
  354. $this->Tree = new $modelClass();
  355. $this->Tree->initialize(2, 2);
  356. $this->Tree->save(array('name' => 'testAddNotIndexed', $parentField => null));
  357. $result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
  358. $expected = array($modelClass => array('name' => 'testAddNotIndexed', $parentField => null));
  359. $this->assertEquals($expected, $result);
  360. $validTree = $this->Tree->verify();
  361. $this->assertSame($validTree, true);
  362. }
  363. /**
  364. * testMovePromote method
  365. *
  366. * @return void
  367. */
  368. public function testMovePromote() {
  369. extract($this->settings);
  370. $this->Tree = new $modelClass();
  371. $this->Tree->initialize(2, 2);
  372. $this->Tree->id = null;
  373. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  374. $parentId = $parent[$modelClass]['id'];
  375. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  376. $this->Tree->id = $data[$modelClass]['id'];
  377. $this->Tree->saveField($parentField, $parentId);
  378. $direct = $this->Tree->children($parentId, true, array('id', 'name', $parentField, $leftField, $rightField));
  379. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
  380. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
  381. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
  382. $this->assertEquals($direct, $expects);
  383. $validTree = $this->Tree->verify();
  384. $this->assertSame($validTree, true);
  385. }
  386. /**
  387. * testMoveWithWhitelist method
  388. *
  389. * @return void
  390. */
  391. public function testMoveWithWhitelist() {
  392. extract($this->settings);
  393. $this->Tree = new $modelClass();
  394. $this->Tree->initialize(2, 2);
  395. $this->Tree->id = null;
  396. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  397. $parentId = $parent[$modelClass]['id'];
  398. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  399. $this->Tree->id = $data[$modelClass]['id'];
  400. $this->Tree->whitelist = array($parentField, 'name', 'description');
  401. $this->Tree->saveField($parentField, $parentId);
  402. $result = $this->Tree->children($parentId, true, array('id', 'name', $parentField, $leftField, $rightField));
  403. $expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
  404. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
  405. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
  406. $this->assertEquals($expected, $result);
  407. $this->assertTrue($this->Tree->verify());
  408. }
  409. /**
  410. * testInsertWithWhitelist method
  411. *
  412. * @return void
  413. */
  414. public function testInsertWithWhitelist() {
  415. extract($this->settings);
  416. $this->Tree = new $modelClass();
  417. $this->Tree->initialize(2, 2);
  418. $this->Tree->whitelist = array('name', $parentField);
  419. $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
  420. $result = $this->Tree->findByName('testAddOrphan', array('name', $parentField, $leftField, $rightField));
  421. $expected = array('name' => 'testAddOrphan', $parentField => null, $leftField => '15', $rightField => 16);
  422. $this->assertEquals($expected, $result[$modelClass]);
  423. $this->assertSame($this->Tree->verify(), true);
  424. }
  425. /**
  426. * testMoveBefore method
  427. *
  428. * @return void
  429. */
  430. public function testMoveBefore() {
  431. extract($this->settings);
  432. $this->Tree = new $modelClass();
  433. $this->Tree->initialize(2, 2);
  434. $this->Tree->id = null;
  435. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
  436. $parentId = $parent[$modelClass]['id'];
  437. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
  438. $this->Tree->id = $data[$modelClass]['id'];
  439. $this->Tree->saveField($parentField, $parentId);
  440. $result = $this->Tree->children($parentId, true, array('name'));
  441. $expects = array(array($modelClass => array('name' => '1.1.1')),
  442. array($modelClass => array('name' => '1.1.2')),
  443. array($modelClass => array('name' => '1.2')));
  444. $this->assertEquals($expects, $result);
  445. $validTree = $this->Tree->verify();
  446. $this->assertSame($validTree, true);
  447. }
  448. /**
  449. * testMoveAfter method
  450. *
  451. * @return void
  452. */
  453. public function testMoveAfter() {
  454. extract($this->settings);
  455. $this->Tree = new $modelClass();
  456. $this->Tree->initialize(2, 2);
  457. $this->Tree->id = null;
  458. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
  459. $parentId = $parent[$modelClass]['id'];
  460. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  461. $this->Tree->id = $data[$modelClass]['id'];
  462. $this->Tree->saveField($parentField, $parentId);
  463. $result = $this->Tree->children($parentId, true, array('name'));
  464. $expects = array(array($modelClass => array('name' => '1.2.1')),
  465. array($modelClass => array('name' => '1.2.2')),
  466. array($modelClass => array('name' => '1.1')));
  467. $this->assertEquals($expects, $result);
  468. $validTree = $this->Tree->verify();
  469. $this->assertSame($validTree, true);
  470. }
  471. /**
  472. * testMoveDemoteInvalid method
  473. *
  474. * @return void
  475. */
  476. public function testMoveDemoteInvalid() {
  477. extract($this->settings);
  478. $this->Tree = new $modelClass();
  479. $this->Tree->initialize(2, 2);
  480. $this->Tree->id = null;
  481. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  482. $parentId = $parent[$modelClass]['id'];
  483. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  484. $expects = $this->Tree->find('all');
  485. $before = $this->Tree->read(null, $data[$modelClass]['id']);
  486. $this->Tree->id = $parentId;
  487. $this->Tree->saveField($parentField, $data[$modelClass]['id']);
  488. $results = $this->Tree->find('all');
  489. $after = $this->Tree->read(null, $data[$modelClass]['id']);
  490. $this->assertEquals($expects, $results);
  491. $this->assertEquals($before, $after);
  492. $validTree = $this->Tree->verify();
  493. $this->assertSame($validTree, true);
  494. }
  495. /**
  496. * testMoveInvalid method
  497. *
  498. * @return void
  499. */
  500. public function testMoveInvalid() {
  501. extract($this->settings);
  502. $this->Tree = new $modelClass();
  503. $this->Tree->initialize(2, 2);
  504. $this->Tree->id = null;
  505. $initialCount = $this->Tree->find('count');
  506. $data = $this->Tree->findByName('1.1');
  507. $this->Tree->id = $data[$modelClass]['id'];
  508. $this->Tree->saveField($parentField, 999999);
  509. $laterCount = $this->Tree->find('count');
  510. $this->assertSame($initialCount, $laterCount);
  511. $validTree = $this->Tree->verify();
  512. $this->assertSame($validTree, true);
  513. }
  514. /**
  515. * testMoveSelfInvalid method
  516. *
  517. * @return void
  518. */
  519. public function testMoveSelfInvalid() {
  520. extract($this->settings);
  521. $this->Tree = new $modelClass();
  522. $this->Tree->initialize(2, 2);
  523. $this->Tree->id = null;
  524. $initialCount = $this->Tree->find('count');
  525. $data = $this->Tree->findByName('1.1');
  526. $this->Tree->id = $data[$modelClass]['id'];
  527. $saveSuccess = $this->Tree->saveField($parentField, $this->Tree->id);
  528. $this->assertSame($saveSuccess, false);
  529. $laterCount = $this->Tree->find('count');
  530. $this->assertSame($initialCount, $laterCount);
  531. $validTree = $this->Tree->verify();
  532. $this->assertSame($validTree, true);
  533. }
  534. /**
  535. * testMoveUpSuccess method
  536. *
  537. * @return void
  538. */
  539. public function testMoveUpSuccess() {
  540. extract($this->settings);
  541. $this->Tree = new $modelClass();
  542. $this->Tree->initialize(2, 2);
  543. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
  544. $this->Tree->moveUp($data[$modelClass]['id']);
  545. $parent = $this->Tree->findByName('1. Root', array('id'));
  546. $this->Tree->id = $parent[$modelClass]['id'];
  547. $result = $this->Tree->children(null, true, array('name'));
  548. $expected = array(array($modelClass => array('name' => '1.2', )),
  549. array($modelClass => array('name' => '1.1', )));
  550. $this->assertSame($expected, $result);
  551. }
  552. /**
  553. * testMoveUpFail method
  554. *
  555. * @return void
  556. */
  557. public function testMoveUpFail() {
  558. extract($this->settings);
  559. $this->Tree = new $modelClass();
  560. $this->Tree->initialize(2, 2);
  561. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
  562. $this->Tree->moveUp($data[$modelClass]['id']);
  563. $parent = $this->Tree->findByName('1. Root', array('id'));
  564. $this->Tree->id = $parent[$modelClass]['id'];
  565. $result = $this->Tree->children(null, true, array('name'));
  566. $expected = array(array($modelClass => array('name' => '1.1', )),
  567. array($modelClass => array('name' => '1.2', )));
  568. $this->assertSame($expected, $result);
  569. }
  570. /**
  571. * testMoveUp2 method
  572. *
  573. * @return void
  574. */
  575. public function testMoveUp2() {
  576. extract($this->settings);
  577. $this->Tree = new $modelClass();
  578. $this->Tree->initialize(1, 10);
  579. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  580. $this->Tree->moveUp($data[$modelClass]['id'], 2);
  581. $parent = $this->Tree->findByName('1. Root', array('id'));
  582. $this->Tree->id = $parent[$modelClass]['id'];
  583. $result = $this->Tree->children(null, true, array('name'));
  584. $expected = array(
  585. array($modelClass => array('name' => '1.1', )),
  586. array($modelClass => array('name' => '1.2', )),
  587. array($modelClass => array('name' => '1.5', )),
  588. array($modelClass => array('name' => '1.3', )),
  589. array($modelClass => array('name' => '1.4', )),
  590. array($modelClass => array('name' => '1.6', )),
  591. array($modelClass => array('name' => '1.7', )),
  592. array($modelClass => array('name' => '1.8', )),
  593. array($modelClass => array('name' => '1.9', )),
  594. array($modelClass => array('name' => '1.10', )));
  595. $this->assertSame($expected, $result);
  596. }
  597. /**
  598. * testMoveUpFirst method
  599. *
  600. * @return void
  601. */
  602. public function testMoveUpFirst() {
  603. extract($this->settings);
  604. $this->Tree = new $modelClass();
  605. $this->Tree->initialize(1, 10);
  606. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  607. $this->Tree->moveUp($data[$modelClass]['id'], true);
  608. $parent = $this->Tree->findByName('1. Root', array('id'));
  609. $this->Tree->id = $parent[$modelClass]['id'];
  610. $result = $this->Tree->children(null, true, array('name'));
  611. $expected = array(
  612. array($modelClass => array('name' => '1.5', )),
  613. array($modelClass => array('name' => '1.1', )),
  614. array($modelClass => array('name' => '1.2', )),
  615. array($modelClass => array('name' => '1.3', )),
  616. array($modelClass => array('name' => '1.4', )),
  617. array($modelClass => array('name' => '1.6', )),
  618. array($modelClass => array('name' => '1.7', )),
  619. array($modelClass => array('name' => '1.8', )),
  620. array($modelClass => array('name' => '1.9', )),
  621. array($modelClass => array('name' => '1.10', )));
  622. $this->assertSame($expected, $result);
  623. }
  624. /**
  625. * testMoveDownSuccess method
  626. *
  627. * @return void
  628. */
  629. public function testMoveDownSuccess() {
  630. extract($this->settings);
  631. $this->Tree = new $modelClass();
  632. $this->Tree->initialize(2, 2);
  633. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  634. $this->Tree->moveDown($data[$modelClass]['id']);
  635. $parent = $this->Tree->findByName('1. Root', array('id'));
  636. $this->Tree->id = $parent[$modelClass]['id'];
  637. $result = $this->Tree->children(null, true, array('name'));
  638. $expected = array(array($modelClass => array('name' => '1.2', )),
  639. array($modelClass => array('name' => '1.1', )));
  640. $this->assertSame($expected, $result);
  641. }
  642. /**
  643. * testMoveDownFail method
  644. *
  645. * @return void
  646. */
  647. public function testMoveDownFail() {
  648. extract($this->settings);
  649. $this->Tree = new $modelClass();
  650. $this->Tree->initialize(2, 2);
  651. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
  652. $this->Tree->moveDown($data[$modelClass]['id']);
  653. $parent = $this->Tree->findByName('1. Root', array('id'));
  654. $this->Tree->id = $parent[$modelClass]['id'];
  655. $result = $this->Tree->children(null, true, array('name'));
  656. $expected = array(array($modelClass => array('name' => '1.1', )),
  657. array($modelClass => array('name' => '1.2', )));
  658. $this->assertSame($expected, $result);
  659. }
  660. /**
  661. * testMoveDownLast method
  662. *
  663. * @return void
  664. */
  665. public function testMoveDownLast() {
  666. extract($this->settings);
  667. $this->Tree = new $modelClass();
  668. $this->Tree->initialize(1, 10);
  669. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  670. $this->Tree->moveDown($data[$modelClass]['id'], true);
  671. $parent = $this->Tree->findByName('1. Root', array('id'));
  672. $this->Tree->id = $parent[$modelClass]['id'];
  673. $result = $this->Tree->children(null, true, array('name'));
  674. $expected = array(
  675. array($modelClass => array('name' => '1.1', )),
  676. array($modelClass => array('name' => '1.2', )),
  677. array($modelClass => array('name' => '1.3', )),
  678. array($modelClass => array('name' => '1.4', )),
  679. array($modelClass => array('name' => '1.6', )),
  680. array($modelClass => array('name' => '1.7', )),
  681. array($modelClass => array('name' => '1.8', )),
  682. array($modelClass => array('name' => '1.9', )),
  683. array($modelClass => array('name' => '1.10', )),
  684. array($modelClass => array('name' => '1.5', )));
  685. $this->assertSame($expected, $result);
  686. }
  687. /**
  688. * testMoveDown2 method
  689. *
  690. * @return void
  691. */
  692. public function testMoveDown2() {
  693. extract($this->settings);
  694. $this->Tree = new $modelClass();
  695. $this->Tree->initialize(1, 10);
  696. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  697. $this->Tree->moveDown($data[$modelClass]['id'], 2);
  698. $parent = $this->Tree->findByName('1. Root', array('id'));
  699. $this->Tree->id = $parent[$modelClass]['id'];
  700. $result = $this->Tree->children(null, true, array('name'));
  701. $expected = array(
  702. array($modelClass => array('name' => '1.1', )),
  703. array($modelClass => array('name' => '1.2', )),
  704. array($modelClass => array('name' => '1.3', )),
  705. array($modelClass => array('name' => '1.4', )),
  706. array($modelClass => array('name' => '1.6', )),
  707. array($modelClass => array('name' => '1.7', )),
  708. array($modelClass => array('name' => '1.5', )),
  709. array($modelClass => array('name' => '1.8', )),
  710. array($modelClass => array('name' => '1.9', )),
  711. array($modelClass => array('name' => '1.10', )));
  712. $this->assertSame($expected, $result);
  713. }
  714. /**
  715. * testSaveNoMove method
  716. *
  717. * @return void
  718. */
  719. public function testSaveNoMove() {
  720. extract($this->settings);
  721. $this->Tree = new $modelClass();
  722. $this->Tree->initialize(1, 10);
  723. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  724. $this->Tree->id = $data[$modelClass]['id'];
  725. $this->Tree->saveField('name', 'renamed');
  726. $parent = $this->Tree->findByName('1. Root', array('id'));
  727. $this->Tree->id = $parent[$modelClass]['id'];
  728. $result = $this->Tree->children(null, true, array('name'));
  729. $expected = array(
  730. array($modelClass => array('name' => '1.1', )),
  731. array($modelClass => array('name' => '1.2', )),
  732. array($modelClass => array('name' => '1.3', )),
  733. array($modelClass => array('name' => '1.4', )),
  734. array($modelClass => array('name' => 'renamed', )),
  735. array($modelClass => array('name' => '1.6', )),
  736. array($modelClass => array('name' => '1.7', )),
  737. array($modelClass => array('name' => '1.8', )),
  738. array($modelClass => array('name' => '1.9', )),
  739. array($modelClass => array('name' => '1.10', )));
  740. $this->assertSame($expected, $result);
  741. }
  742. /**
  743. * testMoveToRootAndMoveUp method
  744. *
  745. * @return void
  746. */
  747. public function testMoveToRootAndMoveUp() {
  748. extract($this->settings);
  749. $this->Tree = new $modelClass();
  750. $this->Tree->initialize(1, 1);
  751. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  752. $this->Tree->id = $data[$modelClass]['id'];
  753. $this->Tree->save(array($parentField => null));
  754. $result = $this->Tree->verify();
  755. $this->assertSame($result, true);
  756. $this->Tree->moveUp();
  757. $result = $this->Tree->find('all', array('fields' => 'name', 'order' => $modelClass . '.' . $leftField . ' ASC'));
  758. $expected = array(array($modelClass => array('name' => '1.1')),
  759. array($modelClass => array('name' => '1. Root')));
  760. $this->assertSame($expected, $result);
  761. }
  762. /**
  763. * testDelete method
  764. *
  765. * @return void
  766. */
  767. public function testDelete() {
  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.1');
  773. $return = $this->Tree->delete($result[$modelClass]['id']);
  774. $this->assertEquals(true, $return);
  775. $laterCount = $this->Tree->find('count');
  776. $this->assertEquals($initialCount - 1, $laterCount);
  777. $validTree = $this->Tree->verify();
  778. $this->assertSame($validTree, true);
  779. $initialCount = $this->Tree->find('count');
  780. $result = $this->Tree->findByName('1.1');
  781. $return = $this->Tree->delete($result[$modelClass]['id']);
  782. $this->assertEquals(true, $return);
  783. $laterCount = $this->Tree->find('count');
  784. $this->assertEquals($initialCount - 2, $laterCount);
  785. $validTree = $this->Tree->verify();
  786. $this->assertSame($validTree, true);
  787. }
  788. /**
  789. * Test deleting a record that doesn't exist.
  790. *
  791. * @return void
  792. */
  793. public function testDeleteDoesNotExist() {
  794. extract($this->settings);
  795. $this->Tree = new $modelClass();
  796. $this->Tree->initialize(2, 2);
  797. $this->Tree->delete(99999);
  798. }
  799. /**
  800. * testRemove method
  801. *
  802. * @return void
  803. */
  804. public function testRemove() {
  805. extract($this->settings);
  806. $this->Tree = new $modelClass();
  807. $this->Tree->initialize(2, 2);
  808. $initialCount = $this->Tree->find('count');
  809. $result = $this->Tree->findByName('1.1');
  810. $this->Tree->removeFromTree($result[$modelClass]['id']);
  811. $laterCount = $this->Tree->find('count');
  812. $this->assertEquals($initialCount, $laterCount);
  813. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'));
  814. $expects = array(array($modelClass => array('name' => '1.1.1')),
  815. array($modelClass => array('name' => '1.1.2')),
  816. array($modelClass => array('name' => '1.2')));
  817. $this->assertEquals($children, $expects);
  818. $topNodes = $this->Tree->children(false, true,array('name'));
  819. $expects = array(array($modelClass => array('name' => '1. Root')),
  820. array($modelClass => array('name' => '1.1')));
  821. $this->assertEquals($topNodes, $expects);
  822. $validTree = $this->Tree->verify();
  823. $this->assertSame($validTree, true);
  824. }
  825. /**
  826. * testRemoveLastTopParent method
  827. *
  828. * @return void
  829. */
  830. public function testRemoveLastTopParent() {
  831. extract($this->settings);
  832. $this->Tree = new $modelClass();
  833. $this->Tree->initialize(2, 2);
  834. $initialCount = $this->Tree->find('count');
  835. $initialTopNodes = $this->Tree->childCount(false);
  836. $result = $this->Tree->findByName('1. Root');
  837. $this->Tree->removeFromTree($result[$modelClass]['id']);
  838. $laterCount = $this->Tree->find('count');
  839. $laterTopNodes = $this->Tree->childCount(false);
  840. $this->assertEquals($initialCount, $laterCount);
  841. $this->assertEquals($initialTopNodes, $laterTopNodes);
  842. $topNodes = $this->Tree->children(false, true,array('name'));
  843. $expects = array(array($modelClass => array('name' => '1.1')),
  844. array($modelClass => array('name' => '1.2')),
  845. array($modelClass => array('name' => '1. Root')));
  846. $this->assertEquals($topNodes, $expects);
  847. $validTree = $this->Tree->verify();
  848. $this->assertSame($validTree, true);
  849. }
  850. /**
  851. * testRemoveNoChildren method
  852. *
  853. * @return void
  854. */
  855. public function testRemoveNoChildren() {
  856. extract($this->settings);
  857. $this->Tree = new $modelClass();
  858. $this->Tree->initialize(2, 2);
  859. $initialCount = $this->Tree->find('count');
  860. $result = $this->Tree->findByName('1.1.1');
  861. $this->Tree->removeFromTree($result[$modelClass]['id']);
  862. $laterCount = $this->Tree->find('count');
  863. $this->assertEquals($initialCount, $laterCount);
  864. $nodes = $this->Tree->find('list', array('order' => $leftField));
  865. $expects = array(
  866. 1 => '1. Root',
  867. 2 => '1.1',
  868. 4 => '1.1.2',
  869. 5 => '1.2',
  870. 6 => '1.2.1',
  871. 7 => '1.2.2',
  872. 3 => '1.1.1',
  873. );
  874. $this->assertEquals($nodes, $expects);
  875. $validTree = $this->Tree->verify();
  876. $this->assertSame($validTree, true);
  877. }
  878. /**
  879. * testRemoveAndDelete method
  880. *
  881. * @return void
  882. */
  883. public function testRemoveAndDelete() {
  884. extract($this->settings);
  885. $this->Tree = new $modelClass();
  886. $this->Tree->initialize(2, 2);
  887. $initialCount = $this->Tree->find('count');
  888. $result = $this->Tree->findByName('1.1');
  889. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  890. $laterCount = $this->Tree->find('count');
  891. $this->assertEquals($initialCount - 1, $laterCount);
  892. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'), $leftField . ' asc');
  893. $expects = array(
  894. array($modelClass => array('name' => '1.1.1')),
  895. array($modelClass => array('name' => '1.1.2')),
  896. array($modelClass => array('name' => '1.2'))
  897. );
  898. $this->assertEquals($children, $expects);
  899. $topNodes = $this->Tree->children(false, true,array('name'));
  900. $expects = array(array($modelClass => array('name' => '1. Root')));
  901. $this->assertEquals($topNodes, $expects);
  902. $validTree = $this->Tree->verify();
  903. $this->assertSame($validTree, true);
  904. }
  905. /**
  906. * testRemoveAndDeleteNoChildren method
  907. *
  908. * @return void
  909. */
  910. public function testRemoveAndDeleteNoChildren() {
  911. extract($this->settings);
  912. $this->Tree = new $modelClass();
  913. $this->Tree->initialize(2, 2);
  914. $initialCount = $this->Tree->find('count');
  915. $result = $this->Tree->findByName('1.1.1');
  916. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  917. $laterCount = $this->Tree->find('count');
  918. $this->assertEquals($initialCount - 1, $laterCount);
  919. $nodes = $this->Tree->find('list', array('order' => $leftField));
  920. $expects = array(
  921. 1 => '1. Root',
  922. 2 => '1.1',
  923. 4 => '1.1.2',
  924. 5 => '1.2',
  925. 6 => '1.2.1',
  926. 7 => '1.2.2',
  927. );
  928. $this->assertEquals($nodes, $expects);
  929. $validTree = $this->Tree->verify();
  930. $this->assertSame($validTree, true);
  931. }
  932. /**
  933. * testChildren method
  934. *
  935. * @return void
  936. */
  937. public function testChildren() {
  938. extract($this->settings);
  939. $this->Tree = new $modelClass();
  940. $this->Tree->initialize(2, 2);
  941. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  942. $this->Tree->id = $data[$modelClass]['id'];
  943. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  944. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  945. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  946. $this->assertEquals($direct, $expects);
  947. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  948. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  949. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  950. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  951. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  952. array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  953. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12)));
  954. $this->assertEquals($total, $expects);
  955. $this->assertEquals(array(), $this->Tree->children(10000));
  956. }
  957. /**
  958. * testCountChildren method
  959. *
  960. * @return void
  961. */
  962. public function testCountChildren() {
  963. extract($this->settings);
  964. $this->Tree = new $modelClass();
  965. $this->Tree->initialize(2, 2);
  966. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  967. $this->Tree->id = $data[$modelClass]['id'];
  968. $direct = $this->Tree->childCount(null, true);
  969. $this->assertEquals(2, $direct);
  970. $total = $this->Tree->childCount();
  971. $this->assertEquals(6, $total);
  972. $this->Tree->read(null, $data[$modelClass]['id']);
  973. $id = $this->Tree->field('id', array($modelClass . '.name' => '1.2'));
  974. $total = $this->Tree->childCount($id);
  975. $this->assertEquals(2, $total);
  976. }
  977. /**
  978. * testGetParentNode method
  979. *
  980. * @return void
  981. */
  982. public function testGetParentNode() {
  983. extract($this->settings);
  984. $this->Tree = new $modelClass();
  985. $this->Tree->initialize(2, 2);
  986. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  987. $this->Tree->id = $data[$modelClass]['id'];
  988. $result = $this->Tree->getParentNode(null, array('name'));
  989. $expects = array($modelClass => array('name' => '1.2'));
  990. $this->assertSame($expects, $result);
  991. }
  992. /**
  993. * testGetPath method
  994. *
  995. * @return void
  996. */
  997. public function testGetPath() {
  998. extract($this->settings);
  999. $this->Tree = new $modelClass();
  1000. $this->Tree->initialize(2, 2);
  1001. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  1002. $this->Tree->id = $data[$modelClass]['id'];
  1003. $result = $this->Tree->getPath(null, array('name'));
  1004. $expects = array(array($modelClass => array('name' => '1. Root')),
  1005. array($modelClass => array('name' => '1.2')),
  1006. array($modelClass => array('name' => '1.2.2')));
  1007. $this->assertSame($expects, $result);
  1008. }
  1009. /**
  1010. * testNoAmbiguousColumn method
  1011. *
  1012. * @return void
  1013. */
  1014. public function testNoAmbiguousColumn() {
  1015. extract($this->settings);
  1016. $this->Tree = new $modelClass();
  1017. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  1018. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  1019. $this->Tree->initialize(2, 2);
  1020. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  1021. $this->Tree->id = $data[$modelClass]['id'];
  1022. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  1023. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  1024. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  1025. $this->assertEquals($direct, $expects);
  1026. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  1027. $expects = array(
  1028. array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  1029. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  1030. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  1031. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  1032. array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  1033. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12))
  1034. );
  1035. $this->assertEquals($total, $expects);
  1036. }
  1037. /**
  1038. * testReorderTree method
  1039. *
  1040. * @return void
  1041. */
  1042. public function testReorderTree() {
  1043. extract($this->settings);
  1044. $this->Tree = new $modelClass();
  1045. $this->Tree->initialize(3, 3);
  1046. $nodes = $this->Tree->find('list', array('order' => $leftField));
  1047. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  1048. $this->Tree->moveDown($data[$modelClass]['id']);
  1049. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2.1')));
  1050. $this->Tree->moveDown($data[$modelClass]['id']);
  1051. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.3.2.2')));
  1052. $this->Tree->moveDown($data[$modelClass]['id']);
  1053. $unsortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1054. $this->assertEquals($nodes, $unsortedNodes);
  1055. $this->assertNotEquals(array_keys($nodes), array_keys($unsortedNodes));
  1056. $this->Tree->reorder();
  1057. $sortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1058. $this->assertSame($nodes, $sortedNodes);
  1059. }
  1060. /**
  1061. * test reordering large-ish trees with cacheQueries = true.
  1062. * This caused infinite loops when moving down elements as stale data is returned
  1063. * from the memory cache
  1064. *
  1065. * @return void
  1066. */
  1067. public function testReorderBigTreeWithQueryCaching() {
  1068. extract($this->settings);
  1069. $this->Tree = new $modelClass();
  1070. $this->Tree->initialize(2, 10);
  1071. $original = $this->Tree->cacheQueries;
  1072. $this->Tree->cacheQueries = true;
  1073. $this->Tree->reorder(array('field' => 'name', 'direction' => 'DESC'));
  1074. $this->assertTrue($this->Tree->cacheQueries, 'cacheQueries was not restored after reorder(). %s');
  1075. $this->Tree->cacheQueries = $original;
  1076. }
  1077. /**
  1078. * testGenerateTreeListWithSelfJoin method
  1079. *
  1080. * @return void
  1081. */
  1082. public function testGenerateTreeListWithSelfJoin() {
  1083. extract($this->settings);
  1084. $this->Tree = new $modelClass();
  1085. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  1086. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  1087. $this->Tree->initialize(2, 2);
  1088. $result = $this->Tree->generateTreeList();
  1089. $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');
  1090. $this->assertSame($expected, $result);
  1091. }
  1092. /**
  1093. * Test the formatting options of generateTreeList()
  1094. *
  1095. * @return void
  1096. */
  1097. public function testGenerateTreeListFormatting() {
  1098. extract($this->settings);
  1099. $this->Tree = new $modelClass();
  1100. $this->Tree->initialize(2, 2);
  1101. $result = $this->Tree->generateTreeList(
  1102. null,
  1103. "{n}.$modelClass.id",
  1104. array('%s - %s', "{n}.$modelClass.id", "{n}.$modelClass.name")
  1105. );
  1106. $this->assertEquals('1 - 1. Root', $result[1]);
  1107. $this->assertEquals('_2 - 1.1', $result[2]);
  1108. $this->assertEquals('__3 - 1.1.1', $result[3]);
  1109. }
  1110. /**
  1111. * testArraySyntax method
  1112. *
  1113. * @return void
  1114. */
  1115. public function testArraySyntax() {
  1116. extract($this->settings);
  1117. $this->Tree = new $modelClass();
  1118. $this->Tree->initialize(3, 3);
  1119. $this->assertSame($this->Tree->childCount(2), $this->Tree->childCount(array('id' => 2)));
  1120. $this->assertSame($this->Tree->getParentNode(2), $this->Tree->getParentNode(array('id' => 2)));
  1121. $this->assertSame($this->Tree->getPath(4), $this->Tree->getPath(array('id' => 4)));
  1122. }
  1123. /**
  1124. * testFindThreaded method
  1125. *
  1126. * @return void
  1127. */
  1128. public function testFindThreaded() {
  1129. $Model = new Person();
  1130. $Model->recursive = -1;
  1131. $Model->Behaviors->attach('Tree', array('parent' => 'mother_id'));
  1132. $result = $Model->find('threaded');
  1133. $expected = array(
  1134. array(
  1135. 'Person' => array(
  1136. 'id' => '4',
  1137. 'name' => 'mother - grand mother',
  1138. 'mother_id' => '0',
  1139. 'father_id' => '0'
  1140. ),
  1141. 'children' => array(
  1142. array(
  1143. 'Person' => array(
  1144. 'id' => '2',
  1145. 'name' => 'mother',
  1146. 'mother_id' => '4',
  1147. 'father_id' => '5'
  1148. ),
  1149. 'children' => array(
  1150. array(
  1151. 'Person' => array(
  1152. 'id' => '1',
  1153. 'name' => 'person',
  1154. 'mother_id' => '2',
  1155. 'father_id' => '3'
  1156. ),
  1157. 'children' => array()
  1158. )
  1159. )
  1160. )
  1161. )
  1162. ),
  1163. array(
  1164. 'Person' => array(
  1165. 'id' => '5',
  1166. 'name' => 'mother - grand father',
  1167. 'mother_id' => '0',
  1168. 'father_id' => '0'
  1169. ),
  1170. 'children' => array()
  1171. ),
  1172. array(
  1173. 'Person' => array(
  1174. 'id' => '6',
  1175. 'name' => 'father - grand mother',
  1176. 'mother_id' => '0',
  1177. 'father_id' => '0'
  1178. ),
  1179. 'children' => array(
  1180. array(
  1181. 'Person' => array(
  1182. 'id' => '3',
  1183. 'name' => 'father',
  1184. 'mother_id' => '6',
  1185. 'father_id' => '7'
  1186. ),
  1187. 'children' => array()
  1188. )
  1189. )
  1190. ),
  1191. array(
  1192. 'Person' => array(
  1193. 'id' => '7',
  1194. 'name' => 'father - grand father',
  1195. 'mother_id' => '0',
  1196. 'father_id' => '0'
  1197. ),
  1198. 'children' => array()
  1199. )
  1200. );
  1201. $this->assertEquals($expected, $result);
  1202. }
  1203. }