FormatHelperTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. App::uses('FormatHelper', 'Tools.View/Helper');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. App::uses('HtmlHelper', '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 HtmlHelper(new View(null));
  15. }
  16. /**
  17. */
  18. public function testDisabledLink() {
  19. $content = 'xyz';
  20. $data = array(
  21. array(),
  22. array('class' => 'disabledLink', 'title' => false),
  23. array('class' => 'helloClass', 'title' => 'helloTitle')
  24. );
  25. foreach ($data as $key => $value) {
  26. $res = $this->Format->disabledLink($content, $value);
  27. //echo ''.$res.' (\''.h($res).'\')';
  28. $this->assertTrue(!empty($res));
  29. }
  30. }
  31. /**
  32. * @return void
  33. */
  34. public function testWarning() {
  35. $content = 'xyz';
  36. $data = array(
  37. true,
  38. false
  39. );
  40. foreach ($data as $key => $value) {
  41. $res = $this->Format->warning($content . ' ' . (int)$value, $value);
  42. //echo ''.$res.'';
  43. $this->assertTrue(!empty($res));
  44. }
  45. }
  46. /**
  47. * FormatHelperTest::testIcon()
  48. *
  49. * @return void
  50. */
  51. public function testIcon() {
  52. $result = $this->Format->icon('edit');
  53. $expected = '<img src="/img/icons/edit.gif" title="' . __('Edit') . '" alt="[' . __('Edit') . ']" class="icon" />';
  54. $this->assertEquals($expected, $result);
  55. }
  56. /**
  57. * FormatHelperTest::testIconWithFontIcon()
  58. *
  59. * @return void
  60. */
  61. public function testIconWithFontIcon() {
  62. $this->Format->settings['fontIcons'] = array('edit' => 'fa fa-pencil');
  63. $result = $this->Format->icon('edit');
  64. $expected = '<i class="fa fa-pencil edit" title="' . __('Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  65. $this->assertEquals($expected, $result);
  66. }
  67. /**
  68. * FormatHelperTest::testSpeedOfIcons()
  69. *
  70. * @return void
  71. */
  72. public function testSpeedOfIcons() {
  73. $count = 1000;
  74. $time1 = microtime(true);
  75. for ($i = 0; $i < $count; $i++) {
  76. $result = $this->Format->icon('edit');
  77. }
  78. $time2 = microtime(true);
  79. $this->Format->settings['fontIcons'] = array('edit' => 'fa fa-pencil');
  80. $time3 = microtime(true);
  81. for ($i = 0; $i < $count; $i++) {
  82. $result = $this->Format->icon('edit');
  83. }
  84. $time4 = microtime(true);
  85. $normalIconSpeed = number_format($time2 - $time1, 2);
  86. $this->debug('Normal Icons: ' . $normalIconSpeed);
  87. $fontIconViaStringTemplateSpeed = number_format($time4 - $time3, 2);
  88. $this->debug('StringTemplate and Font Icons: ' . $fontIconViaStringTemplateSpeed);
  89. $this->assertTrue($fontIconViaStringTemplateSpeed < $normalIconSpeed);
  90. }
  91. /**
  92. * @return void
  93. */
  94. public function testFontIcon() {
  95. $result = $this->Format->fontIcon('signin');
  96. $expected = '<i class="icon-signin"></i>';
  97. $this->assertEquals($expected, $result);
  98. $result = $this->Format->fontIcon('signin', array('rotate' => 90));
  99. $expected = '<i class="icon-signin icon-rotate-90"></i>';
  100. $this->assertEquals($expected, $result);
  101. $result = $this->Format->fontIcon('signin', array('size' => 5, 'extra' => array('muted')));
  102. $expected = '<i class="icon-signin icon-muted icon-5x"></i>';
  103. $this->assertEquals($expected, $result);
  104. }
  105. /**
  106. */
  107. public function testOk() {
  108. $content = 'xyz';
  109. $data = array(
  110. true,
  111. false
  112. );
  113. foreach ($data as $key => $value) {
  114. $res = $this->Format->ok($content . ' ' . (int)$value, $value);
  115. //echo ''.$res.'';
  116. $this->assertTrue(!empty($res));
  117. }
  118. }
  119. public function testPriorityIcon() {
  120. $values = array(
  121. array(1, array(), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  122. array(2, array(), '<div class="prio-lower" title="prioLower">&nbsp;</div>'),
  123. array(3, array(), ''),
  124. array(3, array('normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
  125. array(4, array(), '<div class="prio-higher" title="prioHigher">&nbsp;</div>'),
  126. array(5, array(), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  127. array(1, array('max' => 3), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  128. array(2, array('max' => 3), ''),
  129. array(2, array('max' => 3, 'normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
  130. array(3, array('max' => 3), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  131. array(0, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  132. array(1, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), ''),
  133. array(2, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  134. );
  135. foreach ($values as $key => $value) {
  136. $res = $this->Format->priorityIcon($value[0], $value[1]);
  137. //echo $key;
  138. //debug($res, null, false);
  139. $this->assertEquals($value[2], $res);
  140. }
  141. }
  142. /**
  143. */
  144. public function testShortenText() {
  145. $data = array(
  146. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf',
  147. '122 jsdf sjdkf sdfj sdf',
  148. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
  149. '\';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>'
  150. );
  151. foreach ($data as $key => $value) {
  152. $res = $this->Format->shortenText($value, 30);
  153. //echo '\''.h($value).'\' becomes \''.$res.'\'';
  154. $this->assertTrue(!empty($res));
  155. }
  156. }
  157. /**
  158. */
  159. public function testTruncate() {
  160. $data = array(
  161. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf' => 'dfssdfsdj sdkfj sdkfj ksdfj s' . "\xe2\x80\xa6",
  162. '122 jsdf sjdkf sdfj sdf' => '122 jsdf sjdkf sdfj sdf',
  163. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' => 'ddddddddddddddddddddddddddddd' . "\xe2\x80\xa6",
  164. '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"
  165. );
  166. foreach ($data as $value => $expected) {
  167. $res = $this->Format->truncate($value, 30, array('html' => true));
  168. //debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false);
  169. $res = $this->Format->truncate($value, 30, array('html' => true));
  170. //debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false);
  171. //$this->assertTrue(!empty($res));
  172. $this->assertEquals($expected, $res);
  173. }
  174. }
  175. /**
  176. */
  177. public function testHideEmail() {
  178. $mails = array(
  179. 'test@test.de' => 't..t@t..t.de',
  180. 'xx@yy.de' => 'x..x@y..y.de',
  181. 'erk-wf@ve-eeervdg.com' => 'e..f@v..g.com',
  182. );
  183. foreach ($mails as $mail => $expected) {
  184. $res = $this->Format->hideEmail($mail);
  185. //echo '\''.$mail.'\' becomes \''.$res.'\' - expected \''.$expected.'\'';
  186. $this->assertEquals($expected, $res);
  187. }
  188. }
  189. /**
  190. */
  191. public function testWordCensor() {
  192. $data = array(
  193. 'dfssdfsdj sdkfj sdkfj ksdfj bitch ksdfj' => 'dfssdfsdj sdkfj sdkfj ksdfj ##### ksdfj',
  194. '122 jsdf ficken Sjdkf sdfj sdf' => '122 jsdf ###### Sjdkf sdfj sdf',
  195. '122 jsdf FICKEN sjdkf sdfjs sdf' => '122 jsdf ###### sjdkf sdfjs sdf',
  196. 'dddddddddd ARSCH ddddddddddddd' => 'dddddddddd ##### ddddddddddddd',
  197. //'\';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
  198. );
  199. foreach ($data as $value => $expected) {
  200. $res = $this->Format->wordCensor($value, array('Arsch', 'Ficken', 'Bitch'));
  201. //debug('\''.h($value).'\' becomes \''.h($res).'\'', null, false);
  202. $this->assertEquals($expected === null ? $value : $expected, $res);
  203. }
  204. }
  205. /**
  206. */
  207. /*
  208. public function testReverseAscii() {
  209. $is = $this->Format->reverseAscii('f&eacute;s');
  210. $expected = 'fés';
  211. $this->assertEquals($expected, $is);
  212. $is = entDec('f&eacute;s');
  213. $expected = 'fés';
  214. $this->assertEquals($expected, $is);
  215. $is = html_entity_decode('f&eacute;s');
  216. $expected = 'fés';
  217. $this->assertEquals($expected, $is);
  218. #TODO: correct it + more
  219. }
  220. */
  221. /**
  222. */
  223. /*
  224. public function testDecodeEntities() {
  225. $is = $this->Format->decodeEntities('f&eacute;s');
  226. $expected = 'fés';
  227. $this->assertEquals($expected, $is);
  228. }
  229. */
  230. public function testTab2space() {
  231. //echo '<h2>'.__FUNCTION__.'</h2>';
  232. $text = "foo\t\tfoobar\tbla\n";
  233. $text .= "fooo\t\tbar\t\tbla\n";
  234. $text .= "foooo\t\tbar\t\tbla\n";
  235. //echo "<pre>" . $text . "</pre>";
  236. //echo'becomes';
  237. //echo "<pre>" . $this->Format->tab2space($text) . "</pre>";
  238. }
  239. public function testArray2table() {
  240. //echo '<h2>'.__FUNCTION__.'</h2>';
  241. $array = array(
  242. array('x' => '0', 'y' => '0.5', 'z' => '0.9'),
  243. array('1', '2', '3'),
  244. array('4', '5', '6'),
  245. );
  246. $is = $this->Format->array2table($array);
  247. //echo $is;
  248. //$this->assertEquals($expected, $is);
  249. // recursive?
  250. $array = array(
  251. array('a' => array('2'), 'b' => array('2'), 'c' => array('2')),
  252. array(array('2'), array('2'), array('2')),
  253. array(array('2'), array('2'), array(array('s' => '3', 't' => '4'))),
  254. );
  255. $is = $this->Format->array2table($array, array('recursive' => true));
  256. //echo $is;
  257. }
  258. public function tearDown() {
  259. parent::tearDown();
  260. unset($this->Format);
  261. }
  262. }