TreeBehaviorNumberTest.php 46 KB

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