FormatHelperTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace Tools\TestCase\View\Helper;
  3. use Cake\Core\Configure;
  4. use Cake\View\View;
  5. use Tools\TestSuite\TestCase;
  6. use Tools\View\Helper\FormatHelper;
  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. $result = $this->Format->yesNo('2', ['on' => 2, 'onTitle' => 'foo']);
  108. $expected = '<i class="icon icon-yes fa fa-check" title="foo" data-placement="bottom" data-toggle="tooltip"></i>';
  109. $this->assertTextContains($expected, $result);
  110. $result = $this->Format->yesNo('3', ['on' => 4, 'offTitle' => 'nope']);
  111. $expected = '<i class="icon icon-no fa fa-times" title="nope" data-placement="bottom" data-toggle="tooltip"></i>';
  112. $this->assertEquals($expected, $result);
  113. }
  114. /**
  115. * @return void
  116. */
  117. public function testOk() {
  118. $content = 'xyz';
  119. $data = [
  120. true => '<span class="ok-yes" style="color:green">xyz 1</span>',
  121. false => '<span class="ok-no" style="color:red">xyz 0</span>'
  122. ];
  123. foreach ($data as $value => $expected) {
  124. $result = $this->Format->ok($content . ' ' . (int)$value, $value);
  125. $this->assertEquals($expected, $result);
  126. }
  127. }
  128. /**
  129. * FormatHelperTest::testThumbs()
  130. *
  131. * @return void
  132. */
  133. public function testThumbs() {
  134. $result = $this->Format->thumbs(1);
  135. $expected = '<i class="icon icon-pro fa fa-thumbs-up" title="Pro" data-placement="bottom" data-toggle="tooltip"></i>';
  136. $this->assertEquals($expected, $result);
  137. $result = $this->Format->thumbs(0);
  138. $expected = '<i class="icon icon-contra fa fa-thumbs-down" title="Contra" data-placement="bottom" data-toggle="tooltip"></i>';
  139. $this->assertEquals($expected, $result);
  140. }
  141. /**
  142. * FormatHelperTest::testGenderIcon()
  143. *
  144. * @return void
  145. */
  146. public function testGenderIcon() {
  147. $result = $this->Format->genderIcon(0);
  148. $expected = '<i class="icon icon-genderless fa fa-genderless" title="Unknown" data-placement="bottom" data-toggle="tooltip"></i>';
  149. $this->assertEquals($expected, $result);
  150. $result = $this->Format->genderIcon(1);
  151. $expected = '<i class="icon icon-male fa fa-mars" title="Male" data-placement="bottom" data-toggle="tooltip"></i>';
  152. $this->assertEquals($expected, $result);
  153. $result = $this->Format->genderIcon(2);
  154. $expected = '<i class="icon icon-female fa fa-venus" title="Female" data-placement="bottom" data-toggle="tooltip"></i>';
  155. $this->assertEquals($expected, $result);
  156. }
  157. /**
  158. * FormatHelperTest::testPad()
  159. *
  160. * @return void
  161. */
  162. public function testPad() {
  163. $result = $this->Format->pad('foo bär', 20, '-');
  164. $expected = 'foo bär-------------';
  165. $this->assertEquals($expected, $result);
  166. $result = $this->Format->pad('foo bär', 20, '-', STR_PAD_LEFT);
  167. $expected = '-------------foo bär';
  168. $this->assertEquals($expected, $result);
  169. }
  170. /**
  171. * FormatHelperTest::testAbsolutePaginateCount()
  172. *
  173. * @return void
  174. */
  175. public function testAbsolutePaginateCount() {
  176. $paginator = [
  177. 'page' => 1,
  178. 'pageCount' => 3,
  179. 'count' => 25,
  180. 'limit' => 10
  181. ];
  182. $result = $this->Format->absolutePaginateCount($paginator, 2);
  183. $this->assertEquals(2, $result);
  184. }
  185. /**
  186. * FormatHelperTest::testSiteIcon()
  187. *
  188. * @return void
  189. */
  190. public function testSiteIcon() {
  191. $result = $this->Format->siteIcon('http://www.example.org');
  192. $this->debug($result);
  193. $expected = '<img src="http://www.google.com/s2/favicons?domain=www.example.org';
  194. $this->assertContains($expected, $result);
  195. }
  196. /**
  197. * FormatHelperTest::testConfigure()
  198. *
  199. * @return void
  200. */
  201. public function testNeighbors() {
  202. $this->skipIf(true, '//TODO');
  203. $neighbors = [
  204. 'prev' => ['id' => 1, 'foo' => 'bar'],
  205. 'next' => ['id' => 2, 'foo' => 'y'],
  206. ];
  207. $result = $this->Format->neighbors($neighbors, 'foo');
  208. $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>';
  209. $this->assertEquals($expected, $result);
  210. }
  211. /**
  212. * FormatHelperTest::testTab2space()
  213. *
  214. * @return void
  215. */
  216. public function testTab2space() {
  217. $text = "foo\t\tfoobar\tbla\n";
  218. $text .= "fooo\t\tbar\t\tbla\n";
  219. $text .= "foooo\t\tbar\t\tbla\n";
  220. $result = $this->Format->tab2space($text);
  221. //echo "<pre>" . $text . "</pre>";
  222. //echo'becomes';
  223. //echo "<pre>" . $result . "</pre>";
  224. }
  225. /**
  226. * FormatHelperTest::testArray2table()
  227. *
  228. * @return void
  229. */
  230. public function testArray2table() {
  231. $array = [
  232. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  233. ['1', '2', '3'],
  234. ['4', '5', '6'],
  235. ];
  236. $is = $this->Format->array2table($array);
  237. $this->assertTextContains('<table class="table">', $is);
  238. $this->assertTextContains('</table>', $is);
  239. $this->assertTextContains('<th>', $is);
  240. // recursive
  241. $array = [
  242. ['a' => ['2'], 'b' => ['2'], 'c' => ['2']],
  243. [['2'], ['2'], ['2']],
  244. [['2'], ['2'], [['s' => '3', 't' => '4']]],
  245. ];
  246. $is = $this->Format->array2table($array, ['recursive' => true]);
  247. $expected = <<<TEXT
  248. <table class="table">
  249. <tr><th>a</th><th>b</th><th>c</th></tr>
  250. <tr><td>
  251. <table class="table">
  252. <tr><th>0</th></tr>
  253. <tr><td>2</td></tr>
  254. </table>
  255. </td><td>
  256. <table class="table">
  257. <tr><th>0</th></tr>
  258. <tr><td>2</td></tr>
  259. </table>
  260. </td><td>
  261. <table class="table">
  262. <tr><th>0</th></tr>
  263. <tr><td>2</td></tr>
  264. </table>
  265. </td></tr>
  266. <tr><td>
  267. <table class="table">
  268. <tr><th>0</th></tr>
  269. <tr><td>2</td></tr>
  270. </table>
  271. </td><td>
  272. <table class="table">
  273. <tr><th>0</th></tr>
  274. <tr><td>2</td></tr>
  275. </table>
  276. </td><td>
  277. <table class="table">
  278. <tr><th>0</th></tr>
  279. <tr><td>2</td></tr>
  280. </table>
  281. </td></tr>
  282. <tr><td>
  283. <table class="table">
  284. <tr><th>0</th></tr>
  285. <tr><td>2</td></tr>
  286. </table>
  287. </td><td>
  288. <table class="table">
  289. <tr><th>0</th></tr>
  290. <tr><td>2</td></tr>
  291. </table>
  292. </td><td>
  293. <table class="table">
  294. <tr><th>s</th><th>t</th></tr>
  295. <tr><td>3</td><td>4</td></tr>
  296. </table>
  297. </td></tr>
  298. </table>
  299. TEXT;
  300. $this->assertTextEquals($expected, $is);
  301. $array = [
  302. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  303. ['1', '2', '3'],
  304. ];
  305. $options = [
  306. 'heading' => false
  307. ];
  308. $attributes = [
  309. 'class' => 'foo',
  310. 'data-x' => 'y'
  311. ];
  312. $is = $this->Format->array2table($array, $options, $attributes);
  313. $this->assertTextContains('<table class="foo" data-x="y">', $is);
  314. $this->assertTextContains('</table>', $is);
  315. $this->assertTextNotContains('<th>', $is);
  316. }
  317. public function tearDown() {
  318. parent::tearDown();
  319. unset($this->Format);
  320. }
  321. }