FormatHelperTest.php 12 KB

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