PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Model/Behavior/TreeBehaviorScopedTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 316 lines | 205 code | 37 blank | 74 comment | 0 complexity | 9d6fae5c2b19e5019ec3bb4718a4a62c MD5 | raw file
  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/view/1196/Testing>
  10. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://book.cakephp.org/view/1196/Testing 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($this->Tree->children(), array());
  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($this->Tree->find('count'), 11);
  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($this->Tree->children(), array());
  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($this->Tree->find('count'), 11);
  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(Set::extract('/Ad/id', $result), array(6, 5));
  123. $this->assertEquals(Set::extract('/Campaign/id', $result), array(2, 2));
  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(Set::extract('/Ad/id', $result), array(5, 6));
  137. $this->assertEquals(Set::extract('/Campaign/id', $result), array(2, 2));
  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('name'));
  149. //Save
  150. $this->Tree->locale = 'eng';
  151. $data = array('FlagTree' => array(
  152. 'name' => '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. 'name' => 'name #1',
  161. 'parent_id' => null,
  162. 'lft' => 1,
  163. 'rght' => 2,
  164. 'flag' => 0,
  165. 'locale' => 'eng',
  166. )));
  167. $this->assertEquals($expected, $result);
  168. //update existing record, same locale
  169. $this->Tree->create();
  170. $data['FlagTree']['name'] = 'Named 2';
  171. $this->Tree->id = 1;
  172. $this->Tree->save($data);
  173. $result = $this->Tree->find('all');
  174. $expected = array(array('FlagTree' => array(
  175. 'id' => 1,
  176. 'name' => 'Named 2',
  177. 'parent_id' => null,
  178. 'lft' => 1,
  179. 'rght' => 2,
  180. 'flag' => 0,
  181. 'locale' => 'eng',
  182. )));
  183. $this->assertEquals($expected, $result);
  184. //update different locale, same record
  185. $this->Tree->create();
  186. $this->Tree->locale = 'deu';
  187. $this->Tree->id = 1;
  188. $data = array('FlagTree' => array(
  189. 'id' => 1,
  190. 'parent_id' => null,
  191. 'name' => 'namen #1',
  192. 'locale' => 'deu',
  193. ));
  194. $this->Tree->save($data);
  195. $this->Tree->locale = 'deu';
  196. $result = $this->Tree->find('all');
  197. $expected = array(array('FlagTree' => array(
  198. 'id' => 1,
  199. 'name' => 'namen #1',
  200. 'parent_id' => null,
  201. 'lft' => 1,
  202. 'rght' => 2,
  203. 'flag' => 0,
  204. 'locale' => 'deu',
  205. )));
  206. $this->assertEquals($expected, $result);
  207. //Save with bindTranslation
  208. $this->Tree->locale = 'eng';
  209. $data = array(
  210. 'name' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
  211. 'parent_id' => null
  212. );
  213. $this->Tree->create($data);
  214. $this->Tree->save();
  215. $this->Tree->unbindTranslation();
  216. $translations = array('name' => 'Name');
  217. $this->Tree->bindTranslation($translations, false);
  218. $this->Tree->locale = array('eng', 'spa');
  219. $result = $this->Tree->read();
  220. $expected = array(
  221. 'FlagTree' => array('id' => 2, 'parent_id' => null, 'locale' => 'eng', 'name' => 'New title', 'flag' => 0, 'lft' => 3, 'rght' => 4),
  222. 'Name' => array(
  223. array('id' => 21, 'locale' => 'eng', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'New title'),
  224. array('id' => 22, 'locale' => 'spa', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'Nuevo leyenda')
  225. ),
  226. );
  227. $this->assertEquals($expected, $result);
  228. }
  229. /**
  230. * testGenerateTreeListWithSelfJoin method
  231. *
  232. * @return void
  233. */
  234. public function testAliasesWithScopeInTwoTreeAssociations() {
  235. extract($this->settings);
  236. $this->Tree = new $modelClass();
  237. $this->Tree->initialize(2, 2);
  238. $this->TreeTwo = new NumberTreeTwo();
  239. $record = $this->Tree->find('first');
  240. $this->Tree->bindModel(array(
  241. 'hasMany' => array(
  242. 'SecondTree' => array(
  243. 'className' => 'NumberTreeTwo',
  244. 'foreignKey' => 'number_tree_id'
  245. )
  246. )
  247. ));
  248. $this->TreeTwo->bindModel(array(
  249. 'belongsTo' => array(
  250. 'FirstTree' => array(
  251. 'className' => $modelClass,
  252. 'foreignKey' => 'number_tree_id'
  253. )
  254. )
  255. ));
  256. $this->TreeTwo->Behaviors->attach('Tree', array(
  257. 'scope' => 'FirstTree'
  258. ));
  259. $data = array(
  260. 'NumberTreeTwo' => array(
  261. 'name' => 'First',
  262. 'number_tree_id' => $record['FlagTree']['id']
  263. )
  264. );
  265. $this->TreeTwo->create();
  266. $result = $this->TreeTwo->save($data);
  267. $this->assertFalse(empty($result));
  268. $result = $this->TreeTwo->find('first');
  269. $expected = array('NumberTreeTwo' => array(
  270. 'id' => 1,
  271. 'name' => 'First',
  272. 'number_tree_id' => $record['FlagTree']['id'],
  273. 'parent_id' => null,
  274. 'lft' => 1,
  275. 'rght' => 2
  276. ));
  277. $this->assertEquals($expected, $result);
  278. }
  279. }