FormatHelperTest.php 16 KB

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