TreeBehaviorNumberTest.php 46 KB

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