Mercurial > hg > soundsoftware-site
comparison test/functional/custom_fields_controller_test.rb @ 1115:433d4f72a19b redmine-2.2
Update to Redmine SVN revision 11137 on 2.2-stable branch
author | Chris Cannam |
---|---|
date | Mon, 07 Jan 2013 12:01:42 +0000 |
parents | cbb26bc654de |
children | 622f24f53b42 261b3d9a4903 |
comparison
equal
deleted
inserted
replaced
929:5f33065ddc4b | 1115:433d4f72a19b |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2011 Jean-Philippe Lang | 2 # Copyright (C) 2006-2012 Jean-Philippe Lang |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or | 4 # This program is free software; you can redistribute it and/or |
5 # modify it under the terms of the GNU General Public License | 5 # modify it under the terms of the GNU General Public License |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
14 # You should have received a copy of the GNU General Public License | 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 | 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. | 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | 17 |
18 require File.expand_path('../../test_helper', __FILE__) | 18 require File.expand_path('../../test_helper', __FILE__) |
19 require 'custom_fields_controller' | |
20 | |
21 # Re-raise errors caught by the controller. | |
22 class CustomFieldsController; def rescue_action(e) raise e end; end | |
23 | 19 |
24 class CustomFieldsControllerTest < ActionController::TestCase | 20 class CustomFieldsControllerTest < ActionController::TestCase |
25 fixtures :custom_fields, :custom_values, :trackers, :users | 21 fixtures :custom_fields, :custom_values, :trackers, :users |
26 | 22 |
27 def setup | 23 def setup |
28 @controller = CustomFieldsController.new | |
29 @request = ActionController::TestRequest.new | |
30 @response = ActionController::TestResponse.new | |
31 @request.session[:user_id] = 1 | 24 @request.session[:user_id] = 1 |
32 end | 25 end |
33 | 26 |
34 def test_index | 27 def test_index |
35 get :index | 28 get :index |
36 assert_response :success | 29 assert_response :success |
37 assert_template 'index' | 30 assert_template 'index' |
38 end | 31 end |
39 | 32 |
40 def test_get_new_issue_custom_field | 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 | |
41 get :new, :type => 'IssueCustomField' | 47 get :new, :type => 'IssueCustomField' |
42 assert_response :success | 48 assert_response :success |
43 assert_template 'new' | 49 assert_template 'new' |
44 assert_tag :select, | 50 assert_select 'form#custom_field_form' do |
45 :attributes => {:name => 'custom_field[field_format]'}, | 51 assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do |
46 :child => { | 52 assert_select 'option[value=user]', :text => 'User' |
47 :tag => 'option', | 53 assert_select 'option[value=version]', :text => 'Version' |
48 :attributes => {:value => 'user'}, | 54 end |
49 :content => 'User' | 55 assert_select 'input[type=hidden][name=type][value=IssueCustomField]' |
50 } | 56 end |
51 assert_tag :select, | |
52 :attributes => {:name => 'custom_field[field_format]'}, | |
53 :child => { | |
54 :tag => 'option', | |
55 :attributes => {:value => 'version'}, | |
56 :content => 'Version' | |
57 } | |
58 end | 57 end |
59 | 58 |
60 def test_get_new_with_invalid_custom_field_class_should_redirect_to_list | 59 def test_new_js |
61 get :new, :type => 'UnknownCustomField' | 60 get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js' |
62 assert_redirected_to '/custom_fields' | 61 assert_response :success |
62 assert_template 'new' | |
63 assert_equal 'text/javascript', response.content_type | |
64 | |
65 field = assigns(:custom_field) | |
66 assert_equal 'list', field.field_format | |
63 end | 67 end |
64 | 68 |
65 def test_post_new_list_custom_field | 69 def test_new_with_invalid_custom_field_class_should_render_404 |
70 get :new, :type => 'UnknownCustomField' | |
71 assert_response 404 | |
72 end | |
73 | |
74 def test_create_list_custom_field | |
66 assert_difference 'CustomField.count' do | 75 assert_difference 'CustomField.count' do |
67 post :new, :type => "IssueCustomField", | 76 post :create, :type => "IssueCustomField", |
68 :custom_field => {:name => "test_post_new_list", | 77 :custom_field => {:name => "test_post_new_list", |
69 :default_value => "", | 78 :default_value => "", |
70 :min_length => "0", | 79 :min_length => "0", |
71 :searchable => "0", | 80 :searchable => "0", |
72 :regexp => "", | 81 :regexp => "", |
83 assert_not_nil field | 92 assert_not_nil field |
84 assert_equal ["0.1", "0.2"], field.possible_values | 93 assert_equal ["0.1", "0.2"], field.possible_values |
85 assert_equal 1, field.trackers.size | 94 assert_equal 1, field.trackers.size |
86 end | 95 end |
87 | 96 |
88 def test_get_edit | 97 def test_create_with_failure |
98 assert_no_difference 'CustomField.count' do | |
99 post :create, :type => "IssueCustomField", :custom_field => {:name => ''} | |
100 end | |
101 assert_response :success | |
102 assert_template 'new' | |
103 end | |
104 | |
105 def test_edit | |
89 get :edit, :id => 1 | 106 get :edit, :id => 1 |
90 assert_response :success | 107 assert_response :success |
91 assert_template 'edit' | 108 assert_template 'edit' |
92 assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'} | 109 assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'} |
93 end | 110 end |
94 | 111 |
95 def test_post_edit | 112 def test_edit_invalid_custom_field_should_render_404 |
96 post :edit, :id => 1, :custom_field => {:name => 'New name'} | 113 get :edit, :id => 99 |
114 assert_response 404 | |
115 end | |
116 | |
117 def test_update | |
118 put :update, :id => 1, :custom_field => {:name => 'New name'} | |
97 assert_redirected_to '/custom_fields?tab=IssueCustomField' | 119 assert_redirected_to '/custom_fields?tab=IssueCustomField' |
98 | 120 |
99 field = CustomField.find(1) | 121 field = CustomField.find(1) |
100 assert_equal 'New name', field.name | 122 assert_equal 'New name', field.name |
123 end | |
124 | |
125 def test_update_with_failure | |
126 put :update, :id => 1, :custom_field => {:name => ''} | |
127 assert_response :success | |
128 assert_template 'edit' | |
101 end | 129 end |
102 | 130 |
103 def test_destroy | 131 def test_destroy |
104 custom_values_count = CustomValue.count(:conditions => {:custom_field_id => 1}) | 132 custom_values_count = CustomValue.count(:conditions => {:custom_field_id => 1}) |
105 assert custom_values_count > 0 | 133 assert custom_values_count > 0 |
106 | 134 |
107 assert_difference 'CustomField.count', -1 do | 135 assert_difference 'CustomField.count', -1 do |
108 assert_difference 'CustomValue.count', - custom_values_count do | 136 assert_difference 'CustomValue.count', - custom_values_count do |
109 post :destroy, :id => 1 | 137 delete :destroy, :id => 1 |
110 end | 138 end |
111 end | 139 end |
112 | 140 |
113 assert_redirected_to '/custom_fields?tab=IssueCustomField' | 141 assert_redirected_to '/custom_fields?tab=IssueCustomField' |
114 assert_nil CustomField.find_by_id(1) | 142 assert_nil CustomField.find_by_id(1) |
115 assert_nil CustomValue.find_by_custom_field_id(1) | 143 assert_nil CustomValue.find_by_custom_field_id(1) |
116 end | 144 end |
145 | |
146 def custom_field_classes | |
147 files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') } | |
148 classes = files.map(&:classify).map(&:constantize) | |
149 assert classes.size > 0 | |
150 classes | |
151 end | |
117 end | 152 end |