##// END OF EJS Templates
replace non ASCII literal to hexadecimal at ApplicationHelperTest...
Toshi MARUYAMA -
r11648:11b24ab64bb5
parent child
Show More
@@ -1,1214 +1,1222
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2013 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require File.expand_path('../../../test_helper', __FILE__)
21 21
22 22 class ApplicationHelperTest < ActionView::TestCase
23 23 include Redmine::I18n
24 24 include ERB::Util
25 25 include Rails.application.routes.url_helpers
26 26
27 27 fixtures :projects, :roles, :enabled_modules, :users,
28 28 :repositories, :changesets,
29 29 :trackers, :issue_statuses, :issues, :versions, :documents,
30 30 :wikis, :wiki_pages, :wiki_contents,
31 31 :boards, :messages, :news,
32 32 :attachments, :enumerations
33 33
34 34 def setup
35 35 super
36 36 set_tmp_attachments_directory
37 @russian_test = "\xd1\x82\xd0\xb5\xd1\x81\xd1\x82"
38 if @russian_test.respond_to?(:force_encoding)
39 @russian_test.force_encoding('UTF-8')
40 end
37 41 end
38 42
39 43 test "#link_to_if_authorized for authorized user should allow using the :controller and :action for the target link" do
40 44 User.current = User.find_by_login('admin')
41 45
42 46 @project = Issue.first.project # Used by helper
43 47 response = link_to_if_authorized('By controller/actionr',
44 48 {:controller => 'issues', :action => 'edit', :id => Issue.first.id})
45 49 assert_match /href/, response
46 50 end
47 51
48 52 test "#link_to_if_authorized for unauthorized user should display nothing if user isn't authorized" do
49 53 User.current = User.find_by_login('dlopper')
50 54 @project = Project.find('private-child')
51 55 issue = @project.issues.first
52 56 assert !issue.visible?
53 57
54 58 response = link_to_if_authorized('Never displayed',
55 59 {:controller => 'issues', :action => 'show', :id => issue})
56 60 assert_nil response
57 61 end
58 62
59 63 def test_auto_links
60 64 to_test = {
61 65 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
62 66 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
63 67 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
64 68 'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
65 69 'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
66 70 'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
67 71 '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>.',
68 72 'http://www.foo.bar/Test_(foobar)' => '<a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
69 73 '(see inline link : http://www.foo.bar/Test_(foobar))' => '(see inline link : <a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>)',
70 74 '(see inline link : http://www.foo.bar/Test)' => '(see inline link : <a class="external" href="http://www.foo.bar/Test">http://www.foo.bar/Test</a>)',
71 75 '(see inline link : http://www.foo.bar/Test).' => '(see inline link : <a class="external" href="http://www.foo.bar/Test">http://www.foo.bar/Test</a>).',
72 76 '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" class="external">inline link</a>)',
73 77 '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
74 78 '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
75 79 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
76 80 '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>',
77 81 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
78 82 'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
79 83 'http://foo:bar@www.bar.com' => '<a class="external" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
80 84 'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
81 85 'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
82 86 'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
83 87 # two exclamation marks
84 88 'http://example.net/path!602815048C7B5C20!302.html' => '<a class="external" href="http://example.net/path!602815048C7B5C20!302.html">http://example.net/path!602815048C7B5C20!302.html</a>',
85 89 # escaping
86 90 'http://foo"bar' => '<a class="external" href="http://foo&quot;bar">http://foo&quot;bar</a>',
87 91 # wrap in angle brackets
88 92 '<http://foo.bar>' => '&lt;<a class="external" href="http://foo.bar">http://foo.bar</a>&gt;',
89 93 # invalid urls
90 94 'http://' => 'http://',
91 95 'www.' => 'www.',
92 96 'test-www.bar.com' => 'test-www.bar.com',
93 97 }
94 98 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
95 99 end
96 100
97 101 if 'ruby'.respond_to?(:encoding)
98 102 def test_auto_links_with_non_ascii_characters
99 103 to_test = {
100 'http://foo.bar/тСст' => '<a class="external" href="http://foo.bar/тСст">http://foo.bar/тСст</a>'
104 "http://foo.bar/#{@russian_test}" =>
105 %|<a class="external" href="http://foo.bar/#{@russian_test}">http://foo.bar/#{@russian_test}</a>|
101 106 }
102 107 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
103 108 end
104 109 else
105 110 puts 'Skipping test_auto_links_with_non_ascii_characters, unsupported ruby version'
106 111 end
107 112
108 113 def test_auto_mailto
109 114 to_test = {
110 115 'test@foo.bar' => '<a class="email" href="mailto:test@foo.bar">test@foo.bar</a>',
111 116 'test@www.foo.bar' => '<a class="email" href="mailto:test@www.foo.bar">test@www.foo.bar</a>',
112 117 }
113 118 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
114 119 end
115 120
116 121 def test_inline_images
117 122 to_test = {
118 123 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
119 124 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
120 125 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
121 126 'with style !{width:100px;height:100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" style="width:100px;height:100px;" alt="" />',
122 127 'with title !http://foo.bar/image.jpg(This is a title)!' => 'with title <img src="http://foo.bar/image.jpg" title="This is a title" alt="This is a title" />',
123 128 'with title !http://foo.bar/image.jpg(This is a double-quoted "title")!' => 'with title <img src="http://foo.bar/image.jpg" title="This is a double-quoted &quot;title&quot;" alt="This is a double-quoted &quot;title&quot;" />',
124 129 }
125 130 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
126 131 end
127 132
128 133 def test_inline_images_inside_tags
129 134 raw = <<-RAW
130 135 h1. !foo.png! Heading
131 136
132 137 Centered image:
133 138
134 139 p=. !bar.gif!
135 140 RAW
136 141
137 142 assert textilizable(raw).include?('<img src="foo.png" alt="" />')
138 143 assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
139 144 end
140 145
141 146 def test_attached_images
142 147 to_test = {
143 148 'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" />',
144 149 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" />',
145 150 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
146 151 'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />',
147 152 # link image
148 153 '!logo.gif!:http://foo.bar/' => '<a href="http://foo.bar/"><img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" /></a>',
149 154 }
150 155 attachments = Attachment.all
151 156 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
152 157 end
153 158
154 159 def test_attached_images_filename_extension
155 160 set_tmp_attachments_directory
156 161 a1 = Attachment.new(
157 162 :container => Issue.find(1),
158 163 :file => mock_file_with_options({:original_filename => "testtest.JPG"}),
159 164 :author => User.find(1))
160 165 assert a1.save
161 166 assert_equal "testtest.JPG", a1.filename
162 167 assert_equal "image/jpeg", a1.content_type
163 168 assert a1.image?
164 169
165 170 a2 = Attachment.new(
166 171 :container => Issue.find(1),
167 172 :file => mock_file_with_options({:original_filename => "testtest.jpeg"}),
168 173 :author => User.find(1))
169 174 assert a2.save
170 175 assert_equal "testtest.jpeg", a2.filename
171 176 assert_equal "image/jpeg", a2.content_type
172 177 assert a2.image?
173 178
174 179 a3 = Attachment.new(
175 180 :container => Issue.find(1),
176 181 :file => mock_file_with_options({:original_filename => "testtest.JPE"}),
177 182 :author => User.find(1))
178 183 assert a3.save
179 184 assert_equal "testtest.JPE", a3.filename
180 185 assert_equal "image/jpeg", a3.content_type
181 186 assert a3.image?
182 187
183 188 a4 = Attachment.new(
184 189 :container => Issue.find(1),
185 190 :file => mock_file_with_options({:original_filename => "Testtest.BMP"}),
186 191 :author => User.find(1))
187 192 assert a4.save
188 193 assert_equal "Testtest.BMP", a4.filename
189 194 assert_equal "image/x-ms-bmp", a4.content_type
190 195 assert a4.image?
191 196
192 197 to_test = {
193 198 'Inline image: !testtest.jpg!' =>
194 199 'Inline image: <img src="/attachments/download/' + a1.id.to_s + '/testtest.JPG" alt="" />',
195 200 'Inline image: !testtest.jpeg!' =>
196 201 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testtest.jpeg" alt="" />',
197 202 'Inline image: !testtest.jpe!' =>
198 203 'Inline image: <img src="/attachments/download/' + a3.id.to_s + '/testtest.JPE" alt="" />',
199 204 'Inline image: !testtest.bmp!' =>
200 205 'Inline image: <img src="/attachments/download/' + a4.id.to_s + '/Testtest.BMP" alt="" />',
201 206 }
202 207
203 208 attachments = [a1, a2, a3, a4]
204 209 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
205 210 end
206 211
207 212 def test_attached_images_should_read_later
208 213 set_fixtures_attachments_directory
209 214 a1 = Attachment.find(16)
210 215 assert_equal "testfile.png", a1.filename
211 216 assert a1.readable?
212 217 assert (! a1.visible?(User.anonymous))
213 218 assert a1.visible?(User.find(2))
214 219 a2 = Attachment.find(17)
215 220 assert_equal "testfile.PNG", a2.filename
216 221 assert a2.readable?
217 222 assert (! a2.visible?(User.anonymous))
218 223 assert a2.visible?(User.find(2))
219 224 assert a1.created_on < a2.created_on
220 225
221 226 to_test = {
222 227 'Inline image: !testfile.png!' =>
223 228 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testfile.PNG" alt="" />',
224 229 'Inline image: !Testfile.PNG!' =>
225 230 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testfile.PNG" alt="" />',
226 231 }
227 232 attachments = [a1, a2]
228 233 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
229 234 set_tmp_attachments_directory
230 235 end
231 236
232 237 def test_textile_external_links
233 238 to_test = {
234 239 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
235 240 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
236 241 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
237 242 '"link (Link title with "double-quotes")":http://foo.bar' => '<a href="http://foo.bar" title="Link title with &quot;double-quotes&quot;" class="external">link</a>',
238 243 "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
239 244 # no multiline link text
240 245 "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 />and another on a second line\":test",
241 246 # mailto link
242 247 "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
243 248 # two exclamation marks
244 249 '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
245 250 # escaping
246 251 '"test":http://foo"bar' => '<a href="http://foo&quot;bar" class="external">test</a>',
247 252 }
248 253 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
249 254 end
250 255
251 256 if 'ruby'.respond_to?(:encoding)
252 257 def test_textile_external_links_with_non_ascii_characters
253 258 to_test = {
254 'This is a "link":http://foo.bar/тСст' => 'This is a <a href="http://foo.bar/тСст" class="external">link</a>'
259 %|This is a "link":http://foo.bar/#{@russian_test}| =>
260 %|This is a <a href="http://foo.bar/#{@russian_test}" class="external">link</a>|
255 261 }
256 262 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
257 263 end
258 264 else
259 265 puts 'Skipping test_textile_external_links_with_non_ascii_characters, unsupported ruby version'
260 266 end
261 267
262 268 def test_redmine_links
263 269 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
264 270 :class => Issue.find(3).css_classes, :title => 'Error 281 when updating a recipe (New)')
265 271 note_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3, :anchor => 'note-14'},
266 272 :class => Issue.find(3).css_classes, :title => 'Error 281 when updating a recipe (New)')
267 273
268 274 revision_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
269 275 :class => 'changeset', :title => 'My very first commit do not escaping #<>&')
270 276 revision_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
271 277 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
272 278
273 279 changeset_link2 = link_to('691322a8eb01e11fd7',
274 280 {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
275 281 :class => 'changeset', :title => 'My very first commit do not escaping #<>&')
276 282
277 283 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
278 284 :class => 'document')
279 285
280 286 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
281 287 :class => 'version')
282 288
283 289 board_url = {:controller => 'boards', :action => 'show', :id => 2, :project_id => 'ecookbook'}
284 290
285 291 message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
286 292
287 293 news_url = {:controller => 'news', :action => 'show', :id => 1}
288 294
289 295 project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'}
290 296
291 297 source_url = '/projects/ecookbook/repository/entry/some/file'
292 298 source_url_with_rev = '/projects/ecookbook/repository/revisions/52/entry/some/file'
293 299 source_url_with_ext = '/projects/ecookbook/repository/entry/some/file.ext'
294 300 source_url_with_rev_and_ext = '/projects/ecookbook/repository/revisions/52/entry/some/file.ext'
295 301 source_url_with_branch = '/projects/ecookbook/repository/revisions/branch/entry/some/file'
296 302
297 303 export_url = '/projects/ecookbook/repository/raw/some/file'
298 304 export_url_with_rev = '/projects/ecookbook/repository/revisions/52/raw/some/file'
299 305 export_url_with_ext = '/projects/ecookbook/repository/raw/some/file.ext'
300 306 export_url_with_rev_and_ext = '/projects/ecookbook/repository/revisions/52/raw/some/file.ext'
301 307 export_url_with_branch = '/projects/ecookbook/repository/revisions/branch/raw/some/file'
302 308
303 309 to_test = {
304 310 # tickets
305 311 '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.",
306 312 # ticket notes
307 313 '#3-14' => note_link,
308 314 '#3#note-14' => note_link,
309 315 # should not ignore leading zero
310 316 '#03' => '#03',
311 317 # changesets
312 318 'r1' => revision_link,
313 319 'r1.' => "#{revision_link}.",
314 320 'r1, r2' => "#{revision_link}, #{revision_link2}",
315 321 'r1,r2' => "#{revision_link},#{revision_link2}",
316 322 'commit:691322a8eb01e11fd7' => changeset_link2,
317 323 # documents
318 324 'document#1' => document_link,
319 325 'document:"Test document"' => document_link,
320 326 # versions
321 327 'version#2' => version_link,
322 328 'version:1.0' => version_link,
323 329 'version:"1.0"' => version_link,
324 330 # source
325 331 'source:some/file' => link_to('source:some/file', source_url, :class => 'source'),
326 332 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
327 333 'source:/some/file.' => link_to('source:/some/file', source_url, :class => 'source') + ".",
328 334 'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
329 335 'source:/some/file. ' => link_to('source:/some/file', source_url, :class => 'source') + ".",
330 336 'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
331 337 'source:/some/file, ' => link_to('source:/some/file', source_url, :class => 'source') + ",",
332 338 'source:/some/file@52' => link_to('source:/some/file@52', source_url_with_rev, :class => 'source'),
333 339 'source:/some/file@branch' => link_to('source:/some/file@branch', source_url_with_branch, :class => 'source'),
334 340 'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_rev_and_ext, :class => 'source'),
335 341 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url + "#L110", :class => 'source'),
336 342 'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext + "#L110", :class => 'source'),
337 343 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url_with_rev + "#L110", :class => 'source'),
338 344 # export
339 345 'export:/some/file' => link_to('export:/some/file', export_url, :class => 'source download'),
340 346 'export:/some/file.ext' => link_to('export:/some/file.ext', export_url_with_ext, :class => 'source download'),
341 347 'export:/some/file@52' => link_to('export:/some/file@52', export_url_with_rev, :class => 'source download'),
342 348 'export:/some/file.ext@52' => link_to('export:/some/file.ext@52', export_url_with_rev_and_ext, :class => 'source download'),
343 349 'export:/some/file@branch' => link_to('export:/some/file@branch', export_url_with_branch, :class => 'source download'),
344 350 # forum
345 351 'forum#2' => link_to('Discussion', board_url, :class => 'board'),
346 352 'forum:Discussion' => link_to('Discussion', board_url, :class => 'board'),
347 353 # message
348 354 'message#4' => link_to('Post 2', message_url, :class => 'message'),
349 355 'message#5' => link_to('RE: post 2', message_url.merge(:anchor => 'message-5', :r => 5), :class => 'message'),
350 356 # news
351 357 'news#1' => link_to('eCookbook first release !', news_url, :class => 'news'),
352 358 'news:"eCookbook first release !"' => link_to('eCookbook first release !', news_url, :class => 'news'),
353 359 # project
354 360 'project#3' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
355 361 'project:subproject1' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
356 362 'project:"eCookbook subProject 1"' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
357 363 # not found
358 364 '#0123456789' => '#0123456789',
359 365 # invalid expressions
360 366 'source:' => 'source:',
361 367 # url hash
362 368 "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
363 369 }
364 370 @project = Project.find(1)
365 371 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
366 372 end
367 373
368 374 def test_redmine_links_with_a_different_project_before_current_project
369 375 vp1 = Version.generate!(:project_id => 1, :name => '1.4.4')
370 376 vp3 = Version.generate!(:project_id => 3, :name => '1.4.4')
371 377
372 378 @project = Project.find(3)
373 379 assert_equal %(<p><a href="/versions/#{vp1.id}" class="version">1.4.4</a> <a href="/versions/#{vp3.id}" class="version">1.4.4</a></p>),
374 380 textilizable("ecookbook:version:1.4.4 version:1.4.4")
375 381 end
376 382
377 383 def test_escaped_redmine_links_should_not_be_parsed
378 384 to_test = [
379 385 '#3.',
380 386 '#3-14.',
381 387 '#3#-note14.',
382 388 'r1',
383 389 'document#1',
384 390 'document:"Test document"',
385 391 'version#2',
386 392 'version:1.0',
387 393 'version:"1.0"',
388 394 'source:/some/file'
389 395 ]
390 396 @project = Project.find(1)
391 397 to_test.each { |text| assert_equal "<p>#{text}</p>", textilizable("!" + text), "#{text} failed" }
392 398 end
393 399
394 400 def test_cross_project_redmine_links
395 401 source_link = link_to('ecookbook:source:/some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']},
396 402 :class => 'source')
397 403
398 404 changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
399 405 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
400 406
401 407 to_test = {
402 408 # documents
403 409 'document:"Test document"' => 'document:"Test document"',
404 410 'ecookbook:document:"Test document"' => '<a href="/documents/1" class="document">Test document</a>',
405 411 'invalid:document:"Test document"' => 'invalid:document:"Test document"',
406 412 # versions
407 413 'version:"1.0"' => 'version:"1.0"',
408 414 'ecookbook:version:"1.0"' => '<a href="/versions/2" class="version">1.0</a>',
409 415 'invalid:version:"1.0"' => 'invalid:version:"1.0"',
410 416 # changeset
411 417 'r2' => 'r2',
412 418 'ecookbook:r2' => changeset_link,
413 419 'invalid:r2' => 'invalid:r2',
414 420 # source
415 421 'source:/some/file' => 'source:/some/file',
416 422 'ecookbook:source:/some/file' => source_link,
417 423 'invalid:source:/some/file' => 'invalid:source:/some/file',
418 424 }
419 425 @project = Project.find(3)
420 426 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
421 427 end
422 428
423 429 def test_multiple_repositories_redmine_links
424 430 svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn_repo-1', :url => 'file:///foo/hg')
425 431 Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
426 432 hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
427 433 Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
428 434
429 435 changeset_link = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
430 436 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
431 437 svn_changeset_link = link_to('svn_repo-1|r123', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'svn_repo-1', :rev => 123},
432 438 :class => 'changeset', :title => '')
433 439 hg_changeset_link = link_to('hg1|abcd', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
434 440 :class => 'changeset', :title => '')
435 441
436 442 source_link = link_to('source:some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, :class => 'source')
437 443 hg_source_link = link_to('source:hg1|some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :repository_id => 'hg1', :path => ['some', 'file']}, :class => 'source')
438 444
439 445 to_test = {
440 446 'r2' => changeset_link,
441 447 'svn_repo-1|r123' => svn_changeset_link,
442 448 'invalid|r123' => 'invalid|r123',
443 449 'commit:hg1|abcd' => hg_changeset_link,
444 450 'commit:invalid|abcd' => 'commit:invalid|abcd',
445 451 # source
446 452 'source:some/file' => source_link,
447 453 'source:hg1|some/file' => hg_source_link,
448 454 'source:invalid|some/file' => 'source:invalid|some/file',
449 455 }
450 456
451 457 @project = Project.find(1)
452 458 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
453 459 end
454 460
455 461 def test_cross_project_multiple_repositories_redmine_links
456 462 svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn1', :url => 'file:///foo/hg')
457 463 Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
458 464 hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
459 465 Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
460 466
461 467 changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
462 468 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
463 469 svn_changeset_link = link_to('ecookbook:svn1|r123', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'svn1', :rev => 123},
464 470 :class => 'changeset', :title => '')
465 471 hg_changeset_link = link_to('ecookbook:hg1|abcd', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
466 472 :class => 'changeset', :title => '')
467 473
468 474 source_link = link_to('ecookbook:source:some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, :class => 'source')
469 475 hg_source_link = link_to('ecookbook:source:hg1|some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :repository_id => 'hg1', :path => ['some', 'file']}, :class => 'source')
470 476
471 477 to_test = {
472 478 'ecookbook:r2' => changeset_link,
473 479 'ecookbook:svn1|r123' => svn_changeset_link,
474 480 'ecookbook:invalid|r123' => 'ecookbook:invalid|r123',
475 481 'ecookbook:commit:hg1|abcd' => hg_changeset_link,
476 482 'ecookbook:commit:invalid|abcd' => 'ecookbook:commit:invalid|abcd',
477 483 'invalid:commit:invalid|abcd' => 'invalid:commit:invalid|abcd',
478 484 # source
479 485 'ecookbook:source:some/file' => source_link,
480 486 'ecookbook:source:hg1|some/file' => hg_source_link,
481 487 'ecookbook:source:invalid|some/file' => 'ecookbook:source:invalid|some/file',
482 488 'invalid:source:invalid|some/file' => 'invalid:source:invalid|some/file',
483 489 }
484 490
485 491 @project = Project.find(3)
486 492 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
487 493 end
488 494
489 495 def test_redmine_links_git_commit
490 496 changeset_link = link_to('abcd',
491 497 {
492 498 :controller => 'repositories',
493 499 :action => 'revision',
494 500 :id => 'subproject1',
495 501 :rev => 'abcd',
496 502 },
497 503 :class => 'changeset', :title => 'test commit')
498 504 to_test = {
499 505 'commit:abcd' => changeset_link,
500 506 }
501 507 @project = Project.find(3)
502 508 r = Repository::Git.create!(:project => @project, :url => '/tmp/test/git')
503 509 assert r
504 510 c = Changeset.new(:repository => r,
505 511 :committed_on => Time.now,
506 512 :revision => 'abcd',
507 513 :scmid => 'abcd',
508 514 :comments => 'test commit')
509 515 assert( c.save )
510 516 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
511 517 end
512 518
513 519 # TODO: Bazaar commit id contains mail address, so it contains '@' and '_'.
514 520 def test_redmine_links_darcs_commit
515 521 changeset_link = link_to('20080308225258-98289-abcd456efg.gz',
516 522 {
517 523 :controller => 'repositories',
518 524 :action => 'revision',
519 525 :id => 'subproject1',
520 526 :rev => '123',
521 527 },
522 528 :class => 'changeset', :title => 'test commit')
523 529 to_test = {
524 530 'commit:20080308225258-98289-abcd456efg.gz' => changeset_link,
525 531 }
526 532 @project = Project.find(3)
527 533 r = Repository::Darcs.create!(
528 534 :project => @project, :url => '/tmp/test/darcs',
529 535 :log_encoding => 'UTF-8')
530 536 assert r
531 537 c = Changeset.new(:repository => r,
532 538 :committed_on => Time.now,
533 539 :revision => '123',
534 540 :scmid => '20080308225258-98289-abcd456efg.gz',
535 541 :comments => 'test commit')
536 542 assert( c.save )
537 543 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
538 544 end
539 545
540 546 def test_redmine_links_mercurial_commit
541 547 changeset_link_rev = link_to('r123',
542 548 {
543 549 :controller => 'repositories',
544 550 :action => 'revision',
545 551 :id => 'subproject1',
546 552 :rev => '123' ,
547 553 },
548 554 :class => 'changeset', :title => 'test commit')
549 555 changeset_link_commit = link_to('abcd',
550 556 {
551 557 :controller => 'repositories',
552 558 :action => 'revision',
553 559 :id => 'subproject1',
554 560 :rev => 'abcd' ,
555 561 },
556 562 :class => 'changeset', :title => 'test commit')
557 563 to_test = {
558 564 'r123' => changeset_link_rev,
559 565 'commit:abcd' => changeset_link_commit,
560 566 }
561 567 @project = Project.find(3)
562 568 r = Repository::Mercurial.create!(:project => @project, :url => '/tmp/test')
563 569 assert r
564 570 c = Changeset.new(:repository => r,
565 571 :committed_on => Time.now,
566 572 :revision => '123',
567 573 :scmid => 'abcd',
568 574 :comments => 'test commit')
569 575 assert( c.save )
570 576 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
571 577 end
572 578
573 579 def test_attachment_links
574 580 to_test = {
575 581 'attachment:error281.txt' => '<a href="/attachments/download/1/error281.txt" class="attachment">error281.txt</a>'
576 582 }
577 583 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => Issue.find(3).attachments), "#{text} failed" }
578 584 end
579 585
580 586 def test_attachment_link_should_link_to_latest_attachment
581 587 set_tmp_attachments_directory
582 588 a1 = Attachment.generate!(:filename => "test.txt", :created_on => 1.hour.ago)
583 589 a2 = Attachment.generate!(:filename => "test.txt")
584 590
585 591 assert_equal %(<p><a href="/attachments/download/#{a2.id}/test.txt" class="attachment">test.txt</a></p>),
586 592 textilizable('attachment:test.txt', :attachments => [a1, a2])
587 593 end
588 594
589 595 def test_wiki_links
596 russian_eacape = CGI.escape(@russian_test)
590 597 to_test = {
591 598 '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
592 599 '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
593 600 # title content should be formatted
594 601 '[[Another page|With _styled_ *title*]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">With <em>styled</em> <strong>title</strong></a>',
595 602 '[[Another page|With title containing <strong>HTML entities &amp; markups</strong>]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">With title containing &lt;strong&gt;HTML entities &amp; markups&lt;/strong&gt;</a>',
596 603 # link with anchor
597 604 '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
598 605 '[[Another page#anchor|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page#anchor" class="wiki-page">Page</a>',
599 606 # UTF8 anchor
600 '[[Another_page#ВСст|ВСст]]' => %|<a href="/projects/ecookbook/wiki/Another_page##{CGI.escape 'ВСст'}" class="wiki-page">ВСст</a>|,
607 "[[Another_page##{@russian_test}|#{@russian_test}]]" =>
608 %|<a href="/projects/ecookbook/wiki/Another_page##{russian_eacape}" class="wiki-page">#{@russian_test}</a>|,
601 609 # page that doesn't exist
602 610 '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
603 611 '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">404</a>',
604 612 # link to another project wiki
605 613 '[[onlinestore:]]' => '<a href="/projects/onlinestore/wiki" class="wiki-page">onlinestore</a>',
606 614 '[[onlinestore:|Wiki]]' => '<a href="/projects/onlinestore/wiki" class="wiki-page">Wiki</a>',
607 615 '[[onlinestore:Start page]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Start page</a>',
608 616 '[[onlinestore:Start page|Text]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Text</a>',
609 617 '[[onlinestore:Unknown page]]' => '<a href="/projects/onlinestore/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
610 618 # striked through link
611 619 '-[[Another page|Page]]-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a></del>',
612 620 '-[[Another page|Page]] link-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a> link</del>',
613 621 # escaping
614 622 '![[Another page|Page]]' => '[[Another page|Page]]',
615 623 # project does not exist
616 624 '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
617 625 '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
618 626 }
619 627
620 628 @project = Project.find(1)
621 629 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
622 630 end
623 631
624 632 def test_wiki_links_within_local_file_generation_context
625 633
626 634 to_test = {
627 635 # link to a page
628 636 '[[CookBook documentation]]' => '<a href="CookBook_documentation.html" class="wiki-page">CookBook documentation</a>',
629 637 '[[CookBook documentation|documentation]]' => '<a href="CookBook_documentation.html" class="wiki-page">documentation</a>',
630 638 '[[CookBook documentation#One-section]]' => '<a href="CookBook_documentation.html#One-section" class="wiki-page">CookBook documentation</a>',
631 639 '[[CookBook documentation#One-section|documentation]]' => '<a href="CookBook_documentation.html#One-section" class="wiki-page">documentation</a>',
632 640 # page that doesn't exist
633 641 '[[Unknown page]]' => '<a href="Unknown_page.html" class="wiki-page new">Unknown page</a>',
634 642 '[[Unknown page|404]]' => '<a href="Unknown_page.html" class="wiki-page new">404</a>',
635 643 '[[Unknown page#anchor]]' => '<a href="Unknown_page.html#anchor" class="wiki-page new">Unknown page</a>',
636 644 '[[Unknown page#anchor|404]]' => '<a href="Unknown_page.html#anchor" class="wiki-page new">404</a>',
637 645 }
638 646
639 647 @project = Project.find(1)
640 648
641 649 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :local) }
642 650 end
643 651
644 652 def test_wiki_links_within_wiki_page_context
645 653
646 654 page = WikiPage.find_by_title('Another_page' )
647 655
648 656 to_test = {
649 657 # link to another page
650 658 '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
651 659 '[[CookBook documentation|documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">documentation</a>',
652 660 '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
653 661 '[[CookBook documentation#One-section|documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">documentation</a>',
654 662 # link to the current page
655 663 '[[Another page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Another page</a>',
656 664 '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
657 665 '[[Another page#anchor]]' => '<a href="#anchor" class="wiki-page">Another page</a>',
658 666 '[[Another page#anchor|Page]]' => '<a href="#anchor" class="wiki-page">Page</a>',
659 667 # page that doesn't exist
660 668 '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page" class="wiki-page new">Unknown page</a>',
661 669 '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page" class="wiki-page new">404</a>',
662 670 '[[Unknown page#anchor]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor" class="wiki-page new">Unknown page</a>',
663 671 '[[Unknown page#anchor|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor" class="wiki-page new">404</a>',
664 672 }
665 673
666 674 @project = Project.find(1)
667 675
668 676 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(WikiContent.new( :text => text, :page => page ), :text) }
669 677 end
670 678
671 679 def test_wiki_links_anchor_option_should_prepend_page_title_to_href
672 680
673 681 to_test = {
674 682 # link to a page
675 683 '[[CookBook documentation]]' => '<a href="#CookBook_documentation" class="wiki-page">CookBook documentation</a>',
676 684 '[[CookBook documentation|documentation]]' => '<a href="#CookBook_documentation" class="wiki-page">documentation</a>',
677 685 '[[CookBook documentation#One-section]]' => '<a href="#CookBook_documentation_One-section" class="wiki-page">CookBook documentation</a>',
678 686 '[[CookBook documentation#One-section|documentation]]' => '<a href="#CookBook_documentation_One-section" class="wiki-page">documentation</a>',
679 687 # page that doesn't exist
680 688 '[[Unknown page]]' => '<a href="#Unknown_page" class="wiki-page new">Unknown page</a>',
681 689 '[[Unknown page|404]]' => '<a href="#Unknown_page" class="wiki-page new">404</a>',
682 690 '[[Unknown page#anchor]]' => '<a href="#Unknown_page_anchor" class="wiki-page new">Unknown page</a>',
683 691 '[[Unknown page#anchor|404]]' => '<a href="#Unknown_page_anchor" class="wiki-page new">404</a>',
684 692 }
685 693
686 694 @project = Project.find(1)
687 695
688 696 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :anchor) }
689 697 end
690 698
691 699 def test_html_tags
692 700 to_test = {
693 701 "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
694 702 "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
695 703 "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
696 704 # do not escape pre/code tags
697 705 "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
698 706 "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
699 707 "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
700 708 "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
701 709 "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
702 710 # remove attributes except class
703 711 "<pre class='foo'>some text</pre>" => "<pre class='foo'>some text</pre>",
704 712 '<pre class="foo">some text</pre>' => '<pre class="foo">some text</pre>',
705 713 "<pre class='foo bar'>some text</pre>" => "<pre class='foo bar'>some text</pre>",
706 714 '<pre class="foo bar">some text</pre>' => '<pre class="foo bar">some text</pre>',
707 715 "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
708 716 # xss
709 717 '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>',
710 718 '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>',
711 719 }
712 720 to_test.each { |text, result| assert_equal result, textilizable(text) }
713 721 end
714 722
715 723 def test_allowed_html_tags
716 724 to_test = {
717 725 "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
718 726 "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
719 727 "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
720 728 }
721 729 to_test.each { |text, result| assert_equal result, textilizable(text) }
722 730 end
723 731
724 732 def test_pre_tags
725 733 raw = <<-RAW
726 734 Before
727 735
728 736 <pre>
729 737 <prepared-statement-cache-size>32</prepared-statement-cache-size>
730 738 </pre>
731 739
732 740 After
733 741 RAW
734 742
735 743 expected = <<-EXPECTED
736 744 <p>Before</p>
737 745 <pre>
738 746 &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
739 747 </pre>
740 748 <p>After</p>
741 749 EXPECTED
742 750
743 751 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
744 752 end
745 753
746 754 def test_pre_content_should_not_parse_wiki_and_redmine_links
747 755 raw = <<-RAW
748 756 [[CookBook documentation]]
749 757
750 758 #1
751 759
752 760 <pre>
753 761 [[CookBook documentation]]
754 762
755 763 #1
756 764 </pre>
757 765 RAW
758 766
759 767 expected = <<-EXPECTED
760 768 <p><a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a></p>
761 769 <p><a href="/issues/1" class="#{Issue.find(1).css_classes}" title="Can&#x27;t print recipes (New)">#1</a></p>
762 770 <pre>
763 771 [[CookBook documentation]]
764 772
765 773 #1
766 774 </pre>
767 775 EXPECTED
768 776
769 777 @project = Project.find(1)
770 778 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
771 779 end
772 780
773 781 def test_non_closing_pre_blocks_should_be_closed
774 782 raw = <<-RAW
775 783 <pre><code>
776 784 RAW
777 785
778 786 expected = <<-EXPECTED
779 787 <pre><code>
780 788 </code></pre>
781 789 EXPECTED
782 790
783 791 @project = Project.find(1)
784 792 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
785 793 end
786 794
787 795 def test_syntax_highlight
788 796 raw = <<-RAW
789 797 <pre><code class="ruby">
790 798 # Some ruby code here
791 799 </code></pre>
792 800 RAW
793 801
794 802 expected = <<-EXPECTED
795 803 <pre><code class="ruby syntaxhl"><span class=\"CodeRay\"><span class="comment"># Some ruby code here</span></span>
796 804 </code></pre>
797 805 EXPECTED
798 806
799 807 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
800 808 end
801 809
802 810 def test_to_path_param
803 811 assert_equal 'test1/test2', to_path_param('test1/test2')
804 812 assert_equal 'test1/test2', to_path_param('/test1/test2/')
805 813 assert_equal 'test1/test2', to_path_param('//test1/test2/')
806 814 assert_equal nil, to_path_param('/')
807 815 end
808 816
809 817 def test_wiki_links_in_tables
810 818 to_test = {"|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
811 819 '<tr><td><a href="/projects/ecookbook/wiki/Page" class="wiki-page new">Link title</a></td>' +
812 820 '<td><a href="/projects/ecookbook/wiki/Other_Page" class="wiki-page new">Other title</a></td>' +
813 821 '</tr><tr><td>Cell 21</td><td><a href="/projects/ecookbook/wiki/Last_page" class="wiki-page new">Last page</a></td></tr>'
814 822 }
815 823 @project = Project.find(1)
816 824 to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
817 825 end
818 826
819 827 def test_text_formatting
820 828 to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
821 829 '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
822 830 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
823 831 'a H *umane* W *eb* T *ext* G *enerator*' => 'a H <strong>umane</strong> W <strong>eb</strong> T <strong>ext</strong> G <strong>enerator</strong>',
824 832 'a *H* umane *W* eb *T* ext *G* enerator' => 'a <strong>H</strong> umane <strong>W</strong> eb <strong>T</strong> ext <strong>G</strong> enerator',
825 833 }
826 834 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
827 835 end
828 836
829 837 def test_wiki_horizontal_rule
830 838 assert_equal '<hr />', textilizable('---')
831 839 assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
832 840 end
833 841
834 842 def test_footnotes
835 843 raw = <<-RAW
836 844 This is some text[1].
837 845
838 846 fn1. This is the foot note
839 847 RAW
840 848
841 849 expected = <<-EXPECTED
842 850 <p>This is some text<sup><a href=\"#fn1\">1</a></sup>.</p>
843 851 <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
844 852 EXPECTED
845 853
846 854 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
847 855 end
848 856
849 857 def test_headings
850 858 raw = 'h1. Some heading'
851 859 expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>|
852 860
853 861 assert_equal expected, textilizable(raw)
854 862 end
855 863
856 864 def test_headings_with_special_chars
857 865 # This test makes sure that the generated anchor names match the expected
858 866 # ones even if the heading text contains unconventional characters
859 867 raw = 'h1. Some heading related to version 0.5'
860 868 anchor = sanitize_anchor_name("Some-heading-related-to-version-0.5")
861 869 expected = %|<a name="#{anchor}"></a>\n<h1 >Some heading related to version 0.5<a href="##{anchor}" class="wiki-anchor">&para;</a></h1>|
862 870
863 871 assert_equal expected, textilizable(raw)
864 872 end
865 873
866 874 def test_headings_in_wiki_single_page_export_should_be_prepended_with_page_title
867 875 page = WikiPage.new( :title => 'Page Title', :wiki_id => 1 )
868 876 content = WikiContent.new( :text => 'h1. Some heading', :page => page )
869 877
870 878 expected = %|<a name="Page_Title_Some-heading"></a>\n<h1 >Some heading<a href="#Page_Title_Some-heading" class="wiki-anchor">&para;</a></h1>|
871 879
872 880 assert_equal expected, textilizable(content, :text, :wiki_links => :anchor )
873 881 end
874 882
875 883 def test_table_of_content
876 884 raw = <<-RAW
877 885 {{toc}}
878 886
879 887 h1. Title
880 888
881 889 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
882 890
883 891 h2. Subtitle with a [[Wiki]] link
884 892
885 893 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
886 894
887 895 h2. Subtitle with [[Wiki|another Wiki]] link
888 896
889 897 h2. Subtitle with %{color:red}red text%
890 898
891 899 <pre>
892 900 some code
893 901 </pre>
894 902
895 903 h3. Subtitle with *some* _modifiers_
896 904
897 905 h3. Subtitle with @inline code@
898 906
899 907 h1. Another title
900 908
901 909 h3. An "Internet link":http://www.redmine.org/ inside subtitle
902 910
903 911 h2. "Project Name !/attachments/1234/logo_small.gif! !/attachments/5678/logo_2.png!":/projects/projectname/issues
904 912
905 913 RAW
906 914
907 915 expected = '<ul class="toc">' +
908 916 '<li><a href="#Title">Title</a>' +
909 917 '<ul>' +
910 918 '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
911 919 '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
912 920 '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' +
913 921 '<ul>' +
914 922 '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' +
915 923 '<li><a href="#Subtitle-with-inline-code">Subtitle with inline code</a></li>' +
916 924 '</ul>' +
917 925 '</li>' +
918 926 '</ul>' +
919 927 '</li>' +
920 928 '<li><a href="#Another-title">Another title</a>' +
921 929 '<ul>' +
922 930 '<li>' +
923 931 '<ul>' +
924 932 '<li><a href="#An-Internet-link-inside-subtitle">An Internet link inside subtitle</a></li>' +
925 933 '</ul>' +
926 934 '</li>' +
927 935 '<li><a href="#Project-Name">Project Name</a></li>' +
928 936 '</ul>' +
929 937 '</li>' +
930 938 '</ul>'
931 939
932 940 @project = Project.find(1)
933 941 assert textilizable(raw).gsub("\n", "").include?(expected)
934 942 end
935 943
936 944 def test_table_of_content_should_generate_unique_anchors
937 945 raw = <<-RAW
938 946 {{toc}}
939 947
940 948 h1. Title
941 949
942 950 h2. Subtitle
943 951
944 952 h2. Subtitle
945 953 RAW
946 954
947 955 expected = '<ul class="toc">' +
948 956 '<li><a href="#Title">Title</a>' +
949 957 '<ul>' +
950 958 '<li><a href="#Subtitle">Subtitle</a></li>' +
951 959 '<li><a href="#Subtitle-2">Subtitle</a></li>'
952 960 '</ul>'
953 961 '</li>' +
954 962 '</ul>'
955 963
956 964 @project = Project.find(1)
957 965 result = textilizable(raw).gsub("\n", "")
958 966 assert_include expected, result
959 967 assert_include '<a name="Subtitle">', result
960 968 assert_include '<a name="Subtitle-2">', result
961 969 end
962 970
963 971 def test_table_of_content_should_contain_included_page_headings
964 972 raw = <<-RAW
965 973 {{toc}}
966 974
967 975 h1. Included
968 976
969 977 {{include(Child_1)}}
970 978 RAW
971 979
972 980 expected = '<ul class="toc">' +
973 981 '<li><a href="#Included">Included</a></li>' +
974 982 '<li><a href="#Child-page-1">Child page 1</a></li>' +
975 983 '</ul>'
976 984
977 985 @project = Project.find(1)
978 986 assert textilizable(raw).gsub("\n", "").include?(expected)
979 987 end
980 988
981 989 def test_section_edit_links
982 990 raw = <<-RAW
983 991 h1. Title
984 992
985 993 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
986 994
987 995 h2. Subtitle with a [[Wiki]] link
988 996
989 997 h2. Subtitle with *some* _modifiers_
990 998
991 999 h2. Subtitle with @inline code@
992 1000
993 1001 <pre>
994 1002 some code
995 1003
996 1004 h2. heading inside pre
997 1005
998 1006 <h2>html heading inside pre</h2>
999 1007 </pre>
1000 1008
1001 1009 h2. Subtitle after pre tag
1002 1010 RAW
1003 1011
1004 1012 @project = Project.find(1)
1005 1013 set_language_if_valid 'en'
1006 1014 result = textilizable(raw, :edit_section_links => {:controller => 'wiki', :action => 'edit', :project_id => '1', :id => 'Test'}).gsub("\n", "")
1007 1015
1008 1016 # heading that contains inline code
1009 1017 assert_match Regexp.new('<div class="contextual" title="Edit this section">' +
1010 1018 '<a href="/projects/1/wiki/Test/edit\?section=4"><img alt="Edit" src="/images/edit.png(\?\d+)?" /></a></div>' +
1011 1019 '<a name="Subtitle-with-inline-code"></a>' +
1012 1020 '<h2 >Subtitle with <code>inline code</code><a href="#Subtitle-with-inline-code" class="wiki-anchor">&para;</a></h2>'),
1013 1021 result
1014 1022
1015 1023 # last heading
1016 1024 assert_match Regexp.new('<div class="contextual" title="Edit this section">' +
1017 1025 '<a href="/projects/1/wiki/Test/edit\?section=5"><img alt="Edit" src="/images/edit.png(\?\d+)?" /></a></div>' +
1018 1026 '<a name="Subtitle-after-pre-tag"></a>' +
1019 1027 '<h2 >Subtitle after pre tag<a href="#Subtitle-after-pre-tag" class="wiki-anchor">&para;</a></h2>'),
1020 1028 result
1021 1029 end
1022 1030
1023 1031 def test_default_formatter
1024 1032 with_settings :text_formatting => 'unknown' do
1025 1033 text = 'a *link*: http://www.example.net/'
1026 1034 assert_equal '<p>a *link*: <a class="external" href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
1027 1035 end
1028 1036 end
1029 1037
1030 1038 def test_due_date_distance_in_words
1031 1039 to_test = { Date.today => 'Due in 0 days',
1032 1040 Date.today + 1 => 'Due in 1 day',
1033 1041 Date.today + 100 => 'Due in about 3 months',
1034 1042 Date.today + 20000 => 'Due in over 54 years',
1035 1043 Date.today - 1 => '1 day late',
1036 1044 Date.today - 100 => 'about 3 months late',
1037 1045 Date.today - 20000 => 'over 54 years late',
1038 1046 }
1039 1047 ::I18n.locale = :en
1040 1048 to_test.each do |date, expected|
1041 1049 assert_equal expected, due_date_distance_in_words(date)
1042 1050 end
1043 1051 end
1044 1052
1045 1053 def test_avatar_enabled
1046 1054 with_settings :gravatar_enabled => '1' do
1047 1055 assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
1048 1056 assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
1049 1057 # Default size is 50
1050 1058 assert avatar('jsmith <jsmith@somenet.foo>').include?('size=50')
1051 1059 assert avatar('jsmith <jsmith@somenet.foo>', :size => 24).include?('size=24')
1052 1060 # Non-avatar options should be considered html options
1053 1061 assert avatar('jsmith <jsmith@somenet.foo>', :title => 'John Smith').include?('title="John Smith"')
1054 1062 # The default class of the img tag should be gravatar
1055 1063 assert avatar('jsmith <jsmith@somenet.foo>').include?('class="gravatar"')
1056 1064 assert !avatar('jsmith <jsmith@somenet.foo>', :class => 'picture').include?('class="gravatar"')
1057 1065 assert_nil avatar('jsmith')
1058 1066 assert_nil avatar(nil)
1059 1067 end
1060 1068 end
1061 1069
1062 1070 def test_avatar_disabled
1063 1071 with_settings :gravatar_enabled => '0' do
1064 1072 assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
1065 1073 end
1066 1074 end
1067 1075
1068 1076 def test_link_to_user
1069 1077 user = User.find(2)
1070 1078 assert_equal '<a href="/users/2" class="user active">John Smith</a>', link_to_user(user)
1071 1079 end
1072 1080
1073 1081 def test_link_to_user_should_not_link_to_locked_user
1074 1082 with_current_user nil do
1075 1083 user = User.find(5)
1076 1084 assert user.locked?
1077 1085 assert_equal 'Dave2 Lopper2', link_to_user(user)
1078 1086 end
1079 1087 end
1080 1088
1081 1089 def test_link_to_user_should_link_to_locked_user_if_current_user_is_admin
1082 1090 with_current_user User.find(1) do
1083 1091 user = User.find(5)
1084 1092 assert user.locked?
1085 1093 assert_equal '<a href="/users/5" class="user locked">Dave2 Lopper2</a>', link_to_user(user)
1086 1094 end
1087 1095 end
1088 1096
1089 1097 def test_link_to_user_should_not_link_to_anonymous
1090 1098 user = User.anonymous
1091 1099 assert user.anonymous?
1092 1100 t = link_to_user(user)
1093 1101 assert_equal ::I18n.t(:label_user_anonymous), t
1094 1102 end
1095 1103
1096 1104 def test_link_to_attachment
1097 1105 a = Attachment.find(3)
1098 1106 assert_equal '<a href="/attachments/3/logo.gif">logo.gif</a>',
1099 1107 link_to_attachment(a)
1100 1108 assert_equal '<a href="/attachments/3/logo.gif">Text</a>',
1101 1109 link_to_attachment(a, :text => 'Text')
1102 1110 assert_equal '<a href="/attachments/3/logo.gif" class="foo">logo.gif</a>',
1103 1111 link_to_attachment(a, :class => 'foo')
1104 1112 assert_equal '<a href="/attachments/download/3/logo.gif">logo.gif</a>',
1105 1113 link_to_attachment(a, :download => true)
1106 1114 assert_equal '<a href="http://test.host/attachments/3/logo.gif">logo.gif</a>',
1107 1115 link_to_attachment(a, :only_path => false)
1108 1116 end
1109 1117
1110 1118 def test_thumbnail_tag
1111 1119 a = Attachment.find(3)
1112 1120 assert_equal '<a href="/attachments/3/logo.gif" title="logo.gif"><img alt="3" src="/attachments/thumbnail/3" /></a>',
1113 1121 thumbnail_tag(a)
1114 1122 end
1115 1123
1116 1124 def test_link_to_project
1117 1125 project = Project.find(1)
1118 1126 assert_equal %(<a href="/projects/ecookbook">eCookbook</a>),
1119 1127 link_to_project(project)
1120 1128 assert_equal %(<a href="/projects/ecookbook/settings">eCookbook</a>),
1121 1129 link_to_project(project, :action => 'settings')
1122 1130 assert_equal %(<a href="http://test.host/projects/ecookbook?jump=blah">eCookbook</a>),
1123 1131 link_to_project(project, {:only_path => false, :jump => 'blah'})
1124 1132 assert_equal %(<a href="/projects/ecookbook/settings" class="project">eCookbook</a>),
1125 1133 link_to_project(project, {:action => 'settings'}, :class => "project")
1126 1134 end
1127 1135
1128 1136 def test_link_to_project_settings
1129 1137 project = Project.find(1)
1130 1138 assert_equal '<a href="/projects/ecookbook/settings">eCookbook</a>', link_to_project_settings(project)
1131 1139
1132 1140 project.status = Project::STATUS_CLOSED
1133 1141 assert_equal '<a href="/projects/ecookbook">eCookbook</a>', link_to_project_settings(project)
1134 1142
1135 1143 project.status = Project::STATUS_ARCHIVED
1136 1144 assert_equal 'eCookbook', link_to_project_settings(project)
1137 1145 end
1138 1146
1139 1147 def test_link_to_legacy_project_with_numerical_identifier_should_use_id
1140 1148 # numeric identifier are no longer allowed
1141 1149 Project.update_all "identifier=25", "id=1"
1142 1150
1143 1151 assert_equal '<a href="/projects/1">eCookbook</a>',
1144 1152 link_to_project(Project.find(1))
1145 1153 end
1146 1154
1147 1155 def test_principals_options_for_select_with_users
1148 1156 User.current = nil
1149 1157 users = [User.find(2), User.find(4)]
1150 1158 assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>),
1151 1159 principals_options_for_select(users)
1152 1160 end
1153 1161
1154 1162 def test_principals_options_for_select_with_selected
1155 1163 User.current = nil
1156 1164 users = [User.find(2), User.find(4)]
1157 1165 assert_equal %(<option value="2">John Smith</option><option value="4" selected="selected">Robert Hill</option>),
1158 1166 principals_options_for_select(users, User.find(4))
1159 1167 end
1160 1168
1161 1169 def test_principals_options_for_select_with_users_and_groups
1162 1170 User.current = nil
1163 1171 users = [User.find(2), Group.find(11), User.find(4), Group.find(10)]
1164 1172 assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>) +
1165 1173 %(<optgroup label="Groups"><option value="10">A Team</option><option value="11">B Team</option></optgroup>),
1166 1174 principals_options_for_select(users)
1167 1175 end
1168 1176
1169 1177 def test_principals_options_for_select_with_empty_collection
1170 1178 assert_equal '', principals_options_for_select([])
1171 1179 end
1172 1180
1173 1181 def test_principals_options_for_select_should_include_me_option_when_current_user_is_in_collection
1174 1182 users = [User.find(2), User.find(4)]
1175 1183 User.current = User.find(4)
1176 1184 assert_include '<option value="4">&lt;&lt; me &gt;&gt;</option>', principals_options_for_select(users)
1177 1185 end
1178 1186
1179 1187 def test_stylesheet_link_tag_should_pick_the_default_stylesheet
1180 1188 assert_match 'href="/stylesheets/styles.css"', stylesheet_link_tag("styles")
1181 1189 end
1182 1190
1183 1191 def test_stylesheet_link_tag_for_plugin_should_pick_the_plugin_stylesheet
1184 1192 assert_match 'href="/plugin_assets/foo/stylesheets/styles.css"', stylesheet_link_tag("styles", :plugin => :foo)
1185 1193 end
1186 1194
1187 1195 def test_image_tag_should_pick_the_default_image
1188 1196 assert_match 'src="/images/image.png"', image_tag("image.png")
1189 1197 end
1190 1198
1191 1199 def test_image_tag_should_pick_the_theme_image_if_it_exists
1192 1200 theme = Redmine::Themes.themes.last
1193 1201 theme.images << 'image.png'
1194 1202
1195 1203 with_settings :ui_theme => theme.id do
1196 1204 assert_match %|src="/themes/#{theme.dir}/images/image.png"|, image_tag("image.png")
1197 1205 assert_match %|src="/images/other.png"|, image_tag("other.png")
1198 1206 end
1199 1207 ensure
1200 1208 theme.images.delete 'image.png'
1201 1209 end
1202 1210
1203 1211 def test_image_tag_sfor_plugin_should_pick_the_plugin_image
1204 1212 assert_match 'src="/plugin_assets/foo/images/image.png"', image_tag("image.png", :plugin => :foo)
1205 1213 end
1206 1214
1207 1215 def test_javascript_include_tag_should_pick_the_default_javascript
1208 1216 assert_match 'src="/javascripts/scripts.js"', javascript_include_tag("scripts")
1209 1217 end
1210 1218
1211 1219 def test_javascript_include_tag_for_plugin_should_pick_the_plugin_javascript
1212 1220 assert_match 'src="/plugin_assets/foo/javascripts/scripts.js"', javascript_include_tag("scripts", :plugin => :foo)
1213 1221 end
1214 1222 end
General Comments 0
You need to be logged in to leave comments. Login now