FormatHelperTest.php 9.8 KB

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