annotate .svn/pristine/11/11eb00b887f31790fc6e46fc8c8f7f620e8e7372.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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