TreeBehaviorNumberTest.php 41 KB

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