TreeBehaviorScopedTest.php 8.9 KB

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