FormatHelperTest.php 13 KB

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