annotate test/functional/custom_fields_controller_test.rb @ 1600:ed9c467ef922 dockerise

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