comparison .svn/pristine/7a/7a074cee45b6ea26f101ec0ee24551820d4e929e.svn-base @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents
children
comparison
equal deleted inserted replaced
1294:3e4c3460b6ca 1295:622f24f53b42
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 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
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
34 custom_field_classes.each do |klass|
35 get :new, :type => klass.name
36 assert_response :success
37 assert_template 'new'
38 assert_kind_of klass, assigns(:custom_field)
39 assert_select 'form#custom_field_form' do
40 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]'
41 assert_select 'input[type=hidden][name=type][value=?]', klass.name
42 end
43 end
44 end
45
46 def test_new_issue_custom_field
47 get :new, :type => 'IssueCustomField'
48 assert_response :success
49 assert_template 'new'
50 assert_select 'form#custom_field_form' do
51 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do
52 assert_select 'option[value=user]', :text => 'User'
53 assert_select 'option[value=version]', :text => 'Version'
54 end
55 assert_select 'input[type=hidden][name=type][value=IssueCustomField]'
56 end
57 end
58
59 def test_default_value_should_be_an_input_for_string_custom_field
60 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'string'}
61 assert_response :success
62 assert_select 'input[name=?]', 'custom_field[default_value]'
63 end
64
65 def test_default_value_should_be_a_textarea_for_text_custom_field
66 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'text'}
67 assert_response :success
68 assert_select 'textarea[name=?]', 'custom_field[default_value]'
69 end
70
71 def test_default_value_should_be_a_checkbox_for_bool_custom_field
72 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'bool'}
73 assert_response :success
74 assert_select 'input[name=?][type=checkbox]', 'custom_field[default_value]'
75 end
76
77 def test_default_value_should_not_be_present_for_user_custom_field
78 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'user'}
79 assert_response :success
80 assert_select '[name=?]', 'custom_field[default_value]', 0
81 end
82
83 def test_new_js
84 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js'
85 assert_response :success
86 assert_template 'new'
87 assert_equal 'text/javascript', response.content_type
88
89 field = assigns(:custom_field)
90 assert_equal 'list', field.field_format
91 end
92
93 def test_new_with_invalid_custom_field_class_should_render_404
94 get :new, :type => 'UnknownCustomField'
95 assert_response 404
96 end
97
98 def test_create_list_custom_field
99 assert_difference 'CustomField.count' do
100 post :create, :type => "IssueCustomField",
101 :custom_field => {:name => "test_post_new_list",
102 :default_value => "",
103 :min_length => "0",
104 :searchable => "0",
105 :regexp => "",
106 :is_for_all => "1",
107 :possible_values => "0.1\n0.2\n",
108 :max_length => "0",
109 :is_filter => "0",
110 :is_required =>"0",
111 :field_format => "list",
112 :tracker_ids => ["1", ""]}
113 end
114 assert_redirected_to '/custom_fields?tab=IssueCustomField'
115 field = IssueCustomField.find_by_name('test_post_new_list')
116 assert_not_nil field
117 assert_equal ["0.1", "0.2"], field.possible_values
118 assert_equal 1, field.trackers.size
119 end
120
121 def test_create_with_failure
122 assert_no_difference 'CustomField.count' do
123 post :create, :type => "IssueCustomField", :custom_field => {:name => ''}
124 end
125 assert_response :success
126 assert_template 'new'
127 end
128
129 def test_edit
130 get :edit, :id => 1
131 assert_response :success
132 assert_template 'edit'
133 assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'}
134 end
135
136 def test_edit_invalid_custom_field_should_render_404
137 get :edit, :id => 99
138 assert_response 404
139 end
140
141 def test_update
142 put :update, :id => 1, :custom_field => {:name => 'New name'}
143 assert_redirected_to '/custom_fields?tab=IssueCustomField'
144
145 field = CustomField.find(1)
146 assert_equal 'New name', field.name
147 end
148
149 def test_update_with_failure
150 put :update, :id => 1, :custom_field => {:name => ''}
151 assert_response :success
152 assert_template 'edit'
153 end
154
155 def test_destroy
156 custom_values_count = CustomValue.count(:conditions => {:custom_field_id => 1})
157 assert custom_values_count > 0
158
159 assert_difference 'CustomField.count', -1 do
160 assert_difference 'CustomValue.count', - custom_values_count do
161 delete :destroy, :id => 1
162 end
163 end
164
165 assert_redirected_to '/custom_fields?tab=IssueCustomField'
166 assert_nil CustomField.find_by_id(1)
167 assert_nil CustomValue.find_by_custom_field_id(1)
168 end
169
170 def custom_field_classes
171 files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') }
172 classes = files.map(&:classify).map(&:constantize)
173 assert classes.size > 0
174 classes
175 end
176 end