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

https://gitlab.com/manuperazafa/elsartenbackend · PHP · 298 lines · 181 code · 39 blank · 78 comment · 0 complexity · f1327d7f1251d670b442b146a3e61bd9 MD5 · raw file

  1. <?php
  2. /**
  3. * TreeBehaviorUuidTest file
  4. *
  5. * Tree test using UUIDs
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @package Cake.Test.Case.Model.Behavior
  17. * @since CakePHP(tm) v 1.2.0.5330
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('Model', 'Model');
  21. App::uses('AppModel', 'Model');
  22. App::uses('String', 'Utility');
  23. require_once dirname(dirname(__FILE__)) . DS . 'models.php';
  24. /**
  25. * TreeBehaviorUuidTest class
  26. *
  27. * @package Cake.Test.Case.Model.Behavior
  28. */
  29. class TreeBehaviorUuidTest extends CakeTestCase {
  30. /**
  31. * Whether backup global state for each test method or not
  32. *
  33. * @var bool
  34. */
  35. public $backupGlobals = false;
  36. /**
  37. * settings property
  38. *
  39. * @var array
  40. */
  41. public $settings = array(
  42. 'modelClass' => 'UuidTree',
  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.uuid_tree');
  53. /**
  54. * testAddWithPreSpecifiedId method
  55. *
  56. * @return void
  57. */
  58. public function testAddWithPreSpecifiedId() {
  59. extract($this->settings);
  60. $this->Tree = new $modelClass();
  61. $this->Tree->order = null;
  62. $this->Tree->initialize(2, 2);
  63. $data = $this->Tree->find('first', array(
  64. 'fields' => array('id'),
  65. 'conditions' => array($modelClass . '.name' => '1.1')
  66. ));
  67. $id = String::uuid();
  68. $this->Tree->create();
  69. $result = $this->Tree->save(array($modelClass => array(
  70. 'id' => $id,
  71. 'name' => 'testAddMiddle',
  72. $parentField => $data[$modelClass]['id'])
  73. ));
  74. $expected = array_merge(
  75. array($modelClass => array('id' => $id, 'name' => 'testAddMiddle', $parentField => '2')),
  76. $result
  77. );
  78. $this->assertSame($expected, $result);
  79. $this->assertTrue($this->Tree->verify());
  80. }
  81. /**
  82. * testMovePromote method
  83. *
  84. * @return void
  85. */
  86. public function testMovePromote() {
  87. extract($this->settings);
  88. $this->Tree = new $modelClass();
  89. $this->Tree->order = null;
  90. $this->Tree->initialize(2, 2);
  91. $this->Tree->id = null;
  92. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  93. $parentId = $parent[$modelClass]['id'];
  94. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  95. $this->Tree->id = $data[$modelClass]['id'];
  96. $this->Tree->saveField($parentField, $parentId);
  97. $direct = $this->Tree->children($parentId, true, array('name', $leftField, $rightField));
  98. $expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
  99. array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
  100. array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
  101. $this->assertEquals($expected, $direct);
  102. $validTree = $this->Tree->verify();
  103. $this->assertTrue($validTree);
  104. }
  105. /**
  106. * testMoveWithWhitelist method
  107. *
  108. * @return void
  109. */
  110. public function testMoveWithWhitelist() {
  111. extract($this->settings);
  112. $this->Tree = new $modelClass();
  113. $this->Tree->order = null;
  114. $this->Tree->initialize(2, 2);
  115. $this->Tree->id = null;
  116. $parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  117. $parentId = $parent[$modelClass]['id'];
  118. $data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
  119. $this->Tree->id = $data[$modelClass]['id'];
  120. $this->Tree->whitelist = array($parentField, 'name', 'description');
  121. $this->Tree->saveField($parentField, $parentId);
  122. $result = $this->Tree->children($parentId, true, array('name', $leftField, $rightField));
  123. $expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
  124. array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
  125. array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
  126. $this->assertEquals($expected, $result);
  127. $this->assertTrue($this->Tree->verify());
  128. }
  129. /**
  130. * testRemoveNoChildren method
  131. *
  132. * @return void
  133. */
  134. public function testRemoveNoChildren() {
  135. extract($this->settings);
  136. $this->Tree = new $modelClass();
  137. $this->Tree->order = null;
  138. $this->Tree->initialize(2, 2);
  139. $initialCount = $this->Tree->find('count');
  140. $result = $this->Tree->findByName('1.1.1');
  141. $this->Tree->removeFromTree($result[$modelClass]['id']);
  142. $laterCount = $this->Tree->find('count');
  143. $this->assertEquals($initialCount, $laterCount);
  144. $nodes = $this->Tree->find('list', array('order' => $leftField));
  145. $expected = array(
  146. '1. Root',
  147. '1.1',
  148. '1.1.2',
  149. '1.2',
  150. '1.2.1',
  151. '1.2.2',
  152. '1.1.1',
  153. );
  154. $this->assertEquals($expected, array_values($nodes));
  155. $validTree = $this->Tree->verify();
  156. $this->assertTrue($validTree);
  157. }
  158. /**
  159. * testRemoveAndDeleteNoChildren method
  160. *
  161. * @return void
  162. */
  163. public function testRemoveAndDeleteNoChildren() {
  164. extract($this->settings);
  165. $this->Tree = new $modelClass();
  166. $this->Tree->order = null;
  167. $this->Tree->initialize(2, 2);
  168. $initialCount = $this->Tree->find('count');
  169. $result = $this->Tree->findByName('1.1.1');
  170. $this->Tree->removeFromTree($result[$modelClass]['id'], true);
  171. $laterCount = $this->Tree->find('count');
  172. $this->assertEquals($initialCount - 1, $laterCount);
  173. $nodes = $this->Tree->find('list', array('order' => $leftField));
  174. $expected = array(
  175. '1. Root',
  176. '1.1',
  177. '1.1.2',
  178. '1.2',
  179. '1.2.1',
  180. '1.2.2',
  181. );
  182. $this->assertEquals($expected, array_values($nodes));
  183. $validTree = $this->Tree->verify();
  184. $this->assertTrue($validTree);
  185. }
  186. /**
  187. * testChildren method
  188. *
  189. * @return void
  190. */
  191. public function testChildren() {
  192. extract($this->settings);
  193. $this->Tree = new $modelClass();
  194. $this->Tree->order = null;
  195. $this->Tree->initialize(2, 2);
  196. $data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
  197. $this->Tree->id = $data[$modelClass]['id'];
  198. $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
  199. $expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  200. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
  201. $this->assertEquals($expected, $direct);
  202. $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
  203. $expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  204. array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
  205. array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
  206. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
  207. array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
  208. array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12)));
  209. $this->assertEquals($expected, $total);
  210. }
  211. /**
  212. * testNoAmbiguousColumn method
  213. *
  214. * @return void
  215. */
  216. public function testNoAmbiguousColumn() {
  217. extract($this->settings);
  218. $this->Tree = new $modelClass();
  219. $this->Tree->order = null;
  220. $this->Tree->initialize(2, 2);
  221. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  222. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  223. $data = $this->Tree->find('first', array(
  224. 'conditions' => array($modelClass . '.name' => '1. Root'),
  225. 'recursive' => -1
  226. ));
  227. $this->Tree->id = $data[$modelClass]['id'];
  228. $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
  229. $expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  230. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
  231. $this->assertEquals($expected, $direct);
  232. $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
  233. $expected = array(
  234. array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
  235. array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
  236. array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
  237. array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
  238. array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
  239. array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12))
  240. );
  241. $this->assertEquals($expected, $total);
  242. }
  243. /**
  244. * testGenerateTreeListWithSelfJoin method
  245. *
  246. * @return void
  247. */
  248. public function testGenerateTreeListWithSelfJoin() {
  249. extract($this->settings);
  250. $this->Tree = new $modelClass();
  251. $this->Tree->order = null;
  252. $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
  253. array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
  254. $this->Tree->initialize(2, 2);
  255. $result = $this->Tree->generateTreeList();
  256. $expected = array('1. Root', '_1.1', '__1.1.1', '__1.1.2', '_1.2', '__1.2.1', '__1.2.2');
  257. $this->assertSame($expected, array_values($result));
  258. }
  259. }