TreeHelperTest.php 9.2 KB

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