FormatHelperTest.php 16 KB

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