PageRenderTime 44ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/Core/Libs/Test/Case/Model/Behavior/InfiniTreeBehaviorTest.php

http://github.com/infinitas/infinitas
PHP | 578 lines | 424 code | 85 blank | 69 comment | 1 complexity | 38ee053a04cbfbbfaef47f61ffaba3ea MD5 | raw file
  1. <?php
  2. if (!class_exists('ScopedNumberTree')) {
  3. class ScopedNumberTree extends CakeTestModel {
  4. public $actsAs = array('Libs.InfiniTree' => array('scopeField' => 'category_id'));
  5. }
  6. }
  7. class ScopedCounterNumberTree extends CakeTestModel {
  8. public $actsAs = array('Libs.InfiniTree' => array('scopeField' => 'category_id', 'counterCache' => true, 'directCounterCache' => true));
  9. }
  10. class ScopedNumberTreeTest extends CakeTestCase {
  11. public $fixtures = array(
  12. 'plugin.libs.scoped_counter_number_tree',
  13. 'plugin.libs.scoped_number_tree'
  14. );
  15. public function setUp() {
  16. parent::setUp();
  17. $this->ScopedNumberTree = new ScopedNumberTree();
  18. $this->ScopedCounterNumberTree = new ScopedCounterNumberTree();
  19. }
  20. public function tearDown() {
  21. parent::tearDown();
  22. unset($this->ScopedNumberTree, $this->ScopedCounterNumberTree);
  23. }
  24. public function testFixtureIntegrity() {
  25. $validTree = $this->ScopedNumberTree->verify('cat-a');
  26. $this->assertIdentical($validTree, true);
  27. $validTree = $this->ScopedNumberTree->verify('cat-b');
  28. $this->assertIdentical($validTree, true);
  29. }
  30. public function testRecoverFromParent() {
  31. $this->ScopedNumberTree->Behaviors->disable('InfiniTree');
  32. $this->ScopedNumberTree->deleteAll(array('ScopedNumberTree.id !=' => 0));
  33. $data = array(
  34. array(
  35. 'id' => '1',
  36. 'name' => '1',
  37. 'category_id' => 'cat-1',
  38. 'parent_id' => null
  39. ),
  40. array(
  41. 'id' => '1-1',
  42. 'name' => '1-1',
  43. 'category_id' => 'cat-1',
  44. 'parent_id' => '1'
  45. ),
  46. array(
  47. 'id' => '1-1-1',
  48. 'name' => '1-1-1',
  49. 'category_id' => 'cat-1',
  50. 'parent_id' => '1-1'
  51. )
  52. );
  53. $this->ScopedNumberTree->create();
  54. $this->assertTrue((bool)$this->ScopedNumberTree->saveAll($data));
  55. $results = $this->ScopedNumberTree->find('all');
  56. $parent = array_unique(Set::extract($results, '/ScopedNumberTree/parent_id'));
  57. $lft = current(array_unique(Set::extract($results, '/ScopedNumberTree/lft')));
  58. $rght = current(array_unique(Set::extract($results, '/ScopedNumberTree/rght')));
  59. $this->assertEquals(array(null, '1', '1-1'), $parent);
  60. $this->assertEquals(0, $lft);
  61. $this->assertEquals(0, $rght);
  62. $this->ScopedNumberTree->Behaviors->enable('InfiniTree');
  63. $this->assertTrue($this->ScopedNumberTree->recover('parent', null, 'cat-1'));
  64. $results = $this->ScopedNumberTree->find('all');
  65. $parent = array_unique(Set::extract($results, '/ScopedNumberTree/parent_id'));
  66. $lft = array_unique(Set::extract($results, '/ScopedNumberTree/lft'));
  67. $rght = array_unique(Set::extract($results, '/ScopedNumberTree/rght'));
  68. $this->assertEquals(array(null, '1', '1-1'), $parent);
  69. $this->assertEquals(array(1, 2, 3), $lft);
  70. $this->assertEquals(array(6, 5, 4), $rght);
  71. }
  72. public function testTreeSave() {
  73. //Test invalid values
  74. $this->assertFalse($this->ScopedNumberTree->treeSave());
  75. $this->assertFalse($this->ScopedNumberTree->treeSave(false));
  76. $this->assertFalse($this->ScopedNumberTree->treeSave(array()));
  77. $this->assertFalse($this->ScopedNumberTree->treeSave(array('foo' => 'bar')));
  78. $this->assertFalse($this->ScopedNumberTree->treeSave(array('data' => 'field'), array('foo' => 'bar')));
  79. //make sure the treeSave doesnt affect behavior of children()
  80. $numChildrenCatA = count($this->ScopedNumberTree->children(array('id' => null, 'scope' => 'cat-a')));
  81. $data = array('ScopedNumberTree' => array(
  82. array(
  83. 'name' => 'United Kingdom',
  84. 'ScopedNumberTree' => array(
  85. array('name' => 'Sales'),
  86. array('name' => 'Marketing'),
  87. array('name' => 'R&D')
  88. )
  89. ),
  90. array(
  91. 'name' => 'Belgium',
  92. 'ScopedNumberTree' => array(
  93. array('name' => 'Sales'),
  94. array('name' => 'Marketing'),
  95. array('name' => 'R&D')
  96. )
  97. )
  98. ));
  99. $expected = array('United Kingdom', '_Sales', '_Marketing', '_R&D', 'Belgium', '_Sales', '_Marketing', '_R&D');
  100. $this->assertTrue($this->ScopedNumberTree->treeSave($data, array('scope' => 'cat-new')));
  101. $this->assertEqual($expected, array_values($this->ScopedNumberTree->generateTreeList(array('ScopedNumberTree.category_id' => 'cat-new'))));
  102. //This shouldnt have changed
  103. $this->assertEqual($numChildrenCatA, count($this->ScopedNumberTree->children(array('id' => false, 'scope' => 'cat-a'))));
  104. //Check if the trees got corrupt
  105. $validTree = $this->ScopedNumberTree->verify('cat-a');
  106. $this->assertIdentical($validTree, true);
  107. $validTree = $this->ScopedNumberTree->verify('cat-b');
  108. $this->assertIdentical($validTree, true);
  109. $validTree = $this->ScopedNumberTree->verify('cat-new');
  110. $this->assertIdentical($validTree, true);
  111. }
  112. public function testAddingNodes() {
  113. /**
  114. * Root nodes
  115. */
  116. //Try adding a root node without specifying the scope field
  117. //should fail because we cant get the scope
  118. $data = array(
  119. 'name' => '3 Root',
  120. 'parent_id' => null
  121. );
  122. $this->ScopedNumberTree->create();
  123. $this->assertFalse($this->ScopedNumberTree->save($data));
  124. //should work with the scope
  125. $data = array(
  126. 'name' => '3 Root',
  127. 'category_id' => 'cat-a',
  128. 'parent_id' => null
  129. );
  130. $this->ScopedNumberTree->create();
  131. $this->assertTrue((bool)$this->ScopedNumberTree->save($data));
  132. //With the scope variable
  133. $data = array(
  134. 'name' => '1.3.1 node',
  135. 'parent_id' => 'cat-a-1-3',
  136. 'category_id' => 'cat-a'
  137. );
  138. $this->ScopedNumberTree->create();
  139. $this->assertTrue((bool)$this->ScopedNumberTree->save($data));
  140. //Without the scope variable
  141. $data = array(
  142. 'name' => '1.3.2 node',
  143. 'parent_id' => 'cat-a-1-3'
  144. );
  145. $this->ScopedNumberTree->create();
  146. $this->assertTrue((bool)$this->ScopedNumberTree->save($data));
  147. //Should be able to modify the name of a node without breaking stuff
  148. $data = array(
  149. 'name' => '1.2 Node (edited)'
  150. );
  151. $this->ScopedNumberTree->id = 'cat-a-1-2';
  152. $this->assertTrue((bool)$this->ScopedNumberTree->save($data));
  153. //Move 1.2.3 into 1.1 (becomes 1.1.1)
  154. $data = array(
  155. 'name' => '1.1.1 Node (edited)',
  156. 'parent_id' => 'cat-a-1-1'
  157. );
  158. $this->ScopedNumberTree->id = 'cat-a-1-2-3';
  159. $this->assertTrue((bool)$this->ScopedNumberTree->save($data));
  160. //Check the structure after all the changes
  161. $expected = array('1 Root', '_1.1 Node', '__1.1.1 Node (edited)', '_1.2 Node (edited)', '__1.2.1 Node', '__1.2.2 Node', '_1.3 Node', '__1.3.1 node', '__1.3.2 node', '2 Root', '3 Root');
  162. $this->assertEquals($expected, array_values($this->ScopedNumberTree->generateTreeList(array('ScopedNumberTree.category_id' => 'cat-a'))));
  163. //Verify trees after saving a bunch of new nodes
  164. $validTree = $this->ScopedNumberTree->verify('cat-a');
  165. $this->assertIdentical($validTree, true);
  166. $validTree = $this->ScopedNumberTree->verify('cat-b');
  167. $this->assertIdentical($validTree, true);
  168. }
  169. public function testDeletingNodes() {
  170. //Try to delete a leaf node
  171. $this->ScopedNumberTree->id = 'cat-a-1-2-1';
  172. $this->assertTrue($this->ScopedNumberTree->delete());
  173. $expected = array('1 Root', '_1.1 Node', '_1.2 Node', '__1.2.2 Node', '__1.2.3 Node', '_1.3 Node', '2 Root');
  174. $this->assertEqual($expected, array_values($this->ScopedNumberTree->generateTreeList(array('ScopedNumberTree.category_id' => 'cat-a'))));
  175. //Try to delete a node with children
  176. $this->ScopedNumberTree->id = 'cat-a-1-2';
  177. $this->assertTrue($this->ScopedNumberTree->delete());
  178. $expected = array('1 Root', '_1.1 Node', '_1.3 Node', '2 Root');
  179. $this->assertEqual($expected, array_values($this->ScopedNumberTree->generateTreeList(array('ScopedNumberTree.category_id' => 'cat-a'))));
  180. //Verify trees after deleting a bunch of new nodes
  181. $validTree = $this->ScopedNumberTree->verify('cat-a');
  182. $this->assertIdentical($validTree, true);
  183. $validTree = $this->ScopedNumberTree->verify('cat-b');
  184. $this->assertIdentical($validTree, true);
  185. }
  186. public function testMovingNodes() {
  187. //Check invalid id's
  188. $this->ScopedNumberTree->id = false;
  189. $this->assertFalse($this->ScopedNumberTree->movedown());
  190. $this->assertFalse($this->ScopedNumberTree->movedown(false));
  191. $this->assertFalse($this->ScopedNumberTree->moveup());
  192. $this->assertFalse($this->ScopedNumberTree->moveup(false));
  193. $this->assertFalse($this->ScopedNumberTree->removefromtree());
  194. $this->assertFalse($this->ScopedNumberTree->removefromtree(false));
  195. //Nodes are the last in the lists
  196. $this->assertFalse($this->ScopedNumberTree->movedown('cat-a-2', 1));
  197. $this->assertFalse($this->ScopedNumberTree->movedown('cat-a-1-2-3', 1));
  198. //Move node down
  199. $this->assertTrue($this->ScopedNumberTree->movedown('cat-a-1-2', 1));
  200. $expected = array('1 Root', '_1.1 Node', '_1.3 Node', '_1.2 Node', '__1.2.1 Node', '__1.2.2 Node', '__1.2.3 Node', '2 Root');
  201. $this->assertEqual($expected, array_values($this->ScopedNumberTree->generateTreeList(array('ScopedNumberTree.category_id' => 'cat-a'))));
  202. //Move it back up
  203. $this->assertTrue($this->ScopedNumberTree->moveup('cat-a-1-2', 1));
  204. $expected = array('1 Root', '_1.1 Node', '_1.2 Node', '__1.2.1 Node', '__1.2.2 Node', '__1.2.3 Node', '_1.3 Node', '2 Root');
  205. $this->assertEqual($expected, array_values($this->ScopedNumberTree->generateTreeList(array('ScopedNumberTree.category_id' => 'cat-a'))));
  206. //Remove a node to the root level
  207. $this->assertTrue((bool)$this->ScopedNumberTree->removefromtree('cat-a-1-2-3'));
  208. $expected = array('1 Root', '_1.1 Node', '_1.2 Node', '__1.2.1 Node', '__1.2.2 Node', '_1.3 Node', '2 Root', '1.2.3 Node');
  209. $this->assertEqual($expected, array_values($this->ScopedNumberTree->generateTreeList(array('ScopedNumberTree.category_id' => 'cat-a'))));
  210. //Remove a node completely
  211. $this->assertTrue($this->ScopedNumberTree->removefromtree('cat-a-1-2-2', true));
  212. $expected = array('1 Root', '_1.1 Node', '_1.2 Node', '__1.2.1 Node', '_1.3 Node', '2 Root', '1.2.3 Node');
  213. $this->assertEqual($expected, array_values($this->ScopedNumberTree->generateTreeList(array('ScopedNumberTree.category_id' => 'cat-a'))));
  214. //Verify trees after deleting a bunch of new nodes
  215. $validTree = $this->ScopedNumberTree->verify('cat-a');
  216. $this->assertIdentical($validTree, true);
  217. $validTree = $this->ScopedNumberTree->verify('cat-b');
  218. $this->assertIdentical($validTree, true);
  219. }
  220. public function testFinds() {
  221. /**
  222. * childcount()
  223. */
  224. $this->assertFalse($this->ScopedNumberTree->childcount());
  225. //Count all children from a node
  226. $this->assertEqual(6, $this->ScopedNumberTree->childcount(array('id' => 'cat-a-1')));
  227. //Count direct children from a node
  228. $this->assertEqual(3, $this->ScopedNumberTree->childcount('cat-a-1', true));
  229. //Count all children in a scope
  230. $this->ScopedNumberTree->set('id', false);
  231. $this->assertEqual(8, $this->ScopedNumberTree->childcount(array('scope' => 'cat-a')));
  232. //Count all root nodes in a scope
  233. $this->assertEqual(2, $this->ScopedNumberTree->childcount(array('scope' => 'cat-a'), true));
  234. /**
  235. * children()
  236. */
  237. $this->assertFalse($this->ScopedNumberTree->children());
  238. //Get all the children from one scope
  239. $expected = array('cat-a-1', 'cat-a-1-1', 'cat-a-1-2', 'cat-a-1-2-1', 'cat-a-1-2-2', 'cat-a-1-2-3', 'cat-a-1-3', 'cat-a-2');
  240. $children = $this->ScopedNumberTree->children(array('scope' => 'cat-a'));
  241. $this->assertEqual($expected, Set::extract('/ScopedNumberTree/id', $children));
  242. //Get all the children under a specific node
  243. $expected = array('cat-a-1-2-1', 'cat-a-1-2-2', 'cat-a-1-2-3');
  244. $children = $this->ScopedNumberTree->children('cat-a-1-2');
  245. $this->assertEqual($expected, Set::extract('/ScopedNumberTree/id', $children));
  246. /**
  247. * parentNode();
  248. */
  249. $this->assertEqual('cat-a-1-2', current(current($this->ScopedNumberTree->getParentNode('cat-a-1-2-1', 'id'))));
  250. $this->ScopedNumberTree->id = 'cat-a-1-2-2';
  251. $this->assertEqual('cat-a-1-2', current(current($this->ScopedNumberTree->getParentNode(null, 'id'))));
  252. /**
  253. * getPath();
  254. */
  255. $this->ScopedNumberTree->id = false;
  256. $this->assertFalse($this->ScopedNumberTree->getPath());
  257. $this->assertFalse($this->ScopedNumberTree->getPath(false));
  258. $expected = array('cat-b-3', 'cat-b-3-2', 'cat-b-3-2-3');
  259. $this->assertEqual($expected, Set::extract('/ScopedNumberTree/id', $this->ScopedNumberTree->getPath('cat-b-3-2-3')));
  260. //Verify trees after doing lookups
  261. $validTree = $this->ScopedNumberTree->verify('cat-a');
  262. $this->assertIdentical($validTree, true);
  263. $validTree = $this->ScopedNumberTree->verify('cat-b');
  264. $this->assertIdentical($validTree, true);
  265. }
  266. public function testOther() {
  267. //Scope param is required to reorder a scoped tree
  268. $this->assertFalse($this->ScopedNumberTree->reorder());
  269. $tree = array('ScopedNumberTree' => array(
  270. array('name' => 'A'),
  271. array('name' => 'D'),
  272. array('name' => 'C'),
  273. array('name' => 'B', 'ScopedNumberTree' => array(
  274. array('name' => 'BC', 'ScopedNumberTree' => array(
  275. array('name' => 'BCB'),
  276. array('name' => 'BCA')
  277. )),
  278. array('name' => 'BA'),
  279. array('name' => 'BB')
  280. ))
  281. ));
  282. $expected = array('A', 'D', 'C', 'B', '_BC', '__BCB', '__BCA', '_BA', '_BB');
  283. $this->assertTrue($this->ScopedNumberTree->treeSave($tree, array('scope' => 'cat-letters')));
  284. $this->assertEqual($expected, array_values($this->ScopedNumberTree->generateTreeList(array('ScopedNumberTree.category_id' => 'cat-letters'))));
  285. //Reorder on name
  286. $expected = array('A', 'B', '_BA', '_BB', '_BC', '__BCA', '__BCB', 'C', 'D');
  287. $this->assertTrue($this->ScopedNumberTree->reorder(array('scope' => 'cat-letters', 'field' => 'name', 'id' => array('id' => false, 'scope' => 'cat-letters'))));
  288. $this->assertEqual($expected, array_values($this->ScopedNumberTree->generateTreeList(array('ScopedNumberTree.category_id' => 'cat-letters'))));
  289. //Verify trees after doing lookups
  290. $validTree = $this->ScopedNumberTree->verify('cat-a');
  291. $this->assertIdentical($validTree, true);
  292. $validTree = $this->ScopedNumberTree->verify('cat-b');
  293. $this->assertIdentical($validTree, true);
  294. $validTree = $this->ScopedNumberTree->verify('cat-letters');
  295. $this->assertIdentical($validTree, true);
  296. }
  297. function testCounterCache() {
  298. $this->ScopedCounterNumberTree = new ScopedCounterNumberTree();
  299. $tree = array('ScopedCounterNumberTree' => array(
  300. array('name' => 'Root A', 'ScopedCounterNumberTree' => array(
  301. array('name' => 'Category A - 1'),
  302. array('name' => 'Category A - 2')
  303. )),
  304. array('name' => 'Root B'),
  305. array('name' => 'Root C', 'ScopedCounterNumberTree' => array(
  306. array('name' => 'Category C - 1'),
  307. array('name' => 'Category C - 2', 'ScopedCounterNumberTree' => array(
  308. array('name' => 'Category C - 2 - 1'),
  309. array('name' => 'Category C - 2 - 2')
  310. )),
  311. array('name' => 'Category C - 3', 'ScopedCounterNumberTree' => array(
  312. array('name' => 'Category C - 3 - 1')
  313. ))
  314. ))
  315. ));
  316. $expected = array(
  317. 'Root A',
  318. '_Category A - 1',
  319. '_Category A - 2',
  320. 'Root B',
  321. 'Root C',
  322. '_Category C - 1',
  323. '_Category C - 2',
  324. '__Category C - 2 - 1',
  325. '__Category C - 2 - 2',
  326. '_Category C - 3',
  327. '__Category C - 3 - 1'
  328. );
  329. $this->assertTrue($this->ScopedCounterNumberTree->treeSave($tree, array('scope' => 'test-cat')));
  330. $this->assertEqual($expected, array_values($this->ScopedCounterNumberTree->generateTreeList(array('ScopedCounterNumberTree.category_id' => 'test-cat'))));
  331. $children = $this->ScopedCounterNumberTree->children(array('id' => false, 'scope' => 'test-cat'), false, array('id', 'name', 'children_count', 'direct_children_count'));
  332. $childrenIds = Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/id');
  333. //Check children counts
  334. $expected = array(
  335. 'Root A' => 2,
  336. 'Category A - 1' => 0,
  337. 'Category A - 2' => 0,
  338. 'Root B' => 0,
  339. 'Root C' => 6,
  340. 'Category C - 1' => 0,
  341. 'Category C - 2' => 2,
  342. 'Category C - 2 - 1' => 0,
  343. 'Category C - 2 - 2' => 0,
  344. 'Category C - 3' => 1,
  345. 'Category C - 3 - 1' => 0
  346. );
  347. $this->assertEqual($expected, Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/children_count'));
  348. //Check direct children counts
  349. $expected = array(
  350. 'Root A' => 2,
  351. 'Category A - 1' => 0,
  352. 'Category A - 2' => 0,
  353. 'Root B' => 0,
  354. 'Root C' => 3,
  355. 'Category C - 1' => 0,
  356. 'Category C - 2' => 2,
  357. 'Category C - 2 - 1' => 0,
  358. 'Category C - 2 - 2' => 0,
  359. 'Category C - 3' => 1,
  360. 'Category C - 3 - 1' => 0
  361. );
  362. $this->assertEqual($expected, Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/direct_children_count'));
  363. $validTree = $this->ScopedCounterNumberTree->verify('test-cat');
  364. $this->assertIdentical($validTree, true);
  365. /**
  366. * Adding nodes
  367. */
  368. $data = array('name' => 'Root D', 'category_id' => 'test-cat');
  369. $this->ScopedCounterNumberTree->create();
  370. $this->assertTrue((bool)$this->ScopedCounterNumberTree->save($data));
  371. $data = array('name' => 'Category B - 1', 'parent_id' => $childrenIds['Root B']);
  372. $this->ScopedCounterNumberTree->create();
  373. $this->assertTrue((bool)$this->ScopedCounterNumberTree->save($data));
  374. $data = array('name' => 'Category C - 2 - 2 - 1', 'parent_id' => $childrenIds['Category C - 2 - 2']);;
  375. $this->ScopedCounterNumberTree->create();
  376. $this->assertTrue((bool)$this->ScopedCounterNumberTree->save($data));
  377. $validTree = $this->ScopedCounterNumberTree->verify('test-cat');
  378. $this->assertIdentical($validTree, true);
  379. $children = $this->ScopedCounterNumberTree->children(array('id' => false, 'scope' => 'test-cat'), false, array('id', 'name', 'children_count', 'direct_children_count'));
  380. $childrenIds = Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/id');
  381. //Check children counts
  382. $expected = array(
  383. 'Root A' => 2,
  384. 'Category A - 1' => 0,
  385. 'Category A - 2' => 0,
  386. 'Root B' => 1,
  387. 'Category B - 1' => 0,
  388. 'Root C' => 7,
  389. 'Category C - 1' => 0,
  390. 'Category C - 2' => 3,
  391. 'Category C - 2 - 1' => 0,
  392. 'Category C - 2 - 2' => 1,
  393. 'Category C - 2 - 2 - 1' => 0,
  394. 'Category C - 3' => 1,
  395. 'Category C - 3 - 1' => 0,
  396. 'Root D' => 0
  397. );
  398. $this->assertEqual($expected, Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/children_count'));
  399. //Check direct children counts
  400. $expected = array(
  401. 'Root A' => 2,
  402. 'Category A - 1' => 0,
  403. 'Category A - 2' => 0,
  404. 'Root B' => 1,
  405. 'Category B - 1' => 0,
  406. 'Root C' => 3,
  407. 'Category C - 1' => 0,
  408. 'Category C - 2' => 2,
  409. 'Category C - 2 - 1' => 0,
  410. 'Category C - 2 - 2' => 1,
  411. 'Category C - 2 - 2 - 1' => 0,
  412. 'Category C - 3' => 1,
  413. 'Category C - 3 - 1' => 0,
  414. 'Root D' => 0
  415. );
  416. $this->assertEqual($expected, Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/direct_children_count'));
  417. /**
  418. * Deleting nodes
  419. */
  420. //Delete a leaf
  421. $this->assertTrue($this->ScopedCounterNumberTree->delete($childrenIds['Category A - 2']));
  422. //Delete a root
  423. $this->assertTrue($this->ScopedCounterNumberTree->delete($childrenIds['Root D']));
  424. //Delete an entire subtree
  425. $this->assertTrue($this->ScopedCounterNumberTree->delete($childrenIds['Category C - 2']));
  426. $children = $this->ScopedCounterNumberTree->children(array('id' => false, 'scope' => 'test-cat'), false, array('id', 'name', 'children_count', 'direct_children_count', 'lft', 'rght', 'parent_id'));
  427. //Check children counts
  428. $expected = array(
  429. 'Root A' => '1',
  430. 'Category A - 1' => '0',
  431. 'Root B' => '1',
  432. 'Category B - 1' => '0',
  433. 'Root C' => '6',
  434. 'Category C - 1' => '0',
  435. 'Category C - 3' => '1',
  436. 'Category C - 3 - 1' => '0'
  437. );
  438. $result = Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/children_count');
  439. $this->assertEquals($expected, $result);
  440. //Check direct children counts
  441. $expected = array(
  442. 'Root A' => '1',
  443. 'Category A - 1' => '0',
  444. 'Root B' => '1',
  445. 'Category B - 1' => '0',
  446. 'Root C' => '2',
  447. 'Category C - 1' => '0',
  448. 'Category C - 3' => '1',
  449. 'Category C - 3 - 1' => '0'
  450. );
  451. $result = Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/direct_children_count');
  452. $this->assertEquals($expected, $result);
  453. /**
  454. * Moving nodes around
  455. */
  456. $childrenIds = Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/id');
  457. //Move C - 3 into Root B
  458. $this->ScopedCounterNumberTree->id = $childrenIds['Category C - 3'];
  459. $this->assertTrue((bool)$this->ScopedCounterNumberTree->saveField('parent_id', $childrenIds['Root B']));
  460. $children = $this->ScopedCounterNumberTree->children(
  461. array('id' => false, 'scope' => 'test-cat'),
  462. false,
  463. array('id', 'name', 'children_count', 'direct_children_count', 'lft', 'rght')
  464. );
  465. $childrenIds = Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/id');
  466. //Check children counts
  467. $expected = array(
  468. 'Root A' => 1,
  469. 'Category A - 1' => 0,
  470. 'Root B' => 3,
  471. 'Category B - 1' => 0,
  472. 'Category C - 3' => 1,
  473. 'Category C - 3 - 1' => 0,
  474. 'Root C' => 1,
  475. 'Category C - 1' => 0
  476. );
  477. $this->assertEqual($expected, Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/children_count'));
  478. //Check direct children counts
  479. $expected = array(
  480. 'Root A' => 1,
  481. 'Category A - 1' => 0,
  482. 'Root B' => 2,
  483. 'Category B - 1' => 0,
  484. 'Category C - 3' => 1,
  485. 'Category C - 3 - 1' => 0,
  486. 'Root C' => 1,
  487. 'Category C - 1' => 0
  488. );
  489. $this->assertEqual($expected, Set::combine($children, '/ScopedCounterNumberTree/name', '/ScopedCounterNumberTree/direct_children_count'));
  490. $validTree = $this->ScopedNumberTree->verify('test-cat');
  491. $this->assertIdentical($validTree, true);
  492. }
  493. }