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 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. * @return void
  189. */
  190. public function testPad() {
  191. $result = $this->Format->pad('foo bär', 20, '-');
  192. $expected = 'foo bär-------------';
  193. $this->assertEquals($expected, $result);
  194. $result = $this->Format->pad('foo bär', 20, '-', STR_PAD_LEFT);
  195. $expected = '-------------foo bär';
  196. $this->assertEquals($expected, $result);
  197. }
  198. /**
  199. * @return void
  200. */
  201. public function testAbsolutePaginateCount() {
  202. $paginator = [
  203. 'page' => 1,
  204. 'pageCount' => 3,
  205. 'count' => 25,
  206. 'perPage' => 10,
  207. ];
  208. $result = $this->Format->absolutePaginateCount($paginator, 2);
  209. $this->assertSame(2, $result);
  210. }
  211. /**
  212. * @return void
  213. */
  214. public function testAbsolutePaginateCountDesc() {
  215. // 2nd element on page 1/3
  216. $paginator = [
  217. 'page' => 1,
  218. 'pageCount' => 3,
  219. 'count' => 25,
  220. 'perPage' => 10,
  221. ];
  222. $result = $this->Format->absolutePaginateCount($paginator, 2, 'DESC');
  223. $this->assertSame(24, $result);
  224. // 3rd element on page 3/3
  225. $paginator = [
  226. 'page' => 3,
  227. 'pageCount' => 3,
  228. 'count' => 25,
  229. 'perPage' => 10,
  230. ];
  231. $result = $this->Format->absolutePaginateCount($paginator, 3, 'DESC');
  232. $this->assertSame(3, $result);
  233. // 2nd element on page 1/1
  234. $paginator = [
  235. 'page' => 1,
  236. 'pageCount' => 1,
  237. 'count' => 9,
  238. 'perPage' => 10,
  239. ];
  240. $result = $this->Format->absolutePaginateCount($paginator, 2, 'DESC');
  241. $this->assertSame(8, $result);
  242. }
  243. /**
  244. * FormatHelperTest::testSiteIcon()
  245. *
  246. * @return void
  247. */
  248. public function testSiteIcon() {
  249. $result = $this->Format->siteIcon('http://www.example.org');
  250. $this->debug($result);
  251. $expected = '<img src="http://www.google.com/s2/favicons?domain=www.example.org';
  252. $this->assertContains($expected, $result);
  253. }
  254. /**
  255. * @return void
  256. */
  257. public function testSlug() {
  258. $result = $this->Format->slug('A Baz D & Foo');
  259. $this->assertSame('A-Baz-D-Foo', $result);
  260. $this->Format->setConfig('slugger', [Text::class, 'slug']);
  261. $result = $this->Format->slug('A Baz D & Foo');
  262. $this->assertSame('A-Baz-D-Foo', $result);
  263. }
  264. /**
  265. * @return void
  266. */
  267. public function testSlugCustomObject() {
  268. $this->Format->setConfig('slugger', [$this, '_testSlugger']);
  269. $result = $this->Format->slug('A Baz D & Foo');
  270. $this->assertSame('a baz d & foo', $result);
  271. }
  272. /**
  273. * @param string $name
  274. *
  275. * @return string
  276. */
  277. public function _testSlugger($name) {
  278. return mb_strtolower($name);
  279. }
  280. /**
  281. * @return void
  282. */
  283. public function testNeighbors() {
  284. $this->skipIf(true, '//TODO');
  285. $neighbors = [
  286. 'prev' => ['id' => 1, 'foo' => 'bar'],
  287. 'next' => ['id' => 2, 'foo' => 'y'],
  288. ];
  289. $result = $this->Format->neighbors($neighbors, 'foo');
  290. $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>';
  291. $this->assertEquals($expected, $result);
  292. }
  293. /**
  294. * Test slug generation works with new slugger.
  295. *
  296. * @return void
  297. */
  298. public function testSlugGenerationWithNewSlugger() {
  299. $neighbors = [
  300. 'prev' => ['id' => 1, 'foo' => 'My Foo'],
  301. 'next' => ['id' => 2, 'foo' => 'My FooBaz'],
  302. ];
  303. // Only needed for fake requests (tests)
  304. $url = ['controller' => 'MyController', 'action' => 'myAction'];
  305. $result = $this->Format->neighbors($neighbors, 'foo', ['slug' => true, 'url' => $url]);
  306. $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>';
  307. $this->assertEquals($expected, $result);
  308. }
  309. /**
  310. * @return void
  311. */
  312. public function testTab2space() {
  313. $text = "foo\t\tfoobar\tbla\n";
  314. $text .= "fooo\t\tbar\t\tbla\n";
  315. $text .= "foooo\t\tbar\t\tbla\n";
  316. $result = $this->Format->tab2space($text);
  317. $expected = <<<TXT
  318. foo foobar bla
  319. fooo bar bla
  320. foooo bar bla
  321. TXT;
  322. $this->assertTextEquals($expected, $result);
  323. }
  324. /**
  325. * @return void
  326. */
  327. public function testArray2table() {
  328. $array = [
  329. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  330. ['1', '2', '3'],
  331. ['4', '5', '6'],
  332. ];
  333. $is = $this->Format->array2table($array);
  334. $this->assertTextContains('<table class="table">', $is);
  335. $this->assertTextContains('</table>', $is);
  336. $this->assertTextContains('<th>', $is);
  337. // recursive
  338. $array = [
  339. ['a' => ['2'], 'b' => ['2'], 'c' => ['2']],
  340. [['2'], ['2'], ['2']],
  341. [['2'], ['2'], [['s' => '3', 't' => '4']]],
  342. ];
  343. $is = $this->Format->array2table($array, ['recursive' => true]);
  344. $expected = <<<TEXT
  345. <table class="table">
  346. <tr><th>a</th><th>b</th><th>c</th></tr>
  347. <tr><td>
  348. <table class="table">
  349. <tr><th>0</th></tr>
  350. <tr><td>2</td></tr>
  351. </table>
  352. </td><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></tr>
  363. <tr><td>
  364. <table class="table">
  365. <tr><th>0</th></tr>
  366. <tr><td>2</td></tr>
  367. </table>
  368. </td><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></tr>
  379. <tr><td>
  380. <table class="table">
  381. <tr><th>0</th></tr>
  382. <tr><td>2</td></tr>
  383. </table>
  384. </td><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>s</th><th>t</th></tr>
  392. <tr><td>3</td><td>4</td></tr>
  393. </table>
  394. </td></tr>
  395. </table>
  396. TEXT;
  397. $this->assertTextEquals($expected, $is);
  398. $array = [
  399. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  400. ['1', '2', '3'],
  401. ];
  402. $options = [
  403. 'heading' => false,
  404. ];
  405. $attributes = [
  406. 'class' => 'foo',
  407. 'data-x' => 'y',
  408. ];
  409. $is = $this->Format->array2table($array, $options, $attributes);
  410. $this->assertTextContains('<table class="foo" data-x="y">', $is);
  411. $this->assertTextContains('</table>', $is);
  412. $this->assertTextNotContains('<th>', $is);
  413. }
  414. /**
  415. * @return void
  416. */
  417. public function tearDown() {
  418. parent::tearDown();
  419. unset($this->Format);
  420. }
  421. }