TreeBehaviorNumberTest.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  1. <?php
  2. /**
  3. * TreeBehaviorNumberTest file
  4. *
  5. * This is the basic Tree behavior test
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  10. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice
  14. *
  15. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  17. * @package cake.tests.cases.libs.model.behaviors
  18. * @since CakePHP(tm) v 1.2.0.5330
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Model', 'Model');
  22. App::uses('AppModel', 'Model');
  23. require_once(dirname(dirname(__FILE__)) . DS . 'models.php');
  24. /**
  25. * TreeBehaviorNumberTest class
  26. *
  27. * @package cake.tests.cases.libs.model.behaviors
  28. */
  29. class TreeBehaviorNumberTest extends CakeTestCase {
  30. /**
  31. * Whether backup global state for each test method or not
  32. *
  33. * @var bool false
  34. * @access public
  35. */
  36. public $backupGlobals = false;
  37. /**
  38. * settings property
  39. *
  40. * @var array
  41. * @access protected
  42. */
  43. protected $settings = array(
  44. 'modelClass' => 'NumberTree',
  45. 'leftField' => 'lft',
  46. 'rightField' => 'rght',
  47. 'parentField' => 'parent_id'
  48. );
  49. /**
  50. * fixtures property
  51. *
  52. * @var array
  53. * @access public
  54. */
  55. public $fixtures = array('core.number_tree');
  56. /**
  57. * testInitialize method
  58. *
  59. * @access public
  60. * @return void
  61. */
  62. function testInitialize() {
  63. extract($this->settings);
  64. $this->Tree = new $modelClass();
  65. $this->Tree->initialize(2, 2);
  66. $result = $this->Tree->find('count');
  67. $this->assertEqual($result, 7);
  68. $validTree = $this->Tree->verify();
  69. $this->assertIdentical($validTree, true);
  70. }
  71. /**
  72. * testDetectInvalidLeft method
  73. *
  74. * @access public
  75. * @return void
  76. */
  77. function testDetectInvalidLeft() {
  78. extract($this->settings);
  79. $this->Tree = new $modelClass();
  80. $this->Tree->initialize(2, 2);
  81. $result = $this->Tree->findByName('1.1');
  82. $save[$modelClass]['id'] = $result[$modelClass]['id'];
  83. $save[$modelClass][$leftField] = 0;
  84. $this->Tree->save($save);
  85. $result = $this->Tree->verify();
  86. $this->assertNotIdentical($result, true);
  87. $result = $this->Tree->recover();
  88. $this->assertIdentical($result, true);
  89. $result = $this->Tree->verify();
  90. $this->assertIdentical($result, true);
  91. }
  92. /**
  93. * testDetectInvalidRight method
  94. *
  95. * @access public
  96. * @return void
  97. */
  98. function testDetectInvalidRight() {
  99. extract($this->settings);
  100. $this->Tree = new $modelClass();
  101. $this->Tree->initialize(2, 2);
  102. $result = $this->Tree->findByName('1.1');
  103. $save[$modelClass]['id'] = $result[$modelClass]['id'];
  104. $save[$modelClass][$rightField] = 0;
  105. $this->Tree->save($save);
  106. $result = $this->Tree->verify();
  107. $this->assertNotIdentical($result, true);
  108. $result = $this->Tree->recover();
  109. $this->assertIdentical($result, true);
  110. $result = $this->Tree->verify();
  111. $this->assertIdentical($result, true);
  112. }
  113. /**
  114. * testDetectInvalidParent method
  115. *
  116. * @access public
  117. * @return void
  118. */
  119. function testDetectInvalidParent() {
  120. extract($this->settings);
  121. $this->Tree = new $modelClass();
  122. $this->Tree->initialize(2, 2);
  123. $result = $this->Tree->findByName('1.1');
  124. // Bypass behavior and any other logic
  125. $this->Tree->updateAll(array($parentField => null), array('id' => $result[$modelClass]['id']));
  126. $result = $this->Tree->verify();
  127. $this->assertNotIdentical($result, true);
  128. $result = $this->Tree->recover();
  129. $this->assertIdentical($result, true);
  130. $result = $this->Tree->verify();
  131. $this->assertIdentical($result, true);
  132. }
  133. /**
  134. * testDetectNoneExistantParent method
  135. *
  136. * @access public
  137. * @return void
  138. */
  139. function testDetectNoneExistantParent() {
  140. extract($this->settings);
  141. $this->Tree = new $modelClass();
  142. $this->Tree->initialize(2, 2);
  143. $result = $this->Tree->findByName('1.1');
  144. $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
  145. $result = $this->Tree->verify();
  146. $this->assertNotIdentical($result, true);
  147. $result = $this->Tree->recover('MPTT');
  148. $this->assertIdentical($result, true);
  149. $result = $this->Tree->verify();
  150. $this->assertIdentical($result, true);
  151. }
  152. /**
  153. * testRecoverFromMissingParent method
  154. *
  155. * @access public
  156. * @return void
  157. */
  158. function testRecoverFromMissingParent() {
  159. extract($this->settings);
  160. $this->Tree = new $modelClass();
  161. $this->Tree->initialize(2, 2);
  162. $result = $this->Tree->findByName('1.1');
  163. $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
  164. $result = $this->Tree->verify();
  165. $this->assertNotIdentical($result, true);
  166. $result = $this->Tree->recover();
  167. $this->assertIdentical($result, true);
  168. $result = $this->Tree->verify();
  169. $this->assertIdentical($result, true);
  170. }
  171. /**
  172. * testDetectInvalidParents method
  173. *
  174. * @access public
  175. * @return void
  176. */
  177. function testDetectInvalidParents() {
  178. extract($this->settings);
  179. $this->Tree = new $modelClass();
  180. $this->Tree->initialize(2, 2);
  181. $this->Tree->updateAll(array($parentField => null));
  182. $result = $this->Tree->verify();
  183. $this->assertNotIdentical($result, true);
  184. $result = $this->Tree->recover();
  185. $this->assertIdentical($result, true);
  186. $result = $this->Tree->verify();
  187. $this->assertIdentical($result, true);
  188. }
  189. /**
  190. * testDetectInvalidLftsRghts method
  191. *
  192. * @access public
  193. * @return void
  194. */
  195. function testDetectInvalidLftsRghts() {
  196. extract($this->settings);
  197. $this->Tree = new $modelClass();
  198. $this->Tree->initialize(2, 2);
  199. $this->Tree->updateAll(array($leftField => 0, $rightField => 0));
  200. $result = $this->Tree->verify();
  201. $this->assertNotIdentical($result, true);
  202. $this->Tree->recover();
  203. $result = $this->Tree->verify();
  204. $this->assertIdentical($result, true);
  205. }
  206. /**
  207. * Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
  208. *
  209. * @access public
  210. * @return void
  211. */
  212. function testDetectEqualLftsRghts() {
  213. extract($this->settings);
  214. $this->Tree = new $modelClass();
  215. $this->Tree->initialize(1, 3);
  216. $result = $this->Tree->findByName('1.1');
  217. $this->Tree->updateAll(array($rightField => $result[$modelClass][$leftField]), array('id' => $result[$modelClass]['id']));
  218. $this->Tree->updateAll(array($leftField => $this->Tree->escapeField($leftField) . ' -1'),
  219. array($leftField . ' >' => $result[$modelClass][$leftField]));
  220. $this->Tree->updateAll(array($rightField => $this->Tree->escapeField($rightField) . ' -1'),
  221. array($rightField . ' >' => $result[$modelClass][$leftField]));
  222. $result = $this->Tree->verify();
  223. $this->assertNotIdentical($result, true);
  224. $result = $this->Tree->recover();
  225. $this->assertTrue($result);
  226. $result = $this->Tree->verify();
  227. $this->assertTrue($result);
  228. }
  229. /**
  230. * testAddOrphan method
  231. *
  232. * @access public
  233. * @return void
  234. */
  235. function testAddOrphan() {
  236. extract($this->settings);
  237. $this->Tree = new $modelClass();
  238. $this->Tree->initialize(2, 2);
  239. $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
  240. $result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
  241. $expected = array($modelClass => array('name' => 'testAddOrphan', $parentField => null));
  242. $this->assertEqual($result, $expected);
  243. $validTree = $this->Tree->verify();
  244. $this->assertIdentical($validTree, true);
  245. }
  246. /**
  247. * testAddMiddle method
  248. *
  249. * @access public
  250. * @return void
  251. */
  252. function testAddMiddle() {
  253. extract($this->settings);
  254. $this->Tree = new $modelClass();
  255. $this->Tree->initialize(2, 2);
  256. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  257. $initialCount = $this->Tree->find('count');
  258. $this->Tree->create();
  259. $result = $this->Tree->save(array($modelClass => array('name' => 'testAddMiddle', $parentField => $data[$modelClass]['id'])));
  260. $expected = array_merge(array($modelClass => array('name' => 'testAddMiddle', $parentField => '2')), $result);
  261. $this->assertIdentical($result, $expected);
  262. $laterCount = $this->Tree->find('count');
  263. $laterCount = $this->Tree->find('count');
  264. $this->assertEqual($initialCount + 1, $laterCount);
  265. $children = $this->Tree->children($data[$modelClass]['id'], true, array('name'));
  266. $expects = array(array($modelClass => array('name' => '1.1.1')),
  267. array($modelClass => array('name' => '1.1.2')),
  268. array($modelClass => array('name' => 'testAddMiddle')));
  269. $this->assertIdentical($children, $expects);
  270. $validTree = $this->Tree->verify();
  271. $this->assertIdentical($validTree, true);
  272. }
  273. /**
  274. * testAddInvalid method
  275. *
  276. * @access public
  277. * @return void
  278. */
  279. function testAddInvalid() {
  280. extract($this->settings);
  281. $this->Tree = new $modelClass();
  282. $this->Tree->initialize(2, 2);
  283. $this->Tree->id = null;
  284. $initialCount = $this->Tree->find('count');
  285. //$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
  286. $saveSuccess = $this->Tree->save(array($modelClass => array('name' => 'testAddInvalid', $parentField => 99999)));
  287. $this->assertIdentical($saveSuccess, false);
  288. $laterCount = $this->Tree->find('count');
  289. $this->assertIdentical($initialCount, $laterCount);
  290. $validTree = $this->Tree->verify();
  291. $this->assertIdentical($validTree, true);
  292. }
  293. /**
  294. * testAddNotIndexedByModel method
  295. *
  296. * @access public
  297. * @return void
  298. */
  299. function testAddNotIndexedByModel() {
  300. extract($this->settings);
  301. $this->Tree = new $modelClass();
  302. $this->Tree->initialize(2, 2);
  303. $this->Tree->save(array('name' => 'testAddNotIndexed', $parentField => null));
  304. $result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
  305. $expected = array($modelClass => array('name' => 'testAddNotIndexed', $parentField => null));
  306. $this->assertEqual($result, $expected);
  307. $validTree = $this->Tree->verify();
  308. $this->assertIdentical($validTree, true);
  309. }
  310. /**
  311. * testMovePromote method
  312. *
  313. * @access public
  314. * @return void
  315. */
  316. function testMovePromote() {
  317. extract($this->settings);
  318. $this->Tree = new $modelClass();
  319. $this->Tree->initialize(2, 2);
  320. $this->Tree->id = null;
  321. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  322. $parent_id = $parent[$modelClass]['id'];
  323. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  324. $this->Tree->id= $data[$modelClass]['id'];
  325. $this->Tree->saveField($parentField, $parent_id);
  326. $direct = $this->Tree->children($parent_id, true, array('id', 'name', $parentField, $leftField, $rightField));
  327. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
  328. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
  329. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
  330. $this->assertEqual($direct, $expects);
  331. $validTree = $this->Tree->verify();
  332. $this->assertIdentical($validTree, true);
  333. }
  334. /**
  335. * testMoveWithWhitelist method
  336. *
  337. * @access public
  338. * @return void
  339. */
  340. function testMoveWithWhitelist() {
  341. extract($this->settings);
  342. $this->Tree = new $modelClass();
  343. $this->Tree->initialize(2, 2);
  344. $this->Tree->id = null;
  345. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  346. $parent_id = $parent[$modelClass]['id'];
  347. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  348. $this->Tree->id = $data[$modelClass]['id'];
  349. $this->Tree->whitelist = array($parentField, 'name', 'description');
  350. $this->Tree->saveField($parentField, $parent_id);
  351. $result = $this->Tree->children($parent_id, true, array('id', 'name', $parentField, $leftField, $rightField));
  352. $expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
  353. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
  354. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
  355. $this->assertEqual($result, $expected);
  356. $this->assertTrue($this->Tree->verify());
  357. }
  358. /**
  359. * testInsertWithWhitelist method
  360. *
  361. * @access public
  362. * @return void
  363. */
  364. function testInsertWithWhitelist() {
  365. extract($this->settings);
  366. $this->Tree = new $modelClass();
  367. $this->Tree->initialize(2, 2);
  368. $this->Tree->whitelist = array('name', $parentField);
  369. $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
  370. $result = $this->Tree->findByName('testAddOrphan', array('name', $parentField, $leftField, $rightField));
  371. $expected = array('name' => 'testAddOrphan', $parentField => null, $leftField => '15', $rightField => 16);
  372. $this->assertEqual($result[$modelClass], $expected);
  373. $this->assertIdentical($this->Tree->verify(), true);
  374. }
  375. /**
  376. * testMoveBefore method
  377. *
  378. * @access public
  379. * @return void
  380. */
  381. function testMoveBefore() {
  382. extract($this->settings);
  383. $this->Tree = new $modelClass();
  384. $this->Tree->initialize(2, 2);
  385. $this->Tree->id = null;
  386. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
  387. $parent_id = $parent[$modelClass]['id'];
  388. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
  389. $this->Tree->id = $data[$modelClass]['id'];
  390. $this->Tree->saveField($parentField, $parent_id);
  391. $result = $this->Tree->children($parent_id, true, array('name'));
  392. $expects = array(array($modelClass => array('name' => '1.1.1')),
  393. array($modelClass => array('name' => '1.1.2')),
  394. array($modelClass => array('name' => '1.2')));
  395. $this->assertEqual($result, $expects);
  396. $validTree = $this->Tree->verify();
  397. $this->assertIdentical($validTree, true);
  398. }
  399. /**
  400. * testMoveAfter method
  401. *
  402. * @access public
  403. * @return void
  404. */
  405. function testMoveAfter() {
  406. extract($this->settings);
  407. $this->Tree = new $modelClass();
  408. $this->Tree->initialize(2, 2);
  409. $this->Tree->id = null;
  410. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
  411. $parent_id = $parent[$modelClass]['id'];
  412. $data= $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  413. $this->Tree->id = $data[$modelClass]['id'];
  414. $this->Tree->saveField($parentField, $parent_id);
  415. $result = $this->Tree->children($parent_id, true, array('name'));
  416. $expects = array(array($modelClass => array('name' => '1.2.1')),
  417. array($modelClass => array('name' => '1.2.2')),
  418. array($modelClass => array('name' => '1.1')));
  419. $this->assertEqual($result, $expects);
  420. $validTree = $this->Tree->verify();
  421. $this->assertIdentical($validTree, true);
  422. }
  423. /**
  424. * testMoveDemoteInvalid method
  425. *
  426. * @access public
  427. * @return void
  428. */
  429. function testMoveDemoteInvalid() {
  430. extract($this->settings);
  431. $this->Tree = new $modelClass();
  432. $this->Tree->initialize(2, 2);
  433. $this->Tree->id = null;
  434. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  435. $parent_id = $parent[$modelClass]['id'];
  436. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  437. $expects = $this->Tree->find('all');
  438. $before = $this->Tree->read(null, $data[$modelClass]['id']);
  439. $this->Tree->id = $parent_id;
  440. //$this->expectError('Trying to save a node under itself in TreeBehavior::beforeSave');
  441. $this->Tree->saveField($parentField, $data[$modelClass]['id']);
  442. $results = $this->Tree->find('all');
  443. $after = $this->Tree->read(null, $data[$modelClass]['id']);
  444. $this->assertEqual($results, $expects);
  445. $this->assertEqual($before, $after);
  446. $validTree = $this->Tree->verify();
  447. $this->assertIdentical($validTree, true);
  448. }
  449. /**
  450. * testMoveInvalid method
  451. *
  452. * @access public
  453. * @return void
  454. */
  455. function testMoveInvalid() {
  456. extract($this->settings);
  457. $this->Tree = new $modelClass();
  458. $this->Tree->initialize(2, 2);
  459. $this->Tree->id = null;
  460. $initialCount = $this->Tree->find('count');
  461. $data= $this->Tree->findByName('1.1');
  462. //$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
  463. $this->Tree->id = $data[$modelClass]['id'];
  464. $this->Tree->saveField($parentField, 999999);
  465. //$this->assertIdentical($saveSuccess, false);
  466. $laterCount = $this->Tree->find('count');
  467. $this->assertIdentical($initialCount, $laterCount);
  468. $validTree = $this->Tree->verify();
  469. $this->assertIdentical($validTree, true);
  470. }
  471. /**
  472. * testMoveSelfInvalid method
  473. *
  474. * @access public
  475. * @return void
  476. */
  477. function testMoveSelfInvalid() {
  478. extract($this->settings);
  479. $this->Tree = new $modelClass();
  480. $this->Tree->initialize(2, 2);
  481. $this->Tree->id = null;
  482. $initialCount = $this->Tree->find('count');
  483. $data= $this->Tree->findByName('1.1');
  484. //$this->expectError('Trying to set a node to be the parent of itself in TreeBehavior::beforeSave');
  485. $this->Tree->id = $data[$modelClass]['id'];
  486. $saveSuccess = $this->Tree->saveField($parentField, $this->Tree->id);
  487. $this->assertIdentical($saveSuccess, false);
  488. $laterCount = $this->Tree->find('count');
  489. $this->assertIdentical($initialCount, $laterCount);
  490. $validTree = $this->Tree->verify();
  491. $this->assertIdentical($validTree, true);
  492. }
  493. /**
  494. * testMoveUpSuccess method
  495. *
  496. * @access public
  497. * @return void
  498. */
  499. function testMoveUpSuccess() {
  500. extract($this->settings);
  501. $this->Tree = new $modelClass();
  502. $this->Tree->initialize(2, 2);
  503. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
  504. $this->Tree->moveUp($data[$modelClass]['id']);
  505. $parent = $this->Tree->findByName('1. Root', array('id'));
  506. $this->Tree->id = $parent[$modelClass]['id'];
  507. $result = $this->Tree->children(null, true, array('name'));
  508. $expected = array(array($modelClass => array('name' => '1.2', )),
  509. array($modelClass => array('name' => '1.1', )));
  510. $this->assertIdentical($result, $expected);
  511. }
  512. /**
  513. * testMoveUpFail method
  514. *
  515. * @access public
  516. * @return void
  517. */
  518. function testMoveUpFail() {
  519. extract($this->settings);
  520. $this->Tree = new $modelClass();
  521. $this->Tree->initialize(2, 2);
  522. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
  523. $this->Tree->moveUp($data[$modelClass]['id']);
  524. $parent = $this->Tree->findByName('1. Root', array('id'));
  525. $this->Tree->id = $parent[$modelClass]['id'];
  526. $result = $this->Tree->children(null, true, array('name'));
  527. $expected = array(array($modelClass => array('name' => '1.1', )),
  528. array($modelClass => array('name' => '1.2', )));
  529. $this->assertIdentical($result, $expected);
  530. }
  531. /**
  532. * testMoveUp2 method
  533. *
  534. * @access public
  535. * @return void
  536. */
  537. function testMoveUp2() {
  538. extract($this->settings);
  539. $this->Tree = new $modelClass();
  540. $this->Tree->initialize(1, 10);
  541. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  542. $this->Tree->moveUp($data[$modelClass]['id'], 2);
  543. $parent = $this->Tree->findByName('1. Root', array('id'));
  544. $this->Tree->id = $parent[$modelClass]['id'];
  545. $result = $this->Tree->children(null, true, array('name'));
  546. $expected = array(
  547. array($modelClass => array('name' => '1.1', )),
  548. array($modelClass => array('name' => '1.2', )),
  549. array($modelClass => array('name' => '1.5', )),
  550. array($modelClass => array('name' => '1.3', )),
  551. array($modelClass => array('name' => '1.4', )),
  552. array($modelClass => array('name' => '1.6', )),
  553. array($modelClass => array('name' => '1.7', )),
  554. array($modelClass => array('name' => '1.8', )),
  555. array($modelClass => array('name' => '1.9', )),
  556. array($modelClass => array('name' => '1.10', )));
  557. $this->assertIdentical($result, $expected);
  558. }
  559. /**
  560. * testMoveUpFirst method
  561. *
  562. * @access public
  563. * @return void
  564. */
  565. function testMoveUpFirst() {
  566. extract($this->settings);
  567. $this->Tree = new $modelClass();
  568. $this->Tree->initialize(1, 10);
  569. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  570. $this->Tree->moveUp($data[$modelClass]['id'], true);
  571. $parent = $this->Tree->findByName('1. Root', array('id'));
  572. $this->Tree->id = $parent[$modelClass]['id'];
  573. $result = $this->Tree->children(null, true, array('name'));
  574. $expected = array(
  575. array($modelClass => array('name' => '1.5', )),
  576. array($modelClass => array('name' => '1.1', )),
  577. array($modelClass => array('name' => '1.2', )),
  578. array($modelClass => array('name' => '1.3', )),
  579. array($modelClass => array('name' => '1.4', )),
  580. array($modelClass => array('name' => '1.6', )),
  581. array($modelClass => array('name' => '1.7', )),
  582. array($modelClass => array('name' => '1.8', )),
  583. array($modelClass => array('name' => '1.9', )),
  584. array($modelClass => array('name' => '1.10', )));
  585. $this->assertIdentical($result, $expected);
  586. }
  587. /**
  588. * testMoveDownSuccess method
  589. *
  590. * @access public
  591. * @return void
  592. */
  593. function testMoveDownSuccess() {
  594. extract($this->settings);
  595. $this->Tree = new $modelClass();
  596. $this->Tree->initialize(2, 2);
  597. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  598. $this->Tree->moveDown($data[$modelClass]['id']);
  599. $parent = $this->Tree->findByName('1. Root', array('id'));
  600. $this->Tree->id = $parent[$modelClass]['id'];
  601. $result = $this->Tree->children(null, true, array('name'));
  602. $expected = array(array($modelClass => array('name' => '1.2', )),
  603. array($modelClass => array('name' => '1.1', )));
  604. $this->assertIdentical($result, $expected);
  605. }
  606. /**
  607. * testMoveDownFail method
  608. *
  609. * @access public
  610. * @return void
  611. */
  612. function testMoveDownFail() {
  613. extract($this->settings);
  614. $this->Tree = new $modelClass();
  615. $this->Tree->initialize(2, 2);
  616. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
  617. $this->Tree->moveDown($data[$modelClass]['id']);
  618. $parent = $this->Tree->findByName('1. Root', array('id'));
  619. $this->Tree->id = $parent[$modelClass]['id'];
  620. $result = $this->Tree->children(null, true, array('name'));
  621. $expected = array(array($modelClass => array('name' => '1.1', )),
  622. array($modelClass => array('name' => '1.2', )));
  623. $this->assertIdentical($result, $expected);
  624. }
  625. /**
  626. * testMoveDownLast method
  627. *
  628. * @access public
  629. * @return void
  630. */
  631. function testMoveDownLast() {
  632. extract($this->settings);
  633. $this->Tree = new $modelClass();
  634. $this->Tree->initialize(1, 10);
  635. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
  636. $this->Tree->moveDown($data[$modelClass]['id'], true);
  637. $parent = $this->Tree->findByName('1. Root', array('id'));
  638. $this->Tree->id = $parent[$modelClass]['id'];
  639. $result = $this->Tree->children(null, true, array('name'));
  640. $expected = array(
  641. array($modelClass => array('name' => '1.1', )),
  642. array($modelClass => array('name' => '1.2', )),
  643. array($modelClass => array('name' => '1.3', )),
  644. array($modelClass => array('name' => '1.4', )),
  645. array($modelClass => array('name' => '1.6', )),
  646. array($modelClass => array('name' => '1.7', )),
  647. array($modelClass => array('name' => '1.8', )),
  648. array($modelClass => array('name' => '1.9', )),
  649. array($modelClass => array('name' => '1.10', )),
  650. array($modelClass => array('name' => '1.5', )));
  651. $this->assertIdentical($result, $expected);
  652. }
  653. /**
  654. * testMoveDown2 method
  655. *
  656. * @access public
  657. * @return void
  658. */
  659. function testMoveDown2() {
  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->moveDown($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.3', )),
  672. array($modelClass => array('name' => '1.4', )),
  673. array($modelClass => array('name' => '1.6', )),
  674. array($modelClass => array('name' => '1.7', )),
  675. array($modelClass => array('name' => '1.5', )),
  676. array($modelClass => array('name' => '1.8', )),
  677. array($modelClass => array('name' => '1.9', )),
  678. array($modelClass => array('name' => '1.10', )));
  679. $this->assertIdentical($result, $expected);
  680. }
  681. /**
  682. * testSaveNoMove method
  683. *
  684. * @access public
  685. * @return void
  686. */
  687. function testSaveNoMove() {
  688. extract($this->settings);
  689. $this->Tree = new $modelClass();
  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->id = $data[$modelClass]['id'];
  693. $this->Tree->saveField('name', 'renamed');
  694. $parent = $this->Tree->findByName('1. Root', array('id'));
  695. $this->Tree->id = $parent[$modelClass]['id'];
  696. $result = $this->Tree->children(null, true, array('name'));
  697. $expected = array(
  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' => 'renamed', )),
  703. array($modelClass => array('name' => '1.6', )),
  704. array($modelClass => array('name' => '1.7', )),
  705. array($modelClass => array('name' => '1.8', )),
  706. array($modelClass => array('name' => '1.9', )),
  707. array($modelClass => array('name' => '1.10', )));
  708. $this->assertIdentical($result, $expected);
  709. }
  710. /**
  711. * testMoveToRootAndMoveUp method
  712. *
  713. * @access public
  714. * @return void
  715. */
  716. function testMoveToRootAndMoveUp() {
  717. extract($this->settings);
  718. $this->Tree = new $modelClass();
  719. $this->Tree->initialize(1, 1);
  720. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  721. $this->Tree->id = $data[$modelClass]['id'];
  722. $this->Tree->save(array($parentField => null));
  723. $result = $this->Tree->verify();
  724. $this->assertIdentical($result, true);
  725. $this->Tree->moveUp();
  726. $result = $this->Tree->find('all', array('fields' => 'name', 'order' => $modelClass . '.' . $leftField . ' ASC'));
  727. $expected = array(array($modelClass => array('name' => '1.1')),
  728. array($modelClass => array('name' => '1. Root')));
  729. $this->assertIdentical($result, $expected);
  730. }
  731. /**
  732. * testDelete method
  733. *
  734. * @access public
  735. * @return void
  736. */
  737. function testDelete() {
  738. extract($this->settings);
  739. $this->Tree = new $modelClass();
  740. $this->Tree->initialize(2, 2);
  741. $initialCount = $this->Tree->find('count');
  742. $result = $this->Tree->findByName('1.1.1');
  743. $return = $this->Tree->delete($result[$modelClass]['id']);
  744. $this->assertEqual($return, true);
  745. $laterCount = $this->Tree->find('count');
  746. $this->assertEqual($initialCount - 1, $laterCount);
  747. $validTree= $this->Tree->verify();
  748. $this->assertIdentical($validTree, true);
  749. $initialCount = $this->Tree->find('count');
  750. $result= $this->Tree->findByName('1.1');
  751. $return = $this->Tree->delete($result[$modelClass]['id']);
  752. $this->assertEqual($return, true);
  753. $laterCount = $this->Tree->find('count');
  754. $this->assertEqual($initialCount - 2, $laterCount);
  755. $validTree = $this->Tree->verify();
  756. $this->assertIdentical($validTree, true);
  757. }
  758. /**
  759. * testRemove method
  760. *
  761. * @access public
  762. * @return void
  763. */
  764. function testRemove() {
  765. extract($this->settings);
  766. $this->Tree = new $modelClass();
  767. $this->Tree->initialize(2, 2);
  768. $initialCount = $this->Tree->find('count');
  769. $result = $this->Tree->findByName('1.1');
  770. $this->Tree->removeFromTree($result[$modelClass]['id']);
  771. $laterCount = $this->Tree->find('count');
  772. $this->assertEqual($initialCount, $laterCount);
  773. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'));
  774. $expects = array(array($modelClass => array('name' => '1.1.1')),
  775. array($modelClass => array('name' => '1.1.2')),
  776. array($modelClass => array('name' => '1.2')));
  777. $this->assertEqual($children, $expects);
  778. $topNodes = $this->Tree->children(false, true,array('name'));
  779. $expects = array(array($modelClass => array('name' => '1. Root')),
  780. array($modelClass => array('name' => '1.1')));
  781. $this->assertEqual($topNodes, $expects);
  782. $validTree = $this->Tree->verify();
  783. $this->assertIdentical($validTree, true);
  784. }
  785. /**
  786. * testRemoveLastTopParent method
  787. *
  788. * @access public
  789. * @return void
  790. */
  791. function testRemoveLastTopParent() {
  792. extract($this->settings);
  793. $this->Tree = new $modelClass();
  794. $this->Tree->initialize(2, 2);
  795. $initialCount = $this->Tree->find('count');
  796. $initialTopNodes = $this->Tree->childCount(false);
  797. $result = $this->Tree->findByName('1. Root');
  798. $this->Tree->removeFromTree($result[$modelClass]['id']);
  799. $laterCount = $this->Tree->find('count');
  800. $laterTopNodes = $this->Tree->childCount(false);
  801. $this->assertEqual($initialCount, $laterCount);
  802. $this->assertEqual($initialTopNodes, $laterTopNodes);
  803. $topNodes = $this->Tree->children(false, true,array('name'));
  804. $expects = array(array($modelClass => array('name' => '1.1')),
  805. array($modelClass => array('name' => '1.2')),
  806. array($modelClass => array('name' => '1. Root')));
  807. $this->assertEqual($topNodes, $expects);
  808. $validTree = $this->Tree->verify();
  809. $this->assertIdentical($validTree, true);
  810. }
  811. /**
  812. * testRemoveNoChildren method
  813. *
  814. * @return void
  815. */
  816. public function testRemoveNoChildren() {
  817. extract($this->settings);
  818. $this->Tree = new $modelClass();
  819. $this->Tree->initialize(2, 2);
  820. $initialCount = $this->Tree->find('count');
  821. $result = $this->Tree->findByName('1.1.1');
  822. $this->Tree->removeFromTree($result[$modelClass]['id']);
  823. $laterCount = $this->Tree->find('count');
  824. $this->assertEqual($initialCount, $laterCount);
  825. $nodes = $this->Tree->find('list', array('order' => $leftField));
  826. $expects = array(
  827. 1 => '1. Root',
  828. 2 => '1.1',
  829. 4 => '1.1.2',
  830. 5 => '1.2',
  831. 6 => '1.2.1',
  832. 7 => '1.2.2',
  833. 3 => '1.1.1',
  834. );
  835. $this->assertEqual($nodes, $expects);
  836. $validTree = $this->Tree->verify();
  837. $this->assertIdentical($validTree, true);
  838. }
  839. /**
  840. * testRemoveAndDelete method
  841. *
  842. * @access public
  843. * @return void
  844. */
  845. function testRemoveAndDelete() {
  846. extract($this->settings);
  847. $this->Tree = new $modelClass();
  848. $this->Tree->initialize(2, 2);
  849. $initialCount = $this->Tree->find('count');
  850. $result = $this->Tree->findByName('1.1');
  851. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  852. $laterCount = $this->Tree->find('count');
  853. $this->assertEqual($initialCount-1, $laterCount);
  854. $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'), $leftField . ' asc');
  855. $expects= array(array($modelClass => array('name' => '1.1.1')),
  856. array($modelClass => array('name' => '1.1.2')),
  857. array($modelClass => array('name' => '1.2')));
  858. $this->assertEqual($children, $expects);
  859. $topNodes = $this->Tree->children(false, true,array('name'));
  860. $expects = array(array($modelClass => array('name' => '1. Root')));
  861. $this->assertEqual($topNodes, $expects);
  862. $validTree = $this->Tree->verify();
  863. $this->assertIdentical($validTree, true);
  864. }
  865. /**
  866. * testRemoveAndDeleteNoChildren method
  867. *
  868. * @return void
  869. */
  870. public function testRemoveAndDeleteNoChildren() {
  871. extract($this->settings);
  872. $this->Tree = new $modelClass();
  873. $this->Tree->initialize(2, 2);
  874. $initialCount = $this->Tree->find('count');
  875. $result = $this->Tree->findByName('1.1.1');
  876. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  877. $laterCount = $this->Tree->find('count');
  878. $this->assertEqual($initialCount - 1, $laterCount);
  879. $nodes = $this->Tree->find('list', array('order' => $leftField));
  880. $expects = array(
  881. 1 => '1. Root',
  882. 2 => '1.1',
  883. 4 => '1.1.2',
  884. 5 => '1.2',
  885. 6 => '1.2.1',
  886. 7 => '1.2.2',
  887. );
  888. $this->assertEqual($nodes, $expects);
  889. $validTree = $this->Tree->verify();
  890. $this->assertIdentical($validTree, true);
  891. }
  892. /**
  893. * testChildren method
  894. *
  895. * @access public
  896. * @return void
  897. */
  898. function testChildren() {
  899. extract($this->settings);
  900. $this->Tree = new $modelClass();
  901. $this->Tree->initialize(2, 2);
  902. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  903. $this->Tree->id= $data[$modelClass]['id'];
  904. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  905. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  906. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  907. $this->assertEqual($direct, $expects);
  908. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  909. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  910. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  911. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  912. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  913. array($modelClass => array( 'id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  914. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12)));
  915. $this->assertEqual($total, $expects);
  916. $this->assertEqual(array(), $this->Tree->children(10000));
  917. }
  918. /**
  919. * testCountChildren method
  920. *
  921. * @access public
  922. * @return void
  923. */
  924. function testCountChildren() {
  925. extract($this->settings);
  926. $this->Tree = new $modelClass();
  927. $this->Tree->initialize(2, 2);
  928. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  929. $this->Tree->id = $data[$modelClass]['id'];
  930. $direct = $this->Tree->childCount(null, true);
  931. $this->assertEqual($direct, 2);
  932. $total = $this->Tree->childCount();
  933. $this->assertEqual($total, 6);
  934. }
  935. /**
  936. * testGetParentNode method
  937. *
  938. * @access public
  939. * @return void
  940. */
  941. function testGetParentNode() {
  942. extract($this->settings);
  943. $this->Tree = new $modelClass();
  944. $this->Tree->initialize(2, 2);
  945. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  946. $this->Tree->id= $data[$modelClass]['id'];
  947. $result = $this->Tree->getParentNode(null, array('name'));
  948. $expects = array($modelClass => array('name' => '1.2'));
  949. $this->assertIdentical($result, $expects);
  950. }
  951. /**
  952. * testGetPath method
  953. *
  954. * @access public
  955. * @return void
  956. */
  957. function testGetPath() {
  958. extract($this->settings);
  959. $this->Tree = new $modelClass();
  960. $this->Tree->initialize(2, 2);
  961. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
  962. $this->Tree->id= $data[$modelClass]['id'];
  963. $result = $this->Tree->getPath(null, array('name'));
  964. $expects = array(array($modelClass => array('name' => '1. Root')),
  965. array($modelClass => array('name' => '1.2')),
  966. array($modelClass => array('name' => '1.2.2')));
  967. $this->assertIdentical($result, $expects);
  968. }
  969. /**
  970. * testNoAmbiguousColumn method
  971. *
  972. * @access public
  973. * @return void
  974. */
  975. function testNoAmbiguousColumn() {
  976. extract($this->settings);
  977. $this->Tree = new $modelClass();
  978. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  979. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  980. $this->Tree->initialize(2, 2);
  981. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  982. $this->Tree->id= $data[$modelClass]['id'];
  983. $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
  984. $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  985. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
  986. $this->assertEqual($direct, $expects);
  987. $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
  988. $expects = array(
  989. array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
  990. array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
  991. array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
  992. array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
  993. array($modelClass => array( 'id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
  994. array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12))
  995. );
  996. $this->assertEqual($total, $expects);
  997. }
  998. /**
  999. * testReorderTree method
  1000. *
  1001. * @access public
  1002. * @return void
  1003. */
  1004. function testReorderTree() {
  1005. extract($this->settings);
  1006. $this->Tree = new $modelClass();
  1007. $this->Tree->initialize(3, 3);
  1008. $nodes = $this->Tree->find('list', array('order' => $leftField));
  1009. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
  1010. $this->Tree->moveDown($data[$modelClass]['id']);
  1011. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2.1')));
  1012. $this->Tree->moveDown($data[$modelClass]['id']);
  1013. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.3.2.2')));
  1014. $this->Tree->moveDown($data[$modelClass]['id']);
  1015. $unsortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1016. $this->assertEquals($nodes, $unsortedNodes);
  1017. $this->assertNotEquals(array_keys($nodes), array_keys($unsortedNodes));
  1018. $this->Tree->reorder();
  1019. $sortedNodes = $this->Tree->find('list', array('order' => $leftField));
  1020. $this->assertIdentical($nodes, $sortedNodes);
  1021. }
  1022. /**
  1023. * test reordering large-ish trees with cacheQueries = true.
  1024. * This caused infinite loops when moving down elements as stale data is returned
  1025. * from the memory cache
  1026. *
  1027. * @access public
  1028. * @return void
  1029. */
  1030. function testReorderBigTreeWithQueryCaching() {
  1031. extract($this->settings);
  1032. $this->Tree = new $modelClass();
  1033. $this->Tree->initialize(2, 10);
  1034. $original = $this->Tree->cacheQueries;
  1035. $this->Tree->cacheQueries = true;
  1036. $this->Tree->reorder(array('field' => 'name', 'direction' => 'DESC'));
  1037. $this->assertTrue($this->Tree->cacheQueries, 'cacheQueries was not restored after reorder(). %s');
  1038. $this->Tree->cacheQueries = $original;
  1039. }
  1040. /**
  1041. * testGenerateTreeListWithSelfJoin method
  1042. *
  1043. * @access public
  1044. * @return void
  1045. */
  1046. function testGenerateTreeListWithSelfJoin() {
  1047. extract($this->settings);
  1048. $this->Tree = new $modelClass();
  1049. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  1050. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  1051. $this->Tree->initialize(2, 2);
  1052. $result = $this->Tree->generateTreeList();
  1053. $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');
  1054. $this->assertIdentical($result, $expected);
  1055. }
  1056. /**
  1057. * testArraySyntax method
  1058. *
  1059. * @access public
  1060. * @return void
  1061. */
  1062. function testArraySyntax() {
  1063. extract($this->settings);
  1064. $this->Tree = new $modelClass();
  1065. $this->Tree->initialize(3, 3);
  1066. $this->assertIdentical($this->Tree->childCount(2), $this->Tree->childCount(array('id' => 2)));
  1067. $this->assertIdentical($this->Tree->getParentNode(2), $this->Tree->getParentNode(array('id' => 2)));
  1068. $this->assertIdentical($this->Tree->getPath(4), $this->Tree->getPath(array('id' => 4)));
  1069. }
  1070. }