FormatHelperTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 Tools\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
  15. */
  16. public $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 testIconWithCustomFontIcon() {
  89. $this->Format->setConfig('fontIcons', ['edit' => 'fax fax-pen']);
  90. $result = $this->Format->icon('edit');
  91. $expected = '<i class="icon icon-edit fax fax-pen" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  92. $this->assertEquals($expected, $result);
  93. }
  94. /**
  95. * @return void
  96. */
  97. public function testIconWithPrefixedIcon() {
  98. $this->Format->setConfig('iconNamespaces', ['fa', 'glyphicon']);
  99. $result = $this->Format->icon('glyphicon-foo');
  100. $expected = '<i class="icon icon-glyphicon-foo glyphicon glyphicon-foo" title="' . __d('tools', 'Foo') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  101. $this->assertEquals($expected, $result);
  102. $result = $this->Format->icon('glyphicon-edit');
  103. $expected = '<i class="icon icon-glyphicon-edit glyphicon glyphicon-edit" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  104. $this->assertEquals($expected, $result);
  105. }
  106. /**
  107. * @return void
  108. */
  109. public function testFontIcon() {
  110. $result = $this->Format->fontIcon('signin');
  111. $expected = '<i class="fa fa-signin"></i>';
  112. $this->assertEquals($expected, $result);
  113. $result = $this->Format->fontIcon('signin', ['rotate' => 90]);
  114. $expected = '<i class="fa fa-signin fa-rotate-90"></i>';
  115. $this->assertEquals($expected, $result);
  116. $result = $this->Format->fontIcon('signin', ['size' => 5, 'extra' => ['muted']]);
  117. $expected = '<i class="fa fa-signin fa-muted fa-5x"></i>';
  118. $this->assertEquals($expected, $result);
  119. $result = $this->Format->fontIcon('signin', ['size' => 5, 'extra' => ['muted'], 'namespace' => 'myicon']);
  120. $expected = '<i class="myicon myicon-signin myicon-muted myicon-5x"></i>';
  121. $this->assertEquals($expected, $result);
  122. }
  123. /**
  124. * @return void
  125. */
  126. public function testYesNo() {
  127. $result = $this->Format->yesNo(true);
  128. $expected = '<i class="icon icon-yes fa fa-check" title="' . __d('tools', 'Yes') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  129. $this->assertEquals($expected, $result);
  130. $result = $this->Format->yesNo(false);
  131. $expected = '<i class="icon icon-no fa fa-times" title="' . __d('tools', 'No') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  132. $this->assertEquals($expected, $result);
  133. $result = $this->Format->yesNo('2', ['on' => 2, 'onTitle' => 'foo']);
  134. $expected = '<i class="icon icon-yes fa fa-check" title="foo" data-placement="bottom" data-toggle="tooltip"></i>';
  135. $this->assertTextContains($expected, $result);
  136. $result = $this->Format->yesNo('3', ['on' => 4, 'offTitle' => 'nope']);
  137. $expected = '<i class="icon icon-no fa fa-times" title="nope" data-placement="bottom" data-toggle="tooltip"></i>';
  138. $this->assertEquals($expected, $result);
  139. }
  140. /**
  141. * @return void
  142. */
  143. public function testOk() {
  144. $content = 'xyz';
  145. $data = [
  146. true => '<span class="ok-yes" style="color:green">xyz 1</span>',
  147. false => '<span class="ok-no" style="color:red">xyz 0</span>',
  148. ];
  149. foreach ($data as $value => $expected) {
  150. $result = $this->Format->ok($content . ' ' . (int)$value, $value);
  151. $this->assertEquals($expected, $result);
  152. }
  153. }
  154. /**
  155. * FormatHelperTest::testThumbs()
  156. *
  157. * @return void
  158. */
  159. public function testThumbs() {
  160. $result = $this->Format->thumbs(1);
  161. $expected = '<i class="icon icon-pro fa fa-thumbs-up" title="Pro" data-placement="bottom" data-toggle="tooltip"></i>';
  162. $this->assertEquals($expected, $result);
  163. $result = $this->Format->thumbs(0);
  164. $expected = '<i class="icon icon-contra fa fa-thumbs-down" title="Contra" data-placement="bottom" data-toggle="tooltip"></i>';
  165. $this->assertEquals($expected, $result);
  166. }
  167. /**
  168. * FormatHelperTest::testGenderIcon()
  169. *
  170. * @return void
  171. */
  172. public function testGenderIcon() {
  173. $result = $this->Format->genderIcon(0);
  174. $expected = '<i class="icon icon-genderless fa fa-genderless" title="Unknown" data-placement="bottom" data-toggle="tooltip"></i>';
  175. $this->assertEquals($expected, $result);
  176. $result = $this->Format->genderIcon(1);
  177. $expected = '<i class="icon icon-male fa fa-mars" title="Male" data-placement="bottom" data-toggle="tooltip"></i>';
  178. $this->assertEquals($expected, $result);
  179. $result = $this->Format->genderIcon(2);
  180. $expected = '<i class="icon icon-female fa fa-venus" title="Female" data-placement="bottom" data-toggle="tooltip"></i>';
  181. $this->assertEquals($expected, $result);
  182. }
  183. /**
  184. * FormatHelperTest::testPad()
  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. * FormatHelperTest::testAbsolutePaginateCount()
  198. *
  199. * @return void
  200. */
  201. public function testAbsolutePaginateCount() {
  202. $paginator = [
  203. 'page' => 1,
  204. 'pageCount' => 3,
  205. 'count' => 25,
  206. 'limit' => 10,
  207. ];
  208. $result = $this->Format->absolutePaginateCount($paginator, 2);
  209. $this->assertEquals(2, $result);
  210. }
  211. /**
  212. * FormatHelperTest::testSiteIcon()
  213. *
  214. * @return void
  215. */
  216. public function testSiteIcon() {
  217. $result = $this->Format->siteIcon('http://www.example.org');
  218. $this->debug($result);
  219. $expected = '<img src="http://www.google.com/s2/favicons?domain=www.example.org';
  220. $this->assertStringContainsString($expected, $result);
  221. }
  222. /**
  223. * @return void
  224. */
  225. public function testSlug() {
  226. $result = $this->Format->slug('A Baz D & Foo');
  227. $this->assertSame('A-Baz-D-Foo', $result);
  228. $this->Format->setConfig('slugger', [Text::class, 'slug']);
  229. $result = $this->Format->slug('A Baz D & Foo');
  230. $this->assertSame('A-Baz-D-Foo', $result);
  231. }
  232. /**
  233. * @return void
  234. */
  235. public function testSlugCustomObject() {
  236. $this->Format->setConfig('slugger', [$this, '_testSlugger']);
  237. $result = $this->Format->slug('A Baz D & Foo');
  238. $this->assertSame('a baz d & foo', $result);
  239. }
  240. /**
  241. * @param string $name
  242. *
  243. * @return string
  244. */
  245. public function _testSlugger($name) {
  246. return mb_strtolower($name);
  247. }
  248. /**
  249. * @return void
  250. */
  251. public function testNeighbors() {
  252. $this->skipIf(true, '//TODO');
  253. $neighbors = [
  254. 'prev' => ['id' => 1, 'foo' => 'bar'],
  255. 'next' => ['id' => 2, 'foo' => 'y'],
  256. ];
  257. $result = $this->Format->neighbors($neighbors, 'foo');
  258. $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>';
  259. $this->assertEquals($expected, $result);
  260. }
  261. /**
  262. * Test slug generation works with new slugger.
  263. *
  264. * @return void
  265. */
  266. public function testSlugGenerationWithNewSlugger() {
  267. $neighbors = [
  268. 'prev' => ['id' => 1, 'foo' => 'My Foo'],
  269. 'next' => ['id' => 2, 'foo' => 'My FooBaz'],
  270. ];
  271. // Only needed for fake requests (tests)
  272. $url = ['controller' => 'MyController', 'action' => 'myAction'];
  273. $result = $this->Format->neighbors($neighbors, 'foo', ['slug' => true, 'url' => $url]);
  274. $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>';
  275. $this->assertEquals($expected, $result);
  276. }
  277. /**
  278. * @return void
  279. */
  280. public function testTab2space() {
  281. $text = "foo\t\tfoobar\tbla\n";
  282. $text .= "fooo\t\tbar\t\tbla\n";
  283. $text .= "foooo\t\tbar\t\tbla\n";
  284. $result = $this->Format->tab2space($text);
  285. $expected = <<<TXT
  286. foo foobar bla
  287. fooo bar bla
  288. foooo bar bla
  289. TXT;
  290. $this->assertTextEquals($expected, $result);
  291. $this->assertTrue(strpos($result, "\t") === false);
  292. }
  293. /**
  294. * @return void
  295. */
  296. public function testArray2table() {
  297. $array = [
  298. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  299. ['1', '2', '3'],
  300. ['4', '5', '6'],
  301. ];
  302. $is = $this->Format->array2table($array);
  303. $this->assertTextContains('<table class="table">', $is);
  304. $this->assertTextContains('</table>', $is);
  305. $this->assertTextContains('<th>', $is);
  306. // recursive
  307. $array = [
  308. ['a' => ['2'], 'b' => ['2'], 'c' => ['2']],
  309. [['2'], ['2'], ['2']],
  310. [['2'], ['2'], [['s' => '3', 't' => '4']]],
  311. ];
  312. $is = $this->Format->array2table($array, ['recursive' => true]);
  313. $expected = <<<TEXT
  314. <table class="table">
  315. <tr><th>a</th><th>b</th><th>c</th></tr>
  316. <tr><td>
  317. <table class="table">
  318. <tr><th>0</th></tr>
  319. <tr><td>2</td></tr>
  320. </table>
  321. </td><td>
  322. <table class="table">
  323. <tr><th>0</th></tr>
  324. <tr><td>2</td></tr>
  325. </table>
  326. </td><td>
  327. <table class="table">
  328. <tr><th>0</th></tr>
  329. <tr><td>2</td></tr>
  330. </table>
  331. </td></tr>
  332. <tr><td>
  333. <table class="table">
  334. <tr><th>0</th></tr>
  335. <tr><td>2</td></tr>
  336. </table>
  337. </td><td>
  338. <table class="table">
  339. <tr><th>0</th></tr>
  340. <tr><td>2</td></tr>
  341. </table>
  342. </td><td>
  343. <table class="table">
  344. <tr><th>0</th></tr>
  345. <tr><td>2</td></tr>
  346. </table>
  347. </td></tr>
  348. <tr><td>
  349. <table class="table">
  350. <tr><th>0</th></tr>
  351. <tr><td>2</td></tr>
  352. </table>
  353. </td><td>
  354. <table class="table">
  355. <tr><th>0</th></tr>
  356. <tr><td>2</td></tr>
  357. </table>
  358. </td><td>
  359. <table class="table">
  360. <tr><th>s</th><th>t</th></tr>
  361. <tr><td>3</td><td>4</td></tr>
  362. </table>
  363. </td></tr>
  364. </table>
  365. TEXT;
  366. $this->assertTextEquals($expected, $is);
  367. $array = [
  368. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  369. ['1', '2', '3'],
  370. ];
  371. $options = [
  372. 'heading' => false,
  373. ];
  374. $attributes = [
  375. 'class' => 'foo',
  376. 'data-x' => 'y',
  377. ];
  378. $is = $this->Format->array2table($array, $options, $attributes);
  379. $this->assertTextContains('<table class="foo" data-x="y">', $is);
  380. $this->assertTextContains('</table>', $is);
  381. $this->assertTextNotContains('<th>', $is);
  382. }
  383. /**
  384. * @return void
  385. */
  386. public function tearDown(): void {
  387. parent::tearDown();
  388. unset($this->Format);
  389. }
  390. }