TreeBehaviorScopedTest.php 15 KB

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