PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/hardsshah/bookmarks
PHP | 1731 lines | 1058 code | 199 blank | 474 comment | 0 complexity | 0a0e7a2e1c792461821406d65d1f0bbe MD5 | raw file

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

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

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