TreeBehaviorNumberTest.php 47 KB

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