FormatHelperTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * @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. public function testPriorityIcon() {
  69. $values = array(
  70. array(1, array(), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  71. array(2, array(), '<div class="prio-lower" title="prioLower">&nbsp;</div>'),
  72. array(3, array(), ''),
  73. array(3, array('normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
  74. array(4, array(), '<div class="prio-higher" title="prioHigher">&nbsp;</div>'),
  75. array(5, array(), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  76. array(1, array('max' => 3), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  77. array(2, array('max' => 3), ''),
  78. array(2, array('max' => 3, 'normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
  79. array(3, array('max' => 3), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  80. array(0, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  81. array(1, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), ''),
  82. array(2, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  83. );
  84. foreach ($values as $key => $value) {
  85. $res = $this->Format->priorityIcon($value[0], $value[1]);
  86. echo $key;
  87. //debug($res, null, false); ob_flush();
  88. $this->assertEquals($value[2], $res);
  89. }
  90. }
  91. /**
  92. * 2009-08-30 ms
  93. */
  94. public function testShortenText() {
  95. $data = array(
  96. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf',
  97. '122 jsdf sjdkf sdfj sdf',
  98. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
  99. '\';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>'
  100. );
  101. foreach ($data as $key => $value) {
  102. $res = $this->Format->shortenText($value, 30);
  103. echo '\''.h($value).'\' becomes \''.$res.'\'';
  104. $this->assertTrue(!empty($res));
  105. }
  106. }
  107. /**
  108. * 2009-08-30 ms
  109. */
  110. public function testTruncate() {
  111. $data = array(
  112. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf' => 'dfssdfsdj sdkfj sdkfj k&#8230;',
  113. '122 jsdf sjdkf sdfj sdf' => '122 jsdf sjdkf sdfj sdf',
  114. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' => 'ddddddddddddddddddddddd&#8230;',
  115. 'dsfdsf hods hfoéfh oéfhoéfhoéfhoéfhoéfhiu oéfhoéfhdüf s' => 'dsfdsf hods hfoéfh oéfh&#8230;'
  116. );
  117. foreach ($data as $value => $expected) {
  118. $res = $this->Format->truncate($value, 30);
  119. debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false); ob_flush();
  120. $res = $this->Format->truncate($value, 30, array('html' => true));
  121. debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false); ob_flush();
  122. //$this->assertTrue(!empty($res));
  123. $this->assertEquals($expected, $res);
  124. }
  125. }
  126. /**
  127. * 2009-08-30 ms
  128. */
  129. public function testHideEmail() {
  130. $mails = array(
  131. 'test@test.de' => 't..t@t..t.de',
  132. 'xx@yy.de' => 'x..x@y..y.de',
  133. 'erk-wf@ve-eeervdg.com' => 'e..f@v..g.com',
  134. );
  135. foreach ($mails as $mail => $expected) {
  136. $res = $this->Format->hideEmail($mail);
  137. echo '\''.$mail.'\' becomes \''.$res.'\' - expected \''.$expected.'\'';
  138. $this->assertEquals($expected, $res);
  139. }
  140. }
  141. /**
  142. * 2009-08-30 ms
  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); ob_flush();
  155. $this->assertEquals($expected === null ? $value : $expected, $res);
  156. }
  157. }
  158. /**
  159. * 2009-03-11 ms
  160. */
  161. /*
  162. public function testReverseAscii() {
  163. $is = $this->Format->reverseAscii('f&eacute;s');
  164. $expected = 'fés';
  165. $this->assertEquals($expected, $is);
  166. $is = entDec('f&eacute;s');
  167. $expected = 'fés';
  168. $this->assertEquals($expected, $is);
  169. $is = html_entity_decode('f&eacute;s');
  170. $expected = 'fés';
  171. $this->assertEquals($expected, $is);
  172. #TODO: correct it + more
  173. }
  174. */
  175. /**
  176. * 2009-03-11 ms
  177. */
  178. /*
  179. public function testDecodeEntities() {
  180. $is = $this->Format->decodeEntities('f&eacute;s');
  181. $expected = 'fés';
  182. $this->assertEquals($expected, $is);
  183. }
  184. */
  185. public function testTab2space() {
  186. echo '<h2>'.__FUNCTION__.'</h2>';
  187. $text = "foo\t\tfoobar\tbla\n";
  188. $text .= "fooo\t\tbar\t\tbla\n";
  189. $text .= "foooo\t\tbar\t\tbla\n";
  190. echo "<pre>" . $text . "</pre>";
  191. echo'becomes';
  192. echo "<pre>" . $this->Format->tab2space($text) . "</pre>";
  193. }
  194. public function testArray2table() {
  195. echo '<h2>'.__FUNCTION__.'</h2>';
  196. $array = array(
  197. array('x'=>'0', 'y'=>'0.5', 'z'=>'0.9'),
  198. array('1', '2', '3'),
  199. array('4', '5', '6'),
  200. );
  201. $is = $this->Format->array2table($array);
  202. echo $is;
  203. //$this->assertEquals($expected, $is);
  204. # recursive?
  205. $array = array(
  206. array('a'=>array('2'), 'b'=>array('2'), 'c'=>array('2')),
  207. array(array('2'), array('2'), array('2')),
  208. array(array('2'), array('2'), array(array('s'=>'3', 't'=>'4'))),
  209. );
  210. $is = $this->Format->array2table($array, array('recursive'=>true));
  211. echo $is;
  212. }
  213. /**
  214. * tearDown method
  215. *
  216. * @access public
  217. * @return void
  218. */
  219. public function tearDown() {
  220. unset($this->Format);
  221. }
  222. }