TreeBehaviorScopedTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <?php
  2. /**
  3. * TreeBehaviorScopedTest file
  4. *
  5. * A tree test using scope
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  10. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  18. * @package Cake.Test.Case.Model.Behavior
  19. * @since CakePHP(tm) v 1.2.0.5330
  20. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  21. */
  22. App::uses('Model', 'Model');
  23. App::uses('AppModel', 'Model');
  24. require_once dirname(dirname(__FILE__)) . DS . 'models.php';
  25. /**
  26. * TreeBehaviorScopedTest class
  27. *
  28. * @package Cake.Test.Case.Model.Behavior
  29. */
  30. class TreeBehaviorScopedTest extends CakeTestCase {
  31. /**
  32. * Whether backup global state for each test method or not
  33. *
  34. * @var boolean
  35. */
  36. public $backupGlobals = false;
  37. /**
  38. * settings property
  39. *
  40. * @var array
  41. */
  42. public $settings = array(
  43. 'modelClass' => 'FlagTree',
  44. 'leftField' => 'lft',
  45. 'rightField' => 'rght',
  46. 'parentField' => 'parent_id'
  47. );
  48. /**
  49. * fixtures property
  50. *
  51. * @var array
  52. */
  53. public $fixtures = array('core.flag_tree', 'core.ad', 'core.campaign', 'core.translate', 'core.number_tree_two');
  54. /**
  55. * testStringScope method
  56. *
  57. * @return void
  58. */
  59. public function testStringScope() {
  60. $this->Tree = new FlagTree();
  61. $this->Tree->order = null;
  62. $this->Tree->initialize(2, 3);
  63. $this->Tree->id = 1;
  64. $this->Tree->saveField('flag', 1);
  65. $this->Tree->id = 2;
  66. $this->Tree->saveField('flag', 1);
  67. $result = $this->Tree->children();
  68. $expected = array(
  69. array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
  70. array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
  71. array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
  72. );
  73. $this->assertEquals($expected, $result);
  74. $this->Tree->Behaviors->load('Tree', array('scope' => 'FlagTree.flag = 1'));
  75. $this->assertEquals(array(), $this->Tree->children());
  76. $this->Tree->id = 1;
  77. $this->Tree->Behaviors->load('Tree', array('scope' => 'FlagTree.flag = 1'));
  78. $result = $this->Tree->children();
  79. $expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
  80. $this->assertEquals($expected, $result);
  81. $this->assertTrue($this->Tree->delete());
  82. $this->assertEquals(11, $this->Tree->find('count'));
  83. }
  84. /**
  85. * testArrayScope method
  86. *
  87. * @return void
  88. */
  89. public function testArrayScope() {
  90. $this->Tree = new FlagTree();
  91. $this->Tree->order = null;
  92. $this->Tree->initialize(2, 3);
  93. $this->Tree->id = 1;
  94. $this->Tree->saveField('flag', 1);
  95. $this->Tree->id = 2;
  96. $this->Tree->saveField('flag', 1);
  97. $result = $this->Tree->children();
  98. $expected = array(
  99. array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
  100. array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
  101. array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
  102. );
  103. $this->assertEquals($expected, $result);
  104. $this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
  105. $this->assertEquals(array(), $this->Tree->children());
  106. $this->Tree->id = 1;
  107. $this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
  108. $result = $this->Tree->children();
  109. $expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
  110. $this->assertEquals($expected, $result);
  111. $this->assertTrue($this->Tree->delete());
  112. $this->assertEquals(11, $this->Tree->find('count'));
  113. }
  114. /**
  115. * testMoveUpWithScope method
  116. *
  117. * @return void
  118. */
  119. public function testMoveUpWithScope() {
  120. $this->Ad = new Ad();
  121. $this->Ad->order = null;
  122. $this->Ad->Behaviors->load('Tree', array('scope' => 'Campaign'));
  123. $this->Ad->moveUp(6);
  124. $this->Ad->id = 4;
  125. $result = $this->Ad->children();
  126. $this->assertEquals(array(6, 5), Hash::extract($result, '{n}.Ad.id'));
  127. $this->assertEquals(array(2, 2), Hash::extract($result, '{n}.Campaign.id'));
  128. }
  129. /**
  130. * testMoveDownWithScope method
  131. *
  132. * @return void
  133. */
  134. public function testMoveDownWithScope() {
  135. $this->Ad = new Ad();
  136. $this->Ad->order = null;
  137. $this->Ad->Behaviors->load('Tree', array('scope' => 'Campaign'));
  138. $this->Ad->moveDown(6);
  139. $this->Ad->id = 4;
  140. $result = $this->Ad->children();
  141. $this->assertEquals(array(5, 6), Hash::extract($result, '{n}.Ad.id'));
  142. $this->assertEquals(array(2, 2), Hash::extract($result, '{n}.Campaign.id'));
  143. }
  144. /**
  145. * Tests the interaction (non-interference) between TreeBehavior and other behaviors with respect
  146. * to callback hooks
  147. *
  148. * @return void
  149. */
  150. public function testTranslatingTree() {
  151. $this->Tree = new FlagTree();
  152. $this->Tree->order = null;
  153. $this->Tree->cacheQueries = false;
  154. $this->Tree->Behaviors->load('Translate', array('title'));
  155. //Save
  156. $this->Tree->create();
  157. $this->Tree->locale = 'eng';
  158. $data = array('FlagTree' => array(
  159. 'title' => 'name #1',
  160. 'name' => 'test',
  161. 'locale' => 'eng',
  162. 'parent_id' => null,
  163. ));
  164. $this->Tree->save($data);
  165. $result = $this->Tree->find('all');
  166. $expected = array(array('FlagTree' => array(
  167. 'id' => 1,
  168. 'title' => 'name #1',
  169. 'name' => 'test',
  170. 'parent_id' => null,
  171. 'lft' => 1,
  172. 'rght' => 2,
  173. 'flag' => 0,
  174. 'locale' => 'eng',
  175. )));
  176. $this->assertEquals($expected, $result);
  177. // update existing record, same locale
  178. $this->Tree->create();
  179. $data['FlagTree']['title'] = 'Named 2';
  180. $this->Tree->id = 1;
  181. $this->Tree->save($data);
  182. $result = $this->Tree->find('all');
  183. $expected = array(array('FlagTree' => array(
  184. 'id' => 1,
  185. 'title' => 'Named 2',
  186. 'name' => 'test',
  187. 'parent_id' => null,
  188. 'lft' => 1,
  189. 'rght' => 2,
  190. 'flag' => 0,
  191. 'locale' => 'eng',
  192. )));
  193. $this->assertEquals($expected, $result);
  194. // update different locale, same record
  195. $this->Tree->create();
  196. $this->Tree->locale = 'deu';
  197. $this->Tree->id = 1;
  198. $data = array('FlagTree' => array(
  199. 'id' => 1,
  200. 'parent_id' => null,
  201. 'title' => 'namen #1',
  202. 'name' => 'test',
  203. 'locale' => 'deu',
  204. ));
  205. $this->Tree->save($data);
  206. $this->Tree->locale = 'deu';
  207. $result = $this->Tree->find('all');
  208. $expected = array(
  209. array(
  210. 'FlagTree' => array(
  211. 'id' => 1,
  212. 'title' => 'namen #1',
  213. 'name' => 'test',
  214. 'parent_id' => null,
  215. 'lft' => 1,
  216. 'rght' => 2,
  217. 'flag' => 0,
  218. 'locale' => 'deu',
  219. )
  220. )
  221. );
  222. $this->assertEquals($expected, $result);
  223. // Save with bindTranslation
  224. $this->Tree->locale = 'eng';
  225. $data = array(
  226. 'title' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
  227. 'name' => 'test',
  228. 'parent_id' => null
  229. );
  230. $this->Tree->create($data);
  231. $this->Tree->save();
  232. $this->Tree->unbindTranslation();
  233. $translations = array('title' => 'Title');
  234. $this->Tree->bindTranslation($translations, false);
  235. $this->Tree->locale = array('eng', 'spa');
  236. $result = $this->Tree->read();
  237. $expected = array(
  238. 'FlagTree' => array(
  239. 'id' => 2,
  240. 'parent_id' => null,
  241. 'locale' => 'eng',
  242. 'name' => 'test',
  243. 'title' => 'New title',
  244. 'flag' => 0,
  245. 'lft' => 3,
  246. 'rght' => 4
  247. ),
  248. 'Title' => array(
  249. array('id' => 21, 'locale' => 'eng', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'title', 'content' => 'New title'),
  250. array('id' => 22, 'locale' => 'spa', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Nuevo leyenda')
  251. ),
  252. );
  253. $this->assertEquals($expected, $result);
  254. }
  255. /**
  256. * testGenerateTreeListWithSelfJoin method
  257. *
  258. * @return void
  259. */
  260. public function testAliasesWithScopeInTwoTreeAssociations() {
  261. extract($this->settings);
  262. $this->Tree = new $modelClass();
  263. $this->Tree->order = null;
  264. $this->Tree->initialize(2, 2);
  265. $this->TreeTwo = new NumberTreeTwo();
  266. $this->TreeTwo->order = null;
  267. $record = $this->Tree->find('first');
  268. $this->Tree->bindModel(array(
  269. 'hasMany' => array(
  270. 'SecondTree' => array(
  271. 'className' => 'NumberTreeTwo',
  272. 'foreignKey' => 'number_tree_id'
  273. )
  274. )
  275. ));
  276. $this->TreeTwo->bindModel(array(
  277. 'belongsTo' => array(
  278. 'FirstTree' => array(
  279. 'className' => $modelClass,
  280. 'foreignKey' => 'number_tree_id'
  281. )
  282. )
  283. ));
  284. $this->TreeTwo->Behaviors->load('Tree', array(
  285. 'scope' => 'FirstTree'
  286. ));
  287. $data = array(
  288. 'NumberTreeTwo' => array(
  289. 'name' => 'First',
  290. 'number_tree_id' => $record['FlagTree']['id']
  291. )
  292. );
  293. $this->TreeTwo->create();
  294. $result = $this->TreeTwo->save($data);
  295. $this->assertFalse(empty($result));
  296. $result = $this->TreeTwo->find('first');
  297. $expected = array('NumberTreeTwo' => array(
  298. 'id' => 1,
  299. 'name' => 'First',
  300. 'number_tree_id' => $record['FlagTree']['id'],
  301. 'parent_id' => null,
  302. 'lft' => 1,
  303. 'rght' => 2
  304. ));
  305. $this->assertEquals($expected, $result);
  306. }
  307. /**
  308. * testGenerateTreeListWithScope method
  309. *
  310. * @return void
  311. */
  312. public function testGenerateTreeListWithScope() {
  313. extract($this->settings);
  314. $this->Tree = new $modelClass();
  315. $this->Tree->order = null;
  316. $this->Tree->initialize(2, 3);
  317. $this->Tree->id = 1;
  318. $this->Tree->saveField('flag', 1);
  319. $this->Tree->id = 2;
  320. $this->Tree->saveField('flag', 1);
  321. $this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
  322. $result = $this->Tree->generateTreeList();
  323. $expected = array(
  324. 1 => '1. Root',
  325. 2 => '_1.1'
  326. );
  327. $this->assertEquals($expected, $result);
  328. // As string.
  329. $this->Tree->Behaviors->load('Tree', array('scope' => 'FlagTree.flag = 1'));
  330. $result = $this->Tree->generateTreeList();
  331. $this->assertEquals($expected, $result);
  332. // Merging conditions.
  333. $result = $this->Tree->generateTreeList(array('FlagTree.id >' => 1));
  334. $expected = array(
  335. 2 => '1.1'
  336. );
  337. $this->assertEquals($expected, $result);
  338. }
  339. /**
  340. * testRecoverUsingParentMode method
  341. *
  342. * @return void
  343. */
  344. public function testRecoverUsingParentMode() {
  345. extract($this->settings);
  346. $this->Tree = new $modelClass();
  347. $this->Tree->order = null;
  348. $this->Tree->initialize(2, 3);
  349. $this->Tree->Behaviors->load('Tree', array('scope' => 'FlagTree.flag = 1'));
  350. $this->Tree->Behaviors->disable('Tree');
  351. $this->Tree->create();
  352. $this->Tree->save(array('name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0, 'flag' => 1));
  353. $node1 = $this->Tree->id;
  354. $this->Tree->create();
  355. $this->Tree->save(array('name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0, 'flag' => 1));
  356. $node11 = $this->Tree->id;
  357. $this->Tree->create();
  358. $this->Tree->save(array('name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0, 'flag' => 1));
  359. $node12 = $this->Tree->id;
  360. $this->Tree->create();
  361. $this->Tree->save(array('name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0, 'flag' => 1));
  362. $this->Tree->create();
  363. $this->Tree->save(array('name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0, 'flag' => 1));
  364. $this->Tree->Behaviors->enable('Tree');
  365. $result = $this->Tree->verify();
  366. $this->assertNotSame($result, true);
  367. $result = $this->Tree->recover();
  368. $this->assertTrue($result);
  369. $result = $this->Tree->verify();
  370. $this->assertTrue($result);
  371. $result = $this->Tree->find('first', array(
  372. 'fields' => array('name', $parentField, $leftField, $rightField, 'flag'),
  373. 'conditions' => array('name' => 'Main'),
  374. 'recursive' => -1
  375. ));
  376. $expected = array(
  377. $modelClass => array(
  378. 'name' => 'Main',
  379. $parentField => null,
  380. $leftField => 1,
  381. $rightField => 10,
  382. 'flag' => 1
  383. )
  384. );
  385. $this->assertEquals($expected, $result);
  386. }
  387. /**
  388. * testRecoverFromMissingParent method
  389. *
  390. * @return void
  391. */
  392. public function testRecoverFromMissingParent() {
  393. extract($this->settings);
  394. $this->Tree = new $modelClass();
  395. $this->Tree->order = null;
  396. $this->Tree->initialize(2, 2);
  397. $this->Tree->id = 1;
  398. $this->Tree->saveField('flag', 1);
  399. $this->Tree->id = 2;
  400. $this->Tree->saveField('flag', 1);
  401. $this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
  402. $result = $this->Tree->findByName('1.1');
  403. $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
  404. $result = $this->Tree->verify();
  405. $this->assertNotSame($result, true);
  406. $result = $this->Tree->recover();
  407. $this->assertTrue($result);
  408. $result = $this->Tree->verify();
  409. $this->assertTrue($result);
  410. }
  411. /**
  412. * testDetectInvalidParents method
  413. *
  414. * @return void
  415. */
  416. public function testDetectInvalidParents() {
  417. extract($this->settings);
  418. $this->Tree = new $modelClass();
  419. $this->Tree->order = null;
  420. $this->Tree->initialize(2, 2);
  421. $this->Tree->id = 1;
  422. $this->Tree->saveField('flag', 1);
  423. $this->Tree->id = 2;
  424. $this->Tree->saveField('flag', 1);
  425. $this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
  426. $this->Tree->updateAll(array($parentField => null));
  427. $result = $this->Tree->verify();
  428. $this->assertNotSame($result, true);
  429. $result = $this->Tree->recover();
  430. $this->assertTrue($result);
  431. $result = $this->Tree->verify();
  432. $this->assertTrue($result);
  433. }
  434. /**
  435. * testDetectInvalidLftsRghts method
  436. *
  437. * @return void
  438. */
  439. public function testDetectInvalidLftsRghts() {
  440. extract($this->settings);
  441. $this->Tree = new $modelClass();
  442. $this->Tree->order = null;
  443. $this->Tree->initialize(2, 2);
  444. $this->Tree->id = 1;
  445. $this->Tree->saveField('flag', 1);
  446. $this->Tree->id = 2;
  447. $this->Tree->saveField('flag', 1);
  448. $this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
  449. $this->Tree->updateAll(array($leftField => 0, $rightField => 0));
  450. $result = $this->Tree->verify();
  451. $this->assertNotSame($result, true);
  452. $this->Tree->recover();
  453. $result = $this->Tree->verify();
  454. $this->assertTrue($result);
  455. }
  456. /**
  457. * Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
  458. *
  459. * @return void
  460. */
  461. public function testDetectEqualLftsRghts() {
  462. extract($this->settings);
  463. $this->Tree = new $modelClass();
  464. $this->Tree->order = null;
  465. $this->Tree->initialize(1, 3);
  466. $this->Tree->id = 1;
  467. $this->Tree->saveField('flag', 1);
  468. $this->Tree->id = 2;
  469. $this->Tree->saveField('flag', 1);
  470. $this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
  471. $result = $this->Tree->findByName('1.1');
  472. $this->Tree->updateAll(array($rightField => $result[$modelClass][$leftField]), array('id' => $result[$modelClass]['id']));
  473. $this->Tree->updateAll(array($leftField => $this->Tree->escapeField($leftField) . ' -1'),
  474. array($leftField . ' >' => $result[$modelClass][$leftField]));
  475. $this->Tree->updateAll(array($rightField => $this->Tree->escapeField($rightField) . ' -1'),
  476. array($rightField . ' >' => $result[$modelClass][$leftField]));
  477. $result = $this->Tree->verify();
  478. $this->assertNotSame($result, true);
  479. $result = $this->Tree->recover();
  480. $this->assertTrue($result);
  481. $result = $this->Tree->verify();
  482. $this->assertTrue($result);
  483. }
  484. }