FormatHelperTest.php 12 KB

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