##// END OF EJS Templates
Small fix to the Redmine links regexp....
Jean-Philippe Lang -
r1253:030afe742816
parent child
Show More
@@ -1,487 +1,487
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module ApplicationHelper
19 19 include Redmine::WikiFormatting::Macros::Definitions
20 20
21 21 def current_role
22 22 @current_role ||= User.current.role_for_project(@project)
23 23 end
24 24
25 25 # Return true if user is authorized for controller/action, otherwise false
26 26 def authorize_for(controller, action)
27 27 User.current.allowed_to?({:controller => controller, :action => action}, @project)
28 28 end
29 29
30 30 # Display a link if user is authorized
31 31 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
32 32 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
33 33 end
34 34
35 35 # Display a link to user's account page
36 36 def link_to_user(user)
37 37 user ? link_to(user, :controller => 'account', :action => 'show', :id => user) : 'Anonymous'
38 38 end
39 39
40 40 def link_to_issue(issue, options={})
41 41 link_to "#{issue.tracker.name} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue}, options
42 42 end
43 43
44 44 def toggle_link(name, id, options={})
45 45 onclick = "Element.toggle('#{id}'); "
46 46 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
47 47 onclick << "return false;"
48 48 link_to(name, "#", :onclick => onclick)
49 49 end
50 50
51 51 def show_and_goto_link(name, id, options={})
52 52 onclick = "Element.show('#{id}'); "
53 53 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
54 54 onclick << "Element.scrollTo('#{id}'); "
55 55 onclick << "return false;"
56 56 link_to(name, "#", options.merge(:onclick => onclick))
57 57 end
58 58
59 59 def image_to_function(name, function, html_options = {})
60 60 html_options.symbolize_keys!
61 61 tag(:input, html_options.merge({
62 62 :type => "image", :src => image_path(name),
63 63 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
64 64 }))
65 65 end
66 66
67 67 def prompt_to_remote(name, text, param, url, html_options = {})
68 68 html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
69 69 link_to name, {}, html_options
70 70 end
71 71
72 72 def format_date(date)
73 73 return nil unless date
74 74 # "Setting.date_format.size < 2" is a temporary fix (content of date_format setting changed)
75 75 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
76 76 date.strftime(@date_format)
77 77 end
78 78
79 79 def format_time(time, include_date = true)
80 80 return nil unless time
81 81 time = time.to_time if time.is_a?(String)
82 82 zone = User.current.time_zone
83 83 if time.utc?
84 84 local = zone ? zone.adjust(time) : time.getlocal
85 85 else
86 86 local = zone ? zone.adjust(time.getutc) : time
87 87 end
88 88 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
89 89 @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format)
90 90 include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format)
91 91 end
92 92
93 93 def html_hours(text)
94 94 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>')
95 95 end
96 96
97 97 def authoring(created, author)
98 98 time_tag = content_tag('acronym', distance_of_time_in_words(Time.now, created), :title => format_time(created))
99 99 l(:label_added_time_by, author || 'Anonymous', time_tag)
100 100 end
101 101
102 102 def l_or_humanize(s)
103 103 l_has_string?("label_#{s}".to_sym) ? l("label_#{s}".to_sym) : s.to_s.humanize
104 104 end
105 105
106 106 def day_name(day)
107 107 l(:general_day_names).split(',')[day-1]
108 108 end
109 109
110 110 def month_name(month)
111 111 l(:actionview_datehelper_select_month_names).split(',')[month-1]
112 112 end
113 113
114 114 def pagination_links_full(paginator, count=nil, options={})
115 115 page_param = options.delete(:page_param) || :page
116 116 url_param = params.dup
117 117 # don't reuse params if filters are present
118 118 url_param.clear if url_param.has_key?(:set_filter)
119 119
120 120 html = ''
121 121 html << link_to_remote(('&#171; ' + l(:label_previous)),
122 122 {:update => 'content',
123 123 :url => url_param.merge(page_param => paginator.current.previous),
124 124 :complete => 'window.scrollTo(0,0)'},
125 125 {:href => url_for(:params => url_param.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous
126 126
127 127 html << (pagination_links_each(paginator, options) do |n|
128 128 link_to_remote(n.to_s,
129 129 {:url => {:params => url_param.merge(page_param => n)},
130 130 :update => 'content',
131 131 :complete => 'window.scrollTo(0,0)'},
132 132 {:href => url_for(:params => url_param.merge(page_param => n))})
133 133 end || '')
134 134
135 135 html << ' ' + link_to_remote((l(:label_next) + ' &#187;'),
136 136 {:update => 'content',
137 137 :url => url_param.merge(page_param => paginator.current.next),
138 138 :complete => 'window.scrollTo(0,0)'},
139 139 {:href => url_for(:params => url_param.merge(page_param => paginator.current.next))}) if paginator.current.next
140 140
141 141 unless count.nil?
142 142 html << [" (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", per_page_links(paginator.items_per_page)].compact.join(' | ')
143 143 end
144 144
145 145 html
146 146 end
147 147
148 148 def per_page_links(selected=nil)
149 149 url_param = params.dup
150 150 url_param.clear if url_param.has_key?(:set_filter)
151 151
152 152 links = Setting.per_page_options_array.collect do |n|
153 153 n == selected ? n : link_to_remote(n, {:update => "content", :url => params.dup.merge(:per_page => n)},
154 154 {:href => url_for(url_param.merge(:per_page => n))})
155 155 end
156 156 links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
157 157 end
158 158
159 159 def html_title(*args)
160 160 if args.empty?
161 161 title = []
162 162 title << @project.name if @project
163 163 title += @html_title if @html_title
164 164 title << Setting.app_title
165 165 title.compact.join(' - ')
166 166 else
167 167 @html_title ||= []
168 168 @html_title += args
169 169 end
170 170 end
171 171
172 172 def accesskey(s)
173 173 Redmine::AccessKeys.key_for s
174 174 end
175 175
176 176 # Formats text according to system settings.
177 177 # 2 ways to call this method:
178 178 # * with a String: textilizable(text, options)
179 179 # * with an object and one of its attribute: textilizable(issue, :description, options)
180 180 def textilizable(*args)
181 181 options = args.last.is_a?(Hash) ? args.pop : {}
182 182 case args.size
183 183 when 1
184 184 obj = nil
185 185 text = args.shift
186 186 when 2
187 187 obj = args.shift
188 188 text = obj.send(args.shift).to_s
189 189 else
190 190 raise ArgumentError, 'invalid arguments to textilizable'
191 191 end
192 192 return '' if text.blank?
193 193
194 194 only_path = options.delete(:only_path) == false ? false : true
195 195
196 196 # when using an image link, try to use an attachment, if possible
197 197 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
198 198
199 199 if attachments
200 200 text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(gif|jpg|jpeg|png))!/) do |m|
201 201 style = $1
202 202 filename = $6
203 203 rf = Regexp.new(filename, Regexp::IGNORECASE)
204 204 # search for the picture in attachments
205 205 if found = attachments.detect { |att| att.filename =~ rf }
206 206 image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found.id
207 207 "!#{style}#{image_url}!"
208 208 else
209 209 "!#{style}#{filename}!"
210 210 end
211 211 end
212 212 end
213 213
214 214 text = (Setting.text_formatting == 'textile') ?
215 215 Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } :
216 216 simple_format(auto_link(h(text)))
217 217
218 218 # different methods for formatting wiki links
219 219 case options[:wiki_links]
220 220 when :local
221 221 # used for local links to html files
222 222 format_wiki_link = Proc.new {|project, title| "#{title}.html" }
223 223 when :anchor
224 224 # used for single-file wiki export
225 225 format_wiki_link = Proc.new {|project, title| "##{title}" }
226 226 else
227 227 format_wiki_link = Proc.new {|project, title| url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => project, :page => title) }
228 228 end
229 229
230 230 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
231 231
232 232 # Wiki links
233 233 #
234 234 # Examples:
235 235 # [[mypage]]
236 236 # [[mypage|mytext]]
237 237 # wiki links can refer other project wikis, using project name or identifier:
238 238 # [[project:]] -> wiki starting page
239 239 # [[project:|mytext]]
240 240 # [[project:mypage]]
241 241 # [[project:mypage|mytext]]
242 242 text = text.gsub(/(!)?(\[\[([^\]\|]+)(\|([^\]\|]+))?\]\])/) do |m|
243 243 link_project = project
244 244 esc, all, page, title = $1, $2, $3, $5
245 245 if esc.nil?
246 246 if page =~ /^([^\:]+)\:(.*)$/
247 247 link_project = Project.find_by_name($1) || Project.find_by_identifier($1)
248 248 page = $2
249 249 title ||= $1 if page.blank?
250 250 end
251 251
252 252 if link_project && link_project.wiki
253 253 # check if page exists
254 254 wiki_page = link_project.wiki.find_page(page)
255 255 link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page)),
256 256 :class => ('wiki-page' + (wiki_page ? '' : ' new')))
257 257 else
258 258 # project or wiki doesn't exist
259 259 title || page
260 260 end
261 261 else
262 262 all
263 263 end
264 264 end
265 265
266 266 # Redmine links
267 267 #
268 268 # Examples:
269 269 # Issues:
270 270 # #52 -> Link to issue #52
271 271 # Changesets:
272 272 # r52 -> Link to revision 52
273 273 # commit:a85130f -> Link to scmid starting with a85130f
274 274 # Documents:
275 275 # document#17 -> Link to document with id 17
276 276 # document:Greetings -> Link to the document with title "Greetings"
277 277 # document:"Some document" -> Link to the document with title "Some document"
278 278 # Versions:
279 279 # version#3 -> Link to version with id 3
280 280 # version:1.0.0 -> Link to version named "1.0.0"
281 281 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
282 282 # Attachments:
283 283 # attachment:file.zip -> Link to the attachment of the current object named file.zip
284 284 # Source files:
285 285 # source:some/file -> Link to the file located at /some/file in the project's repository
286 286 # source:some/file@52 -> Link to the file's revision 52
287 287 # source:some/file#L120 -> Link to line 120 of the file
288 288 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
289 289 # export:some/file -> Force the download of the file
290 text = text.gsub(%r{([\s\(,-^])(!)?(attachment|document|version|commit|source|export)?((#|r)(\d+)|(:)([^"][^\s<>]+|"[^"]+"))(?=[[:punct:]]|\s|<|$)}) do |m|
290 text = text.gsub(%r{([\s\(,-^])(!)?(attachment|document|version|commit|source|export)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*|"[^"]+"))(?=[[:punct:]]|\s|<|$)}) do |m|
291 291 leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
292 292 link = nil
293 293 if esc.nil?
294 294 if prefix.nil? && sep == 'r'
295 295 if project && (changeset = project.changesets.find_by_revision(oid))
296 296 link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
297 297 :class => 'changeset',
298 298 :title => truncate(changeset.comments, 100))
299 299 end
300 300 elsif sep == '#'
301 301 oid = oid.to_i
302 302 case prefix
303 303 when nil
304 304 if issue = Issue.find_by_id(oid, :include => [:project, :status], :conditions => Project.visible_by(User.current))
305 305 link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
306 306 :class => (issue.closed? ? 'issue closed' : 'issue'),
307 307 :title => "#{truncate(issue.subject, 100)} (#{issue.status.name})")
308 308 link = content_tag('del', link) if issue.closed?
309 309 end
310 310 when 'document'
311 311 if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
312 312 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
313 313 :class => 'document'
314 314 end
315 315 when 'version'
316 316 if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
317 317 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
318 318 :class => 'version'
319 319 end
320 320 end
321 321 elsif sep == ':'
322 322 # removes the double quotes if any
323 323 name = oid.gsub(%r{^"(.*)"$}, "\\1")
324 324 case prefix
325 325 when 'document'
326 326 if project && document = project.documents.find_by_title(name)
327 327 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
328 328 :class => 'document'
329 329 end
330 330 when 'version'
331 331 if project && version = project.versions.find_by_name(name)
332 332 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
333 333 :class => 'version'
334 334 end
335 335 when 'commit'
336 336 if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
337 337 link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision}, :class => 'changeset', :title => truncate(changeset.comments, 100)
338 338 end
339 339 when 'source', 'export'
340 340 if project && project.repository
341 341 name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
342 342 path, rev, anchor = $1, $3, $5
343 343 link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project, :path => path,
344 344 :rev => rev,
345 345 :anchor => anchor,
346 346 :format => (prefix == 'export' ? 'raw' : nil)},
347 347 :class => (prefix == 'export' ? 'source download' : 'source')
348 348 end
349 349 when 'attachment'
350 350 if attachments && attachment = attachments.detect {|a| a.filename == name }
351 351 link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
352 352 :class => 'attachment'
353 353 end
354 354 end
355 355 end
356 356 end
357 357 leading + (link || "#{prefix}#{sep}#{oid}")
358 358 end
359 359
360 360 text
361 361 end
362 362
363 363 # Same as Rails' simple_format helper without using paragraphs
364 364 def simple_format_without_paragraph(text)
365 365 text.to_s.
366 366 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
367 367 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
368 368 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
369 369 end
370 370
371 371 def error_messages_for(object_name, options = {})
372 372 options = options.symbolize_keys
373 373 object = instance_variable_get("@#{object_name}")
374 374 if object && !object.errors.empty?
375 375 # build full_messages here with controller current language
376 376 full_messages = []
377 377 object.errors.each do |attr, msg|
378 378 next if msg.nil?
379 379 msg = msg.first if msg.is_a? Array
380 380 if attr == "base"
381 381 full_messages << l(msg)
382 382 else
383 383 full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg) unless attr == "custom_values"
384 384 end
385 385 end
386 386 # retrieve custom values error messages
387 387 if object.errors[:custom_values]
388 388 object.custom_values.each do |v|
389 389 v.errors.each do |attr, msg|
390 390 next if msg.nil?
391 391 msg = msg.first if msg.is_a? Array
392 392 full_messages << "&#171; " + v.custom_field.name + " &#187; " + l(msg)
393 393 end
394 394 end
395 395 end
396 396 content_tag("div",
397 397 content_tag(
398 398 options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":"
399 399 ) +
400 400 content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
401 401 "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
402 402 )
403 403 else
404 404 ""
405 405 end
406 406 end
407 407
408 408 def lang_options_for_select(blank=true)
409 409 (blank ? [["(auto)", ""]] : []) +
410 410 GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
411 411 end
412 412
413 413 def label_tag_for(name, option_tags = nil, options = {})
414 414 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
415 415 content_tag("label", label_text)
416 416 end
417 417
418 418 def labelled_tabular_form_for(name, object, options, &proc)
419 419 options[:html] ||= {}
420 420 options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
421 421 form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
422 422 end
423 423
424 424 def check_all_links(form_name)
425 425 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
426 426 " | " +
427 427 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
428 428 end
429 429
430 430 def progress_bar(pcts, options={})
431 431 pcts = [pcts, pcts] unless pcts.is_a?(Array)
432 432 pcts[1] = pcts[1] - pcts[0]
433 433 pcts << (100 - pcts[1] - pcts[0])
434 434 width = options[:width] || '100px;'
435 435 legend = options[:legend] || ''
436 436 content_tag('table',
437 437 content_tag('tr',
438 438 (pcts[0] > 0 ? content_tag('td', '', :width => "#{pcts[0].floor}%;", :class => 'closed') : '') +
439 439 (pcts[1] > 0 ? content_tag('td', '', :width => "#{pcts[1].floor}%;", :class => 'done') : '') +
440 440 (pcts[2] > 0 ? content_tag('td', '', :width => "#{pcts[2].floor}%;", :class => 'todo') : '')
441 441 ), :class => 'progress', :style => "width: #{width};") +
442 442 content_tag('p', legend, :class => 'pourcent')
443 443 end
444 444
445 445 def context_menu_link(name, url, options={})
446 446 options[:class] ||= ''
447 447 if options.delete(:selected)
448 448 options[:class] << ' icon-checked disabled'
449 449 options[:disabled] = true
450 450 end
451 451 if options.delete(:disabled)
452 452 options.delete(:method)
453 453 options.delete(:confirm)
454 454 options.delete(:onclick)
455 455 options[:class] << ' disabled'
456 456 url = '#'
457 457 end
458 458 link_to name, url, options
459 459 end
460 460
461 461 def calendar_for(field_id)
462 462 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
463 463 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
464 464 end
465 465
466 466 def wikitoolbar_for(field_id)
467 467 return '' unless Setting.text_formatting == 'textile'
468 468
469 469 help_link = l(:setting_text_formatting) + ': ' +
470 470 link_to(l(:label_help), compute_public_path('wiki_syntax', 'help', 'html'),
471 471 :onclick => "window.open(\"#{ compute_public_path('wiki_syntax', 'help', 'html') }\", \"\", \"resizable=yes, location=no, width=300, height=640, menubar=no, status=no, scrollbars=yes\"); return false;")
472 472
473 473 javascript_include_tag('jstoolbar/jstoolbar') +
474 474 javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language}") +
475 475 javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.setHelpLink('#{help_link}'); toolbar.draw();")
476 476 end
477 477
478 478 def content_for(name, content = nil, &block)
479 479 @has_content ||= {}
480 480 @has_content[name] = true
481 481 super(name, content, &block)
482 482 end
483 483
484 484 def has_content?(name)
485 485 (@has_content && @has_content[name]) || false
486 486 end
487 487 end
@@ -1,168 +1,170
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../../test_helper'
19 19
20 20 class ApplicationHelperTest < HelperTestCase
21 21 include ApplicationHelper
22 22 include ActionView::Helpers::TextHelper
23 23 fixtures :projects, :repositories, :changesets, :trackers, :issue_statuses, :issues, :documents, :versions, :wikis, :wiki_pages, :wiki_contents
24 24
25 25 def setup
26 26 super
27 27 end
28 28
29 29 def test_auto_links
30 30 to_test = {
31 31 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
32 32 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
33 33 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
34 34 '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>.',
35 35 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
36 36 '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>',
37 37 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>'
38 38 }
39 39 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
40 40 end
41 41
42 42 def test_auto_mailto
43 43 assert_equal '<p><a href="mailto:test@foo.bar" class="email">test@foo.bar</a></p>',
44 44 textilizable('test@foo.bar')
45 45 end
46 46
47 47 def test_inline_images
48 48 to_test = {
49 49 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
50 50 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
51 51 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
52 52 'with style !{width:100px;height100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" style="width:100px;height100px;" alt="" />',
53 53 }
54 54 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
55 55 end
56 56
57 57 def test_textile_external_links
58 58 to_test = {
59 59 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
60 60 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
61 61 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>'
62 62 }
63 63 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
64 64 end
65 65
66 66 def test_redmine_links
67 67 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
68 68 :class => 'issue', :title => 'Error 281 when updating a recipe (New)')
69 69
70 70 changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
71 71 :class => 'changeset', :title => 'My very first commit')
72 72
73 73 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
74 74 :class => 'document')
75 75
76 76 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
77 77 :class => 'version')
78 78
79 79 source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => 'some/file'}
80 80
81 81 to_test = {
82 82 # tickets
83 83 '#3, #3 and #3.' => "#{issue_link}, #{issue_link} and #{issue_link}.",
84 84 # changesets
85 85 'r1' => changeset_link,
86 86 # documents
87 87 'document#1' => document_link,
88 88 'document:"Test document"' => document_link,
89 89 # versions
90 90 'version#2' => version_link,
91 91 'version:1.0' => version_link,
92 92 'version:"1.0"' => version_link,
93 93 # source
94 94 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
95 95 'source:/some/file@52' => link_to('source:/some/file@52', source_url.merge(:rev => 52), :class => 'source'),
96 96 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url.merge(:anchor => 'L110'), :class => 'source'),
97 97 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url.merge(:rev => 52, :anchor => 'L110'), :class => 'source'),
98 98 'export:/some/file' => link_to('export:/some/file', source_url.merge(:format => 'raw'), :class => 'source download'),
99 99 # escaping
100 100 '!#3.' => '#3.',
101 101 '!r1' => 'r1',
102 102 '!document#1' => 'document#1',
103 103 '!document:"Test document"' => 'document:"Test document"',
104 104 '!version#2' => 'version#2',
105 105 '!version:1.0' => 'version:1.0',
106 106 '!version:"1.0"' => 'version:"1.0"',
107 107 '!source:/some/file' => 'source:/some/file',
108 # invalid expressions
109 'source:' => 'source:'
108 110 }
109 111 @project = Project.find(1)
110 112 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
111 113 end
112 114
113 115 def test_wiki_links
114 116 to_test = {
115 117 '[[CookBook documentation]]' => '<a href="/wiki/ecookbook/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
116 118 '[[Another page|Page]]' => '<a href="/wiki/ecookbook/Another_page" class="wiki-page">Page</a>',
117 119 # page that doesn't exist
118 120 '[[Unknown page]]' => '<a href="/wiki/ecookbook/Unknown_page" class="wiki-page new">Unknown page</a>',
119 121 '[[Unknown page|404]]' => '<a href="/wiki/ecookbook/Unknown_page" class="wiki-page new">404</a>',
120 122 # link to another project wiki
121 123 '[[onlinestore:]]' => '<a href="/wiki/onlinestore/" class="wiki-page">onlinestore</a>',
122 124 '[[onlinestore:|Wiki]]' => '<a href="/wiki/onlinestore/" class="wiki-page">Wiki</a>',
123 125 '[[onlinestore:Start page]]' => '<a href="/wiki/onlinestore/Start_page" class="wiki-page">Start page</a>',
124 126 '[[onlinestore:Start page|Text]]' => '<a href="/wiki/onlinestore/Start_page" class="wiki-page">Text</a>',
125 127 '[[onlinestore:Unknown page]]' => '<a href="/wiki/onlinestore/Unknown_page" class="wiki-page new">Unknown page</a>',
126 128 # escaping
127 129 '![[Another page|Page]]' => '[[Another page|Page]]',
128 130 }
129 131 @project = Project.find(1)
130 132 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
131 133 end
132 134
133 135 def test_macro_hello_world
134 136 text = "{{hello_world}}"
135 137 assert textilizable(text).match(/Hello world!/)
136 138 # escaping
137 139 text = "!{{hello_world}}"
138 140 assert_equal '<p>{{hello_world}}</p>', textilizable(text)
139 141 end
140 142
141 143 def test_date_format_default
142 144 today = Date.today
143 145 Setting.date_format = ''
144 146 assert_equal l_date(today), format_date(today)
145 147 end
146 148
147 149 def test_date_format
148 150 today = Date.today
149 151 Setting.date_format = '%d %m %Y'
150 152 assert_equal today.strftime('%d %m %Y'), format_date(today)
151 153 end
152 154
153 155 def test_time_format_default
154 156 now = Time.now
155 157 Setting.date_format = ''
156 158 Setting.time_format = ''
157 159 assert_equal l_datetime(now), format_time(now)
158 160 assert_equal l_time(now), format_time(now, false)
159 161 end
160 162
161 163 def test_time_format
162 164 now = Time.now
163 165 Setting.date_format = '%d %m %Y'
164 166 Setting.time_format = '%H %M'
165 167 assert_equal now.strftime('%d %m %Y %H %M'), format_time(now)
166 168 assert_equal now.strftime('%H %M'), format_time(now, false)
167 169 end
168 170 end
General Comments 0
You need to be logged in to leave comments. Login now