FormatHelperTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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="fa-signin"></i>';
  118. $this->assertEquals($expected, $result);
  119. $result = $this->Format->fontIcon('signin', array('rotate' => 90));
  120. $expected = '<i class="fa-signin fa-rotate-90"></i>';
  121. $this->assertEquals($expected, $result);
  122. $result = $this->Format->fontIcon('signin', array('size' => 5, 'extra' => array('muted')));
  123. $expected = '<i class="fa-signin fa-muted fa-5x"></i>';
  124. $this->assertEquals($expected, $result);
  125. $result = $this->Format->fontIcon('signin', array('size' => 5, 'extra' => array('muted'), 'namespace' => 'icon'));
  126. $expected = '<i class="icon-signin icon-muted icon-5x"></i>';
  127. $this->assertEquals($expected, $result);
  128. }
  129. /**
  130. * @return void
  131. */
  132. public function testOk() {
  133. $content = 'xyz';
  134. $data = array(
  135. true,
  136. false
  137. );
  138. foreach ($data as $key => $value) {
  139. $res = $this->Format->ok($content . ' ' . (int)$value, $value);
  140. //echo ''.$res.'';
  141. $this->assertTrue(!empty($res));
  142. }
  143. }
  144. /**
  145. * FormatHelperTest::testConfigure()
  146. *
  147. * @return void
  148. */
  149. public function testNeighbors() {
  150. if (!defined('ICON_PREV')) {
  151. define('ICON_PREV', 'prev');
  152. }
  153. if (!defined('ICON_NEXT')) {
  154. define('ICON_NEXT', 'next');
  155. }
  156. $neighbors = array(
  157. 'prev' => array('ModelName' => array('id' => 1, 'foo' => 'bar')),
  158. 'next' => array('ModelName' => array('id' => 2, 'foo' => 'y')),
  159. );
  160. $result = $this->Format->neighbors($neighbors, 'foo');
  161. $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>';
  162. $this->assertEquals($expected, $result);
  163. $this->Format->settings['fontIcons'] = array(
  164. 'prev' => 'fa fa-prev',
  165. 'next' => 'fa fa-next');
  166. $result = $this->Format->neighbors($neighbors, 'foo');
  167. $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>';
  168. $this->assertEquals($expected, $result);
  169. }
  170. /**
  171. * FormatHelperTest::testPriorityIcon()
  172. *
  173. * @return void
  174. */
  175. public function testPriorityIcon() {
  176. $values = array(
  177. array(1, array(), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  178. array(2, array(), '<div class="prio-lower" title="prioLower">&nbsp;</div>'),
  179. array(3, array(), ''),
  180. array(3, array('normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
  181. array(4, array(), '<div class="prio-higher" title="prioHigher">&nbsp;</div>'),
  182. array(5, array(), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  183. array(1, array('max' => 3), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  184. array(2, array('max' => 3), ''),
  185. array(2, array('max' => 3, 'normal' => true), '<div class="prio-normal" title="prioNormal">&nbsp;</div>'),
  186. array(3, array('max' => 3), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  187. array(0, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-low" title="prioLow">&nbsp;</div>'),
  188. array(1, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), ''),
  189. array(2, array('max' => 3, 'map' => array(0 => 1, 1 => 2, 2 => 3)), '<div class="prio-high" title="prioHigh">&nbsp;</div>'),
  190. );
  191. foreach ($values as $key => $value) {
  192. $res = $this->Format->priorityIcon($value[0], $value[1]);
  193. //echo $key;
  194. //debug($res, null, false);
  195. $this->assertEquals($value[2], $res);
  196. }
  197. }
  198. /**
  199. */
  200. public function testShortenText() {
  201. $data = array(
  202. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf',
  203. '122 jsdf sjdkf sdfj sdf',
  204. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd',
  205. '\';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>'
  206. );
  207. foreach ($data as $key => $value) {
  208. $res = $this->Format->shortenText($value, 30);
  209. //echo '\''.h($value).'\' becomes \''.$res.'\'';
  210. $this->assertTrue(!empty($res));
  211. }
  212. }
  213. /**
  214. */
  215. public function testTruncate() {
  216. $data = array(
  217. 'dfssdfsdj sdkfj sdkfj ksdfj sdkf ksdfj ksdfjsd kfjsdk fjsdkfj ksdjf ksdf jsdsdf' => 'dfssdfsdj sdkfj sdkfj ksdfj s' . "\xe2\x80\xa6",
  218. '122 jsdf sjdkf sdfj sdf' => '122 jsdf sjdkf sdfj sdf',
  219. 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd' => 'ddddddddddddddddddddddddddddd' . "\xe2\x80\xa6",
  220. '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"
  221. );
  222. foreach ($data as $value => $expected) {
  223. $res = $this->Format->truncate($value, 30, array('html' => true));
  224. //debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false);
  225. $res = $this->Format->truncate($value, 30, array('html' => true));
  226. //debug( '\''.h($value).'\' becomes \''.$res.'\'', null, false);
  227. //$this->assertTrue(!empty($res));
  228. $this->assertEquals($expected, $res);
  229. }
  230. }
  231. /**
  232. */
  233. public function testHideEmail() {
  234. $mails = array(
  235. 'test@test.de' => 't..t@t..t.de',
  236. 'xx@yy.de' => 'x..x@y..y.de',
  237. 'erk-wf@ve-eeervdg.com' => 'e..f@v..g.com',
  238. );
  239. foreach ($mails as $mail => $expected) {
  240. $res = $this->Format->hideEmail($mail);
  241. //echo '\''.$mail.'\' becomes \''.$res.'\' - expected \''.$expected.'\'';
  242. $this->assertEquals($expected, $res);
  243. }
  244. }
  245. /**
  246. */
  247. public function testWordCensor() {
  248. $data = array(
  249. 'dfssdfsdj sdkfj sdkfj ksdfj bitch ksdfj' => 'dfssdfsdj sdkfj sdkfj ksdfj ##### ksdfj',
  250. '122 jsdf ficken Sjdkf sdfj sdf' => '122 jsdf ###### Sjdkf sdfj sdf',
  251. '122 jsdf FICKEN sjdkf sdfjs sdf' => '122 jsdf ###### sjdkf sdfjs sdf',
  252. 'dddddddddd ARSCH ddddddddddddd' => 'dddddddddd ##### ddddddddddddd',
  253. //'\';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
  254. );
  255. foreach ($data as $value => $expected) {
  256. $res = $this->Format->wordCensor($value, array('Arsch', 'Ficken', 'Bitch'));
  257. //debug('\''.h($value).'\' becomes \''.h($res).'\'', null, false);
  258. $this->assertEquals($expected === null ? $value : $expected, $res);
  259. }
  260. }
  261. /**
  262. */
  263. /*
  264. public function testReverseAscii() {
  265. $is = $this->Format->reverseAscii('f&eacute;s');
  266. $expected = 'fés';
  267. $this->assertEquals($expected, $is);
  268. $is = entDec('f&eacute;s');
  269. $expected = 'fés';
  270. $this->assertEquals($expected, $is);
  271. $is = html_entity_decode('f&eacute;s');
  272. $expected = 'fés';
  273. $this->assertEquals($expected, $is);
  274. #TODO: correct it + more
  275. }
  276. */
  277. /**
  278. */
  279. /*
  280. public function testDecodeEntities() {
  281. $is = $this->Format->decodeEntities('f&eacute;s');
  282. $expected = 'fés';
  283. $this->assertEquals($expected, $is);
  284. }
  285. */
  286. public function testTab2space() {
  287. //echo '<h2>'.__FUNCTION__.'</h2>';
  288. $text = "foo\t\tfoobar\tbla\n";
  289. $text .= "fooo\t\tbar\t\tbla\n";
  290. $text .= "foooo\t\tbar\t\tbla\n";
  291. //echo "<pre>" . $text . "</pre>";
  292. //echo'becomes';
  293. //echo "<pre>" . $this->Format->tab2space($text) . "</pre>";
  294. }
  295. public function testArray2table() {
  296. //echo '<h2>'.__FUNCTION__.'</h2>';
  297. $array = array(
  298. array('x' => '0', 'y' => '0.5', 'z' => '0.9'),
  299. array('1', '2', '3'),
  300. array('4', '5', '6'),
  301. );
  302. $is = $this->Format->array2table($array);
  303. //echo $is;
  304. //$this->assertEquals($expected, $is);
  305. // recursive?
  306. $array = array(
  307. array('a' => array('2'), 'b' => array('2'), 'c' => array('2')),
  308. array(array('2'), array('2'), array('2')),
  309. array(array('2'), array('2'), array(array('s' => '3', 't' => '4'))),
  310. );
  311. $is = $this->Format->array2table($array, array('recursive' => true));
  312. //echo $is;
  313. }
  314. public function tearDown() {
  315. parent::tearDown();
  316. unset($this->Format);
  317. }
  318. }