TreeBehaviorNumberTest.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. * 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($expected, $result[$modelClass]);
  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($expects, $result);
  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($expects, $result);
  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($expects, $results);
  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(true, $return);
  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(true, $return);
  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. * Test deleting a record that doesn't exist.
  764. *
  765. * @return void
  766. */
  767. public function testDeleteDoesNotExist() {
  768. extract($this->settings);
  769. $this->Tree = new $modelClass();
  770. $this->Tree->initialize(2, 2);
  771. $this->Tree->delete(99999);
  772. }
  773. /**
  774. * testRemove method
  775. *
  776. * @return void
  777. */
  778. public function testRemove() {
  779. extract($this->settings);
  780. $this->Tree = new $modelClass();
  781. $this->Tree->initialize(2, 2);
  782. $initialCount = $this->Tree->find('count');
  783. $result = $this->Tree->findByName('1.1');
  784. $this->Tree->removeFromTree($result[$modelClass]['id']);
  785. $laterCount = $this->Tree->find('count');
  786. $this->assertEquals($initialCount, $laterCount);
  787. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'));
  788. $expects = array(array($modelClass => array('name' => '1.1.1')),
  789. array($modelClass => array('name' => '1.1.2')),
  790. array($modelClass => array('name' => '1.2')));
  791. $this->assertEquals($children, $expects);
  792. $topNodes = $this->Tree->children(false, true,array('name'));
  793. $expects = array(array($modelClass => array('name' => '1. Root')),
  794. array($modelClass => array('name' => '1.1')));
  795. $this->assertEquals($topNodes, $expects);
  796. $validTree = $this->Tree->verify();
  797. $this->assertSame($validTree, true);
  798. }
  799. /**
  800. * testRemoveLastTopParent method
  801. *
  802. * @return void
  803. */
  804. public function testRemoveLastTopParent() {
  805. extract($this->settings);
  806. $this->Tree = new $modelClass();
  807. $this->Tree->initialize(2, 2);
  808. $initialCount = $this->Tree->find('count');
  809. $initialTopNodes = $this->Tree->childCount(false);
  810. $result = $this->Tree->findByName('1. Root');
  811. $this->Tree->removeFromTree($result[$modelClass]['id']);
  812. $laterCount = $this->Tree->find('count');
  813. $laterTopNodes = $this->Tree->childCount(false);
  814. $this->assertEquals($initialCount, $laterCount);
  815. $this->assertEquals($initialTopNodes, $laterTopNodes);
  816. $topNodes = $this->Tree->children(false, true,array('name'));
  817. $expects = array(array($modelClass => array('name' => '1.1')),
  818. array($modelClass => array('name' => '1.2')),
  819. array($modelClass => array('name' => '1. Root')));
  820. $this->assertEquals($topNodes, $expects);
  821. $validTree = $this->Tree->verify();
  822. $this->assertSame($validTree, true);
  823. }
  824. /**
  825. * testRemoveNoChildren method
  826. *
  827. * @return void
  828. */
  829. public function testRemoveNoChildren() {
  830. extract($this->settings);
  831. $this->Tree = new $modelClass();
  832. $this->Tree->initialize(2, 2);
  833. $initialCount = $this->Tree->find('count');
  834. $result = $this->Tree->findByName('1.1.1');
  835. $this->Tree->removeFromTree($result[$modelClass]['id']);
  836. $laterCount = $this->Tree->find('count');
  837. $this->assertEquals($initialCount, $laterCount);
  838. $nodes = $this->Tree->find('list', array('order' => $leftField));
  839. $expects = array(
  840. 1 => '1. Root',
  841. 2 => '1.1',
  842. 4 => '1.1.2',
  843. 5 => '1.2',
  844. 6 => '1.2.1',
  845. 7 => '1.2.2',
  846. 3 => '1.1.1',
  847. );
  848. $this->assertEquals($nodes, $expects);
  849. $validTree = $this->Tree->verify();
  850. $this->assertSame($validTree, true);
  851. }
  852. /**
  853. * testRemoveAndDelete method
  854. *
  855. * @return void
  856. */
  857. public function testRemoveAndDelete() {
  858. extract($this->settings);
  859. $this->Tree = new $modelClass();
  860. $this->Tree->initialize(2, 2);
  861. $initialCount = $this->Tree->find('count');
  862. $result = $this->Tree->findByName('1.1');
  863. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  864. $laterCount = $this->Tree->find('count');
  865. $this->assertEquals($initialCount - 1, $laterCount);
  866. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'), $leftField . ' asc');
  867. $expects = array(
  868. array($modelClass => array('name' => '1.1.1')),
  869. array($modelClass => array('name' => '1.1.2')),
  870. array($modelClass => array('name' => '1.2'))
  871. );
  872. $this->assertEquals($children, $expects);
  873. $topNodes = $this->Tree->children(false, true,array('name'));
  874. $expects = array(array($modelClass => array('name' => '1. Root')));
  875. $this->assertEquals($topNodes, $expects);
  876. $validTree = $this->Tree->verify();
  877. $this->assertSame($validTree, true);
  878. }
  879. /**
  880. * testRemoveAndDeleteNoChildren method
  881. *
  882. * @return void
  883. */
  884. public function testRemoveAndDeleteNoChildren() {
  885. extract($this->settings);
  886. $this->Tree = new $modelClass();
  887. $this->Tree->initialize(2, 2);
  888. $initialCount = $this->Tree->find('count');
  889. $result = $this->Tree->findByName('1.1.1');
  890. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  891. $laterCount = $this->Tree->find('count');
  892. $this->assertEquals($initialCount - 1, $laterCount);
  893. $nodes = $this->Tree->find('list', array('order' => $leftField));
  894. $expects = array(
  895. 1 => '1. Root',
  896. 2 => '1.1',
  897. 4 => '1.1.2',
  898. 5 => '1.2',
  899. 6 => '1.2.1',
  900. 7 => '1.2.2',
  901. );
  902. $this->assertEquals($nodes, $expects);
  903. $validTree = $this->Tree->verify();
  904. $this->assertSame($validTree, true);
  905. }
  906. /**
  907. * testChildren method
  908. *
  909. * @return void
  910. */
  911. public function testChildren() {
  912. extract($this->settings);
  913. $this->Tree = new $modelClass();
  914. $this->Tree->initialize(2, 2);
  915. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  916. $this->Tree->id = $data[$modelClass]['id'];
  917. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  918. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  919. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  920. $this->assertEquals($direct, $expects);
  921. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  922. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  923. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  924. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  925. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  926. array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  927. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12)));
  928. $this->assertEquals($total, $expects);
  929. $this->assertEquals(array(), $this->Tree->children(10000));
  930. }
  931. /**
  932. * testCountChildren method
  933. *
  934. * @return void
  935. */
  936. public function testCountChildren() {
  937. extract($this->settings);
  938. $this->Tree = new $modelClass();
  939. $this->Tree->initialize(2, 2);
  940. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  941. $this->Tree->id = $data[$modelClass]['id'];
  942. $direct = $this->Tree->childCount(null, true);
  943. $this->assertEquals(2, $direct);
  944. $total = $this->Tree->childCount();
  945. $this->assertEquals(6, $total);
  946. $this->Tree->read(null, $data[$modelClass]['id']);
  947. $id = $this->Tree->field('id', array($modelClass . '.name' => '1.2'));
  948. $total = $this->Tree->childCount($id);
  949. $this->assertEquals(2, $total);
  950. }
  951. /**
  952. * testGetParentNode method
  953. *
  954. * @return void
  955. */
  956. public function testGetParentNode() {
  957. extract($this->settings);
  958. $this->Tree = new $modelClass();
  959. $this->Tree->initialize(2, 2);
  960. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  961. $this->Tree->id = $data[$modelClass]['id'];
  962. $result = $this->Tree->getParentNode(null, array('name'));
  963. $expects = array($modelClass => array('name' => '1.2'));
  964. $this->assertSame($expects, $result);
  965. }
  966. /**
  967. * testGetPath method
  968. *
  969. * @return void
  970. */
  971. public function testGetPath() {
  972. extract($this->settings);
  973. $this->Tree = new $modelClass();
  974. $this->Tree->initialize(2, 2);
  975. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  976. $this->Tree->id = $data[$modelClass]['id'];
  977. $result = $this->Tree->getPath(null, array('name'));
  978. $expects = array(array($modelClass => array('name' => '1. Root')),
  979. array($modelClass => array('name' => '1.2')),
  980. array($modelClass => array('name' => '1.2.2')));
  981. $this->assertSame($expects, $result);
  982. }
  983. /**
  984. * testNoAmbiguousColumn method
  985. *
  986. * @return void
  987. */
  988. public function testNoAmbiguousColumn() {
  989. extract($this->settings);
  990. $this->Tree = new $modelClass();
  991. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  992. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  993. $this->Tree->initialize(2, 2);
  994. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  995. $this->Tree->id = $data[$modelClass]['id'];
  996. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  997. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  998. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  999. $this->assertEquals($direct, $expects);
  1000. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  1001. $expects = array(
  1002. array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  1003. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  1004. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  1005. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  1006. array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  1007. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12))
  1008. );
  1009. $this->assertEquals($total, $expects);
  1010. }
  1011. /**
  1012. * testReorderTree method
  1013. *
  1014. * @return void
  1015. */
  1016. public function testReorderTree() {
  1017. extract($this->settings);
  1018. $this->Tree = new $modelClass();
  1019. $this->Tree->initialize(3, 3);
  1020. $nodes = $this->Tree->find('list', array('order' => $leftField));
  1021. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  1022. $this->Tree->moveDown($data[$modelClass]['id']);
  1023. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2.1')));
  1024. $this->Tree->moveDown($data[$modelClass]['id']);
  1025. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.3.2.2')));
  1026. $this->Tree->moveDown($data[$modelClass]['id']);
  1027. $unsortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1028. $this->assertEquals($nodes, $unsortedNodes);
  1029. $this->assertNotEquals(array_keys($nodes), array_keys($unsortedNodes));
  1030. $this->Tree->reorder();
  1031. $sortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1032. $this->assertSame($nodes, $sortedNodes);
  1033. }
  1034. /**
  1035. * test reordering large-ish trees with cacheQueries = true.
  1036. * This caused infinite loops when moving down elements as stale data is returned
  1037. * from the memory cache
  1038. *
  1039. * @return void
  1040. */
  1041. public function testReorderBigTreeWithQueryCaching() {
  1042. extract($this->settings);
  1043. $this->Tree = new $modelClass();
  1044. $this->Tree->initialize(2, 10);
  1045. $original = $this->Tree->cacheQueries;
  1046. $this->Tree->cacheQueries = true;
  1047. $this->Tree->reorder(array('field' => 'name', 'direction' => 'DESC'));
  1048. $this->assertTrue($this->Tree->cacheQueries, 'cacheQueries was not restored after reorder(). %s');
  1049. $this->Tree->cacheQueries = $original;
  1050. }
  1051. /**
  1052. * testGenerateTreeListWithSelfJoin method
  1053. *
  1054. * @return void
  1055. */
  1056. public function testGenerateTreeListWithSelfJoin() {
  1057. extract($this->settings);
  1058. $this->Tree = new $modelClass();
  1059. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  1060. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  1061. $this->Tree->initialize(2, 2);
  1062. $result = $this->Tree->generateTreeList();
  1063. $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');
  1064. $this->assertSame($expected, $result);
  1065. }
  1066. /**
  1067. * Test the formatting options of generateTreeList()
  1068. *
  1069. * @return void
  1070. */
  1071. public function testGenerateTreeListFormatting() {
  1072. extract($this->settings);
  1073. $this->Tree = new $modelClass();
  1074. $this->Tree->initialize(2, 2);
  1075. $result = $this->Tree->generateTreeList(
  1076. null,
  1077. "{n}.$modelClass.id",
  1078. array('%s - %s', "{n}.$modelClass.id", "{n}.$modelClass.name")
  1079. );
  1080. $this->assertEquals('1 - 1. Root', $result[1]);
  1081. $this->assertEquals('_2 - 1.1', $result[2]);
  1082. $this->assertEquals('__3 - 1.1.1', $result[3]);
  1083. }
  1084. /**
  1085. * testArraySyntax method
  1086. *
  1087. * @return void
  1088. */
  1089. public function testArraySyntax() {
  1090. extract($this->settings);
  1091. $this->Tree = new $modelClass();
  1092. $this->Tree->initialize(3, 3);
  1093. $this->assertSame($this->Tree->childCount(2), $this->Tree->childCount(array('id' => 2)));
  1094. $this->assertSame($this->Tree->getParentNode(2), $this->Tree->getParentNode(array('id' => 2)));
  1095. $this->assertSame($this->Tree->getPath(4), $this->Tree->getPath(array('id' => 4)));
  1096. }
  1097. /**
  1098. * testFindThreaded method
  1099. *
  1100. * @return void
  1101. */
  1102. public function testFindThreaded() {
  1103. $Model = new Person();
  1104. $Model->recursive = -1;
  1105. $Model->Behaviors->attach('Tree', array('parent' => 'mother_id'));
  1106. $result = $Model->find('threaded');
  1107. $expected = array(
  1108. array(
  1109. 'Person' => array(
  1110. 'id' => '4',
  1111. 'name' => 'mother - grand mother',
  1112. 'mother_id' => '0',
  1113. 'father_id' => '0'
  1114. ),
  1115. 'children' => array(
  1116. array(
  1117. 'Person' => array(
  1118. 'id' => '2',
  1119. 'name' => 'mother',
  1120. 'mother_id' => '4',
  1121. 'father_id' => '5'
  1122. ),
  1123. 'children' => array(
  1124. array(
  1125. 'Person' => array(
  1126. 'id' => '1',
  1127. 'name' => 'person',
  1128. 'mother_id' => '2',
  1129. 'father_id' => '3'
  1130. ),
  1131. 'children' => array()
  1132. )
  1133. )
  1134. )
  1135. )
  1136. ),
  1137. array(
  1138. 'Person' => array(
  1139. 'id' => '5',
  1140. 'name' => 'mother - grand father',
  1141. 'mother_id' => '0',
  1142. 'father_id' => '0'
  1143. ),
  1144. 'children' => array()
  1145. ),
  1146. array(
  1147. 'Person' => array(
  1148. 'id' => '6',
  1149. 'name' => 'father - grand mother',
  1150. 'mother_id' => '0',
  1151. 'father_id' => '0'
  1152. ),
  1153. 'children' => array(
  1154. array(
  1155. 'Person' => array(
  1156. 'id' => '3',
  1157. 'name' => 'father',
  1158. 'mother_id' => '6',
  1159. 'father_id' => '7'
  1160. ),
  1161. 'children' => array()
  1162. )
  1163. )
  1164. ),
  1165. array(
  1166. 'Person' => array(
  1167. 'id' => '7',
  1168. 'name' => 'father - grand father',
  1169. 'mother_id' => '0',
  1170. 'father_id' => '0'
  1171. ),
  1172. 'children' => array()
  1173. )
  1174. );
  1175. $this->assertEquals($expected, $result);
  1176. }
  1177. }