FormatHelperTest.php 7.4 KB

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