FormatHelperTest.php 9.7 KB

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