FormatHelperTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. App::import('Helper', 'Tools.Format');
  3. App::uses('MyCakeTestCase', 'Tools.Lib');
  4. App::import('Helper', 'Html');
  5. App::uses('View', 'View');
  6. /**
  7. * Datetime Test Case
  8. *
  9. * @package cake.tests
  10. * @subpackage cake.tests.cases.libs.view.helpers
  11. */
  12. class FormatHelperTest extends MyCakeTestCase {
  13. /**
  14. * setUp method
  15. * @access public
  16. * @return void
  17. */
  18. public function setUp() {
  19. $this->Format = new FormatHelper(new View(null));
  20. $this->Format->Html = new HtmlHelper(new View(null));
  21. }
  22. /**
  23. * 2009-08-30 ms
  24. */
  25. public function testDisabledLink() {
  26. $content = 'xyz';
  27. $data = array(
  28. array(),
  29. array('class'=>'disabledLink', 'title'=>false),
  30. array('class'=>'helloClass', 'title'=>'helloTitle')
  31. );
  32. foreach ($data as $key => $value) {
  33. $res = $this->Format->disabledLink($content, $value);
  34. echo ''.$res.' (\''.h($res).'\')';
  35. $this->assertTrue(!empty($res));
  36. }
  37. }
  38. /**
  39. * 2009-08-30 ms
  40. */
  41. public function testWarning() {
  42. $content = 'xyz';
  43. $data = array(
  44. true,
  45. false
  46. );
  47. foreach ($data as $key => $value) {
  48. $res = $this->Format->warning($content.' '.(int)$value, $value);
  49. echo ''.$res.'';
  50. $this->assertTrue(!empty($res));
  51. }
  52. }
  53. /**
  54. * 2009-08-30 ms
  55. */
  56. public function testOk() {
  57. $content = 'xyz';
  58. $data = array(
  59. true,
  60. false
  61. );
  62. foreach ($data as $key => $value) {
  63. $res = $this->Format->ok($content.' '.(int)$value, $value);
  64. echo ''.$res.'';
  65. $this->assertTrue(!empty($res));
  66. }
  67. }
  68. /**
  69. * 2009-08-30 ms
  70. */
  71. public function testShortenText() {
  72. $data = array(
  73. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf',
  74. '122 jsdf sjdkf sdfj sdf',
  75. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
  76. '\';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>'
  77. );
  78. foreach ($data as $key => $value) {
  79. $res = $this->Format->shortenText($value, 30);
  80. echo '\''.h($value).'\' becomes \''.$res.'\'';
  81. $this->assertTrue(!empty($res));
  82. }
  83. }
  84. /**
  85. * 2009-08-30 ms
  86. */
  87. public function testTruncate() {
  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->truncate($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 testHideEmail() {
  104. $mails = array(
  105. 'test@test.de' => 't..t@t..t.de',
  106. 'xx@yy.de' => 'x..x@y..y.de',
  107. 'erk-wf@ve-eeervdg.com' => 'e..f@v..g.com',
  108. );
  109. foreach ($mails as $mail => $expected) {
  110. $res = $this->Format->hideEmail($mail);
  111. echo '\''.$mail.'\' becomes \''.$res.'\' - expected \''.$expected.'\'';
  112. $this->assertEquals($res, $expected);
  113. }
  114. }
  115. /**
  116. * 2009-08-30 ms
  117. */
  118. public function testWordCensor() {
  119. $data = array(
  120. 'dfssdfsdj sdkfj sdkfj ksdfj arsch ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf',
  121. '122 jsdf ficken Sjdkf sdfj sdf',
  122. '122 jsdf FICKEN sjdkf sdfj sdf',
  123. 'ddddddddddddddddddddddddd ARSCH dddddddddddddddddddddddddddddddddddddddddddddddddd',
  124. '\';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>'
  125. );
  126. foreach ($data as $key => $value) {
  127. $res = $this->Format->wordCensor($value, array('Arsch', 'Ficken'));
  128. echo '\''.h($value).'\' becomes \''.h($res).'\'';
  129. $this->assertTrue(!empty($res));
  130. }
  131. }
  132. /**
  133. * 2009-03-11 ms
  134. */
  135. /*
  136. public function testReverseAscii() {
  137. $is = $this->Format->reverseAscii('f&eacute;s');
  138. $expected = 'fés';
  139. $this->assertEquals($expected, $is);
  140. $is = entDec('f&eacute;s');
  141. $expected = 'fés';
  142. $this->assertEquals($expected, $is);
  143. $is = html_entity_decode('f&eacute;s');
  144. $expected = 'fés';
  145. $this->assertEquals($expected, $is);
  146. #TODO: correct it + more
  147. }
  148. */
  149. /**
  150. * 2009-03-11 ms
  151. */
  152. /*
  153. public function testDecodeEntities() {
  154. $is = $this->Format->decodeEntities('f&eacute;s');
  155. $expected = 'fés';
  156. $this->assertEquals($expected, $is);
  157. }
  158. */
  159. public function testTab2space() {
  160. echo '<h2>'.__FUNCTION__.'</h2>';
  161. $text = "foo\t\tfoobar\tbla\n";
  162. $text .= "fooo\t\tbar\t\tbla\n";
  163. $text .= "foooo\t\tbar\t\tbla\n";
  164. echo "<pre>" . $text . "</pre>";
  165. echo'becomes';
  166. echo "<pre>" . $this->Format->tab2space($text) . "</pre>";
  167. }
  168. public function testArray2table() {
  169. echo '<h2>'.__FUNCTION__.'</h2>';
  170. $array = array(
  171. array('x'=>'0', 'y'=>'0.5', 'z'=>'0.9'),
  172. array('1', '2', '3'),
  173. array('4', '5', '6'),
  174. );
  175. $is = $this->Format->array2table($array);
  176. echo $is;
  177. //$this->assertEquals($expected, $is);
  178. # recursive?
  179. $array = array(
  180. array('a'=>array('2'), 'b'=>array('2'), 'c'=>array('2')),
  181. array(array('2'), array('2'), array('2')),
  182. array(array('2'), array('2'), array(array('s'=>'3', 't'=>'4'))),
  183. );
  184. $is = $this->Format->array2table($array, array('recursive'=>true));
  185. echo $is;
  186. }
  187. /**
  188. * tearDown method
  189. *
  190. * @access public
  191. * @return void
  192. */
  193. public function tearDown() {
  194. unset($this->Format);
  195. }
  196. }