FormatHelperTest.php 12 KB

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