FormatHelperTest.php 13 KB

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