Revision 1297:0a574315af3e .svn/pristine/14

View differences:

.svn/pristine/14/1411d2a90c9093fc6ada5ec42e5d250259f58396.svn-base
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

  
18
class SettingsController < ApplicationController
19
  layout 'admin'
20
  menu_item :plugins, :only => :plugin
21

  
22
  before_filter :require_admin
23

  
24
  def index
25
    edit
26
    render :action => 'edit'
27
  end
28

  
29
  def edit
30
    @notifiables = Redmine::Notifiable.all
31
    if request.post? && params[:settings] && params[:settings].is_a?(Hash)
32
      settings = (params[:settings] || {}).dup.symbolize_keys
33
      settings.each do |name, value|
34
        # remove blank values in array settings
35
        value.delete_if {|v| v.blank? } if value.is_a?(Array)
36
        Setting[name] = value
37
      end
38
      flash[:notice] = l(:notice_successful_update)
39
      redirect_to :action => 'edit', :tab => params[:tab]
40
    else
41
      @options = {}
42
      user_format = User::USER_FORMATS.collect{|key, value| [key, value[:setting_order]]}.sort{|a, b| a[1] <=> b[1]}
43
      @options[:user_format] = user_format.collect{|f| [User.current.name(f[0]), f[0].to_s]}
44
      @deliveries = ActionMailer::Base.perform_deliveries
45

  
46
      @guessed_host_and_path = request.host_with_port.dup
47
      @guessed_host_and_path << ('/'+ Redmine::Utils.relative_url_root.gsub(%r{^\/}, '')) unless Redmine::Utils.relative_url_root.blank?
48

  
49
      Redmine::Themes.rescan
50
    end
51
  end
52

  
53
  def plugin
54
    @plugin = Redmine::Plugin.find(params[:id])
55
    if request.post?
56
      Setting.send "plugin_#{@plugin.id}=", params[:settings]
57
      flash[:notice] = l(:notice_successful_update)
58
      redirect_to :action => 'plugin', :id => @plugin.id
59
    else
60
      @partial = @plugin.settings[:partial]
61
      @settings = Setting.send "plugin_#{@plugin.id}"
62
    end
63
  rescue Redmine::PluginNotFound
64
    render_404
65
  end
66
end
.svn/pristine/14/14309d64e537e5e954feb549559e6200d128036a.svn-base
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

  
18
class CustomField < ActiveRecord::Base
19
  include Redmine::SubclassFactory
20

  
21
  has_many :custom_values, :dependent => :delete_all
22
  acts_as_list :scope => 'type = \'#{self.class}\''
23
  serialize :possible_values
24

  
25
  validates_presence_of :name, :field_format
26
  validates_uniqueness_of :name, :scope => :type
27
  validates_length_of :name, :maximum => 30
28
  validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats
29

  
30
  validate :validate_custom_field
31
  before_validation :set_searchable
32

  
33
  CUSTOM_FIELDS_TABS = [
34
    {:name => 'IssueCustomField', :partial => 'custom_fields/index',
35
     :label => :label_issue_plural},
36
    {:name => 'TimeEntryCustomField', :partial => 'custom_fields/index',
37
     :label => :label_spent_time},
38
    {:name => 'ProjectCustomField', :partial => 'custom_fields/index',
39
     :label => :label_project_plural},
40
    {:name => 'VersionCustomField', :partial => 'custom_fields/index',
41
     :label => :label_version_plural},
42
    {:name => 'UserCustomField', :partial => 'custom_fields/index',
43
     :label => :label_user_plural},
44
    {:name => 'GroupCustomField', :partial => 'custom_fields/index',
45
     :label => :label_group_plural},
46
    {:name => 'TimeEntryActivityCustomField', :partial => 'custom_fields/index',
47
     :label => TimeEntryActivity::OptionName},
48
    {:name => 'IssuePriorityCustomField', :partial => 'custom_fields/index',
49
     :label => IssuePriority::OptionName},
50
    {:name => 'DocumentCategoryCustomField', :partial => 'custom_fields/index',
51
     :label => DocumentCategory::OptionName}
52
  ]
53

  
54
  CUSTOM_FIELDS_NAMES = CUSTOM_FIELDS_TABS.collect{|v| v[:name]}
55

  
56
  def field_format=(arg)
57
    # cannot change format of a saved custom field
58
    super if new_record?
59
  end
60

  
61
  def set_searchable
62
    # make sure these fields are not searchable
63
    self.searchable = false if %w(int float date bool).include?(field_format)
64
    # make sure only these fields can have multiple values
65
    self.multiple = false unless %w(list user version).include?(field_format)
66
    true
67
  end
68

  
69
  def validate_custom_field
70
    if self.field_format == "list"
71
      errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty?
72
      errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array
73
    end
74

  
75
    if regexp.present?
76
      begin
77
        Regexp.new(regexp)
78
      rescue
79
        errors.add(:regexp, :invalid)
80
      end
81
    end
82

  
83
    if default_value.present? && !valid_field_value?(default_value)
84
      errors.add(:default_value, :invalid)
85
    end
86
  end
87

  
88
  def possible_values_options(obj=nil)
89
    case field_format
90
    when 'user', 'version'
91
      if obj.respond_to?(:project) && obj.project
92
        case field_format
93
        when 'user'
94
          obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]}
95
        when 'version'
96
          obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]}
97
        end
98
      elsif obj.is_a?(Array)
99
        obj.collect {|o| possible_values_options(o)}.reduce(:&)
100
      else
101
        []
102
      end
103
    when 'bool'
104
      [[l(:general_text_Yes), '1'], [l(:general_text_No), '0']]
105
    else
106
      possible_values || []
107
    end
108
  end
109

  
110
  def possible_values(obj=nil)
111
    case field_format
112
    when 'user', 'version'
113
      possible_values_options(obj).collect(&:last)
114
    when 'bool'
115
      ['1', '0']
116
    else
117
      values = super()
118
      if values.is_a?(Array)
119
        values.each do |value|
120
          value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
121
        end
122
      end
123
      values || []
124
    end
125
  end
126

  
127
  # Makes possible_values accept a multiline string
128
  def possible_values=(arg)
129
    if arg.is_a?(Array)
130
      super(arg.compact.collect(&:strip).select {|v| !v.blank?})
131
    else
132
      self.possible_values = arg.to_s.split(/[\n\r]+/)
133
    end
134
  end
135

  
136
  def cast_value(value)
137
    casted = nil
138
    unless value.blank?
139
      case field_format
140
      when 'string', 'text', 'list'
141
        casted = value
142
      when 'date'
143
        casted = begin; value.to_date; rescue; nil end
144
      when 'bool'
145
        casted = (value == '1' ? true : false)
146
      when 'int'
147
        casted = value.to_i
148
      when 'float'
149
        casted = value.to_f
150
      when 'user', 'version'
151
        casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i))
152
      end
153
    end
154
    casted
155
  end
156

  
157
  def value_from_keyword(keyword, customized)
158
    possible_values_options = possible_values_options(customized)
159
    if possible_values_options.present?
160
      keyword = keyword.to_s.downcase
161
      if v = possible_values_options.detect {|text, id| text.downcase == keyword}
162
        if v.is_a?(Array)
163
          v.last
164
        else
165
          v
166
        end
167
      end
168
    else
169
      keyword
170
    end
171
  end
172
 
173
  # Returns a ORDER BY clause that can used to sort customized
174
  # objects by their value of the custom field.
175
  # Returns nil if the custom field can not be used for sorting.
176
  def order_statement
177
    return nil if multiple?
178
    case field_format
179
      when 'string', 'text', 'list', 'date', 'bool'
180
        # COALESCE is here to make sure that blank and NULL values are sorted equally
181
        "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
182
          " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
183
          " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
184
          " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
185
      when 'int', 'float'
186
        # Make the database cast values into numeric
187
        # Postgresql will raise an error if a value can not be casted!
188
        # CustomValue validations should ensure that it doesn't occur
189
        "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" +
190
          " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
191
          " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
192
          " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
193
      when 'user', 'version'
194
        value_class.fields_for_order_statement(value_join_alias)
195
      else
196
        nil
197
    end
198
  end
199

  
200
  # Returns a GROUP BY clause that can used to group by custom value
201
  # Returns nil if the custom field can not be used for grouping.
202
  def group_statement 
203
    return nil if multiple?
204
    case field_format
205
      when 'list', 'date', 'bool', 'int'
206
        order_statement
207
      when 'user', 'version'
208
        "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
209
          " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
210
          " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
211
          " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
212
      else
213
        nil
214
    end
215
  end
216

  
217
  def join_for_order_statement
218
    case field_format
219
      when 'user', 'version'
220
        "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
221
          " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
222
          " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
223
          " AND #{join_alias}.custom_field_id = #{id}" +
224
          " AND #{join_alias}.value <> ''" +
225
          " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
226
            " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
227
            " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
228
            " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" +
229
          " LEFT OUTER JOIN #{value_class.table_name} #{value_join_alias}" +
230
          " ON CAST(#{join_alias}.value as decimal(60,0)) = #{value_join_alias}.id"
231
      else
232
        nil
233
    end
234
  end
235

  
236
  def join_alias
237
    "cf_#{id}"
238
  end
239

  
240
  def value_join_alias
241
    join_alias + "_" + field_format
242
  end
243

  
244
  def <=>(field)
245
    position <=> field.position
246
  end
247

  
248
  # Returns the class that values represent
249
  def value_class
250
    case field_format
251
      when 'user', 'version'
252
        field_format.classify.constantize
253
      else
254
        nil
255
    end
256
  end
257

  
258
  def self.customized_class
259
    self.name =~ /^(.+)CustomField$/
260
    begin; $1.constantize; rescue nil; end
261
  end
262

  
263
  # to move in project_custom_field
264
  def self.for_all
265
    find(:all, :conditions => ["is_for_all=?", true], :order => 'position')
266
  end
267

  
268
  def type_name
269
    nil
270
  end
271

  
272
  # Returns the error messages for the given value
273
  # or an empty array if value is a valid value for the custom field
274
  def validate_field_value(value)
275
    errs = []
276
    if value.is_a?(Array)
277
      if !multiple?
278
        errs << ::I18n.t('activerecord.errors.messages.invalid')
279
      end
280
      if is_required? && value.detect(&:present?).nil?
281
        errs << ::I18n.t('activerecord.errors.messages.blank')
282
      end
283
      value.each {|v| errs += validate_field_value_format(v)}
284
    else
285
      if is_required? && value.blank?
286
        errs << ::I18n.t('activerecord.errors.messages.blank')
287
      end
288
      errs += validate_field_value_format(value)
289
    end
290
    errs
291
  end
292

  
293
  # Returns true if value is a valid value for the custom field
294
  def valid_field_value?(value)
295
    validate_field_value(value).empty?
296
  end
297

  
298
  def format_in?(*args)
299
    args.include?(field_format)
300
  end
301

  
302
  protected
303

  
304
  # Returns the error message for the given value regarding its format
305
  def validate_field_value_format(value)
306
    errs = []
307
    if value.present?
308
      errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp)
309
      errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length
310
      errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length
311

  
312
      # Format specific validations
313
      case field_format
314
      when 'int'
315
        errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/
316
      when 'float'
317
        begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end
318
      when 'date'
319
        errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end
320
      when 'list'
321
        errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value)
322
      end
323
    end
324
    errs
325
  end
326
end
.svn/pristine/14/146e04b787ce58184275bdd027f7e85c4c9ff45f.svn-base
1
<h2><%=l(:label_information_plural)%></h2>
2

  
3
<p><strong><%= Redmine::Info.versioned_name %></strong></p>
4

  
5
<table class="list">
6
<% @checklist.each do |label, result| %>
7
  <tr class="<%= cycle 'odd', 'even' %>">
8
    <td><%= l(label) %></td>
9
    <td width="30px"><%= image_tag((result ? 'true.png' : 'exclamation.png'),
10
                                    :style => "vertical-align:bottom;") %></td>
11
  </tr>
12
<% end %>
13
</table>
14
<br />
15
<div class="box">
16
<pre><%= Redmine::Info.environment %></pre>
17
</div>
18

  
19
<% html_title(l(:label_information_plural)) -%>
.svn/pristine/14/149fe7d9ecdbec895382a22950f9b721ccb3953a.svn-base
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

  
18
class EnumerationsController < ApplicationController
19
  layout 'admin'
20

  
21
  before_filter :require_admin, :except => :index
22
  before_filter :require_admin_or_api_request, :only => :index
23
  before_filter :build_new_enumeration, :only => [:new, :create]
24
  before_filter :find_enumeration, :only => [:edit, :update, :destroy]
25
  accept_api_auth :index
26

  
27
  helper :custom_fields
28

  
29
  def index
30
    respond_to do |format|
31
      format.html
32
      format.api {
33
        @klass = Enumeration.get_subclass(params[:type])
34
        if @klass
35
          @enumerations = @klass.shared.sorted.all
36
        else
37
          render_404
38
        end
39
      }
40
    end
41
  end
42

  
43
  def new
44
  end
45

  
46
  def create
47
    if request.post? && @enumeration.save
48
      flash[:notice] = l(:notice_successful_create)
49
      redirect_to :action => 'index'
50
    else
51
      render :action => 'new'
52
    end
53
  end
54

  
55
  def edit
56
  end
57

  
58
  def update
59
    if request.put? && @enumeration.update_attributes(params[:enumeration])
60
      flash[:notice] = l(:notice_successful_update)
61
      redirect_to :action => 'index'
62
    else
63
      render :action => 'edit'
64
    end
65
  end
66

  
67
  def destroy
68
    if !@enumeration.in_use?
69
      # No associated objects
70
      @enumeration.destroy
71
      redirect_to :action => 'index'
72
      return
73
    elsif params[:reassign_to_id]
74
      if reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id])
75
        @enumeration.destroy(reassign_to)
76
        redirect_to :action => 'index'
77
        return
78
      end
79
    end
80
    @enumerations = @enumeration.class.all - [@enumeration]
81
  end
82

  
83
  private
84

  
85
  def build_new_enumeration
86
    class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
87
    @enumeration = Enumeration.new_subclass_instance(class_name, params[:enumeration])
88
    if @enumeration.nil?
89
      render_404
90
    end
91
  end
92

  
93
  def find_enumeration
94
    @enumeration = Enumeration.find(params[:id])
95
  rescue ActiveRecord::RecordNotFound
96
    render_404
97
  end
98
end
.svn/pristine/14/14b30558ff27714592e93e747cd5345cdd0c4589.svn-base
1
# encoding: utf-8
2
#
3
# Redmine - project management software
4
# Copyright (C) 2006-2012  Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19

  
20
require File.expand_path('../../../test_helper', __FILE__)
21

  
22
class ApplicationHelperTest < ActionView::TestCase
23
  include ERB::Util
24

  
25
  fixtures :projects, :roles, :enabled_modules, :users,
26
           :repositories, :changesets,
27
           :trackers, :issue_statuses, :issues, :versions, :documents,
28
           :wikis, :wiki_pages, :wiki_contents,
29
           :boards, :messages, :news,
30
           :attachments, :enumerations
31

  
32
  def setup
33
    super
34
    set_tmp_attachments_directory
35
  end
36

  
37
  context "#link_to_if_authorized" do
38
    context "authorized user" do
39
      should "be tested"
40
    end
41

  
42
    context "unauthorized user" do
43
      should "be tested"
44
    end
45

  
46
    should "allow using the :controller and :action for the target link" do
47
      User.current = User.find_by_login('admin')
48

  
49
      @project = Issue.first.project # Used by helper
50
      response = link_to_if_authorized("By controller/action",
51
                                       {:controller => 'issues', :action => 'edit', :id => Issue.first.id})
52
      assert_match /href/, response
53
    end
54

  
55
  end
56

  
57
  def test_auto_links
58
    to_test = {
59
      'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
60
      'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
61
      'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
62
      'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
63
      'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
64
      'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
65
      '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>.',
66
      'http://www.foo.bar/Test_(foobar)' => '<a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
67
      '(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>)',
68
      '(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>)',
69
      '(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>).',
70
      '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" class="external">inline link</a>)',
71
      '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
72
      '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
73
      'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
74
      '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>',
75
      'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
76
      'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
77
      'http://foo:bar@www.bar.com' => '<a class="external" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
78
      'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
79
      'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
80
      'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
81
      # two exclamation marks
82
      '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>',
83
      # escaping
84
      'http://foo"bar' => '<a class="external" href="http://foo&quot;bar">http://foo&quot;bar</a>',
85
      # wrap in angle brackets
86
      '<http://foo.bar>' => '&lt;<a class="external" href="http://foo.bar">http://foo.bar</a>&gt;'
87
    }
88
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
89
  end
90

  
91
  if 'ruby'.respond_to?(:encoding)
92
    def test_auto_links_with_non_ascii_characters
93
      to_test = {
94
        'http://foo.bar/тест' => '<a class="external" href="http://foo.bar/тест">http://foo.bar/тест</a>'
95
      }
96
      to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
97
    end
98
  else
99
    puts 'Skipping test_auto_links_with_non_ascii_characters, unsupported ruby version'
100
  end
101

  
102
  def test_auto_mailto
103
    assert_equal '<p><a class="email" href="mailto:test@foo.bar">test@foo.bar</a></p>',
104
      textilizable('test@foo.bar')
105
  end
106

  
107
  def test_inline_images
108
    to_test = {
109
      '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
110
      'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
111
      'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
112
      '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="" />',
113
      '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" />',
114
      '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;" />',
115
    }
116
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
117
  end
118

  
119
  def test_inline_images_inside_tags
120
    raw = <<-RAW
121
h1. !foo.png! Heading
122

  
123
Centered image:
124

  
125
p=. !bar.gif!
126
RAW
127

  
128
    assert textilizable(raw).include?('<img src="foo.png" alt="" />')
129
    assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
130
  end
131

  
132
  def test_attached_images
133
    to_test = {
134
      'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
135
      'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
136
      'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
137
      'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />',
138
      # link image
139
      '!logo.gif!:http://foo.bar/' => '<a href="http://foo.bar/"><img src="/attachments/download/3" title="This is a logo" alt="This is a logo" /></a>',
140
    }
141
    attachments = Attachment.find(:all)
142
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
143
  end
144

  
145
  def test_attached_images_filename_extension
146
    set_tmp_attachments_directory
147
    a1 = Attachment.new(
148
            :container => Issue.find(1),
149
            :file => mock_file_with_options({:original_filename => "testtest.JPG"}),
150
            :author => User.find(1))
151
    assert a1.save
152
    assert_equal "testtest.JPG", a1.filename
153
    assert_equal "image/jpeg", a1.content_type
154
    assert a1.image?
155

  
156
    a2 = Attachment.new(
157
            :container => Issue.find(1),
158
            :file => mock_file_with_options({:original_filename => "testtest.jpeg"}),
159
            :author => User.find(1))
160
    assert a2.save
161
    assert_equal "testtest.jpeg", a2.filename
162
    assert_equal "image/jpeg", a2.content_type
163
    assert a2.image?
164

  
165
    a3 = Attachment.new(
166
            :container => Issue.find(1),
167
            :file => mock_file_with_options({:original_filename => "testtest.JPE"}),
168
            :author => User.find(1))
169
    assert a3.save
170
    assert_equal "testtest.JPE", a3.filename
171
    assert_equal "image/jpeg", a3.content_type
172
    assert a3.image?
173

  
174
    a4 = Attachment.new(
175
            :container => Issue.find(1),
176
            :file => mock_file_with_options({:original_filename => "Testtest.BMP"}),
177
            :author => User.find(1))
178
    assert a4.save
179
    assert_equal "Testtest.BMP", a4.filename
180
    assert_equal "image/x-ms-bmp", a4.content_type
181
    assert a4.image?
182

  
183
    to_test = {
184
      'Inline image: !testtest.jpg!' =>
185
        'Inline image: <img src="/attachments/download/' + a1.id.to_s + '" alt="" />',
186
      'Inline image: !testtest.jpeg!' =>
187
        'Inline image: <img src="/attachments/download/' + a2.id.to_s + '" alt="" />',
188
      'Inline image: !testtest.jpe!' =>
189
        'Inline image: <img src="/attachments/download/' + a3.id.to_s + '" alt="" />',
190
      'Inline image: !testtest.bmp!' =>
191
        'Inline image: <img src="/attachments/download/' + a4.id.to_s + '" alt="" />',
192
    }
193

  
194
    attachments = [a1, a2, a3, a4]
195
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
196
  end
197

  
198
  def test_attached_images_should_read_later
199
    set_fixtures_attachments_directory
200
    a1 = Attachment.find(16)
201
    assert_equal "testfile.png", a1.filename
202
    assert a1.readable?
203
    assert (! a1.visible?(User.anonymous))
204
    assert a1.visible?(User.find(2))
205
    a2 = Attachment.find(17)
206
    assert_equal "testfile.PNG", a2.filename
207
    assert a2.readable?
208
    assert (! a2.visible?(User.anonymous))
209
    assert a2.visible?(User.find(2))
210
    assert a1.created_on < a2.created_on
211

  
212
    to_test = {
213
      'Inline image: !testfile.png!' =>
214
        'Inline image: <img src="/attachments/download/' + a2.id.to_s + '" alt="" />',
215
      'Inline image: !Testfile.PNG!' =>
216
        'Inline image: <img src="/attachments/download/' + a2.id.to_s + '" alt="" />',
217
    }
218
    attachments = [a1, a2]
219
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
220
    set_tmp_attachments_directory
221
  end
222

  
223
  def test_textile_external_links
224
    to_test = {
225
      'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
226
      'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
227
      '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
228
      '"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>',
229
      "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
230
      # no multiline link text
231
      "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",
232
      # mailto link
233
      "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
234
      # two exclamation marks
235
      '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
236
      # escaping
237
      '"test":http://foo"bar' => '<a href="http://foo&quot;bar" class="external">test</a>',
238
    }
239
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
240
  end
241

  
242
  if 'ruby'.respond_to?(:encoding)
243
    def test_textile_external_links_with_non_ascii_characters
244
      to_test = {
245
        'This is a "link":http://foo.bar/тест' => 'This is a <a href="http://foo.bar/тест" class="external">link</a>'
246
      }
247
      to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
248
    end
249
  else
250
    puts 'Skipping test_textile_external_links_with_non_ascii_characters, unsupported ruby version'
251
  end
252

  
253
  def test_redmine_links
254
    issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
255
                               :class => 'issue status-1 priority-4 priority-lowest overdue', :title => 'Error 281 when updating a recipe (New)')
256
    note_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3, :anchor => 'note-14'},
257
                               :class => 'issue status-1 priority-4 priority-lowest overdue', :title => 'Error 281 when updating a recipe (New)')
258

  
259
    changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
260
                                   :class => 'changeset', :title => 'My very first commit')
261
    changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
262
                                    :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
263

  
264
    document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
265
                                             :class => 'document')
266

  
267
    version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
268
                                  :class => 'version')
269

  
270
    board_url = {:controller => 'boards', :action => 'show', :id => 2, :project_id => 'ecookbook'}
271

  
272
    message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
273
    
274
    news_url = {:controller => 'news', :action => 'show', :id => 1}
275

  
276
    project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'}
277

  
278
    source_url = '/projects/ecookbook/repository/entry/some/file'
279
    source_url_with_rev = '/projects/ecookbook/repository/revisions/52/entry/some/file'
280
    source_url_with_ext = '/projects/ecookbook/repository/entry/some/file.ext'
281
    source_url_with_rev_and_ext = '/projects/ecookbook/repository/revisions/52/entry/some/file.ext'
282

  
283
    export_url = '/projects/ecookbook/repository/raw/some/file'
284
    export_url_with_rev = '/projects/ecookbook/repository/revisions/52/raw/some/file'
285
    export_url_with_ext = '/projects/ecookbook/repository/raw/some/file.ext'
286
    export_url_with_rev_and_ext = '/projects/ecookbook/repository/revisions/52/raw/some/file.ext'
287

  
288
    to_test = {
289
      # tickets
290
      '#3, [#3], (#3) and #3.'      => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.",
291
      # ticket notes
292
      '#3-14'                       => note_link,
293
      '#3#note-14'                  => note_link,
294
      # should not ignore leading zero
295
      '#03'                         => '#03',
296
      # changesets
297
      'r1'                          => changeset_link,
298
      'r1.'                         => "#{changeset_link}.",
299
      'r1, r2'                      => "#{changeset_link}, #{changeset_link2}",
300
      'r1,r2'                       => "#{changeset_link},#{changeset_link2}",
301
      # documents
302
      'document#1'                  => document_link,
303
      'document:"Test document"'    => document_link,
304
      # versions
305
      'version#2'                   => version_link,
306
      'version:1.0'                 => version_link,
307
      'version:"1.0"'               => version_link,
308
      # source
309
      'source:some/file'            => link_to('source:some/file', source_url, :class => 'source'),
310
      'source:/some/file'           => link_to('source:/some/file', source_url, :class => 'source'),
311
      'source:/some/file.'          => link_to('source:/some/file', source_url, :class => 'source') + ".",
312
      'source:/some/file.ext.'      => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
313
      'source:/some/file. '         => link_to('source:/some/file', source_url, :class => 'source') + ".",
314
      'source:/some/file.ext. '     => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
315
      'source:/some/file, '         => link_to('source:/some/file', source_url, :class => 'source') + ",",
316
      'source:/some/file@52'        => link_to('source:/some/file@52', source_url_with_rev, :class => 'source'),
317
      'source:/some/file.ext@52'    => link_to('source:/some/file.ext@52', source_url_with_rev_and_ext, :class => 'source'),
318
      'source:/some/file#L110'      => link_to('source:/some/file#L110', source_url + "#L110", :class => 'source'),
319
      'source:/some/file.ext#L110'  => link_to('source:/some/file.ext#L110', source_url_with_ext + "#L110", :class => 'source'),
320
      'source:/some/file@52#L110'   => link_to('source:/some/file@52#L110', source_url_with_rev + "#L110", :class => 'source'),
321
      # export
322
      'export:/some/file'           => link_to('export:/some/file', export_url, :class => 'source download'),
323
      'export:/some/file.ext'       => link_to('export:/some/file.ext', export_url_with_ext, :class => 'source download'),
324
      'export:/some/file@52'        => link_to('export:/some/file@52', export_url_with_rev, :class => 'source download'),
325
      'export:/some/file.ext@52'    => link_to('export:/some/file.ext@52', export_url_with_rev_and_ext, :class => 'source download'),
326
      # forum
327
      'forum#2'                     => link_to('Discussion', board_url, :class => 'board'),
328
      'forum:Discussion'            => link_to('Discussion', board_url, :class => 'board'),
329
      # message
330
      'message#4'                   => link_to('Post 2', message_url, :class => 'message'),
331
      'message#5'                   => link_to('RE: post 2', message_url.merge(:anchor => 'message-5', :r => 5), :class => 'message'),
332
      # news
333
      'news#1'                      => link_to('eCookbook first release !', news_url, :class => 'news'),
334
      'news:"eCookbook first release !"'        => link_to('eCookbook first release !', news_url, :class => 'news'),
335
      # project
336
      'project#3'                   => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
337
      'project:subproject1'         => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
338
      'project:"eCookbook subProject 1"'        => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
339
      # not found
340
      '#0123456789'                 => '#0123456789',
341
      # invalid expressions
342
      'source:'                     => 'source:',
343
      # url hash
344
      "http://foo.bar/FAQ#3"       => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
345
    }
346
    @project = Project.find(1)
347
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
348
  end
349

  
350
  def test_escaped_redmine_links_should_not_be_parsed
351
    to_test = [
352
      '#3.',
353
      '#3-14.',
354
      '#3#-note14.',
355
      'r1',
356
      'document#1',
357
      'document:"Test document"',
358
      'version#2',
359
      'version:1.0',
360
      'version:"1.0"',
361
      'source:/some/file'
362
    ]
363
    @project = Project.find(1)
364
    to_test.each { |text| assert_equal "<p>#{text}</p>", textilizable("!" + text), "#{text} failed" }
365
  end
366

  
367
  def test_cross_project_redmine_links
368
    source_link = link_to('ecookbook:source:/some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']},
369
      :class => 'source')
370

  
371
    changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
372
      :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
373

  
374
    to_test = {
375
      # documents
376
      'document:"Test document"'              => 'document:"Test document"',
377
      'ecookbook:document:"Test document"'    => '<a href="/documents/1" class="document">Test document</a>',
378
      'invalid:document:"Test document"'      => 'invalid:document:"Test document"',
379
      # versions
380
      'version:"1.0"'                         => 'version:"1.0"',
381
      'ecookbook:version:"1.0"'               => '<a href="/versions/2" class="version">1.0</a>',
382
      'invalid:version:"1.0"'                 => 'invalid:version:"1.0"',
383
      # changeset
384
      'r2'                                    => 'r2',
385
      'ecookbook:r2'                          => changeset_link,
386
      'invalid:r2'                            => 'invalid:r2',
387
      # source
388
      'source:/some/file'                     => 'source:/some/file',
389
      'ecookbook:source:/some/file'           => source_link,
390
      'invalid:source:/some/file'             => 'invalid:source:/some/file',
391
    }
392
    @project = Project.find(3)
393
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
394
  end
395

  
396
  def test_multiple_repositories_redmine_links
397
    svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn1', :url => 'file:///foo/hg')
398
    Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
399
    hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
400
    Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
401

  
402
    changeset_link = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
403
                                    :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
404
    svn_changeset_link = link_to('svn1|r123', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'svn1', :rev => 123},
405
                                    :class => 'changeset', :title => '')
406
    hg_changeset_link = link_to('hg1|abcd', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
407
                                    :class => 'changeset', :title => '')
408

  
409
    source_link = link_to('source:some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, :class => 'source')
410
    hg_source_link = link_to('source:hg1|some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :repository_id => 'hg1', :path => ['some', 'file']}, :class => 'source')
411

  
412
    to_test = {
413
      'r2'                          => changeset_link,
414
      'svn1|r123'                   => svn_changeset_link,
415
      'invalid|r123'                => 'invalid|r123',
416
      'commit:hg1|abcd'             => hg_changeset_link,
417
      'commit:invalid|abcd'         => 'commit:invalid|abcd',
418
      # source
419
      'source:some/file'            => source_link,
420
      'source:hg1|some/file'        => hg_source_link,
421
      'source:invalid|some/file'    => 'source:invalid|some/file',
422
    }
423

  
424
    @project = Project.find(1)
425
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
426
  end
427

  
428
  def test_cross_project_multiple_repositories_redmine_links
429
    svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn1', :url => 'file:///foo/hg')
430
    Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
431
    hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
432
    Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
433

  
434
    changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
435
                                    :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
436
    svn_changeset_link = link_to('ecookbook:svn1|r123', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'svn1', :rev => 123},
437
                                    :class => 'changeset', :title => '')
438
    hg_changeset_link = link_to('ecookbook:hg1|abcd', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
439
                                    :class => 'changeset', :title => '')
440

  
441
    source_link = link_to('ecookbook:source:some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, :class => 'source')
442
    hg_source_link = link_to('ecookbook:source:hg1|some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :repository_id => 'hg1', :path => ['some', 'file']}, :class => 'source')
443

  
444
    to_test = {
445
      'ecookbook:r2'                           => changeset_link,
446
      'ecookbook:svn1|r123'                    => svn_changeset_link,
447
      'ecookbook:invalid|r123'                 => 'ecookbook:invalid|r123',
448
      'ecookbook:commit:hg1|abcd'              => hg_changeset_link,
449
      'ecookbook:commit:invalid|abcd'          => 'ecookbook:commit:invalid|abcd',
450
      'invalid:commit:invalid|abcd'            => 'invalid:commit:invalid|abcd',
451
      # source
452
      'ecookbook:source:some/file'             => source_link,
453
      'ecookbook:source:hg1|some/file'         => hg_source_link,
454
      'ecookbook:source:invalid|some/file'     => 'ecookbook:source:invalid|some/file',
455
      'invalid:source:invalid|some/file'       => 'invalid:source:invalid|some/file',
456
    }
457

  
458
    @project = Project.find(3)
459
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
460
  end
461

  
462
  def test_redmine_links_git_commit
463
    changeset_link = link_to('abcd',
464
                               {
465
                                 :controller => 'repositories',
466
                                 :action     => 'revision',
467
                                 :id         => 'subproject1',
468
                                 :rev        => 'abcd',
469
                                },
470
                              :class => 'changeset', :title => 'test commit')
471
    to_test = {
472
      'commit:abcd' => changeset_link,
473
     }
474
    @project = Project.find(3)
475
    r = Repository::Git.create!(:project => @project, :url => '/tmp/test/git')
476
    assert r
477
    c = Changeset.new(:repository => r,
478
                      :committed_on => Time.now,
479
                      :revision => 'abcd',
480
                      :scmid => 'abcd',
481
                      :comments => 'test commit')
482
    assert( c.save )
483
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
484
  end
485

  
486
  # TODO: Bazaar commit id contains mail address, so it contains '@' and '_'.
487
  def test_redmine_links_darcs_commit
488
    changeset_link = link_to('20080308225258-98289-abcd456efg.gz',
489
                               {
490
                                 :controller => 'repositories',
491
                                 :action     => 'revision',
492
                                 :id         => 'subproject1',
493
                                 :rev        => '123',
494
                                },
495
                              :class => 'changeset', :title => 'test commit')
496
    to_test = {
497
      'commit:20080308225258-98289-abcd456efg.gz' => changeset_link,
498
     }
499
    @project = Project.find(3)
500
    r = Repository::Darcs.create!(
501
            :project => @project, :url => '/tmp/test/darcs',
502
            :log_encoding => 'UTF-8')
503
    assert r
504
    c = Changeset.new(:repository => r,
505
                      :committed_on => Time.now,
506
                      :revision => '123',
507
                      :scmid => '20080308225258-98289-abcd456efg.gz',
508
                      :comments => 'test commit')
509
    assert( c.save )
510
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
511
  end
512

  
513
  def test_redmine_links_mercurial_commit
514
    changeset_link_rev = link_to('r123',
515
                                  {
516
                                     :controller => 'repositories',
517
                                     :action     => 'revision',
518
                                     :id         => 'subproject1',
519
                                     :rev        => '123' ,
520
                                  },
521
                              :class => 'changeset', :title => 'test commit')
522
    changeset_link_commit = link_to('abcd',
523
                                  {
524
                                        :controller => 'repositories',
525
                                        :action     => 'revision',
526
                                        :id         => 'subproject1',
527
                                        :rev        => 'abcd' ,
528
                                  },
529
                              :class => 'changeset', :title => 'test commit')
530
    to_test = {
531
      'r123' => changeset_link_rev,
532
      'commit:abcd' => changeset_link_commit,
533
     }
534
    @project = Project.find(3)
535
    r = Repository::Mercurial.create!(:project => @project, :url => '/tmp/test')
536
    assert r
537
    c = Changeset.new(:repository => r,
538
                      :committed_on => Time.now,
539
                      :revision => '123',
540
                      :scmid => 'abcd',
541
                      :comments => 'test commit')
542
    assert( c.save )
543
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
544
  end
545

  
546
  def test_attachment_links
547
    attachment_link = link_to('error281.txt', {:controller => 'attachments', :action => 'download', :id => '1'}, :class => 'attachment')
548
    to_test = {
549
      'attachment:error281.txt'      => attachment_link
550
    }
551
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => Issue.find(3).attachments), "#{text} failed" }
552
  end
553

  
554
  def test_wiki_links
555
    to_test = {
556
      '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
557
      '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
558
      # title content should be formatted
559
      '[[Another page|With _styled_ *title*]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">With <em>styled</em> <strong>title</strong></a>',
560
      '[[Another page|With title containing <strong>HTML entities &amp; markups</strong>]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">With title containing &lt;strong&gt;HTML entities &amp; markups&lt;/strong&gt;</a>',
561
      # link with anchor
562
      '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
563
      '[[Another page#anchor|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page#anchor" class="wiki-page">Page</a>',
564
      # UTF8 anchor
565
      '[[Another_page#Тест|Тест]]' => %|<a href="/projects/ecookbook/wiki/Another_page##{CGI.escape 'Тест'}" class="wiki-page">Тест</a>|,
566
      # page that doesn't exist
567
      '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
568
      '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">404</a>',
569
      # link to another project wiki
570
      '[[onlinestore:]]' => '<a href="/projects/onlinestore/wiki" class="wiki-page">onlinestore</a>',
571
      '[[onlinestore:|Wiki]]' => '<a href="/projects/onlinestore/wiki" class="wiki-page">Wiki</a>',
572
      '[[onlinestore:Start page]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Start page</a>',
573
      '[[onlinestore:Start page|Text]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Text</a>',
574
      '[[onlinestore:Unknown page]]' => '<a href="/projects/onlinestore/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
575
      # striked through link
576
      '-[[Another page|Page]]-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a></del>',
577
      '-[[Another page|Page]] link-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a> link</del>',
578
      # escaping
579
      '![[Another page|Page]]' => '[[Another page|Page]]',
580
      # project does not exist
581
      '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
582
      '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
583
    }
584

  
585
    @project = Project.find(1)
586
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
587
  end
588

  
589
  def test_wiki_links_within_local_file_generation_context
590

  
591
    to_test = {
592
      # link to a page
593
      '[[CookBook documentation]]' => '<a href="CookBook_documentation.html" class="wiki-page">CookBook documentation</a>',
594
      '[[CookBook documentation|documentation]]' => '<a href="CookBook_documentation.html" class="wiki-page">documentation</a>',
595
      '[[CookBook documentation#One-section]]' => '<a href="CookBook_documentation.html#One-section" class="wiki-page">CookBook documentation</a>',
596
      '[[CookBook documentation#One-section|documentation]]' => '<a href="CookBook_documentation.html#One-section" class="wiki-page">documentation</a>',
597
      # page that doesn't exist
598
      '[[Unknown page]]' => '<a href="Unknown_page.html" class="wiki-page new">Unknown page</a>',
599
      '[[Unknown page|404]]' => '<a href="Unknown_page.html" class="wiki-page new">404</a>',
600
      '[[Unknown page#anchor]]' => '<a href="Unknown_page.html#anchor" class="wiki-page new">Unknown page</a>',
601
      '[[Unknown page#anchor|404]]' => '<a href="Unknown_page.html#anchor" class="wiki-page new">404</a>',
602
    }
603

  
604
    @project = Project.find(1)
605

  
606
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :local) }
607
  end
608

  
609
  def test_wiki_links_within_wiki_page_context
610

  
611
    page = WikiPage.find_by_title('Another_page' )
612

  
613
    to_test = {
614
      # link to another page
615
      '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
616
      '[[CookBook documentation|documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">documentation</a>',
617
      '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
618
      '[[CookBook documentation#One-section|documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">documentation</a>',
619
      # link to the current page
620
      '[[Another page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Another page</a>',
621
      '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
622
      '[[Another page#anchor]]' => '<a href="#anchor" class="wiki-page">Another page</a>',
623
      '[[Another page#anchor|Page]]' => '<a href="#anchor" class="wiki-page">Page</a>',
624
      # page that doesn't exist
625
      '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page" class="wiki-page new">Unknown page</a>',
626
      '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page" class="wiki-page new">404</a>',
627
      '[[Unknown page#anchor]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor" class="wiki-page new">Unknown page</a>',
628
      '[[Unknown page#anchor|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor" class="wiki-page new">404</a>',
629
    }
630

  
631
    @project = Project.find(1)
632

  
633
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(WikiContent.new( :text => text, :page => page ), :text) }
634
  end
635

  
636
  def test_wiki_links_anchor_option_should_prepend_page_title_to_href
637

  
638
    to_test = {
639
      # link to a page
640
      '[[CookBook documentation]]' => '<a href="#CookBook_documentation" class="wiki-page">CookBook documentation</a>',
641
      '[[CookBook documentation|documentation]]' => '<a href="#CookBook_documentation" class="wiki-page">documentation</a>',
642
      '[[CookBook documentation#One-section]]' => '<a href="#CookBook_documentation_One-section" class="wiki-page">CookBook documentation</a>',
643
      '[[CookBook documentation#One-section|documentation]]' => '<a href="#CookBook_documentation_One-section" class="wiki-page">documentation</a>',
644
      # page that doesn't exist
645
      '[[Unknown page]]' => '<a href="#Unknown_page" class="wiki-page new">Unknown page</a>',
646
      '[[Unknown page|404]]' => '<a href="#Unknown_page" class="wiki-page new">404</a>',
647
      '[[Unknown page#anchor]]' => '<a href="#Unknown_page_anchor" class="wiki-page new">Unknown page</a>',
648
      '[[Unknown page#anchor|404]]' => '<a href="#Unknown_page_anchor" class="wiki-page new">404</a>',
649
    }
650

  
651
    @project = Project.find(1)
652

  
653
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :anchor) }
654
  end
655

  
656
  def test_html_tags
657
    to_test = {
658
      "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
659
      "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
660
      "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
661
      # do not escape pre/code tags
662
      "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
663
      "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
664
      "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
665
      "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
666
      "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
667
      # remove attributes except class
668
      "<pre class='foo'>some text</pre>" => "<pre class='foo'>some text</pre>",
669
      '<pre class="foo">some text</pre>' => '<pre class="foo">some text</pre>',
670
      "<pre class='foo bar'>some text</pre>" => "<pre class='foo bar'>some text</pre>",
671
      '<pre class="foo bar">some text</pre>' => '<pre class="foo bar">some text</pre>',
672
      "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
673
      # xss
674
      '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>',
675
      '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>',
676
    }
677
    to_test.each { |text, result| assert_equal result, textilizable(text) }
678
  end
679

  
680
  def test_allowed_html_tags
681
    to_test = {
682
      "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
683
      "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
684
      "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
685
    }
686
    to_test.each { |text, result| assert_equal result, textilizable(text) }
687
  end
688

  
689
  def test_pre_tags
690
    raw = <<-RAW
691
Before
692

  
693
<pre>
694
<prepared-statement-cache-size>32</prepared-statement-cache-size>
695
</pre>
696

  
697
After
698
RAW
699

  
700
    expected = <<-EXPECTED
701
<p>Before</p>
702
<pre>
703
&lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
704
</pre>
705
<p>After</p>
706
EXPECTED
707

  
708
    assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
709
  end
710

  
711
  def test_pre_content_should_not_parse_wiki_and_redmine_links
712
    raw = <<-RAW
713
[[CookBook documentation]]
714
  
715
#1
716

  
717
<pre>
718
[[CookBook documentation]]
719
  
720
#1
721
</pre>
722
RAW
723

  
724
    expected = <<-EXPECTED
725
<p><a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a></p>
726
<p><a href="/issues/1" class="issue status-1 priority-4 priority-lowest" title="Can&#x27;t print recipes (New)">#1</a></p>
727
<pre>
728
[[CookBook documentation]]
729

  
730
#1
731
</pre>
732
EXPECTED
733

  
734
    @project = Project.find(1)
735
    assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
736
  end
737

  
738
  def test_non_closing_pre_blocks_should_be_closed
739
    raw = <<-RAW
740
<pre><code>
741
RAW
742

  
743
    expected = <<-EXPECTED
744
<pre><code>
745
</code></pre>
746
EXPECTED
747

  
748
    @project = Project.find(1)
749
    assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
750
  end
751

  
752
  def test_syntax_highlight
753
    raw = <<-RAW
754
<pre><code class="ruby">
755
# Some ruby code here
756
</code></pre>
757
RAW
758

  
759
    expected = <<-EXPECTED
760
<pre><code class="ruby syntaxhl"><span class=\"CodeRay\"><span class="comment"># Some ruby code here</span></span>
761
</code></pre>
762
EXPECTED
763

  
764
    assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
765
  end
766

  
767
  def test_to_path_param
768
    assert_equal 'test1/test2', to_path_param('test1/test2')
769
    assert_equal 'test1/test2', to_path_param('/test1/test2/')
770
    assert_equal 'test1/test2', to_path_param('//test1/test2/')
771
    assert_equal nil, to_path_param('/')
772
  end
773

  
774
  def test_wiki_links_in_tables
775
    to_test = {"|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
776
                 '<tr><td><a href="/projects/ecookbook/wiki/Page" class="wiki-page new">Link title</a></td>' +
777
                 '<td><a href="/projects/ecookbook/wiki/Other_Page" class="wiki-page new">Other title</a></td>' +
778
                 '</tr><tr><td>Cell 21</td><td><a href="/projects/ecookbook/wiki/Last_page" class="wiki-page new">Last page</a></td></tr>'
779
    }
780
    @project = Project.find(1)
781
    to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
782
  end
783

  
784
  def test_text_formatting
785
    to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
786
               '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
787
               'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
788
               '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>',
789
               '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',
790
              }
791
    to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
792
  end
793

  
794
  def test_wiki_horizontal_rule
795
    assert_equal '<hr />', textilizable('---')
796
    assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
797
  end
798

  
799
  def test_footnotes
800
    raw = <<-RAW
801
This is some text[1].
802

  
803
fn1. This is the foot note
804
RAW
805

  
806
    expected = <<-EXPECTED
807
<p>This is some text<sup><a href=\"#fn1\">1</a></sup>.</p>
808
<p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
809
EXPECTED
810

  
811
    assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
812
  end
813

  
814
  def test_headings
815
    raw = 'h1. Some heading'
816
    expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>|
817

  
818
    assert_equal expected, textilizable(raw)
819
  end
820

  
821
  def test_headings_with_special_chars
822
    # This test makes sure that the generated anchor names match the expected
823
    # ones even if the heading text contains unconventional characters
824
    raw = 'h1. Some heading related to version 0.5'
825
    anchor = sanitize_anchor_name("Some-heading-related-to-version-0.5")
826
    expected = %|<a name="#{anchor}"></a>\n<h1 >Some heading related to version 0.5<a href="##{anchor}" class="wiki-anchor">&para;</a></h1>|
827

  
828
    assert_equal expected, textilizable(raw)
829
  end
830

  
831
  def test_headings_in_wiki_single_page_export_should_be_prepended_with_page_title
832
    page = WikiPage.new( :title => 'Page Title', :wiki_id => 1 )
833
    content = WikiContent.new( :text => 'h1. Some heading', :page => page )
834

  
835
    expected = %|<a name="Page_Title_Some-heading"></a>\n<h1 >Some heading<a href="#Page_Title_Some-heading" class="wiki-anchor">&para;</a></h1>|
836

  
837
    assert_equal expected, textilizable(content, :text, :wiki_links => :anchor )
838
  end
839

  
840
  def test_table_of_content
841
    raw = <<-RAW
842
{{toc}}
843

  
844
h1. Title
845

  
846
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
847

  
848
h2. Subtitle with a [[Wiki]] link
849

  
850
Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
851

  
852
h2. Subtitle with [[Wiki|another Wiki]] link
853

  
854
h2. Subtitle with %{color:red}red text%
855

  
856
<pre>
857
some code
858
</pre>
859

  
860
h3. Subtitle with *some* _modifiers_
861

  
862
h3. Subtitle with @inline code@
863

  
864
h1. Another title
865

  
866
h3. An "Internet link":http://www.redmine.org/ inside subtitle
867

  
868
h2. "Project Name !/attachments/1234/logo_small.gif! !/attachments/5678/logo_2.png!":/projects/projectname/issues
869

  
870
RAW
871

  
872
    expected =  '<ul class="toc">' +
873
                  '<li><a href="#Title">Title</a>' +
874
                    '<ul>' +
875
                      '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
876
                      '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
877
                      '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' +
878
                        '<ul>' +
879
                          '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' +
880
                          '<li><a href="#Subtitle-with-inline-code">Subtitle with inline code</a></li>' +
881
                        '</ul>' +
882
                      '</li>' +
883
                    '</ul>' +
884
                  '</li>' +
885
                  '<li><a href="#Another-title">Another title</a>' +
886
                    '<ul>' +
887
                      '<li>' +
888
                        '<ul>' +
889
                          '<li><a href="#An-Internet-link-inside-subtitle">An Internet link inside subtitle</a></li>' +
890
                        '</ul>' +
891
                      '</li>' +
892
                      '<li><a href="#Project-Name">Project Name</a></li>' +
893
                    '</ul>' +
894
                  '</li>' +
895
               '</ul>'
896

  
897
    @project = Project.find(1)
898
    assert textilizable(raw).gsub("\n", "").include?(expected)
899
  end
900

  
901
  def test_table_of_content_should_generate_unique_anchors
902
    raw = <<-RAW
903
{{toc}}
904

  
905
h1. Title
906

  
907
h2. Subtitle
908

  
909
h2. Subtitle
910
RAW
911

  
912
    expected =  '<ul class="toc">' +
913
                  '<li><a href="#Title">Title</a>' +
914
                    '<ul>' +
915
                      '<li><a href="#Subtitle">Subtitle</a></li>' +
916
                      '<li><a href="#Subtitle-2">Subtitle</a></li>'
917
                    '</ul>'
918
                  '</li>' +
919
               '</ul>'
920

  
921
    @project = Project.find(1)
922
    result = textilizable(raw).gsub("\n", "")
923
    assert_include expected, result
924
    assert_include '<a name="Subtitle">', result
925
    assert_include '<a name="Subtitle-2">', result
926
  end
927

  
928
  def test_table_of_content_should_contain_included_page_headings
929
    raw = <<-RAW
930
{{toc}}
931

  
932
h1. Included
933

  
934
{{include(Child_1)}}
935
RAW
936

  
937
    expected = '<ul class="toc">' +
938
               '<li><a href="#Included">Included</a></li>' +
939
               '<li><a href="#Child-page-1">Child page 1</a></li>' +
940
               '</ul>'
941

  
942
    @project = Project.find(1)
943
    assert textilizable(raw).gsub("\n", "").include?(expected)
944
  end
945

  
946
  def test_section_edit_links
947
    raw = <<-RAW
948
h1. Title
949

  
950
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
951

  
952
h2. Subtitle with a [[Wiki]] link
953

  
954
h2. Subtitle with *some* _modifiers_
955

  
956
h2. Subtitle with @inline code@
957

  
958
<pre>
959
some code
960

  
961
h2. heading inside pre
962

  
963
<h2>html heading inside pre</h2>
964
</pre>
965

  
966
h2. Subtitle after pre tag
967
RAW
968

  
969
    @project = Project.find(1)
970
    set_language_if_valid 'en'
971
    result = textilizable(raw, :edit_section_links => {:controller => 'wiki', :action => 'edit', :project_id => '1', :id => 'Test'}).gsub("\n", "")
972

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff