FormatHelperTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. */
  33. public function testWarning() {
  34. $content = 'xyz';
  35. $data = array(
  36. true,
  37. false
  38. );
  39. foreach ($data as $key => $value) {
  40. $res = $this->Format->warning($content . ' ' . (int)$value, $value);
  41. //echo ''.$res.'';
  42. $this->assertTrue(!empty($res));
  43. }
  44. }
  45. /**
  46. */
  47. public function testFontIcon() {
  48. $result = $this->Format->fontIcon('signin');
  49. $expected = '<i class="icon-signin"></i>';
  50. $this->assertEquals($expected, $result);
  51. $result = $this->Format->fontIcon('signin', array('rotate' => 90));
  52. $expected = '<i class="icon-signin icon-rotate-90"></i>';
  53. $this->assertEquals($expected, $result);
  54. $result = $this->Format->fontIcon('signin', array('size' => 5, 'extra' => array('muted')));
  55. $expected = '<i class="icon-signin icon-muted icon-5x"></i>';
  56. $this->assertEquals($expected, $result);
  57. }
  58. /**
  59. */
  60. public function testOk() {
  61. $content = 'xyz';
  62. $data = array(
  63. true,
  64. false
  65. );
  66. foreach ($data as $key => $value) {
  67. $res = $this->Format->ok($content . ' ' . (int)$value, $value);
  68. //echo ''.$res.'';
  69. $this->assertTrue(!empty($res));
  70. }
  71. }
  72. public function testPriorityIcon() {
  73. $values = array(
  74. array(1, array(), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  75. array(2, array(), '<div class="prio-lower" title="prioLower">&nbsp;</div>'),
  76. array(3, array(), ''),
  77. array(3, array('normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
  78. array(4, array(), '<div class="prio-higher" title="prioHigher">&nbsp;</div>'),
  79. array(5, array(), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  80. array(1, array('max' => 3), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  81. array(2, array('max' => 3), ''),
  82. array(2, array('max' => 3, 'normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
  83. array(3, array('max' => 3), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  84. array(0, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  85. array(1, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), ''),
  86. array(2, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  87. );
  88. foreach ($values as $key => $value) {
  89. $res = $this->Format->priorityIcon($value[0], $value[1]);
  90. //echo $key;
  91. //debug($res, null, false);
  92. $this->assertEquals($value[2], $res);
  93. }
  94. }
  95. /**
  96. */
  97. public function testShortenText() {
  98. $data = array(
  99. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf',
  100. '122 jsdf sjdkf sdfj sdf',
  101. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
  102. '\';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>'
  103. );
  104. foreach ($data as $key => $value) {
  105. $res = $this->Format->shortenText($value, 30);
  106. //echo '\''.h($value).'\' becomes \''.$res.'\'';
  107. $this->assertTrue(!empty($res));
  108. }
  109. }
  110. /**
  111. */
  112. public function testTruncate() {
  113. $data = array(
  114. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf' => 'dfssdfsdj sdkfj sdkfj ksdfj s' . "\xe2\x80\xa6",
  115. '122 jsdf sjdkf sdfj sdf' => '122 jsdf sjdkf sdfj sdf',
  116. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' => 'ddddddddddddddddddddddddddddd' . "\xe2\x80\xa6",
  117. '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"
  118. );
  119. foreach ($data as $value => $expected) {
  120. $res = $this->Format->truncate($value, 30, array('html' => true));
  121. //debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false);
  122. $res = $this->Format->truncate($value, 30, array('html' => true));
  123. //debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false);
  124. //$this->assertTrue(!empty($res));
  125. $this->assertEquals($expected, $res);
  126. }
  127. }
  128. /**
  129. */
  130. public function testHideEmail() {
  131. $mails = array(
  132. 'test@test.de' => 't..t@t..t.de',
  133. 'xx@yy.de' => 'x..x@y..y.de',
  134. 'erk-wf@ve-eeervdg.com' => 'e..f@v..g.com',
  135. );
  136. foreach ($mails as $mail => $expected) {
  137. $res = $this->Format->hideEmail($mail);
  138. //echo '\''.$mail.'\' becomes \''.$res.'\' - expected \''.$expected.'\'';
  139. $this->assertEquals($expected, $res);
  140. }
  141. }
  142. /**
  143. */
  144. public function testWordCensor() {
  145. $data = array(
  146. 'dfssdfsdj sdkfj sdkfj ksdfj bitch ksdfj' => 'dfssdfsdj sdkfj sdkfj ksdfj ##### ksdfj',
  147. '122 jsdf ficken Sjdkf sdfj sdf' => '122 jsdf ###### Sjdkf sdfj sdf',
  148. '122 jsdf FICKEN sjdkf sdfjs sdf' => '122 jsdf ###### sjdkf sdfjs sdf',
  149. 'dddddddddd ARSCH ddddddddddddd' => 'dddddddddd ##### ddddddddddddd',
  150. //'\';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
  151. );
  152. foreach ($data as $value => $expected) {
  153. $res = $this->Format->wordCensor($value, array('Arsch', 'Ficken', 'Bitch'));
  154. //debug('\''.h($value).'\' becomes \''.h($res).'\'', null, false);
  155. $this->assertEquals($expected === null ? $value : $expected, $res);
  156. }
  157. }
  158. /**
  159. */
  160. /*
  161. public function testReverseAscii() {
  162. $is = $this->Format->reverseAscii('f&eacute;s');
  163. $expected = 'fés';
  164. $this->assertEquals($expected, $is);
  165. $is = entDec('f&eacute;s');
  166. $expected = 'fés';
  167. $this->assertEquals($expected, $is);
  168. $is = html_entity_decode('f&eacute;s');
  169. $expected = 'fés';
  170. $this->assertEquals($expected, $is);
  171. #TODO: correct it + more
  172. }
  173. */
  174. /**
  175. */
  176. /*
  177. public function testDecodeEntities() {
  178. $is = $this->Format->decodeEntities('f&eacute;s');
  179. $expected = 'fés';
  180. $this->assertEquals($expected, $is);
  181. }
  182. */
  183. public function testTab2space() {
  184. //echo '<h2>'.__FUNCTION__.'</h2>';
  185. $text = "foo\t\tfoobar\tbla\n";
  186. $text .= "fooo\t\tbar\t\tbla\n";
  187. $text .= "foooo\t\tbar\t\tbla\n";
  188. //echo "<pre>" . $text . "</pre>";
  189. //echo'becomes';
  190. //echo "<pre>" . $this->Format->tab2space($text) . "</pre>";
  191. }
  192. public function testArray2table() {
  193. //echo '<h2>'.__FUNCTION__.'</h2>';
  194. $array = array(
  195. array('x' => '0', 'y' => '0.5', 'z' => '0.9'),
  196. array('1', '2', '3'),
  197. array('4', '5', '6'),
  198. );
  199. $is = $this->Format->array2table($array);
  200. //echo $is;
  201. //$this->assertEquals($expected, $is);
  202. // recursive?
  203. $array = array(
  204. array('a' => array('2'), 'b' => array('2'), 'c' => array('2')),
  205. array(array('2'), array('2'), array('2')),
  206. array(array('2'), array('2'), array(array('s' => '3', 't' => '4'))),
  207. );
  208. $is = $this->Format->array2table($array, array('recursive' => true));
  209. //echo $is;
  210. }
  211. public function tearDown() {
  212. parent::tearDown();
  213. unset($this->Format);
  214. }
  215. }