PageRenderTime 34ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/cases/libs/model/behaviors/tree.test.php

http://github.com/Datawalke/Coordino
PHP | 1932 lines | 1154 code | 290 blank | 488 comment | 0 complexity | a0d767ef81ea0c89ad0365d06079fffb MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file