PageRenderTime 24ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/helper/TextHelperTest.php

https://github.com/aminin/Symfony
PHP | 137 lines | 99 code | 24 blank | 14 comment | 1 complexity | 3e57f3c46dde04eb36dd3862ecf7c44d MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once(dirname(__FILE__).'/../../../test/bootstrap/unit.php');
  10. require_once(dirname(__FILE__).'/../../../lib/helper/TagHelper.php');
  11. require_once(dirname(__FILE__).'/../../../lib/helper/TextHelper.php');
  12. $t = new lime_test(54);
  13. // truncate_text()
  14. $t->diag('truncate_text()');
  15. $t->is(truncate_text(''), '', 'text_truncate() does nothing on an empty string');
  16. $t->is(truncate_text('Test'), 'Test', 'text_truncate() truncates to 30 characters by default');
  17. $text = str_repeat('A', 35);
  18. $truncated = str_repeat('A', 27).'...';
  19. $t->is(truncate_text($text), $truncated, 'text_truncate() adds ... to truncated text');
  20. $text = str_repeat('A', 35);
  21. $truncated = str_repeat('A', 22).'...';
  22. $t->is(truncate_text($text, 25), $truncated, 'text_truncate() takes the max length as its second argument');
  23. $text = str_repeat('A', 35);
  24. $truncated = str_repeat('A', 21).'BBBB';
  25. $t->is(truncate_text($text, 25, 'BBBB'), $truncated, 'text_truncate() takes the ... text as its third argument');
  26. $text = str_repeat('A', 10).str_repeat(' ', 10).str_repeat('A', 10);
  27. $truncated_true = str_repeat('A', 10).'...';
  28. $truncated_false = str_repeat('A', 10).str_repeat(' ', 2).'...';
  29. $t->is(truncate_text($text, 15, '...', false), $truncated_false, 'text_truncate() accepts a truncate lastspace boolean as its fourth argument');
  30. $t->is(truncate_text($text, 15, '...', true), $truncated_true, 'text_truncate() accepts a truncate lastspace boolean as its fourth argument');
  31. if(extension_loaded('mbstring'))
  32. {
  33. $oldEncoding = mb_internal_encoding();
  34. $t->is(truncate_text('のビヘイビアにパラメーターを渡すことで特定のモデルでのフォーム生成を無効にできます', 11), 'のビヘイビアにパ...', 'text_truncate() handles unicode characters using mbstring if available');
  35. $t->is(mb_internal_encoding(), $oldEncoding, 'text_truncate() sets back the internal encoding in case it changes it');
  36. }
  37. else
  38. {
  39. $t->skip('mbstring extension is not enabled', 2);
  40. }
  41. // highlight_text()
  42. $t->diag('highlight_text()');
  43. $t->is(highlight_text("This is a beautiful morning", "beautiful"),
  44. "This is a <strong class=\"highlight\">beautiful</strong> morning",
  45. 'text_highlighter() highlights a word given as its second argument'
  46. );
  47. $t->is(highlight_text("This is a beautiful morning, but also a beautiful day", "beautiful"),
  48. "This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day",
  49. 'text_highlighter() highlights all occurrences of a word given as its second argument'
  50. );
  51. $t->is(highlight_text("This is a beautiful morning, but also a beautiful day", "beautiful", '<b>\\1</b>'),
  52. "This is a <b>beautiful</b> morning, but also a <b>beautiful</b> day",
  53. 'text_highlighter() takes a pattern as its third argument'
  54. );
  55. $t->is(highlight_text('', 'beautiful'), '', 'text_highlighter() returns an empty string if input is empty');
  56. $t->is(highlight_text('', ''), '', 'text_highlighter() returns an empty string if input is empty');
  57. $t->is(highlight_text('foobar', 'beautiful'), 'foobar', 'text_highlighter() does nothing is string to highlight is not present');
  58. $t->is(highlight_text('foobar', ''), 'foobar', 'text_highlighter() returns input if string to highlight is not present');
  59. $t->is(highlight_text("This is a beautiful! morning", "beautiful!"), "This is a <strong class=\"highlight\">beautiful!</strong> morning", 'text_highlighter() escapes search string to be safe in a regex');
  60. $t->is(highlight_text("This is a beautiful! morning", "beautiful! morning"), "This is a <strong class=\"highlight\">beautiful! morning</strong>", 'text_highlighter() escapes search string to be safe in a regex');
  61. $t->is(highlight_text("This is a beautiful? morning", "beautiful? morning"), "This is a <strong class=\"highlight\">beautiful? morning</strong>", 'text_highlighter() escapes search string to be safe in a regex');
  62. $t->is(highlight_text("The http://www.google.com/ website is great", "http://www.google.com/"), "The <strong class=\"highlight\">http://www.google.com/</strong> website is great", 'text_highlighter() escapes search string to be safe in a regex');
  63. // excerpt_text()
  64. $t->diag('excerpt_text()');
  65. $t->is(excerpt_text('', 'foo', 5), '', 'text_excerpt() return an empty string if argument is empty');
  66. $t->is(excerpt_text('foo', '', 5), '', 'text_excerpt() return an empty string if phrase is empty');
  67. $t->is(excerpt_text("This is a beautiful morning", "beautiful", 5), "...is a beautiful morn...", 'text_excerpt() creates an excerpt of a text');
  68. $t->is(excerpt_text("This is a beautiful morning", "this", 5), "This is a...", 'text_excerpt() creates an excerpt of a text');
  69. $t->is(excerpt_text("This is a beautiful morning", "morning", 5), "...iful morning", 'text_excerpt() creates an excerpt of a text');
  70. $t->is(excerpt_text("This is a beautiful morning", "morning", 5, '...', true), "... morning", 'text_excerpt() takes a fifth argument allowing excerpt on whitespace');
  71. $t->is(excerpt_text("This is a beautiful morning", "beautiful", 5, '...', true), "... a beautiful ...", 'text_excerpt() takes a fifth argument allowing excerpt on whitespace');
  72. $t->is(excerpt_text("This is a beautiful morning", "This", 5, '...', true), "This is ...", 'text_excerpt() takes a fifth argument allowing excerpt on whitespace');
  73. $t->is(excerpt_text("This is a beautiful morning", "day"), '', 'text_excerpt() does nothing if the search string is not in input');
  74. // wrap_text()
  75. $t->diag('wrap_text()');
  76. $line = 'This is a very long line to be wrapped...';
  77. $t->is(wrap_text($line), "This is a very long line to be wrapped...\n", 'wrap_text() wraps long lines with a default of 80');
  78. $t->is(wrap_text($line, 10), "This is a\nvery long\nline to be\nwrapped...\n", 'wrap_text() takes a line length as its second argument');
  79. $t->is(wrap_text($line, 5), "This\nis a\nvery\nlong\nline\nto be\nwrapped...\n", 'wrap_text() takes a line length as its second argument');
  80. // simple_format_text()
  81. $t->diag('simple_format_text()');
  82. $t->is(simple_format_text("crazy\r\n cross\r platform linebreaks"), "<p>crazy\n<br /> cross\n<br /> platform linebreaks</p>", 'text_simple_format() replaces \n by <br />');
  83. $t->is(simple_format_text("A paragraph\n\nand another one!"), "<p>A paragraph</p><p>and another one!</p>", 'text_simple_format() replaces \n\n by <p>');
  84. $t->is(simple_format_text("A paragraph\n\n\n\nand another one!"), "<p>A paragraph</p><p>and another one!</p>", 'text_simple_format() replaces \n\n\n\n by <p>');
  85. $t->is(simple_format_text("A paragraph\n With a newline"), "<p>A paragraph\n<br /> With a newline</p>", 'text_simple_format() wrap all string with <p>');
  86. $t->is(simple_format_text("1\n2\n3"), "<p>1\n<br />2\n<br />3</p>", 'text_simple_format() Ticket #6824');
  87. // text_strip_links()
  88. $t->diag('text_strip_links()');
  89. $t->is(strip_links_text("<a href='almost'>on my mind</a>"), "on my mind", 'text_strip_links() strips all links in input');
  90. $t->is(strip_links_text('<a href="first.html">first</a> and <a href="second.html">second</a>'), "first and second", 'text_strip_links() strips all links in input');
  91. // auto_link_text()
  92. $t->diag('auto_link_text()');
  93. $email_raw = 'fabien.potencier@symfony-project.com';
  94. $email_result = '<a href="mailto:'.$email_raw.'">'.$email_raw.'</a>';
  95. $link_raw = 'http://www.google.com';
  96. $link_result = '<a href="'.$link_raw.'">'.$link_raw.'</a>';
  97. $link2_raw = 'www.google.com';
  98. $link2_result = '<a href="http://'.$link2_raw.'">'.$link2_raw.'</a>';
  99. $t->is(auto_link_text('hello '.$email_raw, 'email_addresses'), 'hello '.$email_result, 'auto_link_text() converts emails to links');
  100. $t->is(auto_link_text('Go to '.$link_raw, 'urls'), 'Go to '.$link_result, 'auto_link_text() converts absolute URLs to links');
  101. $t->is(auto_link_text('Go to '.$link_raw, 'email_addresses'), 'Go to '.$link_raw, 'auto_link_text() takes a second parameter');
  102. $t->is(auto_link_text('Go to '.$link_raw.' and say hello to '.$email_raw), 'Go to '.$link_result.' and say hello to '.$email_result, 'auto_link_text() converts emails and URLs if no second argument is given');
  103. $t->is(auto_link_text('<p>Link '.$link_raw.'</p>'), '<p>Link '.$link_result.'</p>', 'auto_link_text() converts URLs to links');
  104. $t->is(auto_link_text('<p>'.$link_raw.' Link</p>'), '<p>'.$link_result.' Link</p>', 'auto_link_text() converts URLs to links');
  105. $t->is(auto_link_text('Go to '.$link2_raw, 'urls'), 'Go to '.$link2_result, 'auto_link_text() converts URLs to links even if link does not start with http://');
  106. $t->is(auto_link_text('Go to '.$link2_raw, 'email_addresses'), 'Go to '.$link2_raw, 'auto_link_text() converts URLs to links');
  107. $t->is(auto_link_text('<p>Link '.$link2_raw.'</p>'), '<p>Link '.$link2_result.'</p>', 'auto_link_text() converts URLs to links');
  108. $t->is(auto_link_text('<p>'.$link2_raw.' Link</p>'), '<p>'.$link2_result.' Link</p>', 'auto_link_text() converts URLs to links');
  109. $t->is(auto_link_text('<p>http://www.google.com/?q=symfony Link</p>'), '<p><a href="http://www.google.com/?q=symfony">http://www.google.com/?q=symfony</a> Link</p>', 'auto_link_text() converts URLs to links');
  110. $t->is(auto_link_text('<p>http://www.google.com/?q=symfony+link</p>', 'all', array(), true), '<p><a href="http://www.google.com/?q=symfony+link">http://www.google.com/?q=symfony+li...</a></p>', 'auto_link_text() truncates URLs in links');
  111. $t->is(auto_link_text('<p>http://www.google.com/?q=symfony+link</p>', 'all', array(), true, 32, '***'), '<p><a href="http://www.google.com/?q=symfony+link">http://www.google.com/?q=symfony***</a></p>', 'auto_link_text() takes truncation parameters');
  112. $t->is(auto_link_text('<p>http://twitter.com/#!/fabpot</p>'),'<p><a href="http://twitter.com/#!/fabpot">http://twitter.com/#!/fabpot</a></p>',"auto_link_text() converts URLs with complex fragments to links");
  113. $t->is(auto_link_text('<p>http://twitter.com/#!/fabpot is Fabien Potencier on Twitter</p>'),'<p><a href="http://twitter.com/#!/fabpot">http://twitter.com/#!/fabpot</a> is Fabien Potencier on Twitter</p>',"auto_link_text() converts URLs with complex fragments and trailing text to links");