TreeBehaviorScopedTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
  75. $this->assertEquals(array(), $this->Tree->children());
  76. $this->Tree->id = 1;
  77. $this->Tree->Behaviors->attach('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->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
  105. $this->assertEquals(array(), $this->Tree->children());
  106. $this->Tree->id = 1;
  107. $this->Tree->Behaviors->attach('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->attach('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->attach('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->attach('Translate', array('title'));
  155. //Save
  156. $this->Tree->locale = 'eng';
  157. $data = array('FlagTree' => array(
  158. 'title' => 'name #1',
  159. 'name' => 'test',
  160. 'locale' => 'eng',
  161. 'parent_id' => null,
  162. ));
  163. $this->Tree->save($data);
  164. $result = $this->Tree->find('all');
  165. $expected = array(array('FlagTree' => array(
  166. 'id' => 1,
  167. 'title' => 'name #1',
  168. 'name' => 'test',
  169. 'parent_id' => null,
  170. 'lft' => 1,
  171. 'rght' => 2,
  172. 'flag' => 0,
  173. 'locale' => 'eng',
  174. )));
  175. $this->assertEquals($expected, $result);
  176. // update existing record, same locale
  177. $this->Tree->create();
  178. $data['FlagTree']['title'] = 'Named 2';
  179. $this->Tree->id = 1;
  180. $this->Tree->save($data);
  181. $result = $this->Tree->find('all');
  182. $expected = array(array('FlagTree' => array(
  183. 'id' => 1,
  184. 'title' => 'Named 2',
  185. 'name' => 'test',
  186. 'parent_id' => null,
  187. 'lft' => 1,
  188. 'rght' => 2,
  189. 'flag' => 0,
  190. 'locale' => 'eng',
  191. )));
  192. $this->assertEquals($expected, $result);
  193. // update different locale, same record
  194. $this->Tree->create();
  195. $this->Tree->locale = 'deu';
  196. $this->Tree->id = 1;
  197. $data = array('FlagTree' => array(
  198. 'id' => 1,
  199. 'parent_id' => null,
  200. 'title' => 'namen #1',
  201. 'name' => 'test',
  202. 'locale' => 'deu',
  203. ));
  204. $this->Tree->save($data);
  205. $this->Tree->locale = 'deu';
  206. $result = $this->Tree->find('all');
  207. $expected = array(
  208. array(
  209. 'FlagTree' => array(
  210. 'id' => 1,
  211. 'title' => 'namen #1',
  212. 'name' => 'test',
  213. 'parent_id' => null,
  214. 'lft' => 1,
  215. 'rght' => 2,
  216. 'flag' => 0,
  217. 'locale' => 'deu',
  218. )
  219. )
  220. );
  221. $this->assertEquals($expected, $result);
  222. // Save with bindTranslation
  223. $this->Tree->locale = 'eng';
  224. $data = array(
  225. 'title' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
  226. 'name' => 'test',
  227. 'parent_id' => null
  228. );
  229. $this->Tree->create($data);
  230. $this->Tree->save();
  231. $this->Tree->unbindTranslation();
  232. $translations = array('title' => 'Title');
  233. $this->Tree->bindTranslation($translations, false);
  234. $this->Tree->locale = array('eng', 'spa');
  235. $result = $this->Tree->read();
  236. $expected = array(
  237. 'FlagTree' => array(
  238. 'id' => 2,
  239. 'parent_id' => null,
  240. 'locale' => 'eng',
  241. 'name' => 'test',
  242. 'title' => 'New title',
  243. 'flag' => 0,
  244. 'lft' => 3,
  245. 'rght' => 4
  246. ),
  247. 'Title' => array(
  248. array('id' => 21, 'locale' => 'eng', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'title', 'content' => 'New title'),
  249. array('id' => 22, 'locale' => 'spa', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Nuevo leyenda')
  250. ),
  251. );
  252. $this->assertEquals($expected, $result);
  253. }
  254. /**
  255. * testGenerateTreeListWithSelfJoin method
  256. *
  257. * @return void
  258. */
  259. public function testAliasesWithScopeInTwoTreeAssociations() {
  260. extract($this->settings);
  261. $this->Tree = new $modelClass();
  262. $this->Tree->order = null;
  263. $this->Tree->initialize(2, 2);
  264. $this->TreeTwo = new NumberTreeTwo();
  265. $this->TreeTwo->order = null;
  266. $record = $this->Tree->find('first');
  267. $this->Tree->bindModel(array(
  268. 'hasMany' => array(
  269. 'SecondTree' => array(
  270. 'className' => 'NumberTreeTwo',
  271. 'foreignKey' => 'number_tree_id'
  272. )
  273. )
  274. ));
  275. $this->TreeTwo->bindModel(array(
  276. 'belongsTo' => array(
  277. 'FirstTree' => array(
  278. 'className' => $modelClass,
  279. 'foreignKey' => 'number_tree_id'
  280. )
  281. )
  282. ));
  283. $this->TreeTwo->Behaviors->attach('Tree', array(
  284. 'scope' => 'FirstTree'
  285. ));
  286. $data = array(
  287. 'NumberTreeTwo' => array(
  288. 'name' => 'First',
  289. 'number_tree_id' => $record['FlagTree']['id']
  290. )
  291. );
  292. $this->TreeTwo->create();
  293. $result = $this->TreeTwo->save($data);
  294. $this->assertFalse(empty($result));
  295. $result = $this->TreeTwo->find('first');
  296. $expected = array('NumberTreeTwo' => array(
  297. 'id' => 1,
  298. 'name' => 'First',
  299. 'number_tree_id' => $record['FlagTree']['id'],
  300. 'parent_id' => null,
  301. 'lft' => 1,
  302. 'rght' => 2
  303. ));
  304. $this->assertEquals($expected, $result);
  305. }
  306. /**
  307. * testGenerateTreeListWithScope method
  308. *
  309. * @return void
  310. */
  311. public function testGenerateTreeListWithScope() {
  312. extract($this->settings);
  313. $this->Tree = new $modelClass();
  314. $this->Tree->order = null;
  315. $this->Tree->initialize(2, 3);
  316. $this->Tree->id = 1;
  317. $this->Tree->saveField('flag', 1);
  318. $this->Tree->id = 2;
  319. $this->Tree->saveField('flag', 1);
  320. $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
  321. $result = $this->Tree->generateTreeList();
  322. $expected = array(
  323. 1 => '1. Root',
  324. 2 => '_1.1'
  325. );
  326. $this->assertEquals($expected, $result);
  327. // As string.
  328. $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
  329. $result = $this->Tree->generateTreeList();
  330. $this->assertEquals($expected, $result);
  331. // Merging conditions.
  332. $result = $this->Tree->generateTreeList(array('FlagTree.id >' => 1));
  333. $expected = array(
  334. 2 => '1.1'
  335. );
  336. $this->assertEquals($expected, $result);
  337. }
  338. /**
  339. * testRecoverUsingParentMode method
  340. *
  341. * @return void
  342. */
  343. public function testRecoverUsingParentMode() {
  344. extract($this->settings);
  345. $this->Tree = new $modelClass();
  346. $this->Tree->order = null;
  347. $this->Tree->initialize(2, 3);
  348. $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
  349. $this->Tree->Behaviors->disable('Tree');
  350. $this->Tree->save(array('name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0, 'flag' => 1));
  351. $node1 = $this->Tree->id;
  352. $this->Tree->create();
  353. $this->Tree->save(array('name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0, 'flag' => 1));
  354. $node11 = $this->Tree->id;
  355. $this->Tree->create();
  356. $this->Tree->save(array('name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0, 'flag' => 1));
  357. $node12 = $this->Tree->id;
  358. $this->Tree->create();
  359. $this->Tree->save(array('name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0, 'flag' => 1));
  360. $this->Tree->create();
  361. $this->Tree->save(array('name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0, 'flag' => 1));
  362. $this->Tree->Behaviors->enable('Tree');
  363. $result = $this->Tree->verify();
  364. $this->assertNotSame($result, true);
  365. $result = $this->Tree->recover();
  366. $this->assertTrue($result);
  367. $result = $this->Tree->verify();
  368. $this->assertTrue($result);
  369. $result = $this->Tree->find('first', array(
  370. 'fields' => array('name', $parentField, $leftField, $rightField, 'flag'),
  371. 'conditions' => array('name' => 'Main'),
  372. 'recursive' => -1
  373. ));
  374. $expected = array(
  375. $modelClass => array(
  376. 'name' => 'Main',
  377. $parentField => null,
  378. $leftField => 1,
  379. $rightField => 10,
  380. 'flag' => 1
  381. )
  382. );
  383. $this->assertEquals($expected, $result);
  384. }
  385. /**
  386. * testRecoverFromMissingParent method
  387. *
  388. * @return void
  389. */
  390. public function testRecoverFromMissingParent() {
  391. extract($this->settings);
  392. $this->Tree = new $modelClass();
  393. $this->Tree->order = null;
  394. $this->Tree->initialize(2, 2);
  395. $this->Tree->id = 1;
  396. $this->Tree->saveField('flag', 1);
  397. $this->Tree->id = 2;
  398. $this->Tree->saveField('flag', 1);
  399. $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
  400. $result = $this->Tree->findByName('1.1');
  401. $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
  402. $result = $this->Tree->verify();
  403. $this->assertNotSame($result, true);
  404. $result = $this->Tree->recover();
  405. $this->assertTrue($result);
  406. $result = $this->Tree->verify();
  407. $this->assertTrue($result);
  408. }
  409. /**
  410. * testDetectInvalidParents method
  411. *
  412. * @return void
  413. */
  414. public function testDetectInvalidParents() {
  415. extract($this->settings);
  416. $this->Tree = new $modelClass();
  417. $this->Tree->order = null;
  418. $this->Tree->initialize(2, 2);
  419. $this->Tree->id = 1;
  420. $this->Tree->saveField('flag', 1);
  421. $this->Tree->id = 2;
  422. $this->Tree->saveField('flag', 1);
  423. $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
  424. $this->Tree->updateAll(array($parentField => null));
  425. $result = $this->Tree->verify();
  426. $this->assertNotSame($result, true);
  427. $result = $this->Tree->recover();
  428. $this->assertTrue($result);
  429. $result = $this->Tree->verify();
  430. $this->assertTrue($result);
  431. }
  432. /**
  433. * testDetectInvalidLftsRghts method
  434. *
  435. * @return void
  436. */
  437. public function testDetectInvalidLftsRghts() {
  438. extract($this->settings);
  439. $this->Tree = new $modelClass();
  440. $this->Tree->order = null;
  441. $this->Tree->initialize(2, 2);
  442. $this->Tree->id = 1;
  443. $this->Tree->saveField('flag', 1);
  444. $this->Tree->id = 2;
  445. $this->Tree->saveField('flag', 1);
  446. $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
  447. $this->Tree->updateAll(array($leftField => 0, $rightField => 0));
  448. $result = $this->Tree->verify();
  449. $this->assertNotSame($result, true);
  450. $this->Tree->recover();
  451. $result = $this->Tree->verify();
  452. $this->assertTrue($result);
  453. }
  454. /**
  455. * Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
  456. *
  457. * @return void
  458. */
  459. public function testDetectEqualLftsRghts() {
  460. extract($this->settings);
  461. $this->Tree = new $modelClass();
  462. $this->Tree->order = null;
  463. $this->Tree->initialize(1, 3);
  464. $this->Tree->id = 1;
  465. $this->Tree->saveField('flag', 1);
  466. $this->Tree->id = 2;
  467. $this->Tree->saveField('flag', 1);
  468. $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
  469. $result = $this->Tree->findByName('1.1');
  470. $this->Tree->updateAll(array($rightField => $result[$modelClass][$leftField]), array('id' => $result[$modelClass]['id']));
  471. $this->Tree->updateAll(array($leftField => $this->Tree->escapeField($leftField) . ' -1'),
  472. array($leftField . ' >' => $result[$modelClass][$leftField]));
  473. $this->Tree->updateAll(array($rightField => $this->Tree->escapeField($rightField) . ' -1'),
  474. array($rightField . ' >' => $result[$modelClass][$leftField]));
  475. $result = $this->Tree->verify();
  476. $this->assertNotSame($result, true);
  477. $result = $this->Tree->recover();
  478. $this->assertTrue($result);
  479. $result = $this->Tree->verify();
  480. $this->assertTrue($result);
  481. }
  482. }