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