FormatHelperTest.php 9.2 KB

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