FormatHelperTest.php 10 KB

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