FormatHelperTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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();
  142. $expected = '<i class="icon icon-genderless genderless" title="Unknown" data-placement="bottom" data-toggle="tooltip"></i>';
  143. $this->assertEquals($expected, $result);
  144. }
  145. /**
  146. * FormatHelperTest::testPad()
  147. *
  148. * @return void
  149. */
  150. public function testPad() {
  151. $result = $this->Format->pad('foo bär', 20, '-');
  152. $expected = 'foo bär-------------';
  153. $this->assertEquals($expected, $result);
  154. $result = $this->Format->pad('foo bär', 20, '-', STR_PAD_LEFT);
  155. $expected = '-------------foo bär';
  156. $this->assertEquals($expected, $result);
  157. }
  158. /**
  159. * FormatHelperTest::testAbsolutePaginateCount()
  160. *
  161. * @return void
  162. */
  163. public function testAbsolutePaginateCount() {
  164. $paginator = [
  165. 'page' => 1,
  166. 'pageCount' => 3,
  167. 'count' => 25,
  168. 'limit' => 10
  169. ];
  170. $result = $this->Format->absolutePaginateCount($paginator, 2);
  171. $this->assertEquals(2, $result);
  172. }
  173. /**
  174. * FormatHelperTest::testSiteIcon()
  175. *
  176. * @return void
  177. */
  178. public function testSiteIcon() {
  179. $result = $this->Format->siteIcon('http://www.example.org');
  180. $this->debug($result);
  181. $expected = '<img src="http://www.google.com/s2/favicons?domain=www.example.org';
  182. $this->assertContains($expected, $result);
  183. }
  184. /**
  185. * FormatHelperTest::testConfigure()
  186. *
  187. * @return void
  188. */
  189. public function testNeighbors() {
  190. $this->skipIf(true, '//TODO');
  191. $neighbors = [
  192. 'prev' => ['id' => 1, 'foo' => 'bar'],
  193. 'next' => ['id' => 2, 'foo' => 'y'],
  194. ];
  195. $result = $this->Format->neighbors($neighbors, 'foo');
  196. $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>';
  197. $this->assertEquals($expected, $result);
  198. }
  199. /**
  200. * FormatHelperTest::testTab2space()
  201. *
  202. * @return void
  203. */
  204. public function testTab2space() {
  205. $text = "foo\t\tfoobar\tbla\n";
  206. $text .= "fooo\t\tbar\t\tbla\n";
  207. $text .= "foooo\t\tbar\t\tbla\n";
  208. $result = $this->Format->tab2space($text);
  209. //echo "<pre>" . $text . "</pre>";
  210. //echo'becomes';
  211. //echo "<pre>" . $result . "</pre>";
  212. }
  213. /**
  214. * FormatHelperTest::testArray2table()
  215. *
  216. * @return void
  217. */
  218. public function testArray2table() {
  219. $array = [
  220. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  221. ['1', '2', '3'],
  222. ['4', '5', '6'],
  223. ];
  224. $is = $this->Format->array2table($array);
  225. //echo $is;
  226. //$this->assertEquals($expected, $is);
  227. // recursive?
  228. $array = [
  229. ['a' => ['2'], 'b' => ['2'], 'c' => ['2']],
  230. [['2'], ['2'], ['2']],
  231. [['2'], ['2'], [['s' => '3', 't' => '4']]],
  232. ];
  233. $is = $this->Format->array2table($array, ['recursive' => true]);
  234. //echo $is;
  235. }
  236. public function tearDown() {
  237. parent::tearDown();
  238. unset($this->Format);
  239. }
  240. }