@@ -1,88 +1,88 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class CustomFieldsController < ApplicationController |
|
19 | 19 | layout 'admin' |
|
20 | 20 | |
|
21 | 21 | before_filter :require_admin |
|
22 | 22 | before_filter :build_new_custom_field, :only => [:new, :create] |
|
23 | 23 | before_filter :find_custom_field, :only => [:edit, :update, :destroy] |
|
24 | 24 | accept_api_auth :index |
|
25 | 25 | |
|
26 | 26 | def index |
|
27 | 27 | respond_to do |format| |
|
28 | 28 | format.html { |
|
29 | 29 | @custom_fields_by_type = CustomField.all.group_by {|f| f.class.name } |
|
30 | 30 | } |
|
31 | 31 | format.api { |
|
32 | 32 | @custom_fields = CustomField.all |
|
33 | 33 | } |
|
34 | 34 | end |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | def new |
|
38 | 38 | @custom_field.field_format = 'string' if @custom_field.field_format.blank? |
|
39 | 39 | @custom_field.default_value = nil |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | def create |
|
43 | 43 | if @custom_field.save |
|
44 | 44 | flash[:notice] = l(:notice_successful_create) |
|
45 | 45 | call_hook(:controller_custom_fields_new_after_save, :params => params, :custom_field => @custom_field) |
|
46 |
redirect_to custom_field |
|
|
46 | redirect_to edit_custom_field_path(@custom_field) | |
|
47 | 47 | else |
|
48 | 48 | render :action => 'new' |
|
49 | 49 | end |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | def edit |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def update |
|
56 | 56 | if @custom_field.update_attributes(params[:custom_field]) |
|
57 | 57 | flash[:notice] = l(:notice_successful_update) |
|
58 | 58 | call_hook(:controller_custom_fields_edit_after_save, :params => params, :custom_field => @custom_field) |
|
59 |
redirect_to custom_field |
|
|
59 | redirect_to edit_custom_field_path(@custom_field) | |
|
60 | 60 | else |
|
61 | 61 | render :action => 'edit' |
|
62 | 62 | end |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | def destroy |
|
66 | 66 | begin |
|
67 | 67 | @custom_field.destroy |
|
68 | 68 | rescue |
|
69 | 69 | flash[:error] = l(:error_can_not_delete_custom_field) |
|
70 | 70 | end |
|
71 | 71 | redirect_to custom_fields_path(:tab => @custom_field.class.name) |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | private |
|
75 | 75 | |
|
76 | 76 | def build_new_custom_field |
|
77 | 77 | @custom_field = CustomField.new_subclass_instance(params[:type], params[:custom_field]) |
|
78 | 78 | if @custom_field.nil? |
|
79 | 79 | render :action => 'select_type' |
|
80 | 80 | end |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | def find_custom_field |
|
84 | 84 | @custom_field = CustomField.find(params[:id]) |
|
85 | 85 | rescue ActiveRecord::RecordNotFound |
|
86 | 86 | render_404 |
|
87 | 87 | end |
|
88 | 88 | end |
@@ -1,238 +1,237 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class CustomFieldsControllerTest < ActionController::TestCase |
|
21 | 21 | fixtures :custom_fields, :custom_values, |
|
22 | 22 | :custom_fields_projects, :custom_fields_trackers, |
|
23 | 23 | :roles, :users, |
|
24 | 24 | :members, :member_roles, |
|
25 | 25 | :groups_users, |
|
26 | 26 | :trackers, :projects_trackers, |
|
27 | 27 | :enabled_modules, |
|
28 | 28 | :projects, :issues, |
|
29 | 29 | :issue_statuses, |
|
30 | 30 | :issue_categories, |
|
31 | 31 | :enumerations, |
|
32 | 32 | :workflows |
|
33 | 33 | |
|
34 | 34 | def setup |
|
35 | 35 | @request.session[:user_id] = 1 |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | def test_index |
|
39 | 39 | get :index |
|
40 | 40 | assert_response :success |
|
41 | 41 | assert_template 'index' |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def test_new_without_type_should_render_select_type |
|
45 | 45 | get :new |
|
46 | 46 | assert_response :success |
|
47 | 47 | assert_template 'select_type' |
|
48 | 48 | assert_select 'input[name=type]', CustomFieldsHelper::CUSTOM_FIELDS_TABS.size |
|
49 | 49 | assert_select 'input[name=type][checked=checked]', 1 |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | def test_new_should_work_for_each_customized_class_and_format |
|
53 | 53 | custom_field_classes.each do |klass| |
|
54 | 54 | Redmine::FieldFormat.available_formats.each do |format_name| |
|
55 | 55 | get :new, :type => klass.name, :custom_field => {:field_format => format_name} |
|
56 | 56 | assert_response :success |
|
57 | 57 | assert_template 'new' |
|
58 | 58 | assert_kind_of klass, assigns(:custom_field) |
|
59 | 59 | assert_equal format_name, assigns(:custom_field).format.name |
|
60 | 60 | assert_select 'form#custom_field_form' do |
|
61 | 61 | assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' |
|
62 | 62 | assert_select 'input[type=hidden][name=type][value=?]', klass.name |
|
63 | 63 | end |
|
64 | 64 | end |
|
65 | 65 | end |
|
66 | 66 | end |
|
67 | 67 | |
|
68 | 68 | def test_new_should_have_string_default_format |
|
69 | 69 | get :new, :type => 'IssueCustomField' |
|
70 | 70 | assert_response :success |
|
71 | 71 | assert_equal 'string', assigns(:custom_field).format.name |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | def test_new_issue_custom_field |
|
75 | 75 | get :new, :type => 'IssueCustomField' |
|
76 | 76 | assert_response :success |
|
77 | 77 | assert_template 'new' |
|
78 | 78 | assert_select 'form#custom_field_form' do |
|
79 | 79 | assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do |
|
80 | 80 | assert_select 'option[value=user]', :text => 'User' |
|
81 | 81 | assert_select 'option[value=version]', :text => 'Version' |
|
82 | 82 | end |
|
83 | 83 | assert_select 'input[type=checkbox][name=?]', 'custom_field[project_ids][]', Project.count |
|
84 | 84 | assert_select 'input[type=hidden][name=?]', 'custom_field[project_ids][]', 1 |
|
85 | 85 | assert_select 'input[type=hidden][name=type][value=IssueCustomField]' |
|
86 | 86 | end |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | 89 | def test_new_time_entry_custom_field_should_not_show_trackers_and_projects |
|
90 | 90 | get :new, :type => 'TimeEntryCustomField' |
|
91 | 91 | assert_response :success |
|
92 | 92 | assert_template 'new' |
|
93 | 93 | assert_select 'form#custom_field_form' do |
|
94 | 94 | assert_select 'input[name=?]', 'custom_field[tracker_ids][]', 0 |
|
95 | 95 | assert_select 'input[name=?]', 'custom_field[project_ids][]', 0 |
|
96 | 96 | end |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | def test_default_value_should_be_an_input_for_string_custom_field |
|
100 | 100 | get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'string'} |
|
101 | 101 | assert_response :success |
|
102 | 102 | assert_select 'input[name=?]', 'custom_field[default_value]' |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def test_default_value_should_be_a_textarea_for_text_custom_field |
|
106 | 106 | get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'text'} |
|
107 | 107 | assert_response :success |
|
108 | 108 | assert_select 'textarea[name=?]', 'custom_field[default_value]' |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | def test_default_value_should_be_a_checkbox_for_bool_custom_field |
|
112 | 112 | get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'bool'} |
|
113 | 113 | assert_response :success |
|
114 | 114 | assert_select 'select[name=?]', 'custom_field[default_value]' do |
|
115 | 115 | assert_select 'option', 3 |
|
116 | 116 | end |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | def test_default_value_should_not_be_present_for_user_custom_field |
|
120 | 120 | get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'user'} |
|
121 | 121 | assert_response :success |
|
122 | 122 | assert_select '[name=?]', 'custom_field[default_value]', 0 |
|
123 | 123 | end |
|
124 | 124 | |
|
125 | 125 | def test_new_js |
|
126 | 126 | xhr :get, :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js' |
|
127 | 127 | assert_response :success |
|
128 | 128 | assert_template 'new' |
|
129 | 129 | assert_equal 'text/javascript', response.content_type |
|
130 | 130 | |
|
131 | 131 | field = assigns(:custom_field) |
|
132 | 132 | assert_equal 'list', field.field_format |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | def test_new_with_invalid_custom_field_class_should_render_select_type |
|
136 | 136 | get :new, :type => 'UnknownCustomField' |
|
137 | 137 | assert_response :success |
|
138 | 138 | assert_template 'select_type' |
|
139 | 139 | end |
|
140 | 140 | |
|
141 | 141 | def test_create_list_custom_field |
|
142 | assert_difference 'CustomField.count' do | |
|
142 | field = new_record(IssueCustomField) do | |
|
143 | 143 | post :create, :type => "IssueCustomField", |
|
144 | 144 | :custom_field => {:name => "test_post_new_list", |
|
145 | 145 | :default_value => "", |
|
146 | 146 | :min_length => "0", |
|
147 | 147 | :searchable => "0", |
|
148 | 148 | :regexp => "", |
|
149 | 149 | :is_for_all => "1", |
|
150 | 150 | :possible_values => "0.1\n0.2\n", |
|
151 | 151 | :max_length => "0", |
|
152 | 152 | :is_filter => "0", |
|
153 | 153 | :is_required =>"0", |
|
154 | 154 | :field_format => "list", |
|
155 | 155 | :tracker_ids => ["1", ""]} |
|
156 | 156 | end |
|
157 |
assert_redirected_to |
|
|
158 | field = IssueCustomField.find_by_name('test_post_new_list') | |
|
159 | assert_not_nil field | |
|
157 | assert_redirected_to "/custom_fields/#{field.id}/edit" | |
|
158 | assert_equal "test_post_new_list", field.name | |
|
160 | 159 | assert_equal ["0.1", "0.2"], field.possible_values |
|
161 | 160 | assert_equal 1, field.trackers.size |
|
162 | 161 | end |
|
163 | 162 | |
|
164 | 163 | def test_create_with_project_ids |
|
165 | 164 | assert_difference 'CustomField.count' do |
|
166 | 165 | post :create, :type => "IssueCustomField", :custom_field => { |
|
167 | 166 | :name => "foo", :field_format => "string", :is_for_all => "0", :project_ids => ["1", "3", ""] |
|
168 | 167 | } |
|
169 | 168 | assert_response 302 |
|
170 | 169 | end |
|
171 | 170 | field = IssueCustomField.order("id desc").first |
|
172 | 171 | assert_equal [1, 3], field.projects.map(&:id).sort |
|
173 | 172 | end |
|
174 | 173 | |
|
175 | 174 | def test_create_with_failure |
|
176 | 175 | assert_no_difference 'CustomField.count' do |
|
177 | 176 | post :create, :type => "IssueCustomField", :custom_field => {:name => ''} |
|
178 | 177 | end |
|
179 | 178 | assert_response :success |
|
180 | 179 | assert_template 'new' |
|
181 | 180 | end |
|
182 | 181 | |
|
183 | 182 | def test_create_without_type_should_render_select_type |
|
184 | 183 | assert_no_difference 'CustomField.count' do |
|
185 | 184 | post :create, :custom_field => {:name => ''} |
|
186 | 185 | end |
|
187 | 186 | assert_response :success |
|
188 | 187 | assert_template 'select_type' |
|
189 | 188 | end |
|
190 | 189 | |
|
191 | 190 | def test_edit |
|
192 | 191 | get :edit, :id => 1 |
|
193 | 192 | assert_response :success |
|
194 | 193 | assert_template 'edit' |
|
195 | 194 | assert_select 'input[name=?][value=?]', 'custom_field[name]', 'Database' |
|
196 | 195 | end |
|
197 | 196 | |
|
198 | 197 | def test_edit_invalid_custom_field_should_render_404 |
|
199 | 198 | get :edit, :id => 99 |
|
200 | 199 | assert_response 404 |
|
201 | 200 | end |
|
202 | 201 | |
|
203 | 202 | def test_update |
|
204 | 203 | put :update, :id => 1, :custom_field => {:name => 'New name'} |
|
205 |
assert_redirected_to '/custom_fields |
|
|
204 | assert_redirected_to '/custom_fields/1/edit' | |
|
206 | 205 | |
|
207 | 206 | field = CustomField.find(1) |
|
208 | 207 | assert_equal 'New name', field.name |
|
209 | 208 | end |
|
210 | 209 | |
|
211 | 210 | def test_update_with_failure |
|
212 | 211 | put :update, :id => 1, :custom_field => {:name => ''} |
|
213 | 212 | assert_response :success |
|
214 | 213 | assert_template 'edit' |
|
215 | 214 | end |
|
216 | 215 | |
|
217 | 216 | def test_destroy |
|
218 | 217 | custom_values_count = CustomValue.where(:custom_field_id => 1).count |
|
219 | 218 | assert custom_values_count > 0 |
|
220 | 219 | |
|
221 | 220 | assert_difference 'CustomField.count', -1 do |
|
222 | 221 | assert_difference 'CustomValue.count', - custom_values_count do |
|
223 | 222 | delete :destroy, :id => 1 |
|
224 | 223 | end |
|
225 | 224 | end |
|
226 | 225 | |
|
227 | 226 | assert_redirected_to '/custom_fields?tab=IssueCustomField' |
|
228 | 227 | assert_nil CustomField.find_by_id(1) |
|
229 | 228 | assert_nil CustomValue.find_by_custom_field_id(1) |
|
230 | 229 | end |
|
231 | 230 | |
|
232 | 231 | def custom_field_classes |
|
233 | 232 | files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') } |
|
234 | 233 | classes = files.map(&:classify).map(&:constantize) |
|
235 | 234 | assert classes.size > 0 |
|
236 | 235 | classes |
|
237 | 236 | end |
|
238 | 237 | end |
General Comments 0
You need to be logged in to leave comments.
Login now