FormatHelperTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 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-pencil" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  73. $this->assertEquals($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-pencil" data-x="y" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  83. $this->assertEquals($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-pencil my-extra" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  91. $this->assertEquals($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->assertEquals($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->assertEquals($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->assertEquals($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="Unknown" 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. * FormatHelperTest::testPad()
  193. *
  194. * @return void
  195. */
  196. public function testPad() {
  197. $result = $this->Format->pad('foo bär', 20, '-');
  198. $expected = 'foo bär-------------';
  199. $this->assertEquals($expected, $result);
  200. $result = $this->Format->pad('foo bär', 20, '-', STR_PAD_LEFT);
  201. $expected = '-------------foo bär';
  202. $this->assertEquals($expected, $result);
  203. }
  204. /**
  205. * FormatHelperTest::testAbsolutePaginateCount()
  206. *
  207. * @return void
  208. */
  209. public function testAbsolutePaginateCount() {
  210. $paginator = [
  211. 'page' => 1,
  212. 'pageCount' => 3,
  213. 'count' => 25,
  214. 'limit' => 10,
  215. ];
  216. $result = $this->Format->absolutePaginateCount($paginator, 2);
  217. $this->assertEquals(2, $result);
  218. }
  219. /**
  220. * FormatHelperTest::testSiteIcon()
  221. *
  222. * @return void
  223. */
  224. public function testSiteIcon() {
  225. $result = $this->Format->siteIcon('http://www.example.org');
  226. $this->debug($result);
  227. $expected = '<img src="http://www.google.com/s2/favicons?domain=www.example.org';
  228. $this->assertStringContainsString($expected, $result);
  229. }
  230. /**
  231. * @return void
  232. */
  233. public function testSlug() {
  234. $result = $this->Format->slug('A Baz D & Foo');
  235. $this->assertSame('A-Baz-D-Foo', $result);
  236. $this->Format->setConfig('slugger', [Text::class, 'slug']);
  237. $result = $this->Format->slug('A Baz D & Foo');
  238. $this->assertSame('A-Baz-D-Foo', $result);
  239. }
  240. /**
  241. * @return void
  242. */
  243. public function testSlugCustomObject() {
  244. $this->Format->setConfig('slugger', [$this, '_testSlugger']);
  245. $result = $this->Format->slug('A Baz D & Foo');
  246. $this->assertSame('a baz d & foo', $result);
  247. }
  248. /**
  249. * @param string $name
  250. *
  251. * @return string
  252. */
  253. public function _testSlugger($name) {
  254. return mb_strtolower($name);
  255. }
  256. /**
  257. * @return void
  258. */
  259. public function testNeighbors() {
  260. $this->skipIf(true, '//TODO');
  261. $neighbors = [
  262. 'prev' => ['id' => 1, 'foo' => 'bar'],
  263. 'next' => ['id' => 2, 'foo' => 'y'],
  264. ];
  265. $result = $this->Format->neighbors($neighbors, 'foo');
  266. $expected = '<div class="next-prev-navi"><a href="/index/1" title="bar"><i class="icon icon-prev fa fa-prev prev" 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-next next" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;nextRecord</a></div>';
  267. $this->assertEquals($expected, $result);
  268. }
  269. /**
  270. * Test slug generation works with new slugger.
  271. *
  272. * @return void
  273. */
  274. public function testSlugGenerationWithNewSlugger() {
  275. $neighbors = [
  276. 'prev' => ['id' => 1, 'foo' => 'My Foo'],
  277. 'next' => ['id' => 2, 'foo' => 'My FooBaz'],
  278. ];
  279. // Only needed for fake requests (tests)
  280. $url = ['controller' => 'MyController', 'action' => 'myAction'];
  281. $result = $this->Format->neighbors($neighbors, 'foo', ['slug' => true, 'url' => $url]);
  282. $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-prev" 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-next" title="Next" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;nextRecord</a></div>';
  283. $this->assertEquals($expected, $result);
  284. }
  285. /**
  286. * @return void
  287. */
  288. public function testTab2space() {
  289. $text = "foo\t\tfoobar\tbla\n";
  290. $text .= "fooo\t\tbar\t\tbla\n";
  291. $text .= "foooo\t\tbar\t\tbla\n";
  292. $result = $this->Format->tab2space($text);
  293. $expected = <<<TXT
  294. foo foobar bla
  295. fooo bar bla
  296. foooo bar bla
  297. TXT;
  298. $this->assertTextEquals($expected, $result);
  299. $this->assertTrue(strpos($result, "\t") === false);
  300. }
  301. /**
  302. * @return void
  303. */
  304. public function testArray2table() {
  305. $array = [
  306. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  307. ['1', '2', '3'],
  308. ['4', '5', '6'],
  309. ];
  310. $is = $this->Format->array2table($array);
  311. $this->assertTextContains('<table class="table">', $is);
  312. $this->assertTextContains('</table>', $is);
  313. $this->assertTextContains('<th>', $is);
  314. // recursive
  315. $array = [
  316. ['a' => ['2'], 'b' => ['2'], 'c' => ['2']],
  317. [['2'], ['2'], ['2']],
  318. [['2'], ['2'], [['s' => '3', 't' => '4']]],
  319. ];
  320. $is = $this->Format->array2table($array, ['recursive' => true]);
  321. $expected = <<<TEXT
  322. <table class="table">
  323. <tr><th>a</th><th>b</th><th>c</th></tr>
  324. <tr><td>
  325. <table class="table">
  326. <tr><th>0</th></tr>
  327. <tr><td>2</td></tr>
  328. </table>
  329. </td><td>
  330. <table class="table">
  331. <tr><th>0</th></tr>
  332. <tr><td>2</td></tr>
  333. </table>
  334. </td><td>
  335. <table class="table">
  336. <tr><th>0</th></tr>
  337. <tr><td>2</td></tr>
  338. </table>
  339. </td></tr>
  340. <tr><td>
  341. <table class="table">
  342. <tr><th>0</th></tr>
  343. <tr><td>2</td></tr>
  344. </table>
  345. </td><td>
  346. <table class="table">
  347. <tr><th>0</th></tr>
  348. <tr><td>2</td></tr>
  349. </table>
  350. </td><td>
  351. <table class="table">
  352. <tr><th>0</th></tr>
  353. <tr><td>2</td></tr>
  354. </table>
  355. </td></tr>
  356. <tr><td>
  357. <table class="table">
  358. <tr><th>0</th></tr>
  359. <tr><td>2</td></tr>
  360. </table>
  361. </td><td>
  362. <table class="table">
  363. <tr><th>0</th></tr>
  364. <tr><td>2</td></tr>
  365. </table>
  366. </td><td>
  367. <table class="table">
  368. <tr><th>s</th><th>t</th></tr>
  369. <tr><td>3</td><td>4</td></tr>
  370. </table>
  371. </td></tr>
  372. </table>
  373. TEXT;
  374. $this->assertTextEquals($expected, $is);
  375. $array = [
  376. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  377. ['1', '2', '3'],
  378. ];
  379. $options = [
  380. 'heading' => false,
  381. ];
  382. $attributes = [
  383. 'class' => 'foo',
  384. 'data-x' => 'y',
  385. ];
  386. $is = $this->Format->array2table($array, $options, $attributes);
  387. $this->assertTextContains('<table class="foo" data-x="y">', $is);
  388. $this->assertTextContains('</table>', $is);
  389. $this->assertTextNotContains('<th>', $is);
  390. }
  391. /**
  392. * @return void
  393. */
  394. public function tearDown(): void {
  395. parent::tearDown();
  396. unset($this->Format);
  397. }
  398. }