FormatHelperTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\Core\Configure;
  4. use Cake\View\View;
  5. use Shim\TestSuite\TestCase;
  6. use Tools\Utility\Text;
  7. use Tools\View\Helper\FormatHelper;
  8. /**
  9. * Datetime Test Case
  10. */
  11. class FormatHelperTest extends TestCase {
  12. /**
  13. * @var array<string>
  14. */
  15. protected $fixtures = [
  16. 'core.Sessions',
  17. ];
  18. /**
  19. * @var \Tools\View\Helper\FormatHelper
  20. */
  21. protected $Format;
  22. /**
  23. * @return void
  24. */
  25. public function setUp(): void {
  26. parent::setUp();
  27. $this->loadRoutes();
  28. Configure::write('App.imageBaseUrl', 'img/');
  29. $this->Format = new FormatHelper(new View(null));
  30. }
  31. /**
  32. * @return void
  33. */
  34. public function testDisabledLink() {
  35. $content = 'xyz';
  36. $data = [
  37. [],
  38. ['class' => 'disabledLink', 'title' => false],
  39. ['class' => 'helloClass', 'title' => 'helloTitle'],
  40. ];
  41. foreach ($data as $key => $value) {
  42. $res = $this->Format->disabledLink($content, $value);
  43. //echo ''.$res.' (\''.h($res).'\')';
  44. $this->assertTrue(!empty($res));
  45. }
  46. }
  47. /**
  48. * @return void
  49. */
  50. public function testWarning() {
  51. $content = 'xyz';
  52. $data = [
  53. true,
  54. false,
  55. ];
  56. foreach ($data as $key => $value) {
  57. $res = $this->Format->warning($content . ' ' . (int)$value, $value);
  58. //echo ''.$res.'';
  59. $this->assertTrue(!empty($res));
  60. }
  61. }
  62. /**
  63. * FormatHelperTest::testIcon()
  64. *
  65. * @return void
  66. */
  67. public function testIcon() {
  68. $result = $this->Format->icon('edit');
  69. $expected = '<i class="icon icon-edit fa fa-pen" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  70. $this->assertSame($expected, $result);
  71. }
  72. /**
  73. * FormatHelperTest::testIconWithFontIcon()
  74. *
  75. * @return void
  76. */
  77. public function testIconWithCustomAttributes() {
  78. $result = $this->Format->icon('edit', [], ['data-x' => 'y']);
  79. $expected = '<i class="icon icon-edit fa fa-pen" data-x="y" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  80. $this->assertSame($expected, $result);
  81. }
  82. /**
  83. * @return void
  84. */
  85. public function testIconWithCustomClassAttributes() {
  86. $result = $this->Format->icon('edit', [], ['class' => 'my-extra']);
  87. $expected = '<i class="icon icon-edit fa fa-pen my-extra" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  88. $this->assertSame($expected, $result);
  89. }
  90. /**
  91. * @return void
  92. */
  93. public function testIconWithCustomFontIcon() {
  94. $this->Format->setConfig('fontIcons', ['edit' => 'fax fax-pen']);
  95. $result = $this->Format->icon('edit');
  96. $expected = '<i class="icon icon-edit fax fax-pen" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  97. $this->assertSame($expected, $result);
  98. }
  99. /**
  100. * @return void
  101. */
  102. public function testIconWithPrefixedIcon() {
  103. $this->Format->setConfig('iconNamespaces', ['fa', 'glyphicon']);
  104. $result = $this->Format->icon('glyphicon-foo');
  105. $expected = '<i class="icon icon-glyphicon-foo glyphicon glyphicon-foo" title="' . __d('tools', 'Foo') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  106. $this->assertSame($expected, $result);
  107. $result = $this->Format->icon('glyphicon-edit');
  108. $expected = '<i class="icon icon-glyphicon-edit glyphicon glyphicon-edit" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  109. $this->assertSame($expected, $result);
  110. }
  111. /**
  112. * @return void
  113. */
  114. public function testFontIcon() {
  115. $result = $this->Format->fontIcon('signin');
  116. $expected = '<i class="fa fa-signin"></i>';
  117. $this->assertEquals($expected, $result);
  118. $result = $this->Format->fontIcon('signin', ['rotate' => 90]);
  119. $expected = '<i class="fa fa-signin fa-rotate-90"></i>';
  120. $this->assertEquals($expected, $result);
  121. $result = $this->Format->fontIcon('signin', ['size' => 5, 'extra' => ['muted']]);
  122. $expected = '<i class="fa fa-signin fa-muted fa-5x"></i>';
  123. $this->assertEquals($expected, $result);
  124. $result = $this->Format->fontIcon('signin', ['size' => 5, 'extra' => ['muted'], 'namespace' => 'myicon']);
  125. $expected = '<i class="myicon myicon-signin myicon-muted myicon-5x"></i>';
  126. $this->assertEquals($expected, $result);
  127. }
  128. /**
  129. * @return void
  130. */
  131. public function testYesNo() {
  132. $result = $this->Format->yesNo(true);
  133. $expected = '<i class="icon icon-yes fa fa-check" title="' . __d('tools', 'Yes') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  134. $this->assertEquals($expected, $result);
  135. $result = $this->Format->yesNo(false);
  136. $expected = '<i class="icon icon-no fa fa-times" title="' . __d('tools', 'No') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  137. $this->assertEquals($expected, $result);
  138. $result = $this->Format->yesNo('2', ['on' => 2, 'onTitle' => 'foo']);
  139. $expected = '<i class="icon icon-yes fa fa-check" title="foo" data-placement="bottom" data-toggle="tooltip"></i>';
  140. $this->assertTextContains($expected, $result);
  141. $result = $this->Format->yesNo('3', ['on' => 4, 'offTitle' => 'nope']);
  142. $expected = '<i class="icon icon-no fa fa-times" title="nope" data-placement="bottom" data-toggle="tooltip"></i>';
  143. $this->assertEquals($expected, $result);
  144. }
  145. /**
  146. * @return void
  147. */
  148. public function testOk() {
  149. $content = 'xyz';
  150. $data = [
  151. true => '<span class="ok-yes" style="color:green">xyz 1</span>',
  152. false => '<span class="ok-no" style="color:red">xyz 0</span>',
  153. ];
  154. foreach ($data as $value => $expected) {
  155. $result = $this->Format->ok($content . ' ' . (int)$value, $value);
  156. $this->assertEquals($expected, $result);
  157. }
  158. }
  159. /**
  160. * FormatHelperTest::testThumbs()
  161. *
  162. * @return void
  163. */
  164. public function testThumbs() {
  165. $result = $this->Format->thumbs(1);
  166. $expected = '<i class="icon icon-pro fa fa-thumbs-up" title="Pro" data-placement="bottom" data-toggle="tooltip"></i>';
  167. $this->assertEquals($expected, $result);
  168. $result = $this->Format->thumbs(0);
  169. $expected = '<i class="icon icon-contra fa fa-thumbs-down" title="Contra" data-placement="bottom" data-toggle="tooltip"></i>';
  170. $this->assertEquals($expected, $result);
  171. }
  172. /**
  173. * FormatHelperTest::testGenderIcon()
  174. *
  175. * @return void
  176. */
  177. public function testGenderIcon() {
  178. $result = $this->Format->genderIcon(0);
  179. $expected = '<i class="icon icon-genderless fa fa-genderless" title="Inter" data-placement="bottom" data-toggle="tooltip"></i>';
  180. $this->assertEquals($expected, $result);
  181. $result = $this->Format->genderIcon(1);
  182. $expected = '<i class="icon icon-male fa fa-mars" title="Male" data-placement="bottom" data-toggle="tooltip"></i>';
  183. $this->assertEquals($expected, $result);
  184. $result = $this->Format->genderIcon(2);
  185. $expected = '<i class="icon icon-female fa fa-venus" title="Female" data-placement="bottom" data-toggle="tooltip"></i>';
  186. $this->assertEquals($expected, $result);
  187. }
  188. /**
  189. * @return void
  190. */
  191. public function testPad() {
  192. $result = $this->Format->pad('foo bär', 20, '-');
  193. $expected = 'foo bär-------------';
  194. $this->assertEquals($expected, $result);
  195. $result = $this->Format->pad('foo bär', 20, '-', STR_PAD_LEFT);
  196. $expected = '-------------foo bär';
  197. $this->assertEquals($expected, $result);
  198. }
  199. /**
  200. * @return void
  201. */
  202. public function testAbsolutePaginateCount() {
  203. $paginator = [
  204. 'page' => 1,
  205. 'pageCount' => 3,
  206. 'count' => 25,
  207. 'perPage' => 10,
  208. ];
  209. $result = $this->Format->absolutePaginateCount($paginator, 2);
  210. $this->assertSame(2, $result);
  211. }
  212. /**
  213. * @return void
  214. */
  215. public function testAbsolutePaginateCountDesc() {
  216. // 2nd element on page 1/3
  217. $paginator = [
  218. 'page' => 1,
  219. 'pageCount' => 3,
  220. 'count' => 25,
  221. 'perPage' => 10,
  222. ];
  223. $result = $this->Format->absolutePaginateCount($paginator, 2, 'DESC');
  224. $this->assertSame(24, $result);
  225. // 3rd element on page 3/3
  226. $paginator = [
  227. 'page' => 3,
  228. 'pageCount' => 3,
  229. 'count' => 25,
  230. 'perPage' => 10,
  231. ];
  232. $result = $this->Format->absolutePaginateCount($paginator, 3, 'DESC');
  233. $this->assertSame(3, $result);
  234. // 2nd element on page 1/1
  235. $paginator = [
  236. 'page' => 1,
  237. 'pageCount' => 1,
  238. 'count' => 9,
  239. 'perPage' => 10,
  240. ];
  241. $result = $this->Format->absolutePaginateCount($paginator, 2, 'DESC');
  242. $this->assertSame(8, $result);
  243. }
  244. /**
  245. * FormatHelperTest::testSiteIcon()
  246. *
  247. * @return void
  248. */
  249. public function testSiteIcon() {
  250. $result = $this->Format->siteIcon('http://www.example.org');
  251. $this->debug($result);
  252. $expected = '<img src="http://www.google.com/s2/favicons?domain=www.example.org';
  253. $this->assertStringContainsString($expected, $result);
  254. }
  255. /**
  256. * @return void
  257. */
  258. public function testSlug() {
  259. $result = $this->Format->slug('A Baz D & Foo');
  260. $this->assertSame('A-Baz-D-Foo', $result);
  261. $this->Format->setConfig('slugger', [Text::class, 'slug']);
  262. $result = $this->Format->slug('A Baz D & Foo');
  263. $this->assertSame('A-Baz-D-Foo', $result);
  264. }
  265. /**
  266. * @return void
  267. */
  268. public function testSlugCustomObject() {
  269. $this->Format->setConfig('slugger', [$this, '_testSlugger']);
  270. $result = $this->Format->slug('A Baz D & Foo');
  271. $this->assertSame('a baz d & foo', $result);
  272. }
  273. /**
  274. * @param string $name
  275. *
  276. * @return string
  277. */
  278. public function _testSlugger($name) {
  279. return mb_strtolower($name);
  280. }
  281. /**
  282. * @return void
  283. */
  284. public function testNeighbors() {
  285. $this->skipIf(true, '//TODO');
  286. $neighbors = [
  287. 'prev' => ['id' => 1, 'foo' => 'bar'],
  288. 'next' => ['id' => 2, 'foo' => 'y'],
  289. ];
  290. $result = $this->Format->neighbors($neighbors, 'foo');
  291. $expected = '<div class="next-prev-navi"><a href="/index/1" title="bar"><i class="icon icon-prev fa fa-arrow-left" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/index/2" title="y"><i class="icon icon-next fa fa-arrow-right" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;nextRecord</a></div>';
  292. $this->assertEquals($expected, $result);
  293. }
  294. /**
  295. * Test slug generation works with new slugger.
  296. *
  297. * @return void
  298. */
  299. public function testSlugGenerationWithNewSlugger() {
  300. $neighbors = [
  301. 'prev' => ['id' => 1, 'foo' => 'My Foo'],
  302. 'next' => ['id' => 2, 'foo' => 'My FooBaz'],
  303. ];
  304. // Only needed for fake requests (tests)
  305. $url = ['controller' => 'MyController', 'action' => 'myAction'];
  306. $result = $this->Format->neighbors($neighbors, 'foo', ['slug' => true, 'url' => $url]);
  307. $expected = '<div class="next-prev-navi nextPrevNavi"><a href="/my-controller/my-action/1/My-Foo" title="My Foo"><i class="icon icon-prev fa fa-arrow-left" title="Prev" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/my-controller/my-action/2/My-FooBaz" title="My FooBaz"><i class="icon icon-next fa fa-arrow-right" title="Next" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;nextRecord</a></div>';
  308. $this->assertEquals($expected, $result);
  309. }
  310. /**
  311. * @return void
  312. */
  313. public function testTab2space() {
  314. $text = "foo\t\tfoobar\tbla\n";
  315. $text .= "fooo\t\tbar\t\tbla\n";
  316. $text .= "foooo\t\tbar\t\tbla\n";
  317. $result = $this->Format->tab2space($text);
  318. $expected = <<<TXT
  319. foo foobar bla
  320. fooo bar bla
  321. foooo bar bla
  322. TXT;
  323. $this->assertTextEquals($expected, $result);
  324. $this->assertTrue(strpos($result, "\t") === false);
  325. }
  326. /**
  327. * @return void
  328. */
  329. public function testArray2table() {
  330. $array = [
  331. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  332. ['1', '2', '3'],
  333. ['4', '5', '6'],
  334. ];
  335. $is = $this->Format->array2table($array);
  336. $this->assertTextContains('<table class="table">', $is);
  337. $this->assertTextContains('</table>', $is);
  338. $this->assertTextContains('<th>', $is);
  339. // recursive
  340. $array = [
  341. ['a' => ['2'], 'b' => ['2'], 'c' => ['2']],
  342. [['2'], ['2'], ['2']],
  343. [['2'], ['2'], [['s' => '3', 't' => '4']]],
  344. ];
  345. $is = $this->Format->array2table($array, ['recursive' => true]);
  346. $expected = <<<TEXT
  347. <table class="table">
  348. <tr><th>a</th><th>b</th><th>c</th></tr>
  349. <tr><td>
  350. <table class="table">
  351. <tr><th>0</th></tr>
  352. <tr><td>2</td></tr>
  353. </table>
  354. </td><td>
  355. <table class="table">
  356. <tr><th>0</th></tr>
  357. <tr><td>2</td></tr>
  358. </table>
  359. </td><td>
  360. <table class="table">
  361. <tr><th>0</th></tr>
  362. <tr><td>2</td></tr>
  363. </table>
  364. </td></tr>
  365. <tr><td>
  366. <table class="table">
  367. <tr><th>0</th></tr>
  368. <tr><td>2</td></tr>
  369. </table>
  370. </td><td>
  371. <table class="table">
  372. <tr><th>0</th></tr>
  373. <tr><td>2</td></tr>
  374. </table>
  375. </td><td>
  376. <table class="table">
  377. <tr><th>0</th></tr>
  378. <tr><td>2</td></tr>
  379. </table>
  380. </td></tr>
  381. <tr><td>
  382. <table class="table">
  383. <tr><th>0</th></tr>
  384. <tr><td>2</td></tr>
  385. </table>
  386. </td><td>
  387. <table class="table">
  388. <tr><th>0</th></tr>
  389. <tr><td>2</td></tr>
  390. </table>
  391. </td><td>
  392. <table class="table">
  393. <tr><th>s</th><th>t</th></tr>
  394. <tr><td>3</td><td>4</td></tr>
  395. </table>
  396. </td></tr>
  397. </table>
  398. TEXT;
  399. $this->assertTextEquals($expected, $is);
  400. $array = [
  401. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  402. ['1', '2', '3'],
  403. ];
  404. $options = [
  405. 'heading' => false,
  406. ];
  407. $attributes = [
  408. 'class' => 'foo',
  409. 'data-x' => 'y',
  410. ];
  411. $is = $this->Format->array2table($array, $options, $attributes);
  412. $this->assertTextContains('<table class="foo" data-x="y">', $is);
  413. $this->assertTextContains('</table>', $is);
  414. $this->assertTextNotContains('<th>', $is);
  415. }
  416. /**
  417. * @return void
  418. */
  419. public function tearDown(): void {
  420. parent::tearDown();
  421. unset($this->Format);
  422. }
  423. }