FormatHelperTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\Core\Configure;
  4. use Cake\View\View;
  5. use Shim\TestSuite\TestCase;
  6. use Tools\Utility\Text;
  7. use Tools\View\Helper\FormatHelper;
  8. class FormatHelperTest extends TestCase {
  9. /**
  10. * @var array<string>
  11. */
  12. protected array $fixtures = [
  13. 'plugin.Tools.Sessions',
  14. ];
  15. /**
  16. * @var \Tools\View\Helper\FormatHelper
  17. */
  18. protected $Format;
  19. /**
  20. * @return void
  21. */
  22. public function setUp(): void {
  23. parent::setUp();
  24. $this->loadRoutes();
  25. Configure::write('App.imageBaseUrl', 'img/');
  26. $this->Format = new FormatHelper(new View(null));
  27. }
  28. /**
  29. * @return void
  30. */
  31. public function testDisabledLink() {
  32. $content = 'xyz';
  33. $data = [
  34. [],
  35. ['class' => 'disabledLink', 'title' => false],
  36. ['class' => 'helloClass', 'title' => 'helloTitle'],
  37. ];
  38. foreach ($data as $key => $value) {
  39. $res = $this->Format->disabledLink($content, $value);
  40. //echo ''.$res.' (\''.h($res).'\')';
  41. $this->assertTrue(!empty($res));
  42. }
  43. }
  44. /**
  45. * @return void
  46. */
  47. public function testWarning() {
  48. $content = 'xyz';
  49. $data = [
  50. true,
  51. false,
  52. ];
  53. foreach ($data as $key => $value) {
  54. $res = $this->Format->warning($content . ' ' . (int)$value, $value);
  55. //echo ''.$res.'';
  56. $this->assertTrue(!empty($res));
  57. }
  58. }
  59. /**
  60. * @return void
  61. */
  62. public function testYesNo() {
  63. $result = $this->Format->yesNo(true);
  64. $expected = '<i class="icon icon-yes fa fa-check" title="' . __d('tools', 'Yes') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  65. $this->assertEquals($expected, $result);
  66. $result = $this->Format->yesNo(false);
  67. $expected = '<i class="icon icon-no fa fa-times" title="' . __d('tools', 'No') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  68. $this->assertEquals($expected, $result);
  69. $result = $this->Format->yesNo('2', ['on' => 2, 'onTitle' => 'foo']);
  70. $expected = '<i class="icon icon-yes fa fa-check" title="foo" data-placement="bottom" data-toggle="tooltip"></i>';
  71. $this->assertTextContains($expected, $result);
  72. $result = $this->Format->yesNo('3', ['on' => 4, 'offTitle' => 'nope']);
  73. $expected = '<i class="icon icon-no fa fa-times" title="nope" data-placement="bottom" data-toggle="tooltip"></i>';
  74. $this->assertEquals($expected, $result);
  75. }
  76. /**
  77. * @return void
  78. */
  79. public function testOk() {
  80. $content = 'xyz';
  81. $data = [
  82. true => '<span class="ok-yes" style="color:green">xyz 1</span>',
  83. false => '<span class="ok-no" style="color:red">xyz 0</span>',
  84. ];
  85. foreach ($data as $value => $expected) {
  86. $result = $this->Format->ok($content . ' ' . (int)$value, $value);
  87. $this->assertEquals($expected, $result);
  88. }
  89. }
  90. /**
  91. * FormatHelperTest::testThumbs()
  92. *
  93. * @return void
  94. */
  95. public function testThumbs() {
  96. $result = $this->Format->thumbs(1);
  97. $expected = '<i class="icon icon-pro fa fa-thumbs-up" title="Pro" data-placement="bottom" data-toggle="tooltip"></i>';
  98. $this->assertEquals($expected, $result);
  99. $result = $this->Format->thumbs(0);
  100. $expected = '<i class="icon icon-contra fa fa-thumbs-down" title="Contra" data-placement="bottom" data-toggle="tooltip"></i>';
  101. $this->assertEquals($expected, $result);
  102. }
  103. /**
  104. * FormatHelperTest::testGenderIcon()
  105. *
  106. * @return void
  107. */
  108. public function testGenderIcon() {
  109. $result = $this->Format->genderIcon(0);
  110. $expected = '<i class="icon icon-genderless fa fa-genderless" title="Inter" data-placement="bottom" data-toggle="tooltip"></i>';
  111. $this->assertEquals($expected, $result);
  112. $result = $this->Format->genderIcon(1);
  113. $expected = '<i class="icon icon-male fa fa-mars" title="Male" data-placement="bottom" data-toggle="tooltip"></i>';
  114. $this->assertEquals($expected, $result);
  115. $result = $this->Format->genderIcon(2);
  116. $expected = '<i class="icon icon-female fa fa-venus" title="Female" data-placement="bottom" data-toggle="tooltip"></i>';
  117. $this->assertEquals($expected, $result);
  118. }
  119. /**
  120. * @return void
  121. */
  122. public function testPad() {
  123. $result = $this->Format->pad('foo bär', 20, '-');
  124. $expected = 'foo bär-------------';
  125. $this->assertEquals($expected, $result);
  126. $result = $this->Format->pad('foo bär', 20, '-', STR_PAD_LEFT);
  127. $expected = '-------------foo bär';
  128. $this->assertEquals($expected, $result);
  129. }
  130. /**
  131. * FormatHelperTest::testSiteIcon()
  132. *
  133. * @return void
  134. */
  135. public function testSiteIcon() {
  136. $result = $this->Format->siteIcon('http://www.example.org');
  137. $this->debug($result);
  138. $expected = '<img src="http://www.google.com/s2/favicons?domain=www.example.org';
  139. $this->assertStringContainsString($expected, $result);
  140. }
  141. /**
  142. * @return void
  143. */
  144. public function testSlug() {
  145. $result = $this->Format->slug('A Baz D & Foo');
  146. $this->assertSame('A-Baz-D-Foo', $result);
  147. $this->Format->setConfig('slugger', [Text::class, 'slug']);
  148. $result = $this->Format->slug('A Baz D & Foo');
  149. $this->assertSame('A-Baz-D-Foo', $result);
  150. }
  151. /**
  152. * @return void
  153. */
  154. public function testSlugCustomObject() {
  155. $this->Format->setConfig('slugger', [$this, '_testSlugger']);
  156. $result = $this->Format->slug('A Baz D & Foo');
  157. $this->assertSame('a baz d & foo', $result);
  158. }
  159. /**
  160. * @param string $name
  161. *
  162. * @return string
  163. */
  164. public function _testSlugger($name) {
  165. return mb_strtolower($name);
  166. }
  167. /**
  168. * @return void
  169. */
  170. public function testNeighbors() {
  171. $this->skipIf(true, '//TODO');
  172. $neighbors = [
  173. 'prev' => ['id' => 1, 'foo' => 'bar'],
  174. 'next' => ['id' => 2, 'foo' => 'y'],
  175. ];
  176. $result = $this->Format->neighbors($neighbors, 'foo');
  177. $expected = '<div class="next-prev-navi"><a href="/index/1" title="bar"><i class="icon icon-prev fa fa-arrow-left" 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-arrow-right" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;nextRecord</a></div>';
  178. $this->assertEquals($expected, $result);
  179. }
  180. /**
  181. * Test slug generation works with new slugger.
  182. *
  183. * @return void
  184. */
  185. public function testSlugGenerationWithNewSlugger() {
  186. $neighbors = [
  187. 'prev' => ['id' => 1, 'foo' => 'My Foo'],
  188. 'next' => ['id' => 2, 'foo' => 'My FooBaz'],
  189. ];
  190. // Only needed for fake requests (tests)
  191. $url = ['controller' => 'MyController', 'action' => 'myAction'];
  192. $result = $this->Format->neighbors($neighbors, 'foo', ['slug' => true, 'url' => $url]);
  193. $expected = '<div class="next-prev-navi nextPrevNavi"><a href="/my-controller/my-action/1/My-Foo" title="My Foo"><i class="icon icon-prev fa fa-arrow-left" title="Prev" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/my-controller/my-action/2/My-FooBaz" title="My FooBaz"><i class="icon icon-next fa fa-arrow-right" title="Next" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;nextRecord</a></div>';
  194. $this->assertEquals($expected, $result);
  195. }
  196. /**
  197. * @return void
  198. */
  199. public function testTab2space() {
  200. $text = "foo\t\tfoobar\tbla\n";
  201. $text .= "fooo\t\tbar\t\tbla\n";
  202. $text .= "foooo\t\tbar\t\tbla\n";
  203. $result = $this->Format->tab2space($text);
  204. $expected = <<<TXT
  205. foo foobar bla
  206. fooo bar bla
  207. foooo bar bla
  208. TXT;
  209. $this->assertTextEquals($expected, $result);
  210. $this->assertTrue(strpos($result, "\t") === false);
  211. }
  212. /**
  213. * @return void
  214. */
  215. public function testArray2table() {
  216. $array = [
  217. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  218. ['1', '2', '3'],
  219. ['4', '5', '6'],
  220. ];
  221. $is = $this->Format->array2table($array);
  222. $this->assertTextContains('<table class="table">', $is);
  223. $this->assertTextContains('</table>', $is);
  224. $this->assertTextContains('<th>', $is);
  225. // recursive
  226. $array = [
  227. ['a' => ['2'], 'b' => ['2'], 'c' => ['2']],
  228. [['2'], ['2'], ['2']],
  229. [['2'], ['2'], [['s' => '3', 't' => '4']]],
  230. ];
  231. $is = $this->Format->array2table($array, ['recursive' => true]);
  232. $expected = <<<TEXT
  233. <table class="table">
  234. <tr><th>a</th><th>b</th><th>c</th></tr>
  235. <tr><td>
  236. <table class="table">
  237. <tr><th>0</th></tr>
  238. <tr><td>2</td></tr>
  239. </table>
  240. </td><td>
  241. <table class="table">
  242. <tr><th>0</th></tr>
  243. <tr><td>2</td></tr>
  244. </table>
  245. </td><td>
  246. <table class="table">
  247. <tr><th>0</th></tr>
  248. <tr><td>2</td></tr>
  249. </table>
  250. </td></tr>
  251. <tr><td>
  252. <table class="table">
  253. <tr><th>0</th></tr>
  254. <tr><td>2</td></tr>
  255. </table>
  256. </td><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></tr>
  267. <tr><td>
  268. <table class="table">
  269. <tr><th>0</th></tr>
  270. <tr><td>2</td></tr>
  271. </table>
  272. </td><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>s</th><th>t</th></tr>
  280. <tr><td>3</td><td>4</td></tr>
  281. </table>
  282. </td></tr>
  283. </table>
  284. TEXT;
  285. $this->assertTextEquals($expected, $is);
  286. $array = [
  287. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  288. ['1', '2', '3'],
  289. ];
  290. $options = [
  291. 'heading' => false,
  292. ];
  293. $attributes = [
  294. 'class' => 'foo',
  295. 'data-x' => 'y',
  296. ];
  297. $is = $this->Format->array2table($array, $options, $attributes);
  298. $this->assertTextContains('<table class="foo" data-x="y">', $is);
  299. $this->assertTextContains('</table>', $is);
  300. $this->assertTextNotContains('<th>', $is);
  301. }
  302. /**
  303. * @return void
  304. */
  305. public function tearDown(): void {
  306. parent::tearDown();
  307. unset($this->Format);
  308. }
  309. }