FormatHelperTest.php 7.2 KB

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