list_format_test.rb 8.88 KB
Newer Older
1
# frozen_string_literal: true
2

3
# Redmine - project management software
jplang's avatar
jplang committed
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#
# 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 File.expand_path('../../../../../test_helper', __FILE__)
require 'redmine/field_format'

class Redmine::ListFieldFormatTest < ActionView::TestCase
  include ApplicationHelper
  include Redmine::I18n

  def setup
28
    User.current = nil
29 30 31 32 33 34 35 36 37 38 39 40 41 42
    set_language_if_valid 'en'
  end

  def test_possible_existing_value_should_be_valid
    field = GroupCustomField.create!(:name => 'List', :field_format => 'list', :possible_values => ['Foo', 'Bar'])
    group = Group.new(:name => 'Group')
    group.custom_field_values = {field.id => 'Baz'}
    assert group.save(:validate => false)

    group = Group.order('id DESC').first
    assert_equal ['Foo', 'Bar', 'Baz'], field.possible_custom_value_options(group.custom_value_for(field))
    assert group.valid?
  end

jplang's avatar
jplang committed
43 44 45 46 47 48 49 50 51 52
  def test_non_existing_value_should_be_invalid
    field = GroupCustomField.create!(:name => 'List', :field_format => 'list', :possible_values => ['Foo', 'Bar'])
    group = Group.new(:name => 'Group')
    group.custom_field_values = {field.id => 'Baz'}

    assert_not_include 'Baz', field.possible_custom_value_options(group.custom_value_for(field))
    assert_equal false, group.valid?
    assert_include "List #{::I18n.t('activerecord.errors.messages.inclusion')}", group.errors.full_messages.first
  end

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  def test_edit_tag_should_have_id_and_name
    field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false)
    value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new)

    tag = field.format.edit_tag(self, 'abc', 'xyz', value)
    assert_select_in tag, 'select[id=abc][name=xyz]'
  end

  def test_edit_tag_should_contain_possible_values
    field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false)
    value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new)

    tag = field.format.edit_tag(self, 'id', 'name', value)
    assert_select_in tag, 'select' do
      assert_select 'option', 3
68
      assert_select 'option[value=""]'
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
      assert_select 'option[value=Foo]', :text => 'Foo'
      assert_select 'option[value=Bar]', :text => 'Bar'
    end
  end

  def test_edit_tag_should_select_current_value
    field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false)
    value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new, :value => 'Bar')

    tag = field.format.edit_tag(self, 'id', 'name', value)
    assert_select_in tag, 'select' do
      assert_select 'option[selected=selected]', 1
      assert_select 'option[value=Bar][selected=selected]', :text => 'Bar'
    end
  end

  def test_edit_tag_with_multiple_should_select_current_values
    field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar', 'Baz'], :is_required => false,
      :multiple => true)
    value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new, :value => ['Bar', 'Baz'])

    tag = field.format.edit_tag(self, 'id', 'name', value)
    assert_select_in tag, 'select[multiple=multiple]' do
      assert_select 'option[selected=selected]', 2
      assert_select 'option[value=Bar][selected=selected]', :text => 'Bar'
      assert_select 'option[value=Baz][selected=selected]', :text => 'Baz'
    end
  end

  def test_edit_tag_with_check_box_style_should_contain_possible_values
    field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false,
      :edit_tag_style => 'check_box')
    value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new)

    tag = field.format.edit_tag(self, 'id', 'name', value)
    assert_select_in tag, 'span' do
      assert_select 'input[type=radio]', 3
      assert_select 'label', :text => '(none)' do
107
        assert_select 'input[value=""]'
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
      end
      assert_select 'label', :text => 'Foo' do
        assert_select 'input[value=Foo]'
      end
      assert_select 'label', :text => 'Bar' do
        assert_select 'input[value=Bar]'
      end
    end
  end

  def test_edit_tag_with_check_box_style_should_select_current_value
    field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false,
      :edit_tag_style => 'check_box')
    value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new, :value => 'Bar')

    tag = field.format.edit_tag(self, 'id', 'name', value)
    assert_select_in tag, 'span' do
      assert_select 'input[type=radio][checked=checked]', 1
      assert_select 'label', :text => 'Bar' do
        assert_select 'input[value=Bar][checked=checked]'
      end
    end
  end

132 133 134 135 136 137 138 139 140 141 142 143
  def test_edit_tag_with_check_box_style_and_multiple_values_should_contain_hidden_field_to_clear_value
    field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar'], :is_required => false,
      :edit_tag_style => 'check_box', :multiple => true)
    value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new)

    tag = field.format.edit_tag(self, 'id', 'name', value)
    assert_select_in tag, 'span' do
      assert_select 'input[type=checkbox]', 2
      assert_select 'input[type=hidden]', 1
    end
  end

144 145 146
  def test_field_with_url_pattern_should_link_value
    field = IssueCustomField.new(:field_format => 'list', :url_pattern => 'http://localhost/%value%')
    formatted = field.format.formatted_value(self, field, 'foo', Issue.new, true)
147
    assert_equal '<a class="external" href="http://localhost/foo">foo</a>', formatted
148 149 150 151 152 153
    assert formatted.html_safe?
  end

  def test_field_with_url_pattern_and_multiple_values_should_link_values
    field = IssueCustomField.new(:field_format => 'list', :url_pattern => 'http://localhost/%value%')
    formatted = field.format.formatted_value(self, field, ['foo', 'bar'], Issue.new, true)
154
    assert_equal '<a class="external" href="http://localhost/bar">bar</a>, <a class="external" href="http://localhost/foo">foo</a>', formatted
155 156 157 158 159 160 161 162 163 164
    assert formatted.html_safe?
  end

  def test_field_with_url_pattern_should_not_link_blank_value
    field = IssueCustomField.new(:field_format => 'list', :url_pattern => 'http://localhost/%value%')
    formatted = field.format.formatted_value(self, field, '', Issue.new, true)
    assert_equal '', formatted
    assert formatted.html_safe?
  end

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  def test_edit_tag_with_check_box_style_and_multiple_should_select_current_values
    field = IssueCustomField.new(:field_format => 'list', :possible_values => ['Foo', 'Bar', 'Baz'], :is_required => false,
      :multiple => true, :edit_tag_style => 'check_box')
    value = CustomFieldValue.new(:custom_field => field, :customized => Issue.new, :value => ['Bar', 'Baz'])

    tag = field.format.edit_tag(self, 'id', 'name', value)
    assert_select_in tag, 'span' do
      assert_select 'input[type=checkbox][checked=checked]', 2
      assert_select 'label', :text => 'Bar' do
        assert_select 'input[value=Bar][checked=checked]'
      end
      assert_select 'label', :text => 'Baz' do
        assert_select 'input[value=Baz][checked=checked]'
      end
    end
  end
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

  def test_value_from_keyword_should_return_value
    field = GroupCustomField.create!(:name => 'List', :field_format => 'list', :possible_values => ['Foo', 'Bar', 'Baz,qux'])

    assert_equal 'Foo', field.value_from_keyword('foo', nil)
    assert_equal 'Baz,qux', field.value_from_keyword('baz,qux', nil)
    assert_nil field.value_from_keyword('invalid', nil)
  end

  def test_value_from_keyword_for_multiple_custom_field_should_return_values
    field = GroupCustomField.create!(:name => 'List', :field_format => 'list', :possible_values => ['Foo', 'Bar', 'Baz,qux'], :multiple => true)

    assert_equal ['Foo','Bar'], field.value_from_keyword('foo,bar', nil)
    assert_equal ['Baz,qux'], field.value_from_keyword('baz,qux', nil)
    assert_equal ['Baz,qux', 'Foo'], field.value_from_keyword('baz,qux,foo', nil)
    assert_equal ['Foo'], field.value_from_keyword('foo,invalid', nil)
    assert_equal ['Foo'], field.value_from_keyword(',foo,', nil)
    assert_equal ['Foo'], field.value_from_keyword(',foo, ,,', nil)
    assert_equal [], field.value_from_keyword('invalid', nil)
  end
201
end