TreeHelperTest.php 9.6 KB

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