comparison test/functional/custom_fields_controller_test.rb @ 1526:404aa68d4227

Merge from live branch
author Chris Cannam
date Thu, 11 Sep 2014 12:46:20 +0100
parents dffacf8a6908
children
comparison
equal deleted inserted replaced
1493:a5f2bdf3b486 1526:404aa68d4227
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 require File.expand_path('../../test_helper', __FILE__) 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 class CustomFieldsControllerTest < ActionController::TestCase 20 class CustomFieldsControllerTest < ActionController::TestCase
21 fixtures :custom_fields, :custom_values, :trackers, :users 21 fixtures :custom_fields, :custom_values, :trackers, :users, :projects
22 22
23 def setup 23 def setup
24 @request.session[:user_id] = 1 24 @request.session[:user_id] = 1
25 end 25 end
26 26
28 get :index 28 get :index
29 assert_response :success 29 assert_response :success
30 assert_template 'index' 30 assert_template 'index'
31 end 31 end
32 32
33 def test_new 33 def test_new_without_type_should_render_select_type
34 get :new
35 assert_response :success
36 assert_template 'select_type'
37 assert_select 'input[name=type]', CustomField.subclasses.size
38 assert_select 'input[name=type][checked=checked]', 1
39 end
40
41 def test_new_should_work_for_each_customized_class_and_format
34 custom_field_classes.each do |klass| 42 custom_field_classes.each do |klass|
35 get :new, :type => klass.name 43 Redmine::FieldFormat.available_formats.each do |format_name|
36 assert_response :success 44 get :new, :type => klass.name, :custom_field => {:field_format => format_name}
37 assert_template 'new' 45 assert_response :success
38 assert_kind_of klass, assigns(:custom_field) 46 assert_template 'new'
39 assert_select 'form#custom_field_form' do 47 assert_kind_of klass, assigns(:custom_field)
40 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' 48 assert_equal format_name, assigns(:custom_field).format.name
41 assert_select 'input[type=hidden][name=type][value=?]', klass.name 49 assert_select 'form#custom_field_form' do
50 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]'
51 assert_select 'input[type=hidden][name=type][value=?]', klass.name
52 end
42 end 53 end
43 end 54 end
55 end
56
57 def test_new_should_have_string_default_format
58 get :new, :type => 'IssueCustomField'
59 assert_response :success
60 assert_equal 'string', assigns(:custom_field).format.name
44 end 61 end
45 62
46 def test_new_issue_custom_field 63 def test_new_issue_custom_field
47 get :new, :type => 'IssueCustomField' 64 get :new, :type => 'IssueCustomField'
48 assert_response :success 65 assert_response :success
50 assert_select 'form#custom_field_form' do 67 assert_select 'form#custom_field_form' do
51 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do 68 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
52 assert_select 'option[value=user]', :text => 'User' 69 assert_select 'option[value=user]', :text => 'User'
53 assert_select 'option[value=version]', :text => 'Version' 70 assert_select 'option[value=version]', :text => 'Version'
54 end 71 end
72 assert_select 'input[type=checkbox][name=?]', 'custom_field[project_ids][]', Project.count
73 assert_select 'input[type=hidden][name=?]', 'custom_field[project_ids][]', 1
55 assert_select 'input[type=hidden][name=type][value=IssueCustomField]' 74 assert_select 'input[type=hidden][name=type][value=IssueCustomField]'
56 end 75 end
76 end
77
78 def test_new_time_entry_custom_field_should_not_show_trackers_and_projects
79 get :new, :type => 'TimeEntryCustomField'
80 assert_response :success
81 assert_template 'new'
82 assert_select 'form#custom_field_form' do
83 assert_select 'input[name=?]', 'custom_field[tracker_ids][]', 0
84 assert_select 'input[name=?]', 'custom_field[project_ids][]', 0
85 end
86 end
87
88 def test_default_value_should_be_an_input_for_string_custom_field
89 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'string'}
90 assert_response :success
91 assert_select 'input[name=?]', 'custom_field[default_value]'
92 end
93
94 def test_default_value_should_be_a_textarea_for_text_custom_field
95 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'text'}
96 assert_response :success
97 assert_select 'textarea[name=?]', 'custom_field[default_value]'
98 end
99
100 def test_default_value_should_be_a_checkbox_for_bool_custom_field
101 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'bool'}
102 assert_response :success
103 assert_select 'select[name=?]', 'custom_field[default_value]' do
104 assert_select 'option', 3
105 end
106 end
107
108 def test_default_value_should_not_be_present_for_user_custom_field
109 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'user'}
110 assert_response :success
111 assert_select '[name=?]', 'custom_field[default_value]', 0
57 end 112 end
58 113
59 def test_new_js 114 def test_new_js
60 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js' 115 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js'
61 assert_response :success 116 assert_response :success
64 119
65 field = assigns(:custom_field) 120 field = assigns(:custom_field)
66 assert_equal 'list', field.field_format 121 assert_equal 'list', field.field_format
67 end 122 end
68 123
69 def test_new_with_invalid_custom_field_class_should_render_404 124 def test_new_with_invalid_custom_field_class_should_render_select_type
70 get :new, :type => 'UnknownCustomField' 125 get :new, :type => 'UnknownCustomField'
71 assert_response 404 126 assert_response :success
127 assert_template 'select_type'
72 end 128 end
73 129
74 def test_create_list_custom_field 130 def test_create_list_custom_field
75 assert_difference 'CustomField.count' do 131 assert_difference 'CustomField.count' do
76 post :create, :type => "IssueCustomField", 132 post :create, :type => "IssueCustomField",
92 assert_not_nil field 148 assert_not_nil field
93 assert_equal ["0.1", "0.2"], field.possible_values 149 assert_equal ["0.1", "0.2"], field.possible_values
94 assert_equal 1, field.trackers.size 150 assert_equal 1, field.trackers.size
95 end 151 end
96 152
153 def test_create_with_project_ids
154 assert_difference 'CustomField.count' do
155 post :create, :type => "IssueCustomField", :custom_field => {
156 :name => "foo", :field_format => "string", :is_for_all => "0", :project_ids => ["1", "3", ""]
157 }
158 assert_response 302
159 end
160 field = IssueCustomField.order("id desc").first
161 assert_equal [1, 3], field.projects.map(&:id).sort
162 end
163
97 def test_create_with_failure 164 def test_create_with_failure
98 assert_no_difference 'CustomField.count' do 165 assert_no_difference 'CustomField.count' do
99 post :create, :type => "IssueCustomField", :custom_field => {:name => ''} 166 post :create, :type => "IssueCustomField", :custom_field => {:name => ''}
100 end 167 end
101 assert_response :success 168 assert_response :success
102 assert_template 'new' 169 assert_template 'new'
103 end 170 end
104 171
172 def test_create_without_type_should_render_select_type
173 assert_no_difference 'CustomField.count' do
174 post :create, :custom_field => {:name => ''}
175 end
176 assert_response :success
177 assert_template 'select_type'
178 end
179
105 def test_edit 180 def test_edit
106 get :edit, :id => 1 181 get :edit, :id => 1
107 assert_response :success 182 assert_response :success
108 assert_template 'edit' 183 assert_template 'edit'
109 assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'} 184 assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'}
127 assert_response :success 202 assert_response :success
128 assert_template 'edit' 203 assert_template 'edit'
129 end 204 end
130 205
131 def test_destroy 206 def test_destroy
132 custom_values_count = CustomValue.count(:conditions => {:custom_field_id => 1}) 207 custom_values_count = CustomValue.where(:custom_field_id => 1).count
133 assert custom_values_count > 0 208 assert custom_values_count > 0
134 209
135 assert_difference 'CustomField.count', -1 do 210 assert_difference 'CustomField.count', -1 do
136 assert_difference 'CustomValue.count', - custom_values_count do 211 assert_difference 'CustomValue.count', - custom_values_count do
137 delete :destroy, :id => 1 212 delete :destroy, :id => 1