summaryrefslogtreecommitdiffstats
path: root/test/unit/lib/redmine/field_format/field_format_test.rb
blob: 6418917f29f333dbafaa8b3aa158ea3a5f448582 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# frozen_string_literal: true

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

require_relative '../../../../test_helper'

class Redmine::FieldFormatTest < ActionView::TestCase
  def setup
    User.current = nil
    set_language_if_valid 'en'
  end

  def test_string_field_with_text_formatting_disabled_should_not_format_text
    field = IssueCustomField.new(:field_format => 'string')
    custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*")

    assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, false)
    assert_equal "*foo*", field.format.formatted_custom_value(self, custom_value, true)
  end

  def test_string_field_with_text_formatting_enabled_should_format_text
    field = IssueCustomField.new(:field_format => 'string', :text_formatting => 'full')
    custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*")

    with_settings :text_formatting => 'textile' do
      assert_equal '*foo*', field.format.formatted_custom_value(self, custom_value, false)
      assert_include '<strong>foo</strong>', field.format.formatted_custom_value(self, custom_value, true)
    end
  end

  def test_text_field_with_text_formatting_disabled_should_not_format_text
    field = IssueCustomField.new(:field_format => 'text')
    custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*\nbar")

    assert_equal "*foo*\nbar", field.format.formatted_custom_value(self, custom_value, false)
    assert_include "*foo*\n<br />bar", field.format.formatted_custom_value(self, custom_value, true)
  end

  def test_text_field_with_text_formatting_enabled_should_format_text
    field = IssueCustomField.new(:field_format => 'text', :text_formatting => 'full')
    custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "*foo*\nbar")

    with_settings :text_formatting => 'textile' do
      assert_equal "*foo*\nbar", field.format.formatted_custom_value(self, custom_value, false)
      assert_include '<strong>foo</strong>', field.format.formatted_custom_value(self, custom_value, true)
    end
  end

  def test_should_validate_url_pattern_with_safe_scheme
    field = IssueCustomField.new(:field_format => 'string', :name => 'URL', :url_pattern => 'http://foo/%value%')
    assert_save field
  end

  def test_should_not_validate_url_pattern_with_unsafe_scheme
    field = IssueCustomField.new(:field_format => 'string', :name => 'URL', :url_pattern => 'foo://foo/%value%')
    assert !field.save
    assert_include "URL is invalid", field.errors.full_messages
  end

  def test_text_field_with_url_pattern_should_format_as_link
    field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/%value%')
    custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "bar")

    assert_equal "bar", field.format.formatted_custom_value(self, custom_value, false)
    assert_equal '<a href="http://foo/bar" class="external">bar</a>', field.format.formatted_custom_value(self, custom_value, true)
  end

  def test_text_field_with_url_pattern_and_value_containing_a_space_should_format_as_link
    field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/%value%')
    custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "foo bar")

    assert_equal "foo bar", field.format.formatted_custom_value(self, custom_value, false)
    assert_equal '<a href="http://foo/foo%20bar" class="external">foo bar</a>', field.format.formatted_custom_value(self, custom_value, true)
  end

  def test_text_field_with_url_pattern_and_value_containing_a_colon_preceded_by_a_space_should_format_as_link
    field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/%value%')
    custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => 'foo :bar')

    assert_equal 'foo :bar', field.format.formatted_custom_value(self, custom_value, false)
    assert_equal '<a href="http://foo/foo%20:bar" class="external">foo :bar</a>', field.format.formatted_custom_value(self, custom_value, true)
  end

  def test_text_field_with_url_pattern_should_not_encode_url_pattern
    field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/bar#anchor')
    custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "1")

    assert_equal "1", field.format.formatted_custom_value(self, custom_value, false)
    assert_equal '<a href="http://foo/bar#anchor" class="external">1</a>', field.format.formatted_custom_value(self, custom_value, true)
  end

  def test_text_field_with_url_pattern_should_encode_values
    field = IssueCustomField.new(:field_format => 'string', :url_pattern => 'http://foo/%value%#anchor')
    custom_value = CustomValue.new(:custom_field => field, :customized => Issue.new, :value => "foo bar")

    assert_equal "foo bar", field.format.formatted_custom_value(self, custom_value, false)
    assert_equal '<a href="http://foo/foo%20bar#anchor" class="external">foo bar</a>', field.format.formatted_custom_value(self, custom_value, true)
  end

  def test_as_select_should_return_enumeration_for_all_classes
    %w(Issue TimeEntry Project Version Document User Group TimeEntryActivity IssuePriority DocumentCategory).each do |klass|
      assert_include ['Key/value list', 'enumeration'], Redmine::FieldFormat.as_select(klass)
    end
  end
end