FormatHelperTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. */
  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. * @return void
  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. * FormatHelperTest::testIcon()
  48. *
  49. * @return void
  50. */
  51. public function testIcon() {
  52. $result = $this->Format->icon('edit');
  53. $expected = '<img src="/img/icons/edit.gif" title="' . __('Edit') . '" alt="[' . __('Edit') . ']" class="icon" />';
  54. $this->assertEquals($expected, $result);
  55. }
  56. /**
  57. * FormatHelperTest::testCIcon()
  58. *
  59. * @return void
  60. */
  61. public function testCIcon() {
  62. $result = $this->Format->cIcon('edit.png');
  63. $expected = '<img src="/img/icons/edit.png" title="' . __('Edit') . '" alt="[' . __('Edit') . ']" class="icon" />';
  64. $this->assertEquals($expected, $result);
  65. }
  66. /**
  67. * FormatHelperTest::testIconWithFontIcon()
  68. *
  69. * @return void
  70. */
  71. public function testIconWithFontIcon() {
  72. $this->Format->settings['fontIcons'] = array('edit' => 'fa fa-pencil');
  73. $result = $this->Format->icon('edit');
  74. $expected = '<i class="fa fa-pencil edit" title="' . __('Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  75. $this->assertEquals($expected, $result);
  76. }
  77. /**
  78. * FormatHelperTest::testCIconWithFontIcon()
  79. *
  80. * @return void
  81. */
  82. public function testCIconWithFontIcon() {
  83. $this->Format->settings['fontIcons'] = array('edit' => 'fa fa-pencil');
  84. $result = $this->Format->cIcon('edit.png');
  85. $expected = '<i class="fa fa-pencil edit" title="' . __('Edit') . '" data-placement="bottom" data-toggle="tooltip"></i>';
  86. $this->assertEquals($expected, $result);
  87. }
  88. /**
  89. * FormatHelperTest::testSpeedOfIcons()
  90. *
  91. * @return void
  92. */
  93. public function testSpeedOfIcons() {
  94. $count = 1000;
  95. $time1 = microtime(true);
  96. for ($i = 0; $i < $count; $i++) {
  97. $result = $this->Format->icon('edit');
  98. }
  99. $time2 = microtime(true);
  100. $this->Format->settings['fontIcons'] = array('edit' => 'fa fa-pencil');
  101. $time3 = microtime(true);
  102. for ($i = 0; $i < $count; $i++) {
  103. $result = $this->Format->icon('edit');
  104. }
  105. $time4 = microtime(true);
  106. $normalIconSpeed = number_format($time2 - $time1, 2);
  107. $this->debug('Normal Icons: ' . $normalIconSpeed);
  108. $fontIconViaStringTemplateSpeed = number_format($time4 - $time3, 2);
  109. $this->debug('StringTemplate and Font Icons: ' . $fontIconViaStringTemplateSpeed);
  110. $this->assertTrue($fontIconViaStringTemplateSpeed < $normalIconSpeed);
  111. }
  112. /**
  113. * @return void
  114. */
  115. public function testFontIcon() {
  116. $result = $this->Format->fontIcon('signin');
  117. $expected = '<i class="icon-signin"></i>';
  118. $this->assertEquals($expected, $result);
  119. $result = $this->Format->fontIcon('signin', array('rotate' => 90));
  120. $expected = '<i class="icon-signin icon-rotate-90"></i>';
  121. $this->assertEquals($expected, $result);
  122. $result = $this->Format->fontIcon('signin', array('size' => 5, 'extra' => array('muted')));
  123. $expected = '<i class="icon-signin icon-muted icon-5x"></i>';
  124. $this->assertEquals($expected, $result);
  125. }
  126. /**
  127. * @return void
  128. */
  129. public function testOk() {
  130. $content = 'xyz';
  131. $data = array(
  132. true,
  133. false
  134. );
  135. foreach ($data as $key => $value) {
  136. $res = $this->Format->ok($content . ' ' . (int)$value, $value);
  137. //echo ''.$res.'';
  138. $this->assertTrue(!empty($res));
  139. }
  140. }
  141. /**
  142. * FormatHelperTest::testConfigure()
  143. *
  144. * @return void
  145. */
  146. public function testNeighbors() {
  147. if (!defined('ICON_PREV')) {
  148. define('ICON_PREV', 'prev');
  149. }
  150. if (!defined('ICON_NEXT')) {
  151. define('ICON_NEXT', 'next');
  152. }
  153. $neighbors = array(
  154. 'prev' => array('ModelName' => array('id' => 1, 'foo' => 'bar')),
  155. 'next' => array('ModelName' => array('id' => 2, 'foo' => 'y')),
  156. );
  157. $result = $this->Format->neighbors($neighbors, 'foo');
  158. $expected = '<div class="next-prev-navi nextPrevNavi"><a href="/index/1" title="bar"><img src="/img/icons/prev" alt="[]" class="icon" />&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/index/2" title="y"><img src="/img/icons/next" alt="[]" class="icon" />&nbsp;nextRecord</a></div>';
  159. $this->assertEquals($expected, $result);
  160. $this->Format->settings['fontIcons'] = array(
  161. 'prev' => 'fa fa-prev',
  162. 'next' => 'fa fa-next');
  163. $result = $this->Format->neighbors($neighbors, 'foo');
  164. $expected = '<div class="next-prev-navi nextPrevNavi"><a href="/index/1" title="bar"><i class="fa fa-prev prev" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;prevRecord</a>&nbsp;&nbsp;<a href="/index/2" title="y"><i class="fa fa-next next" title="" data-placement="bottom" data-toggle="tooltip"></i>&nbsp;nextRecord</a></div>';
  165. $this->assertEquals($expected, $result);
  166. }
  167. /**
  168. * FormatHelperTest::testPriorityIcon()
  169. *
  170. * @return void
  171. */
  172. public function testPriorityIcon() {
  173. $values = array(
  174. array(1, array(), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  175. array(2, array(), '<div class="prio-lower" title="prioLower">&nbsp;</div>'),
  176. array(3, array(), ''),
  177. array(3, array('normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
  178. array(4, array(), '<div class="prio-higher" title="prioHigher">&nbsp;</div>'),
  179. array(5, array(), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  180. array(1, array('max' => 3), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  181. array(2, array('max' => 3), ''),
  182. array(2, array('max' => 3, 'normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
  183. array(3, array('max' => 3), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  184. array(0, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  185. array(1, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), ''),
  186. array(2, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  187. );
  188. foreach ($values as $key => $value) {
  189. $res = $this->Format->priorityIcon($value[0], $value[1]);
  190. //echo $key;
  191. //debug($res, null, false);
  192. $this->assertEquals($value[2], $res);
  193. }
  194. }
  195. /**
  196. */
  197. public function testShortenText() {
  198. $data = array(
  199. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf',
  200. '122 jsdf sjdkf sdfj sdf',
  201. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
  202. '\';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>'
  203. );
  204. foreach ($data as $key => $value) {
  205. $res = $this->Format->shortenText($value, 30);
  206. //echo '\''.h($value).'\' becomes \''.$res.'\'';
  207. $this->assertTrue(!empty($res));
  208. }
  209. }
  210. /**
  211. */
  212. public function testTruncate() {
  213. $data = array(
  214. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf' => 'dfssdfsdj sdkfj sdkfj ksdfj s' . "\xe2\x80\xa6",
  215. '122 jsdf sjdkf sdfj sdf' => '122 jsdf sjdkf sdfj sdf',
  216. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' => 'ddddddddddddddddddddddddddddd' . "\xe2\x80\xa6",
  217. '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"
  218. );
  219. foreach ($data as $value => $expected) {
  220. $res = $this->Format->truncate($value, 30, array('html' => true));
  221. //debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false);
  222. $res = $this->Format->truncate($value, 30, array('html' => true));
  223. //debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false);
  224. //$this->assertTrue(!empty($res));
  225. $this->assertEquals($expected, $res);
  226. }
  227. }
  228. /**
  229. */
  230. public function testHideEmail() {
  231. $mails = array(
  232. 'test@test.de' => 't..t@t..t.de',
  233. 'xx@yy.de' => 'x..x@y..y.de',
  234. 'erk-wf@ve-eeervdg.com' => 'e..f@v..g.com',
  235. );
  236. foreach ($mails as $mail => $expected) {
  237. $res = $this->Format->hideEmail($mail);
  238. //echo '\''.$mail.'\' becomes \''.$res.'\' - expected \''.$expected.'\'';
  239. $this->assertEquals($expected, $res);
  240. }
  241. }
  242. /**
  243. */
  244. public function testWordCensor() {
  245. $data = array(
  246. 'dfssdfsdj sdkfj sdkfj ksdfj bitch ksdfj' => 'dfssdfsdj sdkfj sdkfj ksdfj ##### ksdfj',
  247. '122 jsdf ficken Sjdkf sdfj sdf' => '122 jsdf ###### Sjdkf sdfj sdf',
  248. '122 jsdf FICKEN sjdkf sdfjs sdf' => '122 jsdf ###### sjdkf sdfjs sdf',
  249. 'dddddddddd ARSCH ddddddddddddd' => 'dddddddddd ##### ddddddddddddd',
  250. //'\';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
  251. );
  252. foreach ($data as $value => $expected) {
  253. $res = $this->Format->wordCensor($value, array('Arsch', 'Ficken', 'Bitch'));
  254. //debug('\''.h($value).'\' becomes \''.h($res).'\'', null, false);
  255. $this->assertEquals($expected === null ? $value : $expected, $res);
  256. }
  257. }
  258. /**
  259. */
  260. /*
  261. public function testReverseAscii() {
  262. $is = $this->Format->reverseAscii('f&eacute;s');
  263. $expected = 'fés';
  264. $this->assertEquals($expected, $is);
  265. $is = entDec('f&eacute;s');
  266. $expected = 'fés';
  267. $this->assertEquals($expected, $is);
  268. $is = html_entity_decode('f&eacute;s');
  269. $expected = 'fés';
  270. $this->assertEquals($expected, $is);
  271. #TODO: correct it + more
  272. }
  273. */
  274. /**
  275. */
  276. /*
  277. public function testDecodeEntities() {
  278. $is = $this->Format->decodeEntities('f&eacute;s');
  279. $expected = 'fés';
  280. $this->assertEquals($expected, $is);
  281. }
  282. */
  283. public function testTab2space() {
  284. //echo '<h2>'.__FUNCTION__.'</h2>';
  285. $text = "foo\t\tfoobar\tbla\n";
  286. $text .= "fooo\t\tbar\t\tbla\n";
  287. $text .= "foooo\t\tbar\t\tbla\n";
  288. //echo "<pre>" . $text . "</pre>";
  289. //echo'becomes';
  290. //echo "<pre>" . $this->Format->tab2space($text) . "</pre>";
  291. }
  292. public function testArray2table() {
  293. //echo '<h2>'.__FUNCTION__.'</h2>';
  294. $array = array(
  295. array('x' => '0', 'y' => '0.5', 'z' => '0.9'),
  296. array('1', '2', '3'),
  297. array('4', '5', '6'),
  298. );
  299. $is = $this->Format->array2table($array);
  300. //echo $is;
  301. //$this->assertEquals($expected, $is);
  302. // recursive?
  303. $array = array(
  304. array('a' => array('2'), 'b' => array('2'), 'c' => array('2')),
  305. array(array('2'), array('2'), array('2')),
  306. array(array('2'), array('2'), array(array('s' => '3', 't' => '4'))),
  307. );
  308. $is = $this->Format->array2table($array, array('recursive' => true));
  309. //echo $is;
  310. }
  311. public function tearDown() {
  312. parent::tearDown();
  313. unset($this->Format);
  314. }
  315. }