FormatHelperTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <?php
  2. App::uses('FormatHelper', 'Tools.View/Helper');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. App::uses('HtmlExtHelper', 'Tools.View/Helper');
  5. App::uses('View', 'View');
  6. /**
  7. * Datetime Test Case
  8. */
  9. class FormatHelperTest extends MyCakeTestCase {
  10. public $fixtures = ['core.cake_session'];
  11. public $Format;
  12. public function setUp() {
  13. parent::setUp();
  14. Configure::delete('Format');
  15. $this->Format = new FormatHelper(new View(null));
  16. $this->Format->Html = new HtmlExtHelper(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 = '<img src="/img/icons/edit.gif" title="' . __d('tools', 'Edit') . '" alt="[' . __d('tools', 'Edit') . ']" class="icon"/>';
  57. $this->assertEquals($expected, $result);
  58. }
  59. /**
  60. * FormatHelperTest::testCIcon()
  61. *
  62. * @return void
  63. */
  64. public function testCIcon() {
  65. $result = $this->Format->cIcon('edit.png');
  66. $expected = '<img src="/img/icons/edit.png" title="' . __d('tools', 'Edit') . '" alt="[' . __d('tools', 'Edit') . ']" class="icon"/>';
  67. $this->assertEquals($expected, $result);
  68. }
  69. /**
  70. * FormatHelperTest::testIconWithFontIcon()
  71. *
  72. * @return void
  73. */
  74. public function testIconWithFontIcon() {
  75. $this->Format->settings['fontIcons'] = ['edit' => 'fa fa-pencil'];
  76. $result = $this->Format->icon('edit');
  77. $expected = '<i class="fa fa-pencil edit" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  78. $this->assertEquals($expected, $result);
  79. }
  80. /**
  81. * FormatHelperTest::testCIconWithFontIcon()
  82. *
  83. * @return void
  84. */
  85. public function testCIconWithFontIcon() {
  86. $this->Format->settings['fontIcons'] = ['edit' => 'fa fa-pencil'];
  87. $result = $this->Format->cIcon('edit.png');
  88. $expected = '<i class="fa fa-pencil edit" title="' . __d('tools', 'Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  89. $this->assertEquals($expected, $result);
  90. }
  91. /**
  92. * FormatHelperTest::testSpeedOfIcons()
  93. *
  94. * @return void
  95. */
  96. public function testSpeedOfIcons() {
  97. $count = 1000;
  98. $time1 = microtime(true);
  99. for ($i = 0; $i < $count; $i++) {
  100. $result = $this->Format->icon('edit');
  101. }
  102. $time2 = microtime(true);
  103. $this->Format->settings['fontIcons'] = ['edit' => 'fa fa-pencil'];
  104. $time3 = microtime(true);
  105. for ($i = 0; $i < $count; $i++) {
  106. $result = $this->Format->icon('edit');
  107. }
  108. $time4 = microtime(true);
  109. $normalIconSpeed = number_format($time2 - $time1, 2);
  110. $this->debug('Normal Icons: ' . $normalIconSpeed);
  111. $fontIconViaStringTemplateSpeed = number_format($time4 - $time3, 2);
  112. $this->debug('StringTemplate and Font Icons: ' . $fontIconViaStringTemplateSpeed);
  113. $this->assertTrue($fontIconViaStringTemplateSpeed < $normalIconSpeed);
  114. }
  115. /**
  116. * @return void
  117. */
  118. public function testFontIcon() {
  119. $result = $this->Format->fontIcon('signin');
  120. $expected = '<i class="fa fa-signin"></i>';
  121. $this->assertEquals($expected, $result);
  122. $result = $this->Format->fontIcon('signin', ['rotate' => 90]);
  123. $expected = '<i class="fa fa-signin fa-rotate-90"></i>';
  124. $this->assertEquals($expected, $result);
  125. $result = $this->Format->fontIcon('signin', ['size' => 5, 'extra' => ['muted']]);
  126. $expected = '<i class="fa fa-signin fa-muted fa-5x"></i>';
  127. $this->assertEquals($expected, $result);
  128. $result = $this->Format->fontIcon('asterisk', ['namespace' => 'glyphicon']);
  129. $expected = '<i class="glyphicon glyphicon-asterisk"></i>';
  130. $this->assertEquals($expected, $result);
  131. $result = $this->Format->fontIcon('signin', ['size' => 5, 'extra' => ['muted'], 'namespace' => 'icon']);
  132. $expected = '<i class="icon icon-signin icon-muted icon-5x"></i>';
  133. $this->assertEquals($expected, $result);
  134. }
  135. /**
  136. * @return void
  137. */
  138. public function testYesNo() {
  139. $result = $this->Format->yesNo(true);
  140. $expected = '<img src="/img/icons/yes.gif" title="' . __d('tools', 'Yes') . '" alt=';
  141. $this->assertTextContains($expected, $result);
  142. $result = $this->Format->yesNo(false);
  143. $expected = '<img src="/img/icons/no.gif" title="' . __d('tools', 'No') . '" alt=';
  144. $this->assertTextContains($expected, $result);
  145. $result = $this->Format->yesNo(true, ['on' => 1, 'onTitle' => 'foo']);
  146. $expected = '<img src="/img/icons/yes.gif" title="foo" alt=';
  147. $this->assertTextContains($expected, $result);
  148. $this->Format->settings['fontIcons'] = [
  149. 'yes' => 'fa fa-check',
  150. 'no' => 'fa fa-times'];
  151. $result = $this->Format->yesNo(true);
  152. $expected = '<i class="fa fa-check yes" title="' . __d('tools', 'Yes') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  153. $this->assertEquals($expected, $result);
  154. $result = $this->Format->yesNo(false);
  155. $expected = '<i class="fa fa-times no" title="' . __d('tools', 'No') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  156. $this->assertEquals($expected, $result);
  157. }
  158. /**
  159. * @return void
  160. */
  161. public function testOk() {
  162. $content = 'xyz';
  163. $data = [
  164. true,
  165. false
  166. ];
  167. foreach ($data as $key => $value) {
  168. $res = $this->Format->ok($content . ' ' . (int)$value, $value);
  169. //echo ''.$res.'';
  170. $this->assertTrue(!empty($res));
  171. }
  172. }
  173. /**
  174. * FormatHelperTest::testThumbs()
  175. *
  176. * @return void
  177. */
  178. public function testThumbs() {
  179. $result = $this->Format->thumbs(1);
  180. }
  181. /**
  182. * FormatHelperTest::testGenderIcon()
  183. *
  184. * @return void
  185. */
  186. public function testGenderIcon() {
  187. $result = $this->Format->genderIcon();
  188. }
  189. /**
  190. * FormatHelperTest::testShowStars()
  191. *
  192. * @return void
  193. */
  194. public function testShowStars() {
  195. $result = $this->Format->showStars(1, 3);
  196. $expected = '<span class="star-bar';
  197. $this->assertContains($expected, $result);
  198. }
  199. /**
  200. * FormatHelperTest::testTextAsImage()
  201. *
  202. * @return void
  203. */
  204. public function testTextAsImage() {
  205. $command = 'convert';
  206. exec($command, $a, $r);
  207. $this->skipIf($r !== 0, 'convert / imagick is not available');
  208. $result = $this->Format->textAsImage('foo bar');
  209. $expected = '<img src="data:image/png;base64,';
  210. $this->assertContains($expected, $result);
  211. }
  212. /**
  213. * FormatHelperTest::testLanguageFlags()
  214. *
  215. * @return void
  216. */
  217. public function testLanguageFlags() {
  218. $result = $this->Format->languageFlags();
  219. $this->debug($result);
  220. }
  221. /**
  222. * FormatHelperTest::testTipHelp()
  223. *
  224. * @return void
  225. */
  226. public function testTipHelp() {
  227. $result = $this->Format->tipHelp('foo');
  228. $this->debug($result);
  229. $expected = '<img src="/img/icons/help.gif"';
  230. $this->assertContains($expected, $result);
  231. }
  232. /**
  233. * FormatHelperTest::testPad()
  234. *
  235. * @return void
  236. */
  237. public function testPad() {
  238. $result = $this->Format->pad('foo bar', 20, '-');
  239. $expected = 'foo bar-------------';
  240. $this->assertEquals($expected, $result);
  241. $result = $this->Format->pad('foo bar', 20, '-', STR_PAD_LEFT);
  242. $expected = '-------------foo bar';
  243. $this->assertEquals($expected, $result);
  244. }
  245. /**
  246. * FormatHelperTest::testOnlineIcon()
  247. *
  248. * @return void
  249. */
  250. public function testOnlineIcon() {
  251. $result = $this->Format->onlineIcon();
  252. $this->debug($result);
  253. $expected = '<img src="/img/misc/healthbar0.gif"';
  254. $this->assertContains($expected, $result);
  255. }
  256. /**
  257. * FormatHelperTest::testStatusLight()
  258. *
  259. * @return void
  260. */
  261. public function testStatusLight() {
  262. $result = $this->Format->statusLight();
  263. $this->debug($result);
  264. $expected = '<img src="/img/icons/status_light_blank.gif"';
  265. $this->assertContains($expected, $result);
  266. }
  267. /**
  268. * FormatHelperTest::testProgressBar()
  269. *
  270. * @return void
  271. */
  272. public function testProgressBar() {
  273. $result = $this->Format->progressBar(14);
  274. $this->debug($result);
  275. }
  276. /**
  277. * FormatHelperTest::testAbsolutePaginateCount()
  278. *
  279. * @return void
  280. */
  281. public function testAbsolutePaginateCount() {
  282. $paginator = [
  283. 'page' => 1,
  284. 'pageCount' => 3,
  285. 'count' => 25,
  286. 'limit' => 10
  287. ];
  288. $result = $this->Format->absolutePaginateCount($paginator, 2);
  289. $this->debug($result);
  290. $this->assertEquals(2, $result);
  291. }
  292. /**
  293. * FormatHelperTest::testSiteIcon()
  294. *
  295. * @return void
  296. */
  297. public function testSiteIcon() {
  298. $result = $this->Format->siteIcon('http://www.example.org');
  299. $this->debug($result);
  300. $expected = '<img src="http://www.google.com/s2/favicons?domain=www.example.org';
  301. $this->assertContains($expected, $result);
  302. }
  303. /**
  304. * FormatHelperTest::testEncodeEmails()
  305. *
  306. * @return void
  307. */
  308. public function testEncodeEmail() {
  309. $result = $this->Format->encodeEmail('foobar@somedomain.com');
  310. $this->debug($result);
  311. $expected = '<span>@</span>';
  312. $this->assertContains($expected, $result);
  313. }
  314. /**
  315. * FormatHelperTest::testEncodeEmailUrl()
  316. *
  317. * @return void
  318. */
  319. public function testEncodeEmailUrl() {
  320. $result = $this->Format->encodeEmailUrl('foobar@somedomain.com');
  321. $this->debug($result);
  322. $expected = '<script language=javascript>';
  323. $this->assertContains($expected, $result);
  324. }
  325. /**
  326. * FormatHelperTest::testEncodeText()
  327. *
  328. * @return void
  329. */
  330. public function testEncodeText() {
  331. $result = $this->Format->encodeText('foobar@somedomain.com');
  332. $this->debug($result);
  333. $expected = ';&#';
  334. $this->assertContains($expected, $result);
  335. }
  336. /**
  337. * FormatHelperTest::testConfigure()
  338. *
  339. * @return void
  340. */
  341. public function testNeighbors() {
  342. $neighbors = [
  343. 'prev' => ['ModelName' => ['id' => 1, 'foo' => 'bar']],
  344. 'next' => ['ModelName' => ['id' => 2, 'foo' => 'y']],
  345. ];
  346. $result = $this->Format->neighbors($neighbors, 'foo');
  347. $expected = '<div class="next-prev-navi nextPrevNavi"><a href="/index/1" title="bar"><img src="/img/icons/nav_back.png" alt="[]" class="icon"/>&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/index/2" title="y"><img src="/img/icons/nav_forward.png" alt="[]" class="icon"/>&nbsp;nextRecord</a></div>';
  348. $this->assertEquals($expected, $result);
  349. $this->Format->settings['fontIcons'] = [
  350. 'nav_back' => 'fa fa-prev',
  351. 'nav_forward' => 'fa fa-next'];
  352. $result = $this->Format->neighbors($neighbors, 'foo');
  353. $expected = '<div class="next-prev-navi nextPrevNavi"><a href="/index/1" title="bar"><i class="fa fa-prev nav_back" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/index/2" title="y"><i class="fa fa-next nav_forward" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;nextRecord</a></div>';
  354. $this->assertEquals($expected, $result);
  355. }
  356. /**
  357. * FormatHelperTest::testPriorityIcon()
  358. *
  359. * @return void
  360. */
  361. public function testPriorityIcon() {
  362. $values = [
  363. [1, [], '<div class="prio-low" title="prioLow">&nbsp;</div>'],
  364. [2, [], '<div class="prio-lower" title="prioLower">&nbsp;</div>'],
  365. [3, [], ''],
  366. [3, ['normal' => true], '<div class="prio-normal" title="prioNormal">&nbsp;</div>'],
  367. [4, [], '<div class="prio-higher" title="prioHigher">&nbsp;</div>'],
  368. [5, [], '<div class="prio-high" title="prioHigh">&nbsp;</div>'],
  369. [1, ['max' => 3], '<div class="prio-low" title="prioLow">&nbsp;</div>'],
  370. [2, ['max' => 3], ''],
  371. [2, ['max' => 3, 'normal' => true], '<div class="prio-normal" title="prioNormal">&nbsp;</div>'],
  372. [3, ['max' => 3], '<div class="prio-high" title="prioHigh">&nbsp;</div>'],
  373. [0, ['max' => 3, 'map' => [0 => 1, 1 => 2, 2 => 3]], '<div class="prio-low" title="prioLow">&nbsp;</div>'],
  374. [1, ['max' => 3, 'map' => [0 => 1, 1 => 2, 2 => 3]], ''],
  375. [2, ['max' => 3, 'map' => [0 => 1, 1 => 2, 2 => 3]], '<div class="prio-high" title="prioHigh">&nbsp;</div>'],
  376. ];
  377. foreach ($values as $key => $value) {
  378. $res = $this->Format->priorityIcon($value[0], $value[1]);
  379. //echo $key;
  380. //debug($res, null, false);
  381. $this->assertEquals($value[2], $res);
  382. }
  383. }
  384. /**
  385. * @return void
  386. */
  387. public function testShortenText() {
  388. $data = [
  389. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf',
  390. '122 jsdf sjdkf sdfj sdf',
  391. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
  392. '\';alert(String.fromCharCode(88,83,83))//\';alert(String.fromCharCode(88,83,83))//";alert(String.fromCharCode(88,83,83))//\";alert(String.fromCharCode(88,83,83))//--></SCRIPT>">\'><SCRIPT>alert(String.fromCharCode(88,83,83))</SCRIPT>'
  393. ];
  394. foreach ($data as $key => $value) {
  395. $res = $this->Format->shortenText($value, 30);
  396. //echo '\''.h($value).'\' becomes \''.$res.'\'';
  397. $this->assertTrue(!empty($res));
  398. }
  399. }
  400. /**
  401. * @return void
  402. */
  403. public function testTruncate() {
  404. $data = [
  405. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf' => 'dfssdfsdj sdkfj sdkfj ksdfj s' . "\xe2\x80\xa6",
  406. '122 jsdf sjdkf sdfj sdf' => '122 jsdf sjdkf sdfj sdf',
  407. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' => 'ddddddddddddddddddddddddddddd' . "\xe2\x80\xa6",
  408. 'dsfdsf hods hfoéfh oéfhoéfhoéfhoéfhoéfhiu oéfhoéfhdüf s' => 'dsfdsf hods hfoéfh oéfhoéfhoé' . "\xe2\x80\xa6"
  409. ];
  410. foreach ($data as $value => $expected) {
  411. $res = $this->Format->truncate($value, 30, ['html' => true]);
  412. //debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false);
  413. $res = $this->Format->truncate($value, 30, ['html' => true]);
  414. //debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false);
  415. //$this->assertTrue(!empty($res));
  416. $this->assertEquals($expected, $res);
  417. }
  418. }
  419. /**
  420. * @return void
  421. */
  422. public function testHideEmail() {
  423. $mails = [
  424. 'test@test.de' => 't..t@t..t.de',
  425. 'xx@yy.de' => 'x..x@y..y.de',
  426. 'erk-wf@ve-eeervdg.com' => 'e..f@v..g.com',
  427. ];
  428. foreach ($mails as $mail => $expected) {
  429. $res = $this->Format->hideEmail($mail);
  430. //echo '\''.$mail.'\' becomes \''.$res.'\' - expected \''.$expected.'\'';
  431. $this->assertEquals($expected, $res);
  432. }
  433. }
  434. /**
  435. * @return void
  436. */
  437. public function testWordCensor() {
  438. $data = [
  439. 'dfssdfsdj sdkfj sdkfj ksdfj bitch ksdfj' => 'dfssdfsdj sdkfj sdkfj ksdfj ##### ksdfj',
  440. '122 jsdf ficken Sjdkf sdfj sdf' => '122 jsdf ###### Sjdkf sdfj sdf',
  441. '122 jsdf FICKEN sjdkf sdfjs sdf' => '122 jsdf ###### sjdkf sdfjs sdf',
  442. 'dddddddddd ARSCH ddddddddddddd' => 'dddddddddd ##### ddddddddddddd',
  443. ];
  444. foreach ($data as $value => $expected) {
  445. $res = $this->Format->wordCensor($value, ['Arsch', 'Ficken', 'Bitch']);
  446. $this->assertEquals($expected === null ? $value : $expected, $res);
  447. }
  448. $input = 'dfssdfsdj sdkfj sdkfj ksdfj bitch ksdfj';
  449. $result = $this->Format->wordCensor($input, ['Bitch'], '***');
  450. $expected = 'dfssdfsdj sdkfj sdkfj ksdfj *** ksdfj';
  451. $this->assertEquals($expected, $result);
  452. }
  453. /**
  454. * FormatHelperTest::testTab2space()
  455. *
  456. * @return void
  457. */
  458. public function testTab2space() {
  459. $text = "foo\t\tfoobar\tbla\n";
  460. $text .= "fooo\t\tbar\t\tbla\n";
  461. $text .= "foooo\t\tbar\t\tbla\n";
  462. $result = $this->Format->tab2space($text);
  463. //echo "<pre>" . $text . "</pre>";
  464. //echo'becomes';
  465. //echo "<pre>" . $result . "</pre>";
  466. }
  467. /**
  468. * FormatHelperTest::testArray2table()
  469. *
  470. * @return void
  471. */
  472. public function testArray2table() {
  473. $array = [
  474. ['x' => '0', 'y' => '0.5', 'z' => '0.9'],
  475. ['1', '2', '3'],
  476. ['4', '5', '6'],
  477. ];
  478. $is = $this->Format->array2table($array);
  479. //echo $is;
  480. //$this->assertEquals($expected, $is);
  481. // recursive?
  482. $array = [
  483. ['a' => ['2'], 'b' => ['2'], 'c' => ['2']],
  484. [['2'], ['2'], ['2']],
  485. [['2'], ['2'], [['s' => '3', 't' => '4']]],
  486. ];
  487. $is = $this->Format->array2table($array, ['recursive' => true]);
  488. //echo $is;
  489. }
  490. public function tearDown() {
  491. parent::tearDown();
  492. unset($this->Format);
  493. }
  494. }