##// END OF EJS Templates
Redcarpet not available with jruby (#19313)....
Jean-Philippe Lang -
r13723:c7510519e2fc
parent child
Show More
@@ -1,1533 +1,1535
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2015 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 :projects_trackers,
30 30 :trackers, :issue_statuses, :issues, :versions, :documents,
31 31 :wikis, :wiki_pages, :wiki_contents,
32 32 :boards, :messages, :news,
33 33 :attachments, :enumerations
34 34
35 35 def setup
36 36 super
37 37 set_tmp_attachments_directory
38 38 @russian_test = "\xd1\x82\xd0\xb5\xd1\x81\xd1\x82"
39 39 if @russian_test.respond_to?(:force_encoding)
40 40 @russian_test.force_encoding('UTF-8')
41 41 end
42 42 end
43 43
44 44 test "#link_to_if_authorized for authorized user should allow using the :controller and :action for the target link" do
45 45 User.current = User.find_by_login('admin')
46 46
47 47 @project = Issue.first.project # Used by helper
48 48 response = link_to_if_authorized('By controller/actionr',
49 49 {:controller => 'issues', :action => 'edit', :id => Issue.first.id})
50 50 assert_match /href/, response
51 51 end
52 52
53 53 test "#link_to_if_authorized for unauthorized user should display nothing if user isn't authorized" do
54 54 User.current = User.find_by_login('dlopper')
55 55 @project = Project.find('private-child')
56 56 issue = @project.issues.first
57 57 assert !issue.visible?
58 58
59 59 response = link_to_if_authorized('Never displayed',
60 60 {:controller => 'issues', :action => 'show', :id => issue})
61 61 assert_nil response
62 62 end
63 63
64 64 def test_auto_links
65 65 to_test = {
66 66 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
67 67 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
68 68 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
69 69 'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
70 70 'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
71 71 'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
72 72 '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>.',
73 73 'http://www.foo.bar/Test_(foobar)' => '<a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
74 74 '(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>)',
75 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>)',
76 76 '(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>).',
77 77 '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" class="external">inline link</a>)',
78 78 '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
79 79 '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
80 80 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
81 81 '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>',
82 82 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
83 83 'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
84 84 'http://foo:bar@www.bar.com' => '<a class="external" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
85 85 'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
86 86 'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
87 87 'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
88 88 # two exclamation marks
89 89 '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>',
90 90 # escaping
91 91 'http://foo"bar' => '<a class="external" href="http://foo&quot;bar">http://foo&quot;bar</a>',
92 92 # wrap in angle brackets
93 93 '<http://foo.bar>' => '&lt;<a class="external" href="http://foo.bar">http://foo.bar</a>&gt;',
94 94 # invalid urls
95 95 'http://' => 'http://',
96 96 'www.' => 'www.',
97 97 'test-www.bar.com' => 'test-www.bar.com',
98 98 }
99 99 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
100 100 end
101 101
102 102 if 'ruby'.respond_to?(:encoding)
103 103 def test_auto_links_with_non_ascii_characters
104 104 to_test = {
105 105 "http://foo.bar/#{@russian_test}" =>
106 106 %|<a class="external" href="http://foo.bar/#{@russian_test}">http://foo.bar/#{@russian_test}</a>|
107 107 }
108 108 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
109 109 end
110 110 else
111 111 puts 'Skipping test_auto_links_with_non_ascii_characters, unsupported ruby version'
112 112 end
113 113
114 114 def test_auto_mailto
115 115 to_test = {
116 116 'test@foo.bar' => '<a class="email" href="mailto:test@foo.bar">test@foo.bar</a>',
117 117 'test@www.foo.bar' => '<a class="email" href="mailto:test@www.foo.bar">test@www.foo.bar</a>',
118 118 }
119 119 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
120 120 end
121 121
122 122 def test_inline_images
123 123 to_test = {
124 124 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
125 125 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
126 126 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
127 127 '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="" />',
128 128 '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" />',
129 129 '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;" />',
130 130 }
131 131 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
132 132 end
133 133
134 134 def test_inline_images_inside_tags
135 135 raw = <<-RAW
136 136 h1. !foo.png! Heading
137 137
138 138 Centered image:
139 139
140 140 p=. !bar.gif!
141 141 RAW
142 142
143 143 assert textilizable(raw).include?('<img src="foo.png" alt="" />')
144 144 assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
145 145 end
146 146
147 147 def test_attached_images
148 148 to_test = {
149 149 'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" />',
150 150 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" />',
151 151 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
152 152 'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />',
153 153 # link image
154 154 '!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>',
155 155 }
156 156 attachments = Attachment.all
157 157 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
158 158 end
159 159
160 160 def test_attached_images_with_textile_and_non_ascii_filename
161 161 attachment = Attachment.generate!(:filename => 'cafΓ©.jpg')
162 162 with_settings :text_formatting => 'textile' do
163 163 assert_include %(<img src="/attachments/download/#{attachment.id}/caf%C3%A9.jpg" alt="" />),
164 164 textilizable("!cafΓ©.jpg!)", :attachments => [attachment])
165 165 end
166 166 end
167 167
168 if Object.const_defined?(:Redcarpet)
168 169 def test_attached_images_with_markdown_and_non_ascii_filename
169 170 attachment = Attachment.generate!(:filename => 'cafΓ©.jpg')
170 171 with_settings :text_formatting => 'markdown' do
171 172 assert_include %(<img src="/attachments/download/#{attachment.id}/caf%C3%A9.jpg" alt="">),
172 173 textilizable("![](cafΓ©.jpg)", :attachments => [attachment])
173 174 end
174 175 end
176 end
175 177
176 178 def test_attached_images_filename_extension
177 179 set_tmp_attachments_directory
178 180 a1 = Attachment.new(
179 181 :container => Issue.find(1),
180 182 :file => mock_file_with_options({:original_filename => "testtest.JPG"}),
181 183 :author => User.find(1))
182 184 assert a1.save
183 185 assert_equal "testtest.JPG", a1.filename
184 186 assert_equal "image/jpeg", a1.content_type
185 187 assert a1.image?
186 188
187 189 a2 = Attachment.new(
188 190 :container => Issue.find(1),
189 191 :file => mock_file_with_options({:original_filename => "testtest.jpeg"}),
190 192 :author => User.find(1))
191 193 assert a2.save
192 194 assert_equal "testtest.jpeg", a2.filename
193 195 assert_equal "image/jpeg", a2.content_type
194 196 assert a2.image?
195 197
196 198 a3 = Attachment.new(
197 199 :container => Issue.find(1),
198 200 :file => mock_file_with_options({:original_filename => "testtest.JPE"}),
199 201 :author => User.find(1))
200 202 assert a3.save
201 203 assert_equal "testtest.JPE", a3.filename
202 204 assert_equal "image/jpeg", a3.content_type
203 205 assert a3.image?
204 206
205 207 a4 = Attachment.new(
206 208 :container => Issue.find(1),
207 209 :file => mock_file_with_options({:original_filename => "Testtest.BMP"}),
208 210 :author => User.find(1))
209 211 assert a4.save
210 212 assert_equal "Testtest.BMP", a4.filename
211 213 assert_equal "image/x-ms-bmp", a4.content_type
212 214 assert a4.image?
213 215
214 216 to_test = {
215 217 'Inline image: !testtest.jpg!' =>
216 218 'Inline image: <img src="/attachments/download/' + a1.id.to_s + '/testtest.JPG" alt="" />',
217 219 'Inline image: !testtest.jpeg!' =>
218 220 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testtest.jpeg" alt="" />',
219 221 'Inline image: !testtest.jpe!' =>
220 222 'Inline image: <img src="/attachments/download/' + a3.id.to_s + '/testtest.JPE" alt="" />',
221 223 'Inline image: !testtest.bmp!' =>
222 224 'Inline image: <img src="/attachments/download/' + a4.id.to_s + '/Testtest.BMP" alt="" />',
223 225 }
224 226
225 227 attachments = [a1, a2, a3, a4]
226 228 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
227 229 end
228 230
229 231 def test_attached_images_should_read_later
230 232 set_fixtures_attachments_directory
231 233 a1 = Attachment.find(16)
232 234 assert_equal "testfile.png", a1.filename
233 235 assert a1.readable?
234 236 assert (! a1.visible?(User.anonymous))
235 237 assert a1.visible?(User.find(2))
236 238 a2 = Attachment.find(17)
237 239 assert_equal "testfile.PNG", a2.filename
238 240 assert a2.readable?
239 241 assert (! a2.visible?(User.anonymous))
240 242 assert a2.visible?(User.find(2))
241 243 assert a1.created_on < a2.created_on
242 244
243 245 to_test = {
244 246 'Inline image: !testfile.png!' =>
245 247 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testfile.PNG" alt="" />',
246 248 'Inline image: !Testfile.PNG!' =>
247 249 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testfile.PNG" alt="" />',
248 250 }
249 251 attachments = [a1, a2]
250 252 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
251 253 set_tmp_attachments_directory
252 254 end
253 255
254 256 def test_textile_external_links
255 257 to_test = {
256 258 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
257 259 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
258 260 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
259 261 '"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>',
260 262 "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
261 263 # no multiline link text
262 264 "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",
263 265 # mailto link
264 266 "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
265 267 # two exclamation marks
266 268 '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
267 269 # escaping
268 270 '"test":http://foo"bar' => '<a href="http://foo&quot;bar" class="external">test</a>',
269 271 }
270 272 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
271 273 end
272 274
273 275 if 'ruby'.respond_to?(:encoding)
274 276 def test_textile_external_links_with_non_ascii_characters
275 277 to_test = {
276 278 %|This is a "link":http://foo.bar/#{@russian_test}| =>
277 279 %|This is a <a href="http://foo.bar/#{@russian_test}" class="external">link</a>|
278 280 }
279 281 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
280 282 end
281 283 else
282 284 puts 'Skipping test_textile_external_links_with_non_ascii_characters, unsupported ruby version'
283 285 end
284 286
285 287 def test_redmine_links
286 288 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
287 289 :class => Issue.find(3).css_classes, :title => 'Error 281 when updating a recipe (New)')
288 290 note_link = link_to('#3-14', {:controller => 'issues', :action => 'show', :id => 3, :anchor => 'note-14'},
289 291 :class => Issue.find(3).css_classes, :title => 'Error 281 when updating a recipe (New)')
290 292 note_link2 = link_to('#3#note-14', {:controller => 'issues', :action => 'show', :id => 3, :anchor => 'note-14'},
291 293 :class => Issue.find(3).css_classes, :title => 'Error 281 when updating a recipe (New)')
292 294
293 295 revision_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
294 296 :class => 'changeset', :title => 'My very first commit do not escaping #<>&')
295 297 revision_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
296 298 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
297 299
298 300 changeset_link2 = link_to('691322a8eb01e11fd7',
299 301 {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
300 302 :class => 'changeset', :title => 'My very first commit do not escaping #<>&')
301 303
302 304 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
303 305 :class => 'document')
304 306
305 307 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
306 308 :class => 'version')
307 309
308 310 board_url = {:controller => 'boards', :action => 'show', :id => 2, :project_id => 'ecookbook'}
309 311
310 312 message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
311 313
312 314 news_url = {:controller => 'news', :action => 'show', :id => 1}
313 315
314 316 project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'}
315 317
316 318 source_url = '/projects/ecookbook/repository/entry/some/file'
317 319 source_url_with_rev = '/projects/ecookbook/repository/revisions/52/entry/some/file'
318 320 source_url_with_ext = '/projects/ecookbook/repository/entry/some/file.ext'
319 321 source_url_with_rev_and_ext = '/projects/ecookbook/repository/revisions/52/entry/some/file.ext'
320 322 source_url_with_branch = '/projects/ecookbook/repository/revisions/branch/entry/some/file'
321 323
322 324 export_url = '/projects/ecookbook/repository/raw/some/file'
323 325 export_url_with_rev = '/projects/ecookbook/repository/revisions/52/raw/some/file'
324 326 export_url_with_ext = '/projects/ecookbook/repository/raw/some/file.ext'
325 327 export_url_with_rev_and_ext = '/projects/ecookbook/repository/revisions/52/raw/some/file.ext'
326 328 export_url_with_branch = '/projects/ecookbook/repository/revisions/branch/raw/some/file'
327 329
328 330 to_test = {
329 331 # tickets
330 332 '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.",
331 333 # ticket notes
332 334 '#3-14' => note_link,
333 335 '#3#note-14' => note_link2,
334 336 # should not ignore leading zero
335 337 '#03' => '#03',
336 338 # changesets
337 339 'r1' => revision_link,
338 340 'r1.' => "#{revision_link}.",
339 341 'r1, r2' => "#{revision_link}, #{revision_link2}",
340 342 'r1,r2' => "#{revision_link},#{revision_link2}",
341 343 'commit:691322a8eb01e11fd7' => changeset_link2,
342 344 # documents
343 345 'document#1' => document_link,
344 346 'document:"Test document"' => document_link,
345 347 # versions
346 348 'version#2' => version_link,
347 349 'version:1.0' => version_link,
348 350 'version:"1.0"' => version_link,
349 351 # source
350 352 'source:some/file' => link_to('source:some/file', source_url, :class => 'source'),
351 353 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
352 354 'source:/some/file.' => link_to('source:/some/file', source_url, :class => 'source') + ".",
353 355 'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
354 356 'source:/some/file. ' => link_to('source:/some/file', source_url, :class => 'source') + ".",
355 357 'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
356 358 'source:/some/file, ' => link_to('source:/some/file', source_url, :class => 'source') + ",",
357 359 'source:/some/file@52' => link_to('source:/some/file@52', source_url_with_rev, :class => 'source'),
358 360 'source:/some/file@branch' => link_to('source:/some/file@branch', source_url_with_branch, :class => 'source'),
359 361 'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_rev_and_ext, :class => 'source'),
360 362 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url + "#L110", :class => 'source'),
361 363 'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext + "#L110", :class => 'source'),
362 364 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url_with_rev + "#L110", :class => 'source'),
363 365 # export
364 366 'export:/some/file' => link_to('export:/some/file', export_url, :class => 'source download'),
365 367 'export:/some/file.ext' => link_to('export:/some/file.ext', export_url_with_ext, :class => 'source download'),
366 368 'export:/some/file@52' => link_to('export:/some/file@52', export_url_with_rev, :class => 'source download'),
367 369 'export:/some/file.ext@52' => link_to('export:/some/file.ext@52', export_url_with_rev_and_ext, :class => 'source download'),
368 370 'export:/some/file@branch' => link_to('export:/some/file@branch', export_url_with_branch, :class => 'source download'),
369 371 # forum
370 372 'forum#2' => link_to('Discussion', board_url, :class => 'board'),
371 373 'forum:Discussion' => link_to('Discussion', board_url, :class => 'board'),
372 374 # message
373 375 'message#4' => link_to('Post 2', message_url, :class => 'message'),
374 376 'message#5' => link_to('RE: post 2', message_url.merge(:anchor => 'message-5', :r => 5), :class => 'message'),
375 377 # news
376 378 'news#1' => link_to('eCookbook first release !', news_url, :class => 'news'),
377 379 'news:"eCookbook first release !"' => link_to('eCookbook first release !', news_url, :class => 'news'),
378 380 # project
379 381 'project#3' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
380 382 'project:subproject1' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
381 383 'project:"eCookbook subProject 1"' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
382 384 # not found
383 385 '#0123456789' => '#0123456789',
384 386 # invalid expressions
385 387 'source:' => 'source:',
386 388 # url hash
387 389 "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
388 390 }
389 391 @project = Project.find(1)
390 392 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
391 393 end
392 394
393 395 def test_redmine_links_with_a_different_project_before_current_project
394 396 vp1 = Version.generate!(:project_id => 1, :name => '1.4.4')
395 397 vp3 = Version.generate!(:project_id => 3, :name => '1.4.4')
396 398 @project = Project.find(3)
397 399 result1 = link_to("1.4.4", "/versions/#{vp1.id}", :class => "version")
398 400 result2 = link_to("1.4.4", "/versions/#{vp3.id}", :class => "version")
399 401 assert_equal "<p>#{result1} #{result2}</p>",
400 402 textilizable("ecookbook:version:1.4.4 version:1.4.4")
401 403 end
402 404
403 405 def test_escaped_redmine_links_should_not_be_parsed
404 406 to_test = [
405 407 '#3.',
406 408 '#3-14.',
407 409 '#3#-note14.',
408 410 'r1',
409 411 'document#1',
410 412 'document:"Test document"',
411 413 'version#2',
412 414 'version:1.0',
413 415 'version:"1.0"',
414 416 'source:/some/file'
415 417 ]
416 418 @project = Project.find(1)
417 419 to_test.each { |text| assert_equal "<p>#{text}</p>", textilizable("!" + text), "#{text} failed" }
418 420 end
419 421
420 422 def test_cross_project_redmine_links
421 423 source_link = link_to('ecookbook:source:/some/file',
422 424 {:controller => 'repositories', :action => 'entry',
423 425 :id => 'ecookbook', :path => ['some', 'file']},
424 426 :class => 'source')
425 427 changeset_link = link_to('ecookbook:r2',
426 428 {:controller => 'repositories', :action => 'revision',
427 429 :id => 'ecookbook', :rev => 2},
428 430 :class => 'changeset',
429 431 :title => 'This commit fixes #1, #2 and references #1 & #3')
430 432 to_test = {
431 433 # documents
432 434 'document:"Test document"' => 'document:"Test document"',
433 435 'ecookbook:document:"Test document"' =>
434 436 link_to("Test document", "/documents/1", :class => "document"),
435 437 'invalid:document:"Test document"' => 'invalid:document:"Test document"',
436 438 # versions
437 439 'version:"1.0"' => 'version:"1.0"',
438 440 'ecookbook:version:"1.0"' =>
439 441 link_to("1.0", "/versions/2", :class => "version"),
440 442 'invalid:version:"1.0"' => 'invalid:version:"1.0"',
441 443 # changeset
442 444 'r2' => 'r2',
443 445 'ecookbook:r2' => changeset_link,
444 446 'invalid:r2' => 'invalid:r2',
445 447 # source
446 448 'source:/some/file' => 'source:/some/file',
447 449 'ecookbook:source:/some/file' => source_link,
448 450 'invalid:source:/some/file' => 'invalid:source:/some/file',
449 451 }
450 452 @project = Project.find(3)
451 453 to_test.each do |text, result|
452 454 assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed"
453 455 end
454 456 end
455 457
456 458 def test_redmine_links_by_name_should_work_with_html_escaped_characters
457 459 v = Version.generate!(:name => "Test & Show.txt", :project_id => 1)
458 460 link = link_to("Test & Show.txt", "/versions/#{v.id}", :class => "version")
459 461
460 462 @project = v.project
461 463 assert_equal "<p>#{link}</p>", textilizable('version:"Test & Show.txt"')
462 464 end
463 465
464 466 def test_link_to_issue_subject
465 467 issue = Issue.generate!(:subject => "01234567890123456789")
466 468 str = link_to_issue(issue, :truncate => 10)
467 469 result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}", :class => issue.css_classes)
468 470 assert_equal "#{result}: 0123456...", str
469 471
470 472 issue = Issue.generate!(:subject => "<&>")
471 473 str = link_to_issue(issue)
472 474 result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}", :class => issue.css_classes)
473 475 assert_equal "#{result}: &lt;&amp;&gt;", str
474 476
475 477 issue = Issue.generate!(:subject => "<&>0123456789012345")
476 478 str = link_to_issue(issue, :truncate => 10)
477 479 result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}", :class => issue.css_classes)
478 480 assert_equal "#{result}: &lt;&amp;&gt;0123...", str
479 481 end
480 482
481 483 def test_link_to_issue_title
482 484 long_str = "0123456789" * 5
483 485
484 486 issue = Issue.generate!(:subject => "#{long_str}01234567890123456789")
485 487 str = link_to_issue(issue, :subject => false)
486 488 result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}",
487 489 :class => issue.css_classes,
488 490 :title => "#{long_str}0123456...")
489 491 assert_equal result, str
490 492
491 493 issue = Issue.generate!(:subject => "<&>#{long_str}01234567890123456789")
492 494 str = link_to_issue(issue, :subject => false)
493 495 result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}",
494 496 :class => issue.css_classes,
495 497 :title => "<&>#{long_str}0123...")
496 498 assert_equal result, str
497 499 end
498 500
499 501 def test_multiple_repositories_redmine_links
500 502 svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn_repo-1', :url => 'file:///foo/hg')
501 503 Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
502 504 hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
503 505 Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
504 506
505 507 changeset_link = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
506 508 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
507 509 svn_changeset_link = link_to('svn_repo-1|r123', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'svn_repo-1', :rev => 123},
508 510 :class => 'changeset', :title => '')
509 511 hg_changeset_link = link_to('hg1|abcd', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
510 512 :class => 'changeset', :title => '')
511 513
512 514 source_link = link_to('source:some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, :class => 'source')
513 515 hg_source_link = link_to('source:hg1|some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :repository_id => 'hg1', :path => ['some', 'file']}, :class => 'source')
514 516
515 517 to_test = {
516 518 'r2' => changeset_link,
517 519 'svn_repo-1|r123' => svn_changeset_link,
518 520 'invalid|r123' => 'invalid|r123',
519 521 'commit:hg1|abcd' => hg_changeset_link,
520 522 'commit:invalid|abcd' => 'commit:invalid|abcd',
521 523 # source
522 524 'source:some/file' => source_link,
523 525 'source:hg1|some/file' => hg_source_link,
524 526 'source:invalid|some/file' => 'source:invalid|some/file',
525 527 }
526 528
527 529 @project = Project.find(1)
528 530 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
529 531 end
530 532
531 533 def test_cross_project_multiple_repositories_redmine_links
532 534 svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn1', :url => 'file:///foo/hg')
533 535 Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
534 536 hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
535 537 Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
536 538
537 539 changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
538 540 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
539 541 svn_changeset_link = link_to('ecookbook:svn1|r123', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'svn1', :rev => 123},
540 542 :class => 'changeset', :title => '')
541 543 hg_changeset_link = link_to('ecookbook:hg1|abcd', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
542 544 :class => 'changeset', :title => '')
543 545
544 546 source_link = link_to('ecookbook:source:some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, :class => 'source')
545 547 hg_source_link = link_to('ecookbook:source:hg1|some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :repository_id => 'hg1', :path => ['some', 'file']}, :class => 'source')
546 548
547 549 to_test = {
548 550 'ecookbook:r2' => changeset_link,
549 551 'ecookbook:svn1|r123' => svn_changeset_link,
550 552 'ecookbook:invalid|r123' => 'ecookbook:invalid|r123',
551 553 'ecookbook:commit:hg1|abcd' => hg_changeset_link,
552 554 'ecookbook:commit:invalid|abcd' => 'ecookbook:commit:invalid|abcd',
553 555 'invalid:commit:invalid|abcd' => 'invalid:commit:invalid|abcd',
554 556 # source
555 557 'ecookbook:source:some/file' => source_link,
556 558 'ecookbook:source:hg1|some/file' => hg_source_link,
557 559 'ecookbook:source:invalid|some/file' => 'ecookbook:source:invalid|some/file',
558 560 'invalid:source:invalid|some/file' => 'invalid:source:invalid|some/file',
559 561 }
560 562
561 563 @project = Project.find(3)
562 564 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
563 565 end
564 566
565 567 def test_redmine_links_git_commit
566 568 changeset_link = link_to('abcd',
567 569 {
568 570 :controller => 'repositories',
569 571 :action => 'revision',
570 572 :id => 'subproject1',
571 573 :rev => 'abcd',
572 574 },
573 575 :class => 'changeset', :title => 'test commit')
574 576 to_test = {
575 577 'commit:abcd' => changeset_link,
576 578 }
577 579 @project = Project.find(3)
578 580 r = Repository::Git.create!(:project => @project, :url => '/tmp/test/git')
579 581 assert r
580 582 c = Changeset.new(:repository => r,
581 583 :committed_on => Time.now,
582 584 :revision => 'abcd',
583 585 :scmid => 'abcd',
584 586 :comments => 'test commit')
585 587 assert( c.save )
586 588 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
587 589 end
588 590
589 591 # TODO: Bazaar commit id contains mail address, so it contains '@' and '_'.
590 592 def test_redmine_links_darcs_commit
591 593 changeset_link = link_to('20080308225258-98289-abcd456efg.gz',
592 594 {
593 595 :controller => 'repositories',
594 596 :action => 'revision',
595 597 :id => 'subproject1',
596 598 :rev => '123',
597 599 },
598 600 :class => 'changeset', :title => 'test commit')
599 601 to_test = {
600 602 'commit:20080308225258-98289-abcd456efg.gz' => changeset_link,
601 603 }
602 604 @project = Project.find(3)
603 605 r = Repository::Darcs.create!(
604 606 :project => @project, :url => '/tmp/test/darcs',
605 607 :log_encoding => 'UTF-8')
606 608 assert r
607 609 c = Changeset.new(:repository => r,
608 610 :committed_on => Time.now,
609 611 :revision => '123',
610 612 :scmid => '20080308225258-98289-abcd456efg.gz',
611 613 :comments => 'test commit')
612 614 assert( c.save )
613 615 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
614 616 end
615 617
616 618 def test_redmine_links_mercurial_commit
617 619 changeset_link_rev = link_to('r123',
618 620 {
619 621 :controller => 'repositories',
620 622 :action => 'revision',
621 623 :id => 'subproject1',
622 624 :rev => '123' ,
623 625 },
624 626 :class => 'changeset', :title => 'test commit')
625 627 changeset_link_commit = link_to('abcd',
626 628 {
627 629 :controller => 'repositories',
628 630 :action => 'revision',
629 631 :id => 'subproject1',
630 632 :rev => 'abcd' ,
631 633 },
632 634 :class => 'changeset', :title => 'test commit')
633 635 to_test = {
634 636 'r123' => changeset_link_rev,
635 637 'commit:abcd' => changeset_link_commit,
636 638 }
637 639 @project = Project.find(3)
638 640 r = Repository::Mercurial.create!(:project => @project, :url => '/tmp/test')
639 641 assert r
640 642 c = Changeset.new(:repository => r,
641 643 :committed_on => Time.now,
642 644 :revision => '123',
643 645 :scmid => 'abcd',
644 646 :comments => 'test commit')
645 647 assert( c.save )
646 648 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
647 649 end
648 650
649 651 def test_attachment_links
650 652 text = 'attachment:error281.txt'
651 653 result = link_to("error281.txt", "/attachments/download/1/error281.txt",
652 654 :class => "attachment")
653 655 assert_equal "<p>#{result}</p>",
654 656 textilizable(text,
655 657 :attachments => Issue.find(3).attachments),
656 658 "#{text} failed"
657 659 end
658 660
659 661 def test_attachment_link_should_link_to_latest_attachment
660 662 set_tmp_attachments_directory
661 663 a1 = Attachment.generate!(:filename => "test.txt", :created_on => 1.hour.ago)
662 664 a2 = Attachment.generate!(:filename => "test.txt")
663 665 result = link_to("test.txt", "/attachments/download/#{a2.id}/test.txt",
664 666 :class => "attachment")
665 667 assert_equal "<p>#{result}</p>",
666 668 textilizable('attachment:test.txt', :attachments => [a1, a2])
667 669 end
668 670
669 671 def test_wiki_links
670 672 russian_eacape = CGI.escape(@russian_test)
671 673 to_test = {
672 674 '[[CookBook documentation]]' =>
673 675 link_to("CookBook documentation",
674 676 "/projects/ecookbook/wiki/CookBook_documentation",
675 677 :class => "wiki-page"),
676 678 '[[Another page|Page]]' =>
677 679 link_to("Page",
678 680 "/projects/ecookbook/wiki/Another_page",
679 681 :class => "wiki-page"),
680 682 # title content should be formatted
681 683 '[[Another page|With _styled_ *title*]]' =>
682 684 link_to("With <em>styled</em> <strong>title</strong>".html_safe,
683 685 "/projects/ecookbook/wiki/Another_page",
684 686 :class => "wiki-page"),
685 687 '[[Another page|With title containing <strong>HTML entities &amp; markups</strong>]]' =>
686 688 link_to("With title containing &lt;strong&gt;HTML entities &amp; markups&lt;/strong&gt;".html_safe,
687 689 "/projects/ecookbook/wiki/Another_page",
688 690 :class => "wiki-page"),
689 691 # link with anchor
690 692 '[[CookBook documentation#One-section]]' =>
691 693 link_to("CookBook documentation",
692 694 "/projects/ecookbook/wiki/CookBook_documentation#One-section",
693 695 :class => "wiki-page"),
694 696 '[[Another page#anchor|Page]]' =>
695 697 link_to("Page",
696 698 "/projects/ecookbook/wiki/Another_page#anchor",
697 699 :class => "wiki-page"),
698 700 # UTF8 anchor
699 701 "[[Another_page##{@russian_test}|#{@russian_test}]]" =>
700 702 link_to(@russian_test,
701 703 "/projects/ecookbook/wiki/Another_page##{russian_eacape}",
702 704 :class => "wiki-page"),
703 705 # page that doesn't exist
704 706 '[[Unknown page]]' =>
705 707 link_to("Unknown page",
706 708 "/projects/ecookbook/wiki/Unknown_page",
707 709 :class => "wiki-page new"),
708 710 '[[Unknown page|404]]' =>
709 711 link_to("404",
710 712 "/projects/ecookbook/wiki/Unknown_page",
711 713 :class => "wiki-page new"),
712 714 # link to another project wiki
713 715 '[[onlinestore:]]' =>
714 716 link_to("onlinestore",
715 717 "/projects/onlinestore/wiki",
716 718 :class => "wiki-page"),
717 719 '[[onlinestore:|Wiki]]' =>
718 720 link_to("Wiki",
719 721 "/projects/onlinestore/wiki",
720 722 :class => "wiki-page"),
721 723 '[[onlinestore:Start page]]' =>
722 724 link_to("Start page",
723 725 "/projects/onlinestore/wiki/Start_page",
724 726 :class => "wiki-page"),
725 727 '[[onlinestore:Start page|Text]]' =>
726 728 link_to("Text",
727 729 "/projects/onlinestore/wiki/Start_page",
728 730 :class => "wiki-page"),
729 731 '[[onlinestore:Unknown page]]' =>
730 732 link_to("Unknown page",
731 733 "/projects/onlinestore/wiki/Unknown_page",
732 734 :class => "wiki-page new"),
733 735 # struck through link
734 736 '-[[Another page|Page]]-' =>
735 737 "<del>".html_safe +
736 738 link_to("Page",
737 739 "/projects/ecookbook/wiki/Another_page",
738 740 :class => "wiki-page").html_safe +
739 741 "</del>".html_safe,
740 742 '-[[Another page|Page]] link-' =>
741 743 "<del>".html_safe +
742 744 link_to("Page",
743 745 "/projects/ecookbook/wiki/Another_page",
744 746 :class => "wiki-page").html_safe +
745 747 " link</del>".html_safe,
746 748 # escaping
747 749 '![[Another page|Page]]' => '[[Another page|Page]]',
748 750 # project does not exist
749 751 '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
750 752 '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
751 753 }
752 754 @project = Project.find(1)
753 755 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
754 756 end
755 757
756 758 def test_wiki_links_within_local_file_generation_context
757 759 to_test = {
758 760 # link to a page
759 761 '[[CookBook documentation]]' =>
760 762 link_to("CookBook documentation", "CookBook_documentation.html",
761 763 :class => "wiki-page"),
762 764 '[[CookBook documentation|documentation]]' =>
763 765 link_to("documentation", "CookBook_documentation.html",
764 766 :class => "wiki-page"),
765 767 '[[CookBook documentation#One-section]]' =>
766 768 link_to("CookBook documentation", "CookBook_documentation.html#One-section",
767 769 :class => "wiki-page"),
768 770 '[[CookBook documentation#One-section|documentation]]' =>
769 771 link_to("documentation", "CookBook_documentation.html#One-section",
770 772 :class => "wiki-page"),
771 773 # page that doesn't exist
772 774 '[[Unknown page]]' =>
773 775 link_to("Unknown page", "Unknown_page.html",
774 776 :class => "wiki-page new"),
775 777 '[[Unknown page|404]]' =>
776 778 link_to("404", "Unknown_page.html",
777 779 :class => "wiki-page new"),
778 780 '[[Unknown page#anchor]]' =>
779 781 link_to("Unknown page", "Unknown_page.html#anchor",
780 782 :class => "wiki-page new"),
781 783 '[[Unknown page#anchor|404]]' =>
782 784 link_to("404", "Unknown_page.html#anchor",
783 785 :class => "wiki-page new"),
784 786 }
785 787 @project = Project.find(1)
786 788 to_test.each do |text, result|
787 789 assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :local)
788 790 end
789 791 end
790 792
791 793 def test_wiki_links_within_wiki_page_context
792 794 page = WikiPage.find_by_title('Another_page' )
793 795 to_test = {
794 796 '[[CookBook documentation]]' =>
795 797 link_to("CookBook documentation",
796 798 "/projects/ecookbook/wiki/CookBook_documentation",
797 799 :class => "wiki-page"),
798 800 '[[CookBook documentation|documentation]]' =>
799 801 link_to("documentation",
800 802 "/projects/ecookbook/wiki/CookBook_documentation",
801 803 :class => "wiki-page"),
802 804 '[[CookBook documentation#One-section]]' =>
803 805 link_to("CookBook documentation",
804 806 "/projects/ecookbook/wiki/CookBook_documentation#One-section",
805 807 :class => "wiki-page"),
806 808 '[[CookBook documentation#One-section|documentation]]' =>
807 809 link_to("documentation",
808 810 "/projects/ecookbook/wiki/CookBook_documentation#One-section",
809 811 :class => "wiki-page"),
810 812 # link to the current page
811 813 '[[Another page]]' =>
812 814 link_to("Another page",
813 815 "/projects/ecookbook/wiki/Another_page",
814 816 :class => "wiki-page"),
815 817 '[[Another page|Page]]' =>
816 818 link_to("Page",
817 819 "/projects/ecookbook/wiki/Another_page",
818 820 :class => "wiki-page"),
819 821 '[[Another page#anchor]]' =>
820 822 link_to("Another page",
821 823 "#anchor",
822 824 :class => "wiki-page"),
823 825 '[[Another page#anchor|Page]]' =>
824 826 link_to("Page",
825 827 "#anchor",
826 828 :class => "wiki-page"),
827 829 # page that doesn't exist
828 830 '[[Unknown page]]' =>
829 831 link_to("Unknown page",
830 832 "/projects/ecookbook/wiki/Unknown_page?parent=Another_page",
831 833 :class => "wiki-page new"),
832 834 '[[Unknown page|404]]' =>
833 835 link_to("404",
834 836 "/projects/ecookbook/wiki/Unknown_page?parent=Another_page",
835 837 :class => "wiki-page new"),
836 838 '[[Unknown page#anchor]]' =>
837 839 link_to("Unknown page",
838 840 "/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor",
839 841 :class => "wiki-page new"),
840 842 '[[Unknown page#anchor|404]]' =>
841 843 link_to("404",
842 844 "/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor",
843 845 :class => "wiki-page new"),
844 846 }
845 847 @project = Project.find(1)
846 848 to_test.each do |text, result|
847 849 assert_equal "<p>#{result}</p>",
848 850 textilizable(WikiContent.new( :text => text, :page => page ), :text)
849 851 end
850 852 end
851 853
852 854 def test_wiki_links_anchor_option_should_prepend_page_title_to_href
853 855 to_test = {
854 856 # link to a page
855 857 '[[CookBook documentation]]' =>
856 858 link_to("CookBook documentation",
857 859 "#CookBook_documentation",
858 860 :class => "wiki-page"),
859 861 '[[CookBook documentation|documentation]]' =>
860 862 link_to("documentation",
861 863 "#CookBook_documentation",
862 864 :class => "wiki-page"),
863 865 '[[CookBook documentation#One-section]]' =>
864 866 link_to("CookBook documentation",
865 867 "#CookBook_documentation_One-section",
866 868 :class => "wiki-page"),
867 869 '[[CookBook documentation#One-section|documentation]]' =>
868 870 link_to("documentation",
869 871 "#CookBook_documentation_One-section",
870 872 :class => "wiki-page"),
871 873 # page that doesn't exist
872 874 '[[Unknown page]]' =>
873 875 link_to("Unknown page",
874 876 "#Unknown_page",
875 877 :class => "wiki-page new"),
876 878 '[[Unknown page|404]]' =>
877 879 link_to("404",
878 880 "#Unknown_page",
879 881 :class => "wiki-page new"),
880 882 '[[Unknown page#anchor]]' =>
881 883 link_to("Unknown page",
882 884 "#Unknown_page_anchor",
883 885 :class => "wiki-page new"),
884 886 '[[Unknown page#anchor|404]]' =>
885 887 link_to("404",
886 888 "#Unknown_page_anchor",
887 889 :class => "wiki-page new"),
888 890 }
889 891 @project = Project.find(1)
890 892 to_test.each do |text, result|
891 893 assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :anchor)
892 894 end
893 895 end
894 896
895 897 def test_html_tags
896 898 to_test = {
897 899 "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
898 900 "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
899 901 "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
900 902 # do not escape pre/code tags
901 903 "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
902 904 "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
903 905 "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
904 906 "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
905 907 "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
906 908 # remove attributes except class
907 909 "<pre class='foo'>some text</pre>" => "<pre class='foo'>some text</pre>",
908 910 '<pre class="foo">some text</pre>' => '<pre class="foo">some text</pre>',
909 911 "<pre class='foo bar'>some text</pre>" => "<pre class='foo bar'>some text</pre>",
910 912 '<pre class="foo bar">some text</pre>' => '<pre class="foo bar">some text</pre>',
911 913 "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
912 914 # xss
913 915 '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>',
914 916 '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>',
915 917 }
916 918 to_test.each { |text, result| assert_equal result, textilizable(text) }
917 919 end
918 920
919 921 def test_allowed_html_tags
920 922 to_test = {
921 923 "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
922 924 "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
923 925 "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
924 926 }
925 927 to_test.each { |text, result| assert_equal result, textilizable(text) }
926 928 end
927 929
928 930 def test_pre_tags
929 931 raw = <<-RAW
930 932 Before
931 933
932 934 <pre>
933 935 <prepared-statement-cache-size>32</prepared-statement-cache-size>
934 936 </pre>
935 937
936 938 After
937 939 RAW
938 940
939 941 expected = <<-EXPECTED
940 942 <p>Before</p>
941 943 <pre>
942 944 &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
943 945 </pre>
944 946 <p>After</p>
945 947 EXPECTED
946 948
947 949 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
948 950 end
949 951
950 952 def test_pre_content_should_not_parse_wiki_and_redmine_links
951 953 raw = <<-RAW
952 954 [[CookBook documentation]]
953 955
954 956 #1
955 957
956 958 <pre>
957 959 [[CookBook documentation]]
958 960
959 961 #1
960 962 </pre>
961 963 RAW
962 964
963 965 result1 = link_to("CookBook documentation",
964 966 "/projects/ecookbook/wiki/CookBook_documentation",
965 967 :class => "wiki-page")
966 968 result2 = link_to('#1',
967 969 "/issues/1",
968 970 :class => Issue.find(1).css_classes,
969 971 :title => "Can't print recipes (New)")
970 972
971 973 expected = <<-EXPECTED
972 974 <p>#{result1}</p>
973 975 <p>#{result2}</p>
974 976 <pre>
975 977 [[CookBook documentation]]
976 978
977 979 #1
978 980 </pre>
979 981 EXPECTED
980 982
981 983 @project = Project.find(1)
982 984 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
983 985 end
984 986
985 987 def test_non_closing_pre_blocks_should_be_closed
986 988 raw = <<-RAW
987 989 <pre><code>
988 990 RAW
989 991
990 992 expected = <<-EXPECTED
991 993 <pre><code>
992 994 </code></pre>
993 995 EXPECTED
994 996
995 997 @project = Project.find(1)
996 998 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
997 999 end
998 1000
999 1001 def test_syntax_highlight
1000 1002 raw = <<-RAW
1001 1003 <pre><code class="ruby">
1002 1004 # Some ruby code here
1003 1005 </code></pre>
1004 1006 RAW
1005 1007
1006 1008 expected = <<-EXPECTED
1007 1009 <pre><code class="ruby syntaxhl"><span class=\"CodeRay\"><span class="comment"># Some ruby code here</span></span>
1008 1010 </code></pre>
1009 1011 EXPECTED
1010 1012
1011 1013 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
1012 1014 end
1013 1015
1014 1016 def test_to_path_param
1015 1017 assert_equal 'test1/test2', to_path_param('test1/test2')
1016 1018 assert_equal 'test1/test2', to_path_param('/test1/test2/')
1017 1019 assert_equal 'test1/test2', to_path_param('//test1/test2/')
1018 1020 assert_equal nil, to_path_param('/')
1019 1021 end
1020 1022
1021 1023 def test_wiki_links_in_tables
1022 1024 text = "|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|"
1023 1025 link1 = link_to("Link title", "/projects/ecookbook/wiki/Page", :class => "wiki-page new")
1024 1026 link2 = link_to("Other title", "/projects/ecookbook/wiki/Other_Page", :class => "wiki-page new")
1025 1027 link3 = link_to("Last page", "/projects/ecookbook/wiki/Last_page", :class => "wiki-page new")
1026 1028 result = "<tr><td>#{link1}</td>" +
1027 1029 "<td>#{link2}</td>" +
1028 1030 "</tr><tr><td>Cell 21</td><td>#{link3}</td></tr>"
1029 1031 @project = Project.find(1)
1030 1032 assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '')
1031 1033 end
1032 1034
1033 1035 def test_text_formatting
1034 1036 to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
1035 1037 '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
1036 1038 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
1037 1039 '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>',
1038 1040 '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',
1039 1041 }
1040 1042 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
1041 1043 end
1042 1044
1043 1045 def test_wiki_horizontal_rule
1044 1046 assert_equal '<hr />', textilizable('---')
1045 1047 assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
1046 1048 end
1047 1049
1048 1050 def test_footnotes
1049 1051 raw = <<-RAW
1050 1052 This is some text[1].
1051 1053
1052 1054 fn1. This is the foot note
1053 1055 RAW
1054 1056
1055 1057 expected = <<-EXPECTED
1056 1058 <p>This is some text<sup><a href=\"#fn1\">1</a></sup>.</p>
1057 1059 <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
1058 1060 EXPECTED
1059 1061
1060 1062 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
1061 1063 end
1062 1064
1063 1065 def test_headings
1064 1066 raw = 'h1. Some heading'
1065 1067 expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>|
1066 1068
1067 1069 assert_equal expected, textilizable(raw)
1068 1070 end
1069 1071
1070 1072 def test_headings_with_special_chars
1071 1073 # This test makes sure that the generated anchor names match the expected
1072 1074 # ones even if the heading text contains unconventional characters
1073 1075 raw = 'h1. Some heading related to version 0.5'
1074 1076 anchor = sanitize_anchor_name("Some-heading-related-to-version-0.5")
1075 1077 expected = %|<a name="#{anchor}"></a>\n<h1 >Some heading related to version 0.5<a href="##{anchor}" class="wiki-anchor">&para;</a></h1>|
1076 1078
1077 1079 assert_equal expected, textilizable(raw)
1078 1080 end
1079 1081
1080 1082 def test_headings_in_wiki_single_page_export_should_be_prepended_with_page_title
1081 1083 page = WikiPage.new( :title => 'Page Title', :wiki_id => 1 )
1082 1084 content = WikiContent.new( :text => 'h1. Some heading', :page => page )
1083 1085
1084 1086 expected = %|<a name="Page_Title_Some-heading"></a>\n<h1 >Some heading<a href="#Page_Title_Some-heading" class="wiki-anchor">&para;</a></h1>|
1085 1087
1086 1088 assert_equal expected, textilizable(content, :text, :wiki_links => :anchor )
1087 1089 end
1088 1090
1089 1091 def test_table_of_content
1090 1092 raw = <<-RAW
1091 1093 {{toc}}
1092 1094
1093 1095 h1. Title
1094 1096
1095 1097 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
1096 1098
1097 1099 h2. Subtitle with a [[Wiki]] link
1098 1100
1099 1101 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
1100 1102
1101 1103 h2. Subtitle with [[Wiki|another Wiki]] link
1102 1104
1103 1105 h2. Subtitle with %{color:red}red text%
1104 1106
1105 1107 <pre>
1106 1108 some code
1107 1109 </pre>
1108 1110
1109 1111 h3. Subtitle with *some* _modifiers_
1110 1112
1111 1113 h3. Subtitle with @inline code@
1112 1114
1113 1115 h1. Another title
1114 1116
1115 1117 h3. An "Internet link":http://www.redmine.org/ inside subtitle
1116 1118
1117 1119 h2. "Project Name !/attachments/1234/logo_small.gif! !/attachments/5678/logo_2.png!":/projects/projectname/issues
1118 1120
1119 1121 RAW
1120 1122
1121 1123 expected = '<ul class="toc">' +
1122 1124 '<li><a href="#Title">Title</a>' +
1123 1125 '<ul>' +
1124 1126 '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
1125 1127 '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
1126 1128 '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' +
1127 1129 '<ul>' +
1128 1130 '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' +
1129 1131 '<li><a href="#Subtitle-with-inline-code">Subtitle with inline code</a></li>' +
1130 1132 '</ul>' +
1131 1133 '</li>' +
1132 1134 '</ul>' +
1133 1135 '</li>' +
1134 1136 '<li><a href="#Another-title">Another title</a>' +
1135 1137 '<ul>' +
1136 1138 '<li>' +
1137 1139 '<ul>' +
1138 1140 '<li><a href="#An-Internet-link-inside-subtitle">An Internet link inside subtitle</a></li>' +
1139 1141 '</ul>' +
1140 1142 '</li>' +
1141 1143 '<li><a href="#Project-Name">Project Name</a></li>' +
1142 1144 '</ul>' +
1143 1145 '</li>' +
1144 1146 '</ul>'
1145 1147
1146 1148 @project = Project.find(1)
1147 1149 assert textilizable(raw).gsub("\n", "").include?(expected)
1148 1150 end
1149 1151
1150 1152 def test_table_of_content_should_generate_unique_anchors
1151 1153 raw = <<-RAW
1152 1154 {{toc}}
1153 1155
1154 1156 h1. Title
1155 1157
1156 1158 h2. Subtitle
1157 1159
1158 1160 h2. Subtitle
1159 1161 RAW
1160 1162
1161 1163 expected = '<ul class="toc">' +
1162 1164 '<li><a href="#Title">Title</a>' +
1163 1165 '<ul>' +
1164 1166 '<li><a href="#Subtitle">Subtitle</a></li>' +
1165 1167 '<li><a href="#Subtitle-2">Subtitle</a></li>'
1166 1168 '</ul>'
1167 1169 '</li>' +
1168 1170 '</ul>'
1169 1171
1170 1172 @project = Project.find(1)
1171 1173 result = textilizable(raw).gsub("\n", "")
1172 1174 assert_include expected, result
1173 1175 assert_include '<a name="Subtitle">', result
1174 1176 assert_include '<a name="Subtitle-2">', result
1175 1177 end
1176 1178
1177 1179 def test_table_of_content_should_contain_included_page_headings
1178 1180 raw = <<-RAW
1179 1181 {{toc}}
1180 1182
1181 1183 h1. Included
1182 1184
1183 1185 {{include(Child_1)}}
1184 1186 RAW
1185 1187
1186 1188 expected = '<ul class="toc">' +
1187 1189 '<li><a href="#Included">Included</a></li>' +
1188 1190 '<li><a href="#Child-page-1">Child page 1</a></li>' +
1189 1191 '</ul>'
1190 1192
1191 1193 @project = Project.find(1)
1192 1194 assert textilizable(raw).gsub("\n", "").include?(expected)
1193 1195 end
1194 1196
1195 1197 def test_toc_with_textile_formatting_should_be_parsed
1196 1198 with_settings :text_formatting => 'textile' do
1197 1199 assert_select_in textilizable("{{toc}}\n\nh1. Heading"), 'ul.toc li', :text => 'Heading'
1198 1200 assert_select_in textilizable("{{<toc}}\n\nh1. Heading"), 'ul.toc.left li', :text => 'Heading'
1199 1201 assert_select_in textilizable("{{>toc}}\n\nh1. Heading"), 'ul.toc.right li', :text => 'Heading'
1200 1202 end
1201 1203 end
1202 1204
1203 1205 if Object.const_defined?(:Redcarpet)
1204 1206 def test_toc_with_markdown_formatting_should_be_parsed
1205 1207 with_settings :text_formatting => 'markdown' do
1206 1208 assert_select_in textilizable("{{toc}}\n\n# Heading"), 'ul.toc li', :text => 'Heading'
1207 1209 assert_select_in textilizable("{{<toc}}\n\n# Heading"), 'ul.toc.left li', :text => 'Heading'
1208 1210 assert_select_in textilizable("{{>toc}}\n\n# Heading"), 'ul.toc.right li', :text => 'Heading'
1209 1211 end
1210 1212 end
1211 1213 end
1212 1214
1213 1215 def test_section_edit_links
1214 1216 raw = <<-RAW
1215 1217 h1. Title
1216 1218
1217 1219 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
1218 1220
1219 1221 h2. Subtitle with a [[Wiki]] link
1220 1222
1221 1223 h2. Subtitle with *some* _modifiers_
1222 1224
1223 1225 h2. Subtitle with @inline code@
1224 1226
1225 1227 <pre>
1226 1228 some code
1227 1229
1228 1230 h2. heading inside pre
1229 1231
1230 1232 <h2>html heading inside pre</h2>
1231 1233 </pre>
1232 1234
1233 1235 h2. Subtitle after pre tag
1234 1236 RAW
1235 1237
1236 1238 @project = Project.find(1)
1237 1239 set_language_if_valid 'en'
1238 1240 result = textilizable(raw, :edit_section_links => {:controller => 'wiki', :action => 'edit', :project_id => '1', :id => 'Test'}).gsub("\n", "")
1239 1241
1240 1242 # heading that contains inline code
1241 1243 assert_match Regexp.new('<div class="contextual" id="section-4" title="Edit this section">' +
1242 1244 '<a href="/projects/1/wiki/Test/edit\?section=4"><img alt="Edit" src="/images/edit.png(\?\d+)?" /></a></div>' +
1243 1245 '<a name="Subtitle-with-inline-code"></a>' +
1244 1246 '<h2 >Subtitle with <code>inline code</code><a href="#Subtitle-with-inline-code" class="wiki-anchor">&para;</a></h2>'),
1245 1247 result
1246 1248
1247 1249 # last heading
1248 1250 assert_match Regexp.new('<div class="contextual" id="section-5" title="Edit this section">' +
1249 1251 '<a href="/projects/1/wiki/Test/edit\?section=5"><img alt="Edit" src="/images/edit.png(\?\d+)?" /></a></div>' +
1250 1252 '<a name="Subtitle-after-pre-tag"></a>' +
1251 1253 '<h2 >Subtitle after pre tag<a href="#Subtitle-after-pre-tag" class="wiki-anchor">&para;</a></h2>'),
1252 1254 result
1253 1255 end
1254 1256
1255 1257 def test_default_formatter
1256 1258 with_settings :text_formatting => 'unknown' do
1257 1259 text = 'a *link*: http://www.example.net/'
1258 1260 assert_equal '<p>a *link*: <a class="external" href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
1259 1261 end
1260 1262 end
1261 1263
1262 1264 def test_due_date_distance_in_words
1263 1265 to_test = { Date.today => 'Due in 0 days',
1264 1266 Date.today + 1 => 'Due in 1 day',
1265 1267 Date.today + 100 => 'Due in about 3 months',
1266 1268 Date.today + 20000 => 'Due in over 54 years',
1267 1269 Date.today - 1 => '1 day late',
1268 1270 Date.today - 100 => 'about 3 months late',
1269 1271 Date.today - 20000 => 'over 54 years late',
1270 1272 }
1271 1273 ::I18n.locale = :en
1272 1274 to_test.each do |date, expected|
1273 1275 assert_equal expected, due_date_distance_in_words(date)
1274 1276 end
1275 1277 end
1276 1278
1277 1279 def test_avatar_enabled
1278 1280 with_settings :gravatar_enabled => '1' do
1279 1281 assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
1280 1282 assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
1281 1283 # Default size is 50
1282 1284 assert avatar('jsmith <jsmith@somenet.foo>').include?('size=50')
1283 1285 assert avatar('jsmith <jsmith@somenet.foo>', :size => 24).include?('size=24')
1284 1286 # Non-avatar options should be considered html options
1285 1287 assert avatar('jsmith <jsmith@somenet.foo>', :title => 'John Smith').include?('title="John Smith"')
1286 1288 # The default class of the img tag should be gravatar
1287 1289 assert avatar('jsmith <jsmith@somenet.foo>').include?('class="gravatar"')
1288 1290 assert !avatar('jsmith <jsmith@somenet.foo>', :class => 'picture').include?('class="gravatar"')
1289 1291 assert_nil avatar('jsmith')
1290 1292 assert_nil avatar(nil)
1291 1293 end
1292 1294 end
1293 1295
1294 1296 def test_avatar_disabled
1295 1297 with_settings :gravatar_enabled => '0' do
1296 1298 assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
1297 1299 end
1298 1300 end
1299 1301
1300 1302 def test_link_to_user
1301 1303 user = User.find(2)
1302 1304 result = link_to("John Smith", "/users/2", :class => "user active")
1303 1305 assert_equal result, link_to_user(user)
1304 1306 end
1305 1307
1306 1308 def test_link_to_user_should_not_link_to_locked_user
1307 1309 with_current_user nil do
1308 1310 user = User.find(5)
1309 1311 assert user.locked?
1310 1312 assert_equal 'Dave2 Lopper2', link_to_user(user)
1311 1313 end
1312 1314 end
1313 1315
1314 1316 def test_link_to_user_should_link_to_locked_user_if_current_user_is_admin
1315 1317 with_current_user User.find(1) do
1316 1318 user = User.find(5)
1317 1319 assert user.locked?
1318 1320 result = link_to("Dave2 Lopper2", "/users/5", :class => "user locked")
1319 1321 assert_equal result, link_to_user(user)
1320 1322 end
1321 1323 end
1322 1324
1323 1325 def test_link_to_user_should_not_link_to_anonymous
1324 1326 user = User.anonymous
1325 1327 assert user.anonymous?
1326 1328 t = link_to_user(user)
1327 1329 assert_equal ::I18n.t(:label_user_anonymous), t
1328 1330 end
1329 1331
1330 1332 def test_link_to_attachment
1331 1333 a = Attachment.find(3)
1332 1334 assert_equal '<a href="/attachments/3/logo.gif">logo.gif</a>',
1333 1335 link_to_attachment(a)
1334 1336 assert_equal '<a href="/attachments/3/logo.gif">Text</a>',
1335 1337 link_to_attachment(a, :text => 'Text')
1336 1338 result = link_to("logo.gif", "/attachments/3/logo.gif", :class => "foo")
1337 1339 assert_equal result,
1338 1340 link_to_attachment(a, :class => 'foo')
1339 1341 assert_equal '<a href="/attachments/download/3/logo.gif">logo.gif</a>',
1340 1342 link_to_attachment(a, :download => true)
1341 1343 assert_equal '<a href="http://test.host/attachments/3/logo.gif">logo.gif</a>',
1342 1344 link_to_attachment(a, :only_path => false)
1343 1345 end
1344 1346
1345 1347 def test_thumbnail_tag
1346 1348 a = Attachment.find(3)
1347 1349 assert_equal '<a href="/attachments/3/logo.gif" title="logo.gif"><img alt="3" src="/attachments/thumbnail/3" /></a>',
1348 1350 thumbnail_tag(a)
1349 1351 end
1350 1352
1351 1353 def test_link_to_project
1352 1354 project = Project.find(1)
1353 1355 assert_equal %(<a href="/projects/ecookbook">eCookbook</a>),
1354 1356 link_to_project(project)
1355 1357 assert_equal %(<a href="/projects/ecookbook/settings">eCookbook</a>),
1356 1358 link_to_project(project, :action => 'settings')
1357 1359 assert_equal %(<a href="http://test.host/projects/ecookbook?jump=blah">eCookbook</a>),
1358 1360 link_to_project(project, {:only_path => false, :jump => 'blah'})
1359 1361 result = link_to("eCookbook", "/projects/ecookbook/settings", :class => "project")
1360 1362 assert_equal result,
1361 1363 link_to_project(project, {:action => 'settings'}, :class => "project")
1362 1364 end
1363 1365
1364 1366 def test_link_to_project_settings
1365 1367 project = Project.find(1)
1366 1368 assert_equal '<a href="/projects/ecookbook/settings">eCookbook</a>', link_to_project_settings(project)
1367 1369
1368 1370 project.status = Project::STATUS_CLOSED
1369 1371 assert_equal '<a href="/projects/ecookbook">eCookbook</a>', link_to_project_settings(project)
1370 1372
1371 1373 project.status = Project::STATUS_ARCHIVED
1372 1374 assert_equal 'eCookbook', link_to_project_settings(project)
1373 1375 end
1374 1376
1375 1377 def test_link_to_legacy_project_with_numerical_identifier_should_use_id
1376 1378 # numeric identifier are no longer allowed
1377 1379 Project.where(:id => 1).update_all(:identifier => 25)
1378 1380 assert_equal '<a href="/projects/1">eCookbook</a>',
1379 1381 link_to_project(Project.find(1))
1380 1382 end
1381 1383
1382 1384 def test_principals_options_for_select_with_users
1383 1385 User.current = nil
1384 1386 users = [User.find(2), User.find(4)]
1385 1387 assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>),
1386 1388 principals_options_for_select(users)
1387 1389 end
1388 1390
1389 1391 def test_principals_options_for_select_with_selected
1390 1392 User.current = nil
1391 1393 users = [User.find(2), User.find(4)]
1392 1394 assert_equal %(<option value="2">John Smith</option><option value="4" selected="selected">Robert Hill</option>),
1393 1395 principals_options_for_select(users, User.find(4))
1394 1396 end
1395 1397
1396 1398 def test_principals_options_for_select_with_users_and_groups
1397 1399 User.current = nil
1398 1400 users = [User.find(2), Group.find(11), User.find(4), Group.find(10)]
1399 1401 assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>) +
1400 1402 %(<optgroup label="Groups"><option value="10">A Team</option><option value="11">B Team</option></optgroup>),
1401 1403 principals_options_for_select(users)
1402 1404 end
1403 1405
1404 1406 def test_principals_options_for_select_with_empty_collection
1405 1407 assert_equal '', principals_options_for_select([])
1406 1408 end
1407 1409
1408 1410 def test_principals_options_for_select_should_include_me_option_when_current_user_is_in_collection
1409 1411 users = [User.find(2), User.find(4)]
1410 1412 User.current = User.find(4)
1411 1413 assert_include '<option value="4">&lt;&lt; me &gt;&gt;</option>', principals_options_for_select(users)
1412 1414 end
1413 1415
1414 1416 def test_stylesheet_link_tag_should_pick_the_default_stylesheet
1415 1417 assert_match 'href="/stylesheets/styles.css"', stylesheet_link_tag("styles")
1416 1418 end
1417 1419
1418 1420 def test_stylesheet_link_tag_for_plugin_should_pick_the_plugin_stylesheet
1419 1421 assert_match 'href="/plugin_assets/foo/stylesheets/styles.css"', stylesheet_link_tag("styles", :plugin => :foo)
1420 1422 end
1421 1423
1422 1424 def test_image_tag_should_pick_the_default_image
1423 1425 assert_match 'src="/images/image.png"', image_tag("image.png")
1424 1426 end
1425 1427
1426 1428 def test_image_tag_should_pick_the_theme_image_if_it_exists
1427 1429 theme = Redmine::Themes.themes.last
1428 1430 theme.images << 'image.png'
1429 1431
1430 1432 with_settings :ui_theme => theme.id do
1431 1433 assert_match %|src="/themes/#{theme.dir}/images/image.png"|, image_tag("image.png")
1432 1434 assert_match %|src="/images/other.png"|, image_tag("other.png")
1433 1435 end
1434 1436 ensure
1435 1437 theme.images.delete 'image.png'
1436 1438 end
1437 1439
1438 1440 def test_image_tag_sfor_plugin_should_pick_the_plugin_image
1439 1441 assert_match 'src="/plugin_assets/foo/images/image.png"', image_tag("image.png", :plugin => :foo)
1440 1442 end
1441 1443
1442 1444 def test_javascript_include_tag_should_pick_the_default_javascript
1443 1445 assert_match 'src="/javascripts/scripts.js"', javascript_include_tag("scripts")
1444 1446 end
1445 1447
1446 1448 def test_javascript_include_tag_for_plugin_should_pick_the_plugin_javascript
1447 1449 assert_match 'src="/plugin_assets/foo/javascripts/scripts.js"', javascript_include_tag("scripts", :plugin => :foo)
1448 1450 end
1449 1451
1450 1452 def test_raw_json_should_escape_closing_tags
1451 1453 s = raw_json(["<foo>bar</foo>"])
1452 1454 assert_equal '["<foo>bar<\/foo>"]', s
1453 1455 end
1454 1456
1455 1457 def test_raw_json_should_be_html_safe
1456 1458 s = raw_json(["foo"])
1457 1459 assert s.html_safe?
1458 1460 end
1459 1461
1460 1462 def test_html_title_should_app_title_if_not_set
1461 1463 assert_equal 'Redmine', html_title
1462 1464 end
1463 1465
1464 1466 def test_html_title_should_join_items
1465 1467 html_title 'Foo', 'Bar'
1466 1468 assert_equal 'Foo - Bar - Redmine', html_title
1467 1469 end
1468 1470
1469 1471 def test_html_title_should_append_current_project_name
1470 1472 @project = Project.find(1)
1471 1473 html_title 'Foo', 'Bar'
1472 1474 assert_equal 'Foo - Bar - eCookbook - Redmine', html_title
1473 1475 end
1474 1476
1475 1477 def test_title_should_return_a_h2_tag
1476 1478 assert_equal '<h2>Foo</h2>', title('Foo')
1477 1479 end
1478 1480
1479 1481 def test_title_should_set_html_title
1480 1482 title('Foo')
1481 1483 assert_equal 'Foo - Redmine', html_title
1482 1484 end
1483 1485
1484 1486 def test_title_should_turn_arrays_into_links
1485 1487 assert_equal '<h2><a href="/foo">Foo</a></h2>', title(['Foo', '/foo'])
1486 1488 assert_equal 'Foo - Redmine', html_title
1487 1489 end
1488 1490
1489 1491 def test_title_should_join_items
1490 1492 assert_equal '<h2>Foo &#187; Bar</h2>', title('Foo', 'Bar')
1491 1493 assert_equal 'Bar - Foo - Redmine', html_title
1492 1494 end
1493 1495
1494 1496 def test_favicon_path
1495 1497 assert_match %r{^/favicon\.ico}, favicon_path
1496 1498 end
1497 1499
1498 1500 def test_favicon_path_with_suburi
1499 1501 Redmine::Utils.relative_url_root = '/foo'
1500 1502 assert_match %r{^/foo/favicon\.ico}, favicon_path
1501 1503 ensure
1502 1504 Redmine::Utils.relative_url_root = ''
1503 1505 end
1504 1506
1505 1507 def test_favicon_url
1506 1508 assert_match %r{^http://test\.host/favicon\.ico}, favicon_url
1507 1509 end
1508 1510
1509 1511 def test_favicon_url_with_suburi
1510 1512 Redmine::Utils.relative_url_root = '/foo'
1511 1513 assert_match %r{^http://test\.host/foo/favicon\.ico}, favicon_url
1512 1514 ensure
1513 1515 Redmine::Utils.relative_url_root = ''
1514 1516 end
1515 1517
1516 1518 def test_truncate_single_line
1517 1519 str = "01234"
1518 1520 result = truncate_single_line_raw("#{str}\n#{str}", 10)
1519 1521 assert_equal "01234 0...", result
1520 1522 assert !result.html_safe?
1521 1523 result = truncate_single_line_raw("#{str}<&#>\n#{str}#{str}", 16)
1522 1524 assert_equal "01234<&#> 012...", result
1523 1525 assert !result.html_safe?
1524 1526 end
1525 1527
1526 1528 def test_truncate_single_line_non_ascii
1527 1529 ja = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
1528 1530 ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
1529 1531 result = truncate_single_line_raw("#{ja}\n#{ja}\n#{ja}", 10)
1530 1532 assert_equal "#{ja} #{ja}...", result
1531 1533 assert !result.html_safe?
1532 1534 end
1533 1535 end
General Comments 0
You need to be logged in to leave comments. Login now