You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

application_helper_test.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # redMine - project management software
  2. # Copyright (C) 2006-2007 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.dirname(__FILE__) + '/../../test_helper'
  18. class ApplicationHelperTest < HelperTestCase
  19. include ApplicationHelper
  20. include ActionView::Helpers::TextHelper
  21. fixtures :projects, :repositories, :changesets, :trackers, :issue_statuses, :issues, :documents, :versions, :wikis, :wiki_pages, :wiki_contents, :roles, :enabled_modules
  22. def setup
  23. super
  24. end
  25. def test_auto_links
  26. to_test = {
  27. 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
  28. 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
  29. 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
  30. 'http://foo.bar/foo.bar#foo.bar.' => '<a class="external" href="http://foo.bar/foo.bar#foo.bar">http://foo.bar/foo.bar#foo.bar</a>.',
  31. 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
  32. 'http://foo.bar/page?p=1&t=z&s=' => '<a class="external" href="http://foo.bar/page?p=1&#38;t=z&#38;s=">http://foo.bar/page?p=1&#38;t=z&#38;s=</a>',
  33. 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>'
  34. }
  35. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  36. end
  37. def test_auto_mailto
  38. assert_equal '<p><a href="mailto:test@foo.bar" class="email">test@foo.bar</a></p>',
  39. textilizable('test@foo.bar')
  40. end
  41. def test_inline_images
  42. to_test = {
  43. '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
  44. 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
  45. 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
  46. 'with style !{width:100px;height100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" style="width:100px;height100px;" alt="" />',
  47. }
  48. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  49. end
  50. def test_textile_external_links
  51. to_test = {
  52. 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
  53. 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
  54. '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
  55. # no multiline link text
  56. "This is a double quote \"on the first line\nand another on a second line\":test" => "This is a double quote \"on the first line<br />\nand another on a second line\":test"
  57. }
  58. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  59. end
  60. def test_redmine_links
  61. issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
  62. :class => 'issue', :title => 'Error 281 when updating a recipe (New)')
  63. changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
  64. :class => 'changeset', :title => 'My very first commit')
  65. document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
  66. :class => 'document')
  67. version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
  68. :class => 'version')
  69. source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => 'some/file'}
  70. to_test = {
  71. # tickets
  72. '#3, #3 and #3.' => "#{issue_link}, #{issue_link} and #{issue_link}.",
  73. # changesets
  74. 'r1' => changeset_link,
  75. # documents
  76. 'document#1' => document_link,
  77. 'document:"Test document"' => document_link,
  78. # versions
  79. 'version#2' => version_link,
  80. 'version:1.0' => version_link,
  81. 'version:"1.0"' => version_link,
  82. # source
  83. 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
  84. 'source:/some/file@52' => link_to('source:/some/file@52', source_url.merge(:rev => 52), :class => 'source'),
  85. 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url.merge(:anchor => 'L110'), :class => 'source'),
  86. 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url.merge(:rev => 52, :anchor => 'L110'), :class => 'source'),
  87. 'export:/some/file' => link_to('export:/some/file', source_url.merge(:format => 'raw'), :class => 'source download'),
  88. # escaping
  89. '!#3.' => '#3.',
  90. '!r1' => 'r1',
  91. '!document#1' => 'document#1',
  92. '!document:"Test document"' => 'document:"Test document"',
  93. '!version#2' => 'version#2',
  94. '!version:1.0' => 'version:1.0',
  95. '!version:"1.0"' => 'version:"1.0"',
  96. '!source:/some/file' => 'source:/some/file',
  97. # invalid expressions
  98. 'source:' => 'source:',
  99. # url hash
  100. "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
  101. }
  102. @project = Project.find(1)
  103. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  104. end
  105. def test_wiki_links
  106. to_test = {
  107. '[[CookBook documentation]]' => '<a href="/wiki/ecookbook/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
  108. '[[Another page|Page]]' => '<a href="/wiki/ecookbook/Another_page" class="wiki-page">Page</a>',
  109. # page that doesn't exist
  110. '[[Unknown page]]' => '<a href="/wiki/ecookbook/Unknown_page" class="wiki-page new">Unknown page</a>',
  111. '[[Unknown page|404]]' => '<a href="/wiki/ecookbook/Unknown_page" class="wiki-page new">404</a>',
  112. # link to another project wiki
  113. '[[onlinestore:]]' => '<a href="/wiki/onlinestore/" class="wiki-page">onlinestore</a>',
  114. '[[onlinestore:|Wiki]]' => '<a href="/wiki/onlinestore/" class="wiki-page">Wiki</a>',
  115. '[[onlinestore:Start page]]' => '<a href="/wiki/onlinestore/Start_page" class="wiki-page">Start page</a>',
  116. '[[onlinestore:Start page|Text]]' => '<a href="/wiki/onlinestore/Start_page" class="wiki-page">Text</a>',
  117. '[[onlinestore:Unknown page]]' => '<a href="/wiki/onlinestore/Unknown_page" class="wiki-page new">Unknown page</a>',
  118. # escaping
  119. '![[Another page|Page]]' => '[[Another page|Page]]',
  120. }
  121. @project = Project.find(1)
  122. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  123. end
  124. def test_html_tags
  125. to_test = {
  126. "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
  127. "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
  128. "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
  129. # do not escape pre/code tags
  130. "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
  131. "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
  132. "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
  133. "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
  134. "<!-- opening comment" => "<p>&lt;!-- opening comment</p>"
  135. }
  136. to_test.each { |text, result| assert_equal result, textilizable(text) }
  137. end
  138. def test_wiki_links_in_tables
  139. to_test = {"|Cell 11|Cell 12|Cell 13|\n|Cell 21|Cell 22||\n|Cell 31||Cell 33|" =>
  140. '<tr><td>Cell 11</td><td>Cell 12</td><td>Cell 13</td></tr>' +
  141. '<tr><td>Cell 21</td><td>Cell 22</td></tr>' +
  142. '<tr><td>Cell 31</td><td>Cell 33</td></tr>',
  143. "|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
  144. '<tr><td><a href="/wiki/ecookbook/Page" class="wiki-page new">Link title</a></td>' +
  145. '<td><a href="/wiki/ecookbook/Other_Page" class="wiki-page new">Other title</a></td>' +
  146. '</tr><tr><td>Cell 21</td><td><a href="/wiki/ecookbook/Last_page" class="wiki-page new">Last page</a></td></tr>'
  147. }
  148. @project = Project.find(1)
  149. to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
  150. end
  151. def test_text_formatting
  152. to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
  153. '(_text within parentheses_)' => '(<em>text within parentheses</em>)'
  154. }
  155. to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
  156. end
  157. def test_wiki_horizontal_rule
  158. assert_equal '<hr />', textilizable('---')
  159. assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
  160. end
  161. def test_macro_hello_world
  162. text = "{{hello_world}}"
  163. assert textilizable(text).match(/Hello world!/)
  164. # escaping
  165. text = "!{{hello_world}}"
  166. assert_equal '<p>{{hello_world}}</p>', textilizable(text)
  167. end
  168. def test_macro_include
  169. @project = Project.find(1)
  170. # include a page of the current project wiki
  171. text = "{{include(Another page)}}"
  172. assert textilizable(text).match(/This is a link to a ticket/)
  173. @project = nil
  174. # include a page of a specific project wiki
  175. text = "{{include(ecookbook:Another page)}}"
  176. assert textilizable(text).match(/This is a link to a ticket/)
  177. text = "{{include(ecookbook:)}}"
  178. assert textilizable(text).match(/CookBook documentation/)
  179. text = "{{include(unknowidentifier:somepage)}}"
  180. assert textilizable(text).match(/Unknow project/)
  181. end
  182. def test_date_format_default
  183. today = Date.today
  184. Setting.date_format = ''
  185. assert_equal l_date(today), format_date(today)
  186. end
  187. def test_date_format
  188. today = Date.today
  189. Setting.date_format = '%d %m %Y'
  190. assert_equal today.strftime('%d %m %Y'), format_date(today)
  191. end
  192. def test_time_format_default
  193. now = Time.now
  194. Setting.date_format = ''
  195. Setting.time_format = ''
  196. assert_equal l_datetime(now), format_time(now)
  197. assert_equal l_time(now), format_time(now, false)
  198. end
  199. def test_time_format
  200. now = Time.now
  201. Setting.date_format = '%d %m %Y'
  202. Setting.time_format = '%H %M'
  203. assert_equal now.strftime('%d %m %Y %H %M'), format_time(now)
  204. assert_equal now.strftime('%H %M'), format_time(now, false)
  205. end
  206. end