TreeBehaviorScopedTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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 bool false
  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->initialize(2, 3);
  62. $this->Tree->id = 1;
  63. $this->Tree->saveField('flag', 1);
  64. $this->Tree->id = 2;
  65. $this->Tree->saveField('flag', 1);
  66. $result = $this->Tree->children();
  67. $expected = array(
  68. array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
  69. array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
  70. array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
  71. );
  72. $this->assertEquals($expected, $result);
  73. $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
  74. $this->assertEquals(array(), $this->Tree->children());
  75. $this->Tree->id = 1;
  76. $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
  77. $result = $this->Tree->children();
  78. $expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
  79. $this->assertEquals($expected, $result);
  80. $this->assertTrue($this->Tree->delete());
  81. $this->assertEquals(11, $this->Tree->find('count'));
  82. }
  83. /**
  84. * testArrayScope method
  85. *
  86. * @return void
  87. */
  88. public function testArrayScope() {
  89. $this->Tree = new FlagTree();
  90. $this->Tree->initialize(2, 3);
  91. $this->Tree->id = 1;
  92. $this->Tree->saveField('flag', 1);
  93. $this->Tree->id = 2;
  94. $this->Tree->saveField('flag', 1);
  95. $result = $this->Tree->children();
  96. $expected = array(
  97. array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
  98. array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
  99. array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
  100. );
  101. $this->assertEquals($expected, $result);
  102. $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
  103. $this->assertEquals(array(), $this->Tree->children());
  104. $this->Tree->id = 1;
  105. $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
  106. $result = $this->Tree->children();
  107. $expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
  108. $this->assertEquals($expected, $result);
  109. $this->assertTrue($this->Tree->delete());
  110. $this->assertEquals(11, $this->Tree->find('count'));
  111. }
  112. /**
  113. * testMoveUpWithScope method
  114. *
  115. * @return void
  116. */
  117. public function testMoveUpWithScope() {
  118. $this->Ad = new Ad();
  119. $this->Ad->Behaviors->attach('Tree', array('scope' => 'Campaign'));
  120. $this->Ad->moveUp(6);
  121. $this->Ad->id = 4;
  122. $result = $this->Ad->children();
  123. $this->assertEquals(array(6, 5), Hash::extract($result, '{n}.Ad.id'));
  124. $this->assertEquals(array(2, 2), Hash::extract($result, '{n}.Campaign.id'));
  125. }
  126. /**
  127. * testMoveDownWithScope method
  128. *
  129. * @return void
  130. */
  131. public function testMoveDownWithScope() {
  132. $this->Ad = new Ad();
  133. $this->Ad->Behaviors->attach('Tree', array('scope' => 'Campaign'));
  134. $this->Ad->moveDown(6);
  135. $this->Ad->id = 4;
  136. $result = $this->Ad->children();
  137. $this->assertEquals(array(5, 6), Hash::extract($result, '{n}.Ad.id'));
  138. $this->assertEquals(array(2, 2), Hash::extract($result, '{n}.Campaign.id'));
  139. }
  140. /**
  141. * Tests the interaction (non-interference) between TreeBehavior and other behaviors with respect
  142. * to callback hooks
  143. *
  144. * @return void
  145. */
  146. public function testTranslatingTree() {
  147. $this->Tree = new FlagTree();
  148. $this->Tree->cacheQueries = false;
  149. $this->Tree->Behaviors->attach('Translate', array('title'));
  150. //Save
  151. $this->Tree->locale = 'eng';
  152. $data = array('FlagTree' => array(
  153. 'title' => 'name #1',
  154. 'name' => 'test',
  155. 'locale' => 'eng',
  156. 'parent_id' => null,
  157. ));
  158. $this->Tree->save($data);
  159. $result = $this->Tree->find('all');
  160. $expected = array(array('FlagTree' => array(
  161. 'id' => 1,
  162. 'title' => 'name #1',
  163. 'name' => 'test',
  164. 'parent_id' => null,
  165. 'lft' => 1,
  166. 'rght' => 2,
  167. 'flag' => 0,
  168. 'locale' => 'eng',
  169. )));
  170. $this->assertEquals($expected, $result);
  171. // update existing record, same locale
  172. $this->Tree->create();
  173. $data['FlagTree']['title'] = 'Named 2';
  174. $this->Tree->id = 1;
  175. $this->Tree->save($data);
  176. $result = $this->Tree->find('all');
  177. $expected = array(array('FlagTree' => array(
  178. 'id' => 1,
  179. 'title' => 'Named 2',
  180. 'name' => 'test',
  181. 'parent_id' => null,
  182. 'lft' => 1,
  183. 'rght' => 2,
  184. 'flag' => 0,
  185. 'locale' => 'eng',
  186. )));
  187. $this->assertEquals($expected, $result);
  188. // update different locale, same record
  189. $this->Tree->create();
  190. $this->Tree->locale = 'deu';
  191. $this->Tree->id = 1;
  192. $data = array('FlagTree' => array(
  193. 'id' => 1,
  194. 'parent_id' => null,
  195. 'title' => 'namen #1',
  196. 'name' => 'test',
  197. 'locale' => 'deu',
  198. ));
  199. $this->Tree->save($data);
  200. $this->Tree->locale = 'deu';
  201. $result = $this->Tree->find('all');
  202. $expected = array(
  203. array(
  204. 'FlagTree' => array(
  205. 'id' => 1,
  206. 'title' => 'namen #1',
  207. 'name' => 'test',
  208. 'parent_id' => null,
  209. 'lft' => 1,
  210. 'rght' => 2,
  211. 'flag' => 0,
  212. 'locale' => 'deu',
  213. )
  214. )
  215. );
  216. $this->assertEquals($expected, $result);
  217. // Save with bindTranslation
  218. $this->Tree->locale = 'eng';
  219. $data = array(
  220. 'title' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
  221. 'name' => 'test',
  222. 'parent_id' => null
  223. );
  224. $this->Tree->create($data);
  225. $this->Tree->save();
  226. $this->Tree->unbindTranslation();
  227. $translations = array('title' => 'Title');
  228. $this->Tree->bindTranslation($translations, false);
  229. $this->Tree->locale = array('eng', 'spa');
  230. $result = $this->Tree->read();
  231. $expected = array(
  232. 'FlagTree' => array(
  233. 'id' => 2,
  234. 'parent_id' => null,
  235. 'locale' => 'eng',
  236. 'name' => 'test',
  237. 'title' => 'New title',
  238. 'flag' => 0,
  239. 'lft' => 3,
  240. 'rght' => 4
  241. ),
  242. 'Title' => array(
  243. array('id' => 21, 'locale' => 'eng', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'title', 'content' => 'New title'),
  244. array('id' => 22, 'locale' => 'spa', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Nuevo leyenda')
  245. ),
  246. );
  247. $this->assertEquals($expected, $result);
  248. }
  249. /**
  250. * testGenerateTreeListWithSelfJoin method
  251. *
  252. * @return void
  253. */
  254. public function testAliasesWithScopeInTwoTreeAssociations() {
  255. extract($this->settings);
  256. $this->Tree = new $modelClass();
  257. $this->Tree->initialize(2, 2);
  258. $this->TreeTwo = new NumberTreeTwo();
  259. $record = $this->Tree->find('first');
  260. $this->Tree->bindModel(array(
  261. 'hasMany' => array(
  262. 'SecondTree' => array(
  263. 'className' => 'NumberTreeTwo',
  264. 'foreignKey' => 'number_tree_id'
  265. )
  266. )
  267. ));
  268. $this->TreeTwo->bindModel(array(
  269. 'belongsTo' => array(
  270. 'FirstTree' => array(
  271. 'className' => $modelClass,
  272. 'foreignKey' => 'number_tree_id'
  273. )
  274. )
  275. ));
  276. $this->TreeTwo->Behaviors->attach('Tree', array(
  277. 'scope' => 'FirstTree'
  278. ));
  279. $data = array(
  280. 'NumberTreeTwo' => array(
  281. 'name' => 'First',
  282. 'number_tree_id' => $record['FlagTree']['id']
  283. )
  284. );
  285. $this->TreeTwo->create();
  286. $result = $this->TreeTwo->save($data);
  287. $this->assertFalse(empty($result));
  288. $result = $this->TreeTwo->find('first');
  289. $expected = array('NumberTreeTwo' => array(
  290. 'id' => 1,
  291. 'name' => 'First',
  292. 'number_tree_id' => $record['FlagTree']['id'],
  293. 'parent_id' => null,
  294. 'lft' => 1,
  295. 'rght' => 2
  296. ));
  297. $this->assertEquals($expected, $result);
  298. }
  299. }