TreeBehaviorTest.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. <?php
  2. /**
  3. * TreeBehaviorTest file
  4. *
  5. * Holds several Test Cases
  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. * NumberTreeTest class
  26. *
  27. * @package cake.tests.cases.libs.model.behaviors
  28. */
  29. class NumberTreeTest 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. }
  1071. /**
  1072. * ScopedTreeTest class
  1073. *
  1074. * @package cake.tests.cases.libs.model.behaviors
  1075. */
  1076. class ScopedTreeTest extends CakeTestCase {
  1077. /**
  1078. * Whether backup global state for each test method or not
  1079. *
  1080. * @var bool false
  1081. * @access public
  1082. */
  1083. public $backupGlobals = false;
  1084. /**
  1085. * settings property
  1086. *
  1087. * @var array
  1088. * @access public
  1089. */
  1090. public $settings = array(
  1091. 'modelClass' => 'FlagTree',
  1092. 'leftField' => 'lft',
  1093. 'rightField' => 'rght',
  1094. 'parentField' => 'parent_id'
  1095. );
  1096. /**
  1097. * fixtures property
  1098. *
  1099. * @var array
  1100. * @access public
  1101. */
  1102. public $fixtures = array('core.flag_tree', 'core.ad', 'core.campaign', 'core.translate', 'core.number_tree_two');
  1103. /**
  1104. * testStringScope method
  1105. *
  1106. * @access public
  1107. * @return void
  1108. */
  1109. function testStringScope() {
  1110. $this->Tree = new FlagTree();
  1111. $this->Tree->initialize(2, 3);
  1112. $this->Tree->id = 1;
  1113. $this->Tree->saveField('flag', 1);
  1114. $this->Tree->id = 2;
  1115. $this->Tree->saveField('flag', 1);
  1116. $result = $this->Tree->children();
  1117. $expected = array(
  1118. array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
  1119. array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
  1120. array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
  1121. );
  1122. $this->assertEqual($result, $expected);
  1123. $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
  1124. $this->assertEqual($this->Tree->children(), array());
  1125. $this->Tree->id = 1;
  1126. $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
  1127. $result = $this->Tree->children();
  1128. $expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
  1129. $this->assertEqual($result, $expected);
  1130. $this->assertTrue($this->Tree->delete());
  1131. $this->assertEqual($this->Tree->find('count'), 11);
  1132. }
  1133. /**
  1134. * testArrayScope method
  1135. *
  1136. * @access public
  1137. * @return void
  1138. */
  1139. function testArrayScope() {
  1140. $this->Tree = new FlagTree();
  1141. $this->Tree->initialize(2, 3);
  1142. $this->Tree->id = 1;
  1143. $this->Tree->saveField('flag', 1);
  1144. $this->Tree->id = 2;
  1145. $this->Tree->saveField('flag', 1);
  1146. $result = $this->Tree->children();
  1147. $expected = array(
  1148. array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
  1149. array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
  1150. array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
  1151. );
  1152. $this->assertEqual($result, $expected);
  1153. $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
  1154. $this->assertEqual($this->Tree->children(), array());
  1155. $this->Tree->id = 1;
  1156. $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
  1157. $result = $this->Tree->children();
  1158. $expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
  1159. $this->assertEqual($result, $expected);
  1160. $this->assertTrue($this->Tree->delete());
  1161. $this->assertEqual($this->Tree->find('count'), 11);
  1162. }
  1163. /**
  1164. * testMoveUpWithScope method
  1165. *
  1166. * @access public
  1167. * @return void
  1168. */
  1169. function testMoveUpWithScope() {
  1170. $this->Ad = new Ad();
  1171. $this->Ad->Behaviors->attach('Tree', array('scope'=>'Campaign'));
  1172. $this->Ad->moveUp(6);
  1173. $this->Ad->id = 4;
  1174. $result = $this->Ad->children();
  1175. $this->assertEqual(Set::extract('/Ad/id', $result), array(6, 5));
  1176. $this->assertEqual(Set::extract('/Campaign/id', $result), array(2, 2));
  1177. }
  1178. /**
  1179. * testMoveDownWithScope method
  1180. *
  1181. * @access public
  1182. * @return void
  1183. */
  1184. function testMoveDownWithScope() {
  1185. $this->Ad = new Ad();
  1186. $this->Ad->Behaviors->attach('Tree', array('scope' => 'Campaign'));
  1187. $this->Ad->moveDown(6);
  1188. $this->Ad->id = 4;
  1189. $result = $this->Ad->children();
  1190. $this->assertEqual(Set::extract('/Ad/id', $result), array(5, 6));
  1191. $this->assertEqual(Set::extract('/Campaign/id', $result), array(2, 2));
  1192. }
  1193. /**
  1194. * Tests the interaction (non-interference) between TreeBehavior and other behaviors with respect
  1195. * to callback hooks
  1196. *
  1197. * @access public
  1198. * @return void
  1199. */
  1200. function testTranslatingTree() {
  1201. $this->Tree = new FlagTree();
  1202. $this->Tree->cacheQueries = false;
  1203. $this->Tree->Behaviors->attach('Translate', array('name'));
  1204. //Save
  1205. $this->Tree->locale = 'eng';
  1206. $data = array('FlagTree' => array(
  1207. 'name' => 'name #1',
  1208. 'locale' => 'eng',
  1209. 'parent_id' => null,
  1210. ));
  1211. $this->Tree->save($data);
  1212. $result = $this->Tree->find('all');
  1213. $expected = array(array('FlagTree' => array(
  1214. 'id' => 1,
  1215. 'name' => 'name #1',
  1216. 'parent_id' => null,
  1217. 'lft' => 1,
  1218. 'rght' => 2,
  1219. 'flag' => 0,
  1220. 'locale' => 'eng',
  1221. )));
  1222. $this->assertEqual($result, $expected);
  1223. //update existing record, same locale
  1224. $this->Tree->create();
  1225. $data['FlagTree']['name'] = 'Named 2';
  1226. $this->Tree->id = 1;
  1227. $this->Tree->save($data);
  1228. $result = $this->Tree->find('all');
  1229. $expected = array(array('FlagTree' => array(
  1230. 'id' => 1,
  1231. 'name' => 'Named 2',
  1232. 'parent_id' => null,
  1233. 'lft' => 1,
  1234. 'rght' => 2,
  1235. 'flag' => 0,
  1236. 'locale' => 'eng',
  1237. )));
  1238. $this->assertEqual($result, $expected);
  1239. //update different locale, same record
  1240. $this->Tree->create();
  1241. $this->Tree->locale = 'deu';
  1242. $this->Tree->id = 1;
  1243. $data = array('FlagTree' => array(
  1244. 'id' => 1,
  1245. 'parent_id' => null,
  1246. 'name' => 'namen #1',
  1247. 'locale' => 'deu',
  1248. ));
  1249. $this->Tree->save($data);
  1250. $this->Tree->locale = 'deu';
  1251. $result = $this->Tree->find('all');
  1252. $expected = array(array('FlagTree' => array(
  1253. 'id' => 1,
  1254. 'name' => 'namen #1',
  1255. 'parent_id' => null,
  1256. 'lft' => 1,
  1257. 'rght' => 2,
  1258. 'flag' => 0,
  1259. 'locale' => 'deu',
  1260. )));
  1261. $this->assertEqual($result, $expected);
  1262. //Save with bindTranslation
  1263. $this->Tree->locale = 'eng';
  1264. $data = array(
  1265. 'name' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
  1266. 'parent_id' => null
  1267. );
  1268. $this->Tree->create($data);
  1269. $this->Tree->save();
  1270. $this->Tree->unbindTranslation();
  1271. $translations = array('name' => 'Name');
  1272. $this->Tree->bindTranslation($translations, false);
  1273. $this->Tree->locale = array('eng', 'spa');
  1274. $result = $this->Tree->read();
  1275. $expected = array(
  1276. 'FlagTree' => array('id' => 2, 'parent_id' => null, 'locale' => 'eng', 'name' => 'New title', 'flag' => 0, 'lft' => 3, 'rght' => 4),
  1277. 'Name' => array(
  1278. array('id' => 21, 'locale' => 'eng', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'New title'),
  1279. array('id' => 22, 'locale' => 'spa', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'Nuevo leyenda')
  1280. ),
  1281. );
  1282. $this->assertEqual($result, $expected);
  1283. }
  1284. /**
  1285. * testGenerateTreeListWithSelfJoin method
  1286. *
  1287. * @return void
  1288. */
  1289. public function testAliasesWithScopeInTwoTreeAssociations() {
  1290. extract($this->settings);
  1291. $this->Tree = new $modelClass();
  1292. $this->Tree->initialize(2, 2);
  1293. $this->TreeTwo = new NumberTreeTwo();
  1294. $record = $this->Tree->find('first');
  1295. $this->Tree->bindModel(array(
  1296. 'hasMany' => array(
  1297. 'SecondTree' => array(
  1298. 'className' => 'NumberTreeTwo',
  1299. 'foreignKey' => 'number_tree_id'
  1300. )
  1301. )
  1302. ));
  1303. $this->TreeTwo->bindModel(array(
  1304. 'belongsTo' => array(
  1305. 'FirstTree' => array(
  1306. 'className' => $modelClass,
  1307. 'foreignKey' => 'number_tree_id'
  1308. )
  1309. )
  1310. ));
  1311. $this->TreeTwo->Behaviors->attach('Tree', array(
  1312. 'scope' => 'FirstTree'
  1313. ));
  1314. $data = array(
  1315. 'NumberTreeTwo' => array(
  1316. 'name' => 'First',
  1317. 'number_tree_id' => $record['FlagTree']['id']
  1318. )
  1319. );
  1320. $this->TreeTwo->create();
  1321. $result = $this->TreeTwo->save($data);
  1322. $this->assertFalse(empty($result));
  1323. $result = $this->TreeTwo->find('first');
  1324. $expected = array('NumberTreeTwo' => array(
  1325. 'id' => 1,
  1326. 'name' => 'First',
  1327. 'number_tree_id' => $record['FlagTree']['id'],
  1328. 'parent_id' => null,
  1329. 'lft' => 1,
  1330. 'rght' => 2
  1331. ));
  1332. $this->assertEqual($result, $expected);
  1333. }
  1334. }
  1335. /**
  1336. * AfterTreeTest class
  1337. *
  1338. * @package cake.tests.cases.libs.model.behaviors
  1339. */
  1340. class AfterTreeTest extends CakeTestCase {
  1341. /**
  1342. * Whether backup global state for each test method or not
  1343. *
  1344. * @var bool false
  1345. * @access public
  1346. */
  1347. public $backupGlobals = false;
  1348. /**
  1349. * settings property
  1350. *
  1351. * @var array
  1352. * @access public
  1353. */
  1354. public $settings = array(
  1355. 'modelClass' => 'AfterTree',
  1356. 'leftField' => 'lft',
  1357. 'rightField' => 'rght',
  1358. 'parentField' => 'parent_id'
  1359. );
  1360. /**
  1361. * fixtures property
  1362. *
  1363. * @var array
  1364. * @access public
  1365. */
  1366. public $fixtures = array('core.after_tree');
  1367. /**
  1368. * Tests the afterSave callback in the model
  1369. *
  1370. * @access public
  1371. * @return void
  1372. */
  1373. function testAftersaveCallback() {
  1374. $this->Tree = new AfterTree();
  1375. $expected = array('AfterTree' => array('name' => 'Six and One Half Changed in AfterTree::afterSave() but not in database', 'parent_id' => 6, 'lft' => 11, 'rght' => 12));
  1376. $result = $this->Tree->save(array('AfterTree' => array('name' => 'Six and One Half', 'parent_id' => 6)));
  1377. $this->assertEqual($result, $expected);
  1378. $expected = array('AfterTree' => array('name' => 'Six and One Half', 'parent_id' => 6, 'lft' => 11, 'rght' => 12, 'id' => 8));
  1379. $result = $this->Tree->find('all');
  1380. $this->assertEqual($result[7], $expected);
  1381. }
  1382. }
  1383. /**
  1384. * UuidTreeTest class
  1385. *
  1386. * @package cake.tests.cases.libs.model.behaviors
  1387. */
  1388. class UuidTreeTest extends CakeTestCase {
  1389. /**
  1390. * Whether backup global state for each test method or not
  1391. *
  1392. * @var bool false
  1393. * @access public
  1394. */
  1395. public $backupGlobals = false;
  1396. /**
  1397. * settings property
  1398. *
  1399. * @var array
  1400. * @access public
  1401. */
  1402. public $settings = array(
  1403. 'modelClass' => 'UuidTree',
  1404. 'leftField' => 'lft',
  1405. 'rightField' => 'rght',
  1406. 'parentField' => 'parent_id'
  1407. );
  1408. /**
  1409. * fixtures property
  1410. *
  1411. * @var array
  1412. * @access public
  1413. */
  1414. public $fixtures = array('core.uuid_tree');
  1415. /**
  1416. * testMovePromote method
  1417. *
  1418. * @return void
  1419. */
  1420. public function testMovePromote() {
  1421. extract($this->settings);
  1422. $this->Tree = new $modelClass();
  1423. $this->Tree->initialize(2, 2);
  1424. $this->Tree->id = null;
  1425. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  1426. $parent_id = $parent[$modelClass]['id'];
  1427. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  1428. $this->Tree->id= $data[$modelClass]['id'];
  1429. $this->Tree->saveField($parentField, $parent_id);
  1430. $direct = $this->Tree->children($parent_id, true, array('name', $leftField, $rightField));
  1431. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
  1432. array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
  1433. array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
  1434. $this->assertEqual($direct, $expects);
  1435. $validTree = $this->Tree->verify();
  1436. $this->assertIdentical($validTree, true);
  1437. }
  1438. /**
  1439. * testMoveWithWhitelist method
  1440. *
  1441. * @return void
  1442. */
  1443. public function testMoveWithWhitelist() {
  1444. extract($this->settings);
  1445. $this->Tree = new $modelClass();
  1446. $this->Tree->initialize(2, 2);
  1447. $this->Tree->id = null;
  1448. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  1449. $parent_id = $parent[$modelClass]['id'];
  1450. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  1451. $this->Tree->id = $data[$modelClass]['id'];
  1452. $this->Tree->whitelist = array($parentField, 'name', 'description');
  1453. $this->Tree->saveField($parentField, $parent_id);
  1454. $result = $this->Tree->children($parent_id, true, array('name', $leftField, $rightField));
  1455. $expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
  1456. array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
  1457. array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
  1458. $this->assertEqual($result, $expected);
  1459. $this->assertTrue($this->Tree->verify());
  1460. }
  1461. /**
  1462. * testRemoveNoChildren method
  1463. *
  1464. * @return void
  1465. */
  1466. public function testRemoveNoChildren() {
  1467. extract($this->settings);
  1468. $this->Tree = new $modelClass();
  1469. $this->Tree->initialize(2, 2);
  1470. $initialCount = $this->Tree->find('count');
  1471. $result = $this->Tree->findByName('1.1.1');
  1472. $this->Tree->removeFromTree($result[$modelClass]['id']);
  1473. $laterCount = $this->Tree->find('count');
  1474. $this->assertEqual($initialCount, $laterCount);
  1475. $nodes = $this->Tree->find('list', array('order' => $leftField));
  1476. $expects = array(
  1477. '1. Root',
  1478. '1.1',
  1479. '1.1.2',
  1480. '1.2',
  1481. '1.2.1',
  1482. '1.2.2',
  1483. '1.1.1',
  1484. );
  1485. $this->assertEqual(array_values($nodes), $expects);
  1486. $validTree = $this->Tree->verify();
  1487. $this->assertIdentical($validTree, true);
  1488. }
  1489. /**
  1490. * testRemoveAndDeleteNoChildren method
  1491. *
  1492. * @return void
  1493. */
  1494. public function testRemoveAndDeleteNoChildren() {
  1495. extract($this->settings);
  1496. $this->Tree = new $modelClass();
  1497. $this->Tree->initialize(2, 2);
  1498. $initialCount = $this->Tree->find('count');
  1499. $result = $this->Tree->findByName('1.1.1');
  1500. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  1501. $laterCount = $this->Tree->find('count');
  1502. $this->assertEqual($initialCount - 1, $laterCount);
  1503. $nodes = $this->Tree->find('list', array('order' => $leftField));
  1504. $expects = array(
  1505. '1. Root',
  1506. '1.1',
  1507. '1.1.2',
  1508. '1.2',
  1509. '1.2.1',
  1510. '1.2.2',
  1511. );
  1512. $this->assertEqual(array_values($nodes), $expects);
  1513. $validTree = $this->Tree->verify();
  1514. $this->assertIdentical($validTree, true);
  1515. }
  1516. /**
  1517. * testChildren method
  1518. *
  1519. * @return void
  1520. */
  1521. public function testChildren() {
  1522. extract($this->settings);
  1523. $this->Tree = new $modelClass();
  1524. $this->Tree->initialize(2, 2);
  1525. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  1526. $this->Tree->id = $data[$modelClass]['id'];
  1527. $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
  1528. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  1529. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
  1530. $this->assertEqual($direct, $expects);
  1531. $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
  1532. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  1533. array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
  1534. array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
  1535. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
  1536. array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
  1537. array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12)));
  1538. $this->assertEqual($total, $expects);
  1539. }
  1540. /**
  1541. * testNoAmbiguousColumn method
  1542. *
  1543. * @return void
  1544. */
  1545. public function testNoAmbiguousColumn() {
  1546. extract($this->settings);
  1547. $this->Tree = new $modelClass();
  1548. $this->Tree->initialize(2, 2);
  1549. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  1550. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  1551. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  1552. $this->Tree->id = $data[$modelClass]['id'];
  1553. $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
  1554. $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  1555. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
  1556. $this->assertEqual($direct, $expects);
  1557. $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
  1558. $expects = array(
  1559. array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  1560. array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
  1561. array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
  1562. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
  1563. array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
  1564. array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12))
  1565. );
  1566. $this->assertEqual($total, $expects);
  1567. }
  1568. /**
  1569. * testGenerateTreeListWithSelfJoin method
  1570. *
  1571. * @return void
  1572. */
  1573. public function testGenerateTreeListWithSelfJoin() {
  1574. extract($this->settings);
  1575. $this->Tree = new $modelClass();
  1576. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  1577. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  1578. $this->Tree->initialize(2, 2);
  1579. $result = $this->Tree->generateTreeList();
  1580. $expected = array('1. Root', '_1.1', '__1.1.1', '__1.1.2', '_1.2', '__1.2.1', '__1.2.2');
  1581. $this->assertIdentical(array_values($result), $expected);
  1582. }
  1583. }