TreeBehaviorNumberTest.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. <?php
  2. /**
  3. * TreeBehaviorNumberTest file
  4. *
  5. * This is the basic Tree behavior test
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  10. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  17. * @package Cake.Test.Case.Model.Behavior
  18. * @since CakePHP(tm) v 1.2.0.5330
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Model', 'Model');
  22. App::uses('AppModel', 'Model');
  23. require_once(dirname(dirname(__FILE__)) . DS . 'models.php');
  24. /**
  25. * TreeBehaviorNumberTest class
  26. *
  27. * @package Cake.Test.Case.Model.Behavior
  28. */
  29. class TreeBehaviorNumberTest extends CakeTestCase {
  30. /**
  31. * Whether backup global state for each test method or not
  32. *
  33. * @var bool false
  34. */
  35. public $backupGlobals = false;
  36. /**
  37. * settings property
  38. *
  39. * @var array
  40. */
  41. protected $settings = array(
  42. 'modelClass' => 'NumberTree',
  43. 'leftField' => 'lft',
  44. 'rightField' => 'rght',
  45. 'parentField' => 'parent_id'
  46. );
  47. /**
  48. * fixtures property
  49. *
  50. * @var array
  51. */
  52. public $fixtures = array('core.number_tree', 'core.person');
  53. /**
  54. * testInitialize method
  55. *
  56. * @return void
  57. */
  58. public function testInitialize() {
  59. extract($this->settings);
  60. $this->Tree = new $modelClass();
  61. $this->Tree->initialize(2, 2);
  62. $result = $this->Tree->find('count');
  63. $this->assertEquals($result, 7);
  64. $validTree = $this->Tree->verify();
  65. $this->assertSame($validTree, true);
  66. }
  67. /**
  68. * testDetectInvalidLeft method
  69. *
  70. * @return void
  71. */
  72. public function testDetectInvalidLeft() {
  73. extract($this->settings);
  74. $this->Tree = new $modelClass();
  75. $this->Tree->initialize(2, 2);
  76. $result = $this->Tree->findByName('1.1');
  77. $save[$modelClass]['id'] = $result[$modelClass]['id'];
  78. $save[$modelClass][$leftField] = 0;
  79. $this->Tree->save($save);
  80. $result = $this->Tree->verify();
  81. $this->assertNotSame($result, true);
  82. $result = $this->Tree->recover();
  83. $this->assertSame($result, true);
  84. $result = $this->Tree->verify();
  85. $this->assertSame($result, true);
  86. }
  87. /**
  88. * testDetectInvalidRight method
  89. *
  90. * @return void
  91. */
  92. public function testDetectInvalidRight() {
  93. extract($this->settings);
  94. $this->Tree = new $modelClass();
  95. $this->Tree->initialize(2, 2);
  96. $result = $this->Tree->findByName('1.1');
  97. $save[$modelClass]['id'] = $result[$modelClass]['id'];
  98. $save[$modelClass][$rightField] = 0;
  99. $this->Tree->save($save);
  100. $result = $this->Tree->verify();
  101. $this->assertNotSame($result, true);
  102. $result = $this->Tree->recover();
  103. $this->assertSame($result, true);
  104. $result = $this->Tree->verify();
  105. $this->assertSame($result, true);
  106. }
  107. /**
  108. * testDetectInvalidParent method
  109. *
  110. * @return void
  111. */
  112. public function testDetectInvalidParent() {
  113. extract($this->settings);
  114. $this->Tree = new $modelClass();
  115. $this->Tree->initialize(2, 2);
  116. $result = $this->Tree->findByName('1.1');
  117. // Bypass behavior and any other logic
  118. $this->Tree->updateAll(array($parentField => null), array('id' => $result[$modelClass]['id']));
  119. $result = $this->Tree->verify();
  120. $this->assertNotSame($result, true);
  121. $result = $this->Tree->recover();
  122. $this->assertSame($result, true);
  123. $result = $this->Tree->verify();
  124. $this->assertSame($result, true);
  125. }
  126. /**
  127. * testDetectNoneExistantParent method
  128. *
  129. * @return void
  130. */
  131. public function testDetectNoneExistantParent() {
  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. $parent_id = $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, $parent_id);
  352. $direct = $this->Tree->children($parent_id, 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. $parent_id = $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, $parent_id);
  376. $result = $this->Tree->children($parent_id, true, array('id', 'name', $parentField, $leftField, $rightField));
  377. $expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
  378. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
  379. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
  380. $this->assertEquals($expected, $result);
  381. $this->assertTrue($this->Tree->verify());
  382. }
  383. /**
  384. * testInsertWithWhitelist method
  385. *
  386. * @return void
  387. */
  388. public function testInsertWithWhitelist() {
  389. extract($this->settings);
  390. $this->Tree = new $modelClass();
  391. $this->Tree->initialize(2, 2);
  392. $this->Tree->whitelist = array('name', $parentField);
  393. $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
  394. $result = $this->Tree->findByName('testAddOrphan', array('name', $parentField, $leftField, $rightField));
  395. $expected = array('name' => 'testAddOrphan', $parentField => null, $leftField => '15', $rightField => 16);
  396. $this->assertEquals($result[$modelClass], $expected);
  397. $this->assertSame($this->Tree->verify(), true);
  398. }
  399. /**
  400. * testMoveBefore method
  401. *
  402. * @return void
  403. */
  404. public function testMoveBefore() {
  405. extract($this->settings);
  406. $this->Tree = new $modelClass();
  407. $this->Tree->initialize(2, 2);
  408. $this->Tree->id = null;
  409. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
  410. $parent_id = $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, $parent_id);
  414. $result = $this->Tree->children($parent_id, true, array('name'));
  415. $expects = array(array($modelClass => array('name' => '1.1.1')),
  416. array($modelClass => array('name' => '1.1.2')),
  417. array($modelClass => array('name' => '1.2')));
  418. $this->assertEquals($result, $expects);
  419. $validTree = $this->Tree->verify();
  420. $this->assertSame($validTree, true);
  421. }
  422. /**
  423. * testMoveAfter method
  424. *
  425. * @return void
  426. */
  427. public function testMoveAfter() {
  428. extract($this->settings);
  429. $this->Tree = new $modelClass();
  430. $this->Tree->initialize(2, 2);
  431. $this->Tree->id = null;
  432. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
  433. $parent_id = $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, $parent_id);
  437. $result = $this->Tree->children($parent_id, true, array('name'));
  438. $expects = array(array($modelClass => array('name' => '1.2.1')),
  439. array($modelClass => array('name' => '1.2.2')),
  440. array($modelClass => array('name' => '1.1')));
  441. $this->assertEquals($result, $expects);
  442. $validTree = $this->Tree->verify();
  443. $this->assertSame($validTree, true);
  444. }
  445. /**
  446. * testMoveDemoteInvalid method
  447. *
  448. * @return void
  449. */
  450. public function testMoveDemoteInvalid() {
  451. extract($this->settings);
  452. $this->Tree = new $modelClass();
  453. $this->Tree->initialize(2, 2);
  454. $this->Tree->id = null;
  455. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  456. $parent_id = $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 = $parent_id;
  461. //$this->expectError('Trying to save a node under itself in TreeBehavior::beforeSave');
  462. $this->Tree->saveField($parentField, $data[$modelClass]['id']);
  463. $results = $this->Tree->find('all');
  464. $after = $this->Tree->read(null, $data[$modelClass]['id']);
  465. $this->assertEquals($results, $expects);
  466. $this->assertEquals($before, $after);
  467. $validTree = $this->Tree->verify();
  468. $this->assertSame($validTree, true);
  469. }
  470. /**
  471. * testMoveInvalid method
  472. *
  473. * @return void
  474. */
  475. public function testMoveInvalid() {
  476. extract($this->settings);
  477. $this->Tree = new $modelClass();
  478. $this->Tree->initialize(2, 2);
  479. $this->Tree->id = null;
  480. $initialCount = $this->Tree->find('count');
  481. $data= $this->Tree->findByName('1.1');
  482. //$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
  483. $this->Tree->id = $data[$modelClass]['id'];
  484. $this->Tree->saveField($parentField, 999999);
  485. //$this->assertSame($saveSuccess, false);
  486. $laterCount = $this->Tree->find('count');
  487. $this->assertSame($initialCount, $laterCount);
  488. $validTree = $this->Tree->verify();
  489. $this->assertSame($validTree, true);
  490. }
  491. /**
  492. * testMoveSelfInvalid method
  493. *
  494. * @return void
  495. */
  496. public function testMoveSelfInvalid() {
  497. extract($this->settings);
  498. $this->Tree = new $modelClass();
  499. $this->Tree->initialize(2, 2);
  500. $this->Tree->id = null;
  501. $initialCount = $this->Tree->find('count');
  502. $data= $this->Tree->findByName('1.1');
  503. //$this->expectError('Trying to set a node to be the parent of itself in TreeBehavior::beforeSave');
  504. $this->Tree->id = $data[$modelClass]['id'];
  505. $saveSuccess = $this->Tree->saveField($parentField, $this->Tree->id);
  506. $this->assertSame($saveSuccess, false);
  507. $laterCount = $this->Tree->find('count');
  508. $this->assertSame($initialCount, $laterCount);
  509. $validTree = $this->Tree->verify();
  510. $this->assertSame($validTree, true);
  511. }
  512. /**
  513. * testMoveUpSuccess method
  514. *
  515. * @return void
  516. */
  517. public function testMoveUpSuccess() {
  518. extract($this->settings);
  519. $this->Tree = new $modelClass();
  520. $this->Tree->initialize(2, 2);
  521. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
  522. $this->Tree->moveUp($data[$modelClass]['id']);
  523. $parent = $this->Tree->findByName('1. Root', array('id'));
  524. $this->Tree->id = $parent[$modelClass]['id'];
  525. $result = $this->Tree->children(null, true, array('name'));
  526. $expected = array(array($modelClass => array('name' => '1.2', )),
  527. array($modelClass => array('name' => '1.1', )));
  528. $this->assertSame($expected, $result);
  529. }
  530. /**
  531. * testMoveUpFail method
  532. *
  533. * @return void
  534. */
  535. public function testMoveUpFail() {
  536. extract($this->settings);
  537. $this->Tree = new $modelClass();
  538. $this->Tree->initialize(2, 2);
  539. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
  540. $this->Tree->moveUp($data[$modelClass]['id']);
  541. $parent = $this->Tree->findByName('1. Root', array('id'));
  542. $this->Tree->id = $parent[$modelClass]['id'];
  543. $result = $this->Tree->children(null, true, array('name'));
  544. $expected = array(array($modelClass => array('name' => '1.1', )),
  545. array($modelClass => array('name' => '1.2', )));
  546. $this->assertSame($expected, $result);
  547. }
  548. /**
  549. * testMoveUp2 method
  550. *
  551. * @return void
  552. */
  553. public function testMoveUp2() {
  554. extract($this->settings);
  555. $this->Tree = new $modelClass();
  556. $this->Tree->initialize(1, 10);
  557. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  558. $this->Tree->moveUp($data[$modelClass]['id'], 2);
  559. $parent = $this->Tree->findByName('1. Root', array('id'));
  560. $this->Tree->id = $parent[$modelClass]['id'];
  561. $result = $this->Tree->children(null, true, array('name'));
  562. $expected = array(
  563. array($modelClass => array('name' => '1.1', )),
  564. array($modelClass => array('name' => '1.2', )),
  565. array($modelClass => array('name' => '1.5', )),
  566. array($modelClass => array('name' => '1.3', )),
  567. array($modelClass => array('name' => '1.4', )),
  568. array($modelClass => array('name' => '1.6', )),
  569. array($modelClass => array('name' => '1.7', )),
  570. array($modelClass => array('name' => '1.8', )),
  571. array($modelClass => array('name' => '1.9', )),
  572. array($modelClass => array('name' => '1.10', )));
  573. $this->assertSame($expected, $result);
  574. }
  575. /**
  576. * testMoveUpFirst method
  577. *
  578. * @return void
  579. */
  580. public function testMoveUpFirst() {
  581. extract($this->settings);
  582. $this->Tree = new $modelClass();
  583. $this->Tree->initialize(1, 10);
  584. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  585. $this->Tree->moveUp($data[$modelClass]['id'], true);
  586. $parent = $this->Tree->findByName('1. Root', array('id'));
  587. $this->Tree->id = $parent[$modelClass]['id'];
  588. $result = $this->Tree->children(null, true, array('name'));
  589. $expected = array(
  590. array($modelClass => array('name' => '1.5', )),
  591. array($modelClass => array('name' => '1.1', )),
  592. array($modelClass => array('name' => '1.2', )),
  593. array($modelClass => array('name' => '1.3', )),
  594. array($modelClass => array('name' => '1.4', )),
  595. array($modelClass => array('name' => '1.6', )),
  596. array($modelClass => array('name' => '1.7', )),
  597. array($modelClass => array('name' => '1.8', )),
  598. array($modelClass => array('name' => '1.9', )),
  599. array($modelClass => array('name' => '1.10', )));
  600. $this->assertSame($expected, $result);
  601. }
  602. /**
  603. * testMoveDownSuccess method
  604. *
  605. * @return void
  606. */
  607. public function testMoveDownSuccess() {
  608. extract($this->settings);
  609. $this->Tree = new $modelClass();
  610. $this->Tree->initialize(2, 2);
  611. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  612. $this->Tree->moveDown($data[$modelClass]['id']);
  613. $parent = $this->Tree->findByName('1. Root', array('id'));
  614. $this->Tree->id = $parent[$modelClass]['id'];
  615. $result = $this->Tree->children(null, true, array('name'));
  616. $expected = array(array($modelClass => array('name' => '1.2', )),
  617. array($modelClass => array('name' => '1.1', )));
  618. $this->assertSame($expected, $result);
  619. }
  620. /**
  621. * testMoveDownFail method
  622. *
  623. * @return void
  624. */
  625. public function testMoveDownFail() {
  626. extract($this->settings);
  627. $this->Tree = new $modelClass();
  628. $this->Tree->initialize(2, 2);
  629. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
  630. $this->Tree->moveDown($data[$modelClass]['id']);
  631. $parent = $this->Tree->findByName('1. Root', array('id'));
  632. $this->Tree->id = $parent[$modelClass]['id'];
  633. $result = $this->Tree->children(null, true, array('name'));
  634. $expected = array(array($modelClass => array('name' => '1.1', )),
  635. array($modelClass => array('name' => '1.2', )));
  636. $this->assertSame($expected, $result);
  637. }
  638. /**
  639. * testMoveDownLast method
  640. *
  641. * @return void
  642. */
  643. public function testMoveDownLast() {
  644. extract($this->settings);
  645. $this->Tree = new $modelClass();
  646. $this->Tree->initialize(1, 10);
  647. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  648. $this->Tree->moveDown($data[$modelClass]['id'], true);
  649. $parent = $this->Tree->findByName('1. Root', array('id'));
  650. $this->Tree->id = $parent[$modelClass]['id'];
  651. $result = $this->Tree->children(null, true, array('name'));
  652. $expected = array(
  653. array($modelClass => array('name' => '1.1', )),
  654. array($modelClass => array('name' => '1.2', )),
  655. array($modelClass => array('name' => '1.3', )),
  656. array($modelClass => array('name' => '1.4', )),
  657. array($modelClass => array('name' => '1.6', )),
  658. array($modelClass => array('name' => '1.7', )),
  659. array($modelClass => array('name' => '1.8', )),
  660. array($modelClass => array('name' => '1.9', )),
  661. array($modelClass => array('name' => '1.10', )),
  662. array($modelClass => array('name' => '1.5', )));
  663. $this->assertSame($expected, $result);
  664. }
  665. /**
  666. * testMoveDown2 method
  667. *
  668. * @return void
  669. */
  670. public function testMoveDown2() {
  671. extract($this->settings);
  672. $this->Tree = new $modelClass();
  673. $this->Tree->initialize(1, 10);
  674. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  675. $this->Tree->moveDown($data[$modelClass]['id'], 2);
  676. $parent = $this->Tree->findByName('1. Root', array('id'));
  677. $this->Tree->id = $parent[$modelClass]['id'];
  678. $result = $this->Tree->children(null, true, array('name'));
  679. $expected = array(
  680. array($modelClass => array('name' => '1.1', )),
  681. array($modelClass => array('name' => '1.2', )),
  682. array($modelClass => array('name' => '1.3', )),
  683. array($modelClass => array('name' => '1.4', )),
  684. array($modelClass => array('name' => '1.6', )),
  685. array($modelClass => array('name' => '1.7', )),
  686. array($modelClass => array('name' => '1.5', )),
  687. array($modelClass => array('name' => '1.8', )),
  688. array($modelClass => array('name' => '1.9', )),
  689. array($modelClass => array('name' => '1.10', )));
  690. $this->assertSame($expected, $result);
  691. }
  692. /**
  693. * testSaveNoMove method
  694. *
  695. * @return void
  696. */
  697. public function testSaveNoMove() {
  698. extract($this->settings);
  699. $this->Tree = new $modelClass();
  700. $this->Tree->initialize(1, 10);
  701. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  702. $this->Tree->id = $data[$modelClass]['id'];
  703. $this->Tree->saveField('name', 'renamed');
  704. $parent = $this->Tree->findByName('1. Root', array('id'));
  705. $this->Tree->id = $parent[$modelClass]['id'];
  706. $result = $this->Tree->children(null, true, array('name'));
  707. $expected = array(
  708. array($modelClass => array('name' => '1.1', )),
  709. array($modelClass => array('name' => '1.2', )),
  710. array($modelClass => array('name' => '1.3', )),
  711. array($modelClass => array('name' => '1.4', )),
  712. array($modelClass => array('name' => 'renamed', )),
  713. array($modelClass => array('name' => '1.6', )),
  714. array($modelClass => array('name' => '1.7', )),
  715. array($modelClass => array('name' => '1.8', )),
  716. array($modelClass => array('name' => '1.9', )),
  717. array($modelClass => array('name' => '1.10', )));
  718. $this->assertSame($expected, $result);
  719. }
  720. /**
  721. * testMoveToRootAndMoveUp method
  722. *
  723. * @return void
  724. */
  725. public function testMoveToRootAndMoveUp() {
  726. extract($this->settings);
  727. $this->Tree = new $modelClass();
  728. $this->Tree->initialize(1, 1);
  729. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  730. $this->Tree->id = $data[$modelClass]['id'];
  731. $this->Tree->save(array($parentField => null));
  732. $result = $this->Tree->verify();
  733. $this->assertSame($result, true);
  734. $this->Tree->moveUp();
  735. $result = $this->Tree->find('all', array('fields' => 'name', 'order' => $modelClass . '.' . $leftField . ' ASC'));
  736. $expected = array(array($modelClass => array('name' => '1.1')),
  737. array($modelClass => array('name' => '1. Root')));
  738. $this->assertSame($expected, $result);
  739. }
  740. /**
  741. * testDelete method
  742. *
  743. * @return void
  744. */
  745. public function testDelete() {
  746. extract($this->settings);
  747. $this->Tree = new $modelClass();
  748. $this->Tree->initialize(2, 2);
  749. $initialCount = $this->Tree->find('count');
  750. $result = $this->Tree->findByName('1.1.1');
  751. $return = $this->Tree->delete($result[$modelClass]['id']);
  752. $this->assertEquals($return, true);
  753. $laterCount = $this->Tree->find('count');
  754. $this->assertEquals($initialCount - 1, $laterCount);
  755. $validTree= $this->Tree->verify();
  756. $this->assertSame($validTree, true);
  757. $initialCount = $this->Tree->find('count');
  758. $result= $this->Tree->findByName('1.1');
  759. $return = $this->Tree->delete($result[$modelClass]['id']);
  760. $this->assertEquals($return, true);
  761. $laterCount = $this->Tree->find('count');
  762. $this->assertEquals($initialCount - 2, $laterCount);
  763. $validTree = $this->Tree->verify();
  764. $this->assertSame($validTree, true);
  765. }
  766. /**
  767. * testRemove method
  768. *
  769. * @return void
  770. */
  771. public function testRemove() {
  772. extract($this->settings);
  773. $this->Tree = new $modelClass();
  774. $this->Tree->initialize(2, 2);
  775. $initialCount = $this->Tree->find('count');
  776. $result = $this->Tree->findByName('1.1');
  777. $this->Tree->removeFromTree($result[$modelClass]['id']);
  778. $laterCount = $this->Tree->find('count');
  779. $this->assertEquals($initialCount, $laterCount);
  780. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'));
  781. $expects = array(array($modelClass => array('name' => '1.1.1')),
  782. array($modelClass => array('name' => '1.1.2')),
  783. array($modelClass => array('name' => '1.2')));
  784. $this->assertEquals($children, $expects);
  785. $topNodes = $this->Tree->children(false, true,array('name'));
  786. $expects = array(array($modelClass => array('name' => '1. Root')),
  787. array($modelClass => array('name' => '1.1')));
  788. $this->assertEquals($topNodes, $expects);
  789. $validTree = $this->Tree->verify();
  790. $this->assertSame($validTree, true);
  791. }
  792. /**
  793. * testRemoveLastTopParent method
  794. *
  795. * @return void
  796. */
  797. public function testRemoveLastTopParent() {
  798. extract($this->settings);
  799. $this->Tree = new $modelClass();
  800. $this->Tree->initialize(2, 2);
  801. $initialCount = $this->Tree->find('count');
  802. $initialTopNodes = $this->Tree->childCount(false);
  803. $result = $this->Tree->findByName('1. Root');
  804. $this->Tree->removeFromTree($result[$modelClass]['id']);
  805. $laterCount = $this->Tree->find('count');
  806. $laterTopNodes = $this->Tree->childCount(false);
  807. $this->assertEquals($initialCount, $laterCount);
  808. $this->assertEquals($initialTopNodes, $laterTopNodes);
  809. $topNodes = $this->Tree->children(false, true,array('name'));
  810. $expects = array(array($modelClass => array('name' => '1.1')),
  811. array($modelClass => array('name' => '1.2')),
  812. array($modelClass => array('name' => '1. Root')));
  813. $this->assertEquals($topNodes, $expects);
  814. $validTree = $this->Tree->verify();
  815. $this->assertSame($validTree, true);
  816. }
  817. /**
  818. * testRemoveNoChildren method
  819. *
  820. * @return void
  821. */
  822. public function testRemoveNoChildren() {
  823. extract($this->settings);
  824. $this->Tree = new $modelClass();
  825. $this->Tree->initialize(2, 2);
  826. $initialCount = $this->Tree->find('count');
  827. $result = $this->Tree->findByName('1.1.1');
  828. $this->Tree->removeFromTree($result[$modelClass]['id']);
  829. $laterCount = $this->Tree->find('count');
  830. $this->assertEquals($initialCount, $laterCount);
  831. $nodes = $this->Tree->find('list', array('order' => $leftField));
  832. $expects = array(
  833. 1 => '1. Root',
  834. 2 => '1.1',
  835. 4 => '1.1.2',
  836. 5 => '1.2',
  837. 6 => '1.2.1',
  838. 7 => '1.2.2',
  839. 3 => '1.1.1',
  840. );
  841. $this->assertEquals($nodes, $expects);
  842. $validTree = $this->Tree->verify();
  843. $this->assertSame($validTree, true);
  844. }
  845. /**
  846. * testRemoveAndDelete method
  847. *
  848. * @return void
  849. */
  850. public function testRemoveAndDelete() {
  851. extract($this->settings);
  852. $this->Tree = new $modelClass();
  853. $this->Tree->initialize(2, 2);
  854. $initialCount = $this->Tree->find('count');
  855. $result = $this->Tree->findByName('1.1');
  856. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  857. $laterCount = $this->Tree->find('count');
  858. $this->assertEquals($initialCount-1, $laterCount);
  859. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'), $leftField . ' asc');
  860. $expects= array(array($modelClass => array('name' => '1.1.1')),
  861. array($modelClass => array('name' => '1.1.2')),
  862. array($modelClass => array('name' => '1.2')));
  863. $this->assertEquals($children, $expects);
  864. $topNodes = $this->Tree->children(false, true,array('name'));
  865. $expects = array(array($modelClass => array('name' => '1. Root')));
  866. $this->assertEquals($topNodes, $expects);
  867. $validTree = $this->Tree->verify();
  868. $this->assertSame($validTree, true);
  869. }
  870. /**
  871. * testRemoveAndDeleteNoChildren method
  872. *
  873. * @return void
  874. */
  875. public function testRemoveAndDeleteNoChildren() {
  876. extract($this->settings);
  877. $this->Tree = new $modelClass();
  878. $this->Tree->initialize(2, 2);
  879. $initialCount = $this->Tree->find('count');
  880. $result = $this->Tree->findByName('1.1.1');
  881. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  882. $laterCount = $this->Tree->find('count');
  883. $this->assertEquals($initialCount - 1, $laterCount);
  884. $nodes = $this->Tree->find('list', array('order' => $leftField));
  885. $expects = array(
  886. 1 => '1. Root',
  887. 2 => '1.1',
  888. 4 => '1.1.2',
  889. 5 => '1.2',
  890. 6 => '1.2.1',
  891. 7 => '1.2.2',
  892. );
  893. $this->assertEquals($nodes, $expects);
  894. $validTree = $this->Tree->verify();
  895. $this->assertSame($validTree, true);
  896. }
  897. /**
  898. * testChildren method
  899. *
  900. * @return void
  901. */
  902. public function testChildren() {
  903. extract($this->settings);
  904. $this->Tree = new $modelClass();
  905. $this->Tree->initialize(2, 2);
  906. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  907. $this->Tree->id= $data[$modelClass]['id'];
  908. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  909. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  910. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  911. $this->assertEquals($direct, $expects);
  912. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  913. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  914. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  915. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  916. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  917. array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  918. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12)));
  919. $this->assertEquals($total, $expects);
  920. $this->assertEquals(array(), $this->Tree->children(10000));
  921. }
  922. /**
  923. * testCountChildren method
  924. *
  925. * @return void
  926. */
  927. public function testCountChildren() {
  928. extract($this->settings);
  929. $this->Tree = new $modelClass();
  930. $this->Tree->initialize(2, 2);
  931. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  932. $this->Tree->id = $data[$modelClass]['id'];
  933. $direct = $this->Tree->childCount(null, true);
  934. $this->assertEquals($direct, 2);
  935. $total = $this->Tree->childCount();
  936. $this->assertEquals($total, 6);
  937. $this->Tree->read(null, $data[$modelClass]['id']);
  938. $id = $this->Tree->field('id', array($modelClass . '.name' => '1.2'));
  939. $total = $this->Tree->childCount($id);
  940. $this->assertEquals($total, 2);
  941. }
  942. /**
  943. * testGetParentNode method
  944. *
  945. * @return void
  946. */
  947. public function testGetParentNode() {
  948. extract($this->settings);
  949. $this->Tree = new $modelClass();
  950. $this->Tree->initialize(2, 2);
  951. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  952. $this->Tree->id= $data[$modelClass]['id'];
  953. $result = $this->Tree->getParentNode(null, array('name'));
  954. $expects = array($modelClass => array('name' => '1.2'));
  955. $this->assertSame($result, $expects);
  956. }
  957. /**
  958. * testGetPath method
  959. *
  960. * @return void
  961. */
  962. public function testGetPath() {
  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.2.2')));
  967. $this->Tree->id= $data[$modelClass]['id'];
  968. $result = $this->Tree->getPath(null, array('name'));
  969. $expects = array(array($modelClass => array('name' => '1. Root')),
  970. array($modelClass => array('name' => '1.2')),
  971. array($modelClass => array('name' => '1.2.2')));
  972. $this->assertSame($result, $expects);
  973. }
  974. /**
  975. * testNoAmbiguousColumn method
  976. *
  977. * @return void
  978. */
  979. public function testNoAmbiguousColumn() {
  980. extract($this->settings);
  981. $this->Tree = new $modelClass();
  982. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  983. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  984. $this->Tree->initialize(2, 2);
  985. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  986. $this->Tree->id= $data[$modelClass]['id'];
  987. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  988. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  989. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  990. $this->assertEquals($direct, $expects);
  991. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  992. $expects = array(
  993. array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  994. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  995. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  996. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  997. array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  998. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12))
  999. );
  1000. $this->assertEquals($total, $expects);
  1001. }
  1002. /**
  1003. * testReorderTree method
  1004. *
  1005. * @return void
  1006. */
  1007. public function testReorderTree() {
  1008. extract($this->settings);
  1009. $this->Tree = new $modelClass();
  1010. $this->Tree->initialize(3, 3);
  1011. $nodes = $this->Tree->find('list', array('order' => $leftField));
  1012. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  1013. $this->Tree->moveDown($data[$modelClass]['id']);
  1014. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2.1')));
  1015. $this->Tree->moveDown($data[$modelClass]['id']);
  1016. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.3.2.2')));
  1017. $this->Tree->moveDown($data[$modelClass]['id']);
  1018. $unsortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1019. $this->assertEquals($nodes, $unsortedNodes);
  1020. $this->assertNotEquals(array_keys($nodes), array_keys($unsortedNodes));
  1021. $this->Tree->reorder();
  1022. $sortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1023. $this->assertSame($nodes, $sortedNodes);
  1024. }
  1025. /**
  1026. * test reordering large-ish trees with cacheQueries = true.
  1027. * This caused infinite loops when moving down elements as stale data is returned
  1028. * from the memory cache
  1029. *
  1030. * @return void
  1031. */
  1032. public function testReorderBigTreeWithQueryCaching() {
  1033. extract($this->settings);
  1034. $this->Tree = new $modelClass();
  1035. $this->Tree->initialize(2, 10);
  1036. $original = $this->Tree->cacheQueries;
  1037. $this->Tree->cacheQueries = true;
  1038. $this->Tree->reorder(array('field' => 'name', 'direction' => 'DESC'));
  1039. $this->assertTrue($this->Tree->cacheQueries, 'cacheQueries was not restored after reorder(). %s');
  1040. $this->Tree->cacheQueries = $original;
  1041. }
  1042. /**
  1043. * testGenerateTreeListWithSelfJoin method
  1044. *
  1045. * @return void
  1046. */
  1047. public function testGenerateTreeListWithSelfJoin() {
  1048. extract($this->settings);
  1049. $this->Tree = new $modelClass();
  1050. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  1051. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  1052. $this->Tree->initialize(2, 2);
  1053. $result = $this->Tree->generateTreeList();
  1054. $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');
  1055. $this->assertSame($expected, $result);
  1056. }
  1057. /**
  1058. * testArraySyntax method
  1059. *
  1060. * @return void
  1061. */
  1062. public function testArraySyntax() {
  1063. extract($this->settings);
  1064. $this->Tree = new $modelClass();
  1065. $this->Tree->initialize(3, 3);
  1066. $this->assertSame($this->Tree->childCount(2), $this->Tree->childCount(array('id' => 2)));
  1067. $this->assertSame($this->Tree->getParentNode(2), $this->Tree->getParentNode(array('id' => 2)));
  1068. $this->assertSame($this->Tree->getPath(4), $this->Tree->getPath(array('id' => 4)));
  1069. }
  1070. /**
  1071. * testFindThreaded method
  1072. *
  1073. * @return void
  1074. */
  1075. public function testFindThreaded() {
  1076. $Model = new Person();
  1077. $Model->recursive = -1;
  1078. $Model->Behaviors->attach('Tree', array('parent' => 'mother_id'));
  1079. $result = $Model->find('threaded');
  1080. $expected = array(
  1081. array(
  1082. 'Person' => array(
  1083. 'id' => '4',
  1084. 'name' => 'mother - grand mother',
  1085. 'mother_id' => '0',
  1086. 'father_id' => '0'
  1087. ),
  1088. 'children' => array(
  1089. array(
  1090. 'Person' => array(
  1091. 'id' => '2',
  1092. 'name' => 'mother',
  1093. 'mother_id' => '4',
  1094. 'father_id' => '5'
  1095. ),
  1096. 'children' => array(
  1097. array(
  1098. 'Person' => array(
  1099. 'id' => '1',
  1100. 'name' => 'person',
  1101. 'mother_id' => '2',
  1102. 'father_id' => '3'
  1103. ),
  1104. 'children' => array()
  1105. )
  1106. )
  1107. )
  1108. )
  1109. ),
  1110. array(
  1111. 'Person' => array(
  1112. 'id' => '5',
  1113. 'name' => 'mother - grand father',
  1114. 'mother_id' => '0',
  1115. 'father_id' => '0'
  1116. ),
  1117. 'children' => array()
  1118. ),
  1119. array(
  1120. 'Person' => array(
  1121. 'id' => '6',
  1122. 'name' => 'father - grand mother',
  1123. 'mother_id' => '0',
  1124. 'father_id' => '0'
  1125. ),
  1126. 'children' => array(
  1127. array(
  1128. 'Person' => array(
  1129. 'id' => '3',
  1130. 'name' => 'father',
  1131. 'mother_id' => '6',
  1132. 'father_id' => '7'
  1133. ),
  1134. 'children' => array()
  1135. )
  1136. )
  1137. ),
  1138. array(
  1139. 'Person' => array(
  1140. 'id' => '7',
  1141. 'name' => 'father - grand father',
  1142. 'mother_id' => '0',
  1143. 'father_id' => '0'
  1144. ),
  1145. 'children' => array()
  1146. )
  1147. );
  1148. $this->assertEquals($expected, $result);
  1149. }
  1150. }