FormatHelperTest.php 12 KB

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