FormatHelperTest.php 7.7 KB

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