FormatHelperTest.php 8.7 KB

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