TreeBehaviorNumberTest.php 47 KB

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