TreeHelperTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. namespace Tools\TestCase\View\Helper;
  3. use Tools\View\Helper\TreeHelper;
  4. use Cake\TestSuite\TestCase;
  5. use Cake\View\View;
  6. use Cake\ORM\Entity;
  7. use Cake\ORM\Table;
  8. use Cake\ORM\TableRegistry;
  9. use Cake\Datasource\ConnectionManager;
  10. use Cake\Core\Configure;
  11. class TreeHelperTest extends TestCase {
  12. public $fixtures = array('plugin.tools.after_trees');
  13. public $Table;
  14. /**
  15. * Initial Tree
  16. *
  17. * - One
  18. * -- One-SubA
  19. * - Two
  20. * -- Two-SubA
  21. * --- Two-SubA-1
  22. * ---- Two-SubA-1-1
  23. * - Three
  24. * - Four
  25. * -- Four-SubA
  26. *
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. Configure::write('debug', true);
  31. $this->Tree = new TreeHelper(new View(null));
  32. $this->Table = TableRegistry::get('AfterTrees');
  33. $this->Table->addBehavior('Tree');
  34. //$this->Table->truncate();
  35. $connection = ConnectionManager::get('test');
  36. $sql = $this->Table->schema()->truncateSql($connection);
  37. foreach ($sql as $snippet) {
  38. $connection->execute($snippet);
  39. }
  40. //$this->Table->deleteAll(array());
  41. $data = array(
  42. array('name' => 'One'),
  43. array('name' => 'Two'),
  44. array('name' => 'Three'),
  45. array('name' => 'Four'),
  46. array('name' => 'One-SubA', 'parent_id' => 1),
  47. array('name' => 'Two-SubA', 'parent_id' => 2),
  48. array('name' => 'Four-SubA', 'parent_id' => 4),
  49. array('name' => 'Two-SubA-1', 'parent_id' => 6),
  50. array('name' => 'Two-SubA-1-1', 'parent_id' => 8),
  51. );
  52. foreach ($data as $row) {
  53. $row = new Entity($row);
  54. $this->Table->save($row);
  55. }
  56. }
  57. public function tearDown() {
  58. unset($this->Table);
  59. TableRegistry::clear();
  60. parent::tearDown();
  61. }
  62. public function testObject() {
  63. $this->assertInstanceOf('Tools\View\Helper\TreeHelper', $this->Tree);
  64. }
  65. public function testGenerate() {
  66. $tree = $this->Table->find('threaded')->toArray();
  67. $output = $this->Tree->generate($tree);
  68. $expected = <<<TEXT
  69. <ul>
  70. <li>One
  71. <ul>
  72. <li>One-SubA</li>
  73. </ul>
  74. </li>
  75. <li>Two
  76. <ul>
  77. <li>Two-SubA
  78. <ul>
  79. <li>Two-SubA-1
  80. <ul>
  81. <li>Two-SubA-1-1</li>
  82. </ul>
  83. </li>
  84. </ul>
  85. </li>
  86. </ul>
  87. </li>
  88. <li>Three</li>
  89. <li>Four
  90. <ul>
  91. <li>Four-SubA</li>
  92. </ul>
  93. </li>
  94. </ul>
  95. TEXT;
  96. $this->assertTextEquals($expected, $output);
  97. $this->assertTrue(substr_count($output, '<ul>') === substr_count($output, '</ul>'));
  98. $this->assertTrue(substr_count($output, '<li>') === substr_count($output, '</li>'));
  99. }
  100. /**
  101. * TreeHelperTest::testGenerateWithFindAll()
  102. *
  103. * @return void
  104. */
  105. public function testGenerateWithFindAll() {
  106. $tree = $this->Table->find('all', array('order' => array('lft' => 'ASC')))->toArray();
  107. $output = $this->Tree->generate($tree);
  108. //debug($output); return;
  109. $expected = <<<TEXT
  110. <ul>
  111. <li>One
  112. <ul>
  113. <li>One-SubA</li>
  114. </ul>
  115. </li>
  116. <li>Two
  117. <ul>
  118. <li>Two-SubA
  119. <ul>
  120. <li>Two-SubA-1
  121. <ul>
  122. <li>Two-SubA-1-1</li>
  123. </ul>
  124. </li>
  125. </ul>
  126. </li>
  127. </ul>
  128. </li>
  129. <li>Three</li>
  130. <li>Four
  131. <ul>
  132. <li>Four-SubA</li>
  133. </ul>
  134. </li>
  135. </ul>
  136. TEXT;
  137. $output = str_replace(array("\t", "\r", "\n"), '', $output);
  138. $expected = str_replace(array("\t", "\r", "\n"), '', $expected);
  139. $this->assertTextEquals($expected, $output);
  140. }
  141. public function testGenerateWithDepth() {
  142. $tree = $this->Table->find('threaded')->toArray();
  143. $output = $this->Tree->generate($tree, array('depth' => 1));
  144. $expected = <<<TEXT
  145. <ul>
  146. <li>One
  147. <ul>
  148. <li>One-SubA</li>
  149. </ul>
  150. </li>
  151. <li>Two
  152. <ul>
  153. <li>Two-SubA
  154. <ul>
  155. <li>Two-SubA-1
  156. <ul>
  157. <li>Two-SubA-1-1</li>
  158. </ul>
  159. </li>
  160. </ul>
  161. </li>
  162. </ul>
  163. </li>
  164. <li>Three</li>
  165. <li>Four
  166. <ul>
  167. <li>Four-SubA</li>
  168. </ul>
  169. </li>
  170. </ul>
  171. TEXT;
  172. $this->assertTextEquals($expected, $output);
  173. }
  174. public function testGenerateWithSettings() {
  175. $tree = $this->Table->find('threaded')->toArray();
  176. $output = $this->Tree->generate($tree, array('class' => 'navi', 'id' => 'main', 'type' => 'ol'));
  177. $expected = <<<TEXT
  178. <ol class="navi" id="main">
  179. <li>One
  180. <ol>
  181. <li>One-SubA</li>
  182. </ol>
  183. </li>
  184. <li>Two
  185. <ol>
  186. <li>Two-SubA
  187. <ol>
  188. <li>Two-SubA-1
  189. <ol>
  190. <li>Two-SubA-1-1</li>
  191. </ol>
  192. </li>
  193. </ol>
  194. </li>
  195. </ol>
  196. </li>
  197. <li>Three</li>
  198. <li>Four
  199. <ol>
  200. <li>Four-SubA</li>
  201. </ol>
  202. </li>
  203. </ol>
  204. TEXT;
  205. $this->assertTextEquals($expected, $output);
  206. }
  207. public function testGenerateWithMaxDepth() {
  208. $tree = $this->Table->find('threaded')->toArray();
  209. $output = $this->Tree->generate($tree, array('maxDepth' => 2));
  210. $expected = <<<TEXT
  211. <ul>
  212. <li>One
  213. <ul>
  214. <li>One-SubA</li>
  215. </ul>
  216. </li>
  217. <li>Two
  218. <ul>
  219. <li>Two-SubA
  220. <ul>
  221. <li>Two-SubA-1</li>
  222. </ul>
  223. </li>
  224. </ul>
  225. </li>
  226. <li>Three</li>
  227. <li>Four
  228. <ul>
  229. <li>Four-SubA</li>
  230. </ul>
  231. </li>
  232. </ul>
  233. TEXT;
  234. $this->assertTextEquals($expected, $output);
  235. }
  236. public function testGenerateWithAutoPath() {
  237. $tree = $this->Table->find('threaded')->toArray();
  238. //debug($tree);
  239. $output = $this->Tree->generate($tree, array('autoPath' => array(7, 10))); // Two-SubA-1
  240. //debug($output);
  241. $expected = <<<TEXT
  242. <ul>
  243. <li>One
  244. <ul>
  245. <li>One-SubA</li>
  246. </ul>
  247. </li>
  248. <li class="active">Two
  249. <ul>
  250. <li class="active">Two-SubA
  251. <ul>
  252. <li class="active">Two-SubA-1
  253. <ul>
  254. <li>Two-SubA-1-1</li>
  255. </ul>
  256. </li>
  257. </ul>
  258. </li>
  259. </ul>
  260. </li>
  261. <li>Three</li>
  262. <li>Four
  263. <ul>
  264. <li>Four-SubA</li>
  265. </ul>
  266. </li>
  267. </ul>
  268. TEXT;
  269. $this->assertTextEquals($expected, $output);
  270. $output = $this->Tree->generate($tree, array('autoPath' => array(8, 9))); // Two-SubA-1-1
  271. //debug($output);
  272. $expected = <<<TEXT
  273. <ul>
  274. <li>One
  275. <ul>
  276. <li>One-SubA</li>
  277. </ul>
  278. </li>
  279. <li class="active">Two
  280. <ul>
  281. <li class="active">Two-SubA
  282. <ul>
  283. <li class="active">Two-SubA-1
  284. <ul>
  285. <li class="active">Two-SubA-1-1</li>
  286. </ul>
  287. </li>
  288. </ul>
  289. </li>
  290. </ul>
  291. </li>
  292. <li>Three</li>
  293. <li>Four
  294. <ul>
  295. <li>Four-SubA</li>
  296. </ul>
  297. </li>
  298. </ul>
  299. TEXT;
  300. $this->assertTextEquals($expected, $output);
  301. }
  302. /**
  303. *
  304. * - One
  305. * -- One-SubA
  306. * - Two
  307. * -- Two-SubA
  308. * --- Two-SubA-1
  309. * ---- Two-SubA-1-1
  310. * -- Two-SubB
  311. * -- Two-SubC
  312. * - Three
  313. * - Four
  314. * -- Four-SubA
  315. */
  316. public function testGenerateWithAutoPathAndHideUnrelated() {
  317. $this->skipIf(true, 'FIXME');
  318. $data = array(
  319. array('name' => 'Two-SubB', 'parent_id' => 2),
  320. array('name' => 'Two-SubC', 'parent_id' => 2),
  321. );
  322. foreach ($data as $row) {
  323. $row = new Entity($row);
  324. $this->Table->save($row);
  325. }
  326. $tree = $this->Table->find('threaded')->toArray();
  327. $id = 6;
  328. $nodes = $this->Table->find('path', array('for' => $id));
  329. $path = $nodes->extract('id')->toArray();
  330. $output = $this->Tree->generate($tree, array('autoPath' => array(6, 11), 'hideUnrelated' => true, 'treePath' => $path, 'callback' => array($this, '_myCallback'))); // Two-SubA
  331. //debug($output);
  332. $expected = <<<TEXT
  333. <ul>
  334. <li>One</li>
  335. <li class="active">Two (active)
  336. <ul>
  337. <li class="active">Two-SubA (active)
  338. <ul>
  339. <li>Two-SubA-1</li>
  340. </ul>
  341. </li>
  342. <li>Two-SubB</li>
  343. <li>Two-SubC</li>
  344. </ul>
  345. </li>
  346. <li>Three</li>
  347. <li>Four</li>
  348. </ul>
  349. TEXT;
  350. $output = str_replace(array("\t"), '', $output);
  351. $expected = str_replace(array("\t"), '', $expected);
  352. $this->assertTextEquals($expected, $output);
  353. }
  354. /**
  355. *
  356. * - One
  357. * -- One-SubA
  358. * - Two
  359. * -- Two-SubA
  360. * --- Two-SubA-1
  361. * ---- Two-SubA-1-1
  362. * -- Two-SubB
  363. * -- Two-SubC
  364. * - Three
  365. * - Four
  366. * -- Four-SubA
  367. */
  368. public function testGenerateWithAutoPathAndHideUnrelatedAndSiblings() {
  369. $this->skipIf(true, 'FIXME');
  370. $data = array(
  371. array('name' => 'Two-SubB', 'parent_id' => 2),
  372. array('name' => 'Two-SubC', 'parent_id' => 2),
  373. );
  374. foreach ($data as $row) {
  375. $row = new Entity($row);
  376. $this->Table->save($row);
  377. }
  378. $tree = $this->Table->find('threaded')->toArray();
  379. $id = 6;
  380. $nodes = $this->Table->find('path', array('for' => $id));
  381. $path = $nodes->extract('id')->toArray();
  382. $output = $this->Tree->generate($tree, array(
  383. 'autoPath' => array(6, 11), 'hideUnrelated' => true, 'treePath' => $path,
  384. 'callback' => array($this, '_myCallbackSiblings'))); // Two-SubA
  385. //debug($output);
  386. $expected = <<<TEXT
  387. <ul>
  388. <li>One (sibling)</li>
  389. <li class="active">Two (active)
  390. <ul>
  391. <li class="active">Two-SubA (active)
  392. <ul>
  393. <li>Two-SubA-1</li>
  394. </ul>
  395. </li>
  396. <li>Two-SubB</li>
  397. <li>Two-SubC</li>
  398. </ul>
  399. </li>
  400. <li>Three (sibling)</li>
  401. <li>Four (sibling)</li>
  402. </ul>
  403. TEXT;
  404. $output = str_replace(array("\t", "\r", "\n"), '', $output);
  405. $expected = str_replace(array("\t", "\r", "\n"), '', $expected);
  406. //debug($output);
  407. //debug($expected);
  408. $this->assertTextEquals($expected, $output);
  409. }
  410. public function _myCallback($data) {
  411. if (!empty($data['data']['hide'])) {
  412. return;
  413. }
  414. return $data['data']['name'] . ($data['activePathElement'] ? ' (active)' : '');
  415. }
  416. public function _myCallbackSiblings($data) {
  417. if (!empty($data['data']['hide'])) {
  418. return;
  419. }
  420. if ($data['depth'] == 0 && $data['isSibling']) {
  421. return $data['data']['name'] . ' (sibling)';
  422. }
  423. return $data['data']['name'] . ($data['activePathElement'] ? ' (active)' : '');
  424. }
  425. /**
  426. * TreeHelperTest::testGenerateProductive()
  427. *
  428. * @return void
  429. */
  430. public function testGenerateProductive() {
  431. $tree = $this->Table->find('threaded')->toArray();
  432. $output = $this->Tree->generate($tree, array('indent' => false));
  433. $expected = '<ul><li>One<ul><li>One-SubA</li></ul></li><li>Two<ul><li>Two-SubA<ul><li>Two-SubA-1<ul><li>Two-SubA-1-1</li></ul></li></ul></li></ul></li><li>Three</li><li>Four<ul><li>Four-SubA</li></ul></li></ul>';
  434. $this->assertTextEquals($expected, $output);
  435. }
  436. }