comparison .svn/pristine/14/1417ec7b1e59a18d042b1d9264d06470b6e7c6bf.svn-base @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents
children
comparison
equal deleted inserted replaced
1516:b450a9d58aed 1517:dffacf8a6908
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 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 require File.expand_path('../../test_helper', __FILE__)
19
20 class CustomFieldsControllerTest < ActionController::TestCase
21 fixtures :custom_fields, :custom_values, :trackers, :users, :projects
22
23 def setup
24 @request.session[:user_id] = 1
25 end
26
27 def test_index
28 get :index
29 assert_response :success
30 assert_template 'index'
31 end
32
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
42 custom_field_classes.each do |klass|
43 Redmine::FieldFormat.available_formats.each do |format_name|
44 get :new, :type => klass.name, :custom_field => {:field_format => format_name}
45 assert_response :success
46 assert_template 'new'
47 assert_kind_of klass, assigns(:custom_field)
48 assert_equal format_name, assigns(:custom_field).format.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
53 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
61 end
62
63 def test_new_issue_custom_field
64 get :new, :type => 'IssueCustomField'
65 assert_response :success
66 assert_template 'new'
67 assert_select 'form#custom_field_form' do
68 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
69 assert_select 'option[value=user]', :text => 'User'
70 assert_select 'option[value=version]', :text => 'Version'
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
74 assert_select 'input[type=hidden][name=type][value=IssueCustomField]'
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
112 end
113
114 def test_new_js
115 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js'
116 assert_response :success
117 assert_template 'new'
118 assert_equal 'text/javascript', response.content_type
119
120 field = assigns(:custom_field)
121 assert_equal 'list', field.field_format
122 end
123
124 def test_new_with_invalid_custom_field_class_should_render_select_type
125 get :new, :type => 'UnknownCustomField'
126 assert_response :success
127 assert_template 'select_type'
128 end
129
130 def test_create_list_custom_field
131 assert_difference 'CustomField.count' do
132 post :create, :type => "IssueCustomField",
133 :custom_field => {:name => "test_post_new_list",
134 :default_value => "",
135 :min_length => "0",
136 :searchable => "0",
137 :regexp => "",
138 :is_for_all => "1",
139 :possible_values => "0.1\n0.2\n",
140 :max_length => "0",
141 :is_filter => "0",
142 :is_required =>"0",
143 :field_format => "list",
144 :tracker_ids => ["1", ""]}
145 end
146 assert_redirected_to '/custom_fields?tab=IssueCustomField'
147 field = IssueCustomField.find_by_name('test_post_new_list')
148 assert_not_nil field
149 assert_equal ["0.1", "0.2"], field.possible_values
150 assert_equal 1, field.trackers.size
151 end
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
164 def test_create_with_failure
165 assert_no_difference 'CustomField.count' do
166 post :create, :type => "IssueCustomField", :custom_field => {:name => ''}
167 end
168 assert_response :success
169 assert_template 'new'
170 end
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
180 def test_edit
181 get :edit, :id => 1
182 assert_response :success
183 assert_template 'edit'
184 assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'}
185 end
186
187 def test_edit_invalid_custom_field_should_render_404
188 get :edit, :id => 99
189 assert_response 404
190 end
191
192 def test_update
193 put :update, :id => 1, :custom_field => {:name => 'New name'}
194 assert_redirected_to '/custom_fields?tab=IssueCustomField'
195
196 field = CustomField.find(1)
197 assert_equal 'New name', field.name
198 end
199
200 def test_update_with_failure
201 put :update, :id => 1, :custom_field => {:name => ''}
202 assert_response :success
203 assert_template 'edit'
204 end
205
206 def test_destroy
207 custom_values_count = CustomValue.where(:custom_field_id => 1).count
208 assert custom_values_count > 0
209
210 assert_difference 'CustomField.count', -1 do
211 assert_difference 'CustomValue.count', - custom_values_count do
212 delete :destroy, :id => 1
213 end
214 end
215
216 assert_redirected_to '/custom_fields?tab=IssueCustomField'
217 assert_nil CustomField.find_by_id(1)
218 assert_nil CustomValue.find_by_custom_field_id(1)
219 end
220
221 def custom_field_classes
222 files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') }
223 classes = files.map(&:classify).map(&:constantize)
224 assert classes.size > 0
225 classes
226 end
227 end