##// END OF EJS Templates
Additional tests for CustomFieldsController....
Jean-Philippe Lang -
r9303:6f71a508eb9e
parent child
Show More
@@ -1,138 +1,155
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
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'
19 require 'custom_fields_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class CustomFieldsController; def rescue_action(e) raise e end; end
22 class CustomFieldsController; def rescue_action(e) raise e end; end
23
23
24 class CustomFieldsControllerTest < ActionController::TestCase
24 class CustomFieldsControllerTest < ActionController::TestCase
25 fixtures :custom_fields, :custom_values, :trackers, :users
25 fixtures :custom_fields, :custom_values, :trackers, :users
26
26
27 def setup
27 def setup
28 @controller = CustomFieldsController.new
28 @controller = CustomFieldsController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 @request.session[:user_id] = 1
31 @request.session[:user_id] = 1
32 end
32 end
33
33
34 def test_index
34 def test_index
35 get :index
35 get :index
36 assert_response :success
36 assert_response :success
37 assert_template 'index'
37 assert_template 'index'
38 end
38 end
39
39
40 def test_new
41 custom_field_classes.each do |klass|
42 get :new, :type => klass.name
43 assert_response :success
44 assert_template 'new'
45 assert_kind_of klass, assigns(:custom_field)
46 assert_tag :select, :attributes => {:name => 'custom_field[field_format]'}
47 end
48 end
49
40 def test_new_issue_custom_field
50 def test_new_issue_custom_field
41 get :new, :type => 'IssueCustomField'
51 get :new, :type => 'IssueCustomField'
42 assert_response :success
52 assert_response :success
43 assert_template 'new'
53 assert_template 'new'
44 assert_tag :input, :attributes => {:name => 'custom_field[name]'}
54 assert_tag :input, :attributes => {:name => 'custom_field[name]'}
45 assert_tag :select,
55 assert_tag :select,
46 :attributes => {:name => 'custom_field[field_format]'},
56 :attributes => {:name => 'custom_field[field_format]'},
47 :child => {
57 :child => {
48 :tag => 'option',
58 :tag => 'option',
49 :attributes => {:value => 'user'},
59 :attributes => {:value => 'user'},
50 :content => 'User'
60 :content => 'User'
51 }
61 }
52 assert_tag :select,
62 assert_tag :select,
53 :attributes => {:name => 'custom_field[field_format]'},
63 :attributes => {:name => 'custom_field[field_format]'},
54 :child => {
64 :child => {
55 :tag => 'option',
65 :tag => 'option',
56 :attributes => {:value => 'version'},
66 :attributes => {:value => 'version'},
57 :content => 'Version'
67 :content => 'Version'
58 }
68 }
59 assert_tag :input, :attributes => {:name => 'type', :value => 'IssueCustomField'}
69 assert_tag :input, :attributes => {:name => 'type', :value => 'IssueCustomField'}
60 end
70 end
61
71
62 def test_new_with_invalid_custom_field_class_should_render_404
72 def test_new_with_invalid_custom_field_class_should_render_404
63 get :new, :type => 'UnknownCustomField'
73 get :new, :type => 'UnknownCustomField'
64 assert_response 404
74 assert_response 404
65 end
75 end
66
76
67 def test_create_list_custom_field
77 def test_create_list_custom_field
68 assert_difference 'CustomField.count' do
78 assert_difference 'CustomField.count' do
69 post :create, :type => "IssueCustomField",
79 post :create, :type => "IssueCustomField",
70 :custom_field => {:name => "test_post_new_list",
80 :custom_field => {:name => "test_post_new_list",
71 :default_value => "",
81 :default_value => "",
72 :min_length => "0",
82 :min_length => "0",
73 :searchable => "0",
83 :searchable => "0",
74 :regexp => "",
84 :regexp => "",
75 :is_for_all => "1",
85 :is_for_all => "1",
76 :possible_values => "0.1\n0.2\n",
86 :possible_values => "0.1\n0.2\n",
77 :max_length => "0",
87 :max_length => "0",
78 :is_filter => "0",
88 :is_filter => "0",
79 :is_required =>"0",
89 :is_required =>"0",
80 :field_format => "list",
90 :field_format => "list",
81 :tracker_ids => ["1", ""]}
91 :tracker_ids => ["1", ""]}
82 end
92 end
83 assert_redirected_to '/custom_fields?tab=IssueCustomField'
93 assert_redirected_to '/custom_fields?tab=IssueCustomField'
84 field = IssueCustomField.find_by_name('test_post_new_list')
94 field = IssueCustomField.find_by_name('test_post_new_list')
85 assert_not_nil field
95 assert_not_nil field
86 assert_equal ["0.1", "0.2"], field.possible_values
96 assert_equal ["0.1", "0.2"], field.possible_values
87 assert_equal 1, field.trackers.size
97 assert_equal 1, field.trackers.size
88 end
98 end
89
99
90 def test_create_with_failure
100 def test_create_with_failure
91 assert_no_difference 'CustomField.count' do
101 assert_no_difference 'CustomField.count' do
92 post :create, :type => "IssueCustomField", :custom_field => {:name => ''}
102 post :create, :type => "IssueCustomField", :custom_field => {:name => ''}
93 end
103 end
94 assert_response :success
104 assert_response :success
95 assert_template 'new'
105 assert_template 'new'
96 end
106 end
97
107
98 def test_edit
108 def test_edit
99 get :edit, :id => 1
109 get :edit, :id => 1
100 assert_response :success
110 assert_response :success
101 assert_template 'edit'
111 assert_template 'edit'
102 assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'}
112 assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'}
103 end
113 end
104
114
105 def test_edit_invalid_custom_field_should_render_404
115 def test_edit_invalid_custom_field_should_render_404
106 get :edit, :id => 99
116 get :edit, :id => 99
107 assert_response 404
117 assert_response 404
108 end
118 end
109
119
110 def test_update
120 def test_update
111 put :update, :id => 1, :custom_field => {:name => 'New name'}
121 put :update, :id => 1, :custom_field => {:name => 'New name'}
112 assert_redirected_to '/custom_fields?tab=IssueCustomField'
122 assert_redirected_to '/custom_fields?tab=IssueCustomField'
113
123
114 field = CustomField.find(1)
124 field = CustomField.find(1)
115 assert_equal 'New name', field.name
125 assert_equal 'New name', field.name
116 end
126 end
117
127
118 def test_update_with_failure
128 def test_update_with_failure
119 put :update, :id => 1, :custom_field => {:name => ''}
129 put :update, :id => 1, :custom_field => {:name => ''}
120 assert_response :success
130 assert_response :success
121 assert_template 'edit'
131 assert_template 'edit'
122 end
132 end
123
133
124 def test_destroy
134 def test_destroy
125 custom_values_count = CustomValue.count(:conditions => {:custom_field_id => 1})
135 custom_values_count = CustomValue.count(:conditions => {:custom_field_id => 1})
126 assert custom_values_count > 0
136 assert custom_values_count > 0
127
137
128 assert_difference 'CustomField.count', -1 do
138 assert_difference 'CustomField.count', -1 do
129 assert_difference 'CustomValue.count', - custom_values_count do
139 assert_difference 'CustomValue.count', - custom_values_count do
130 delete :destroy, :id => 1
140 delete :destroy, :id => 1
131 end
141 end
132 end
142 end
133
143
134 assert_redirected_to '/custom_fields?tab=IssueCustomField'
144 assert_redirected_to '/custom_fields?tab=IssueCustomField'
135 assert_nil CustomField.find_by_id(1)
145 assert_nil CustomField.find_by_id(1)
136 assert_nil CustomValue.find_by_custom_field_id(1)
146 assert_nil CustomValue.find_by_custom_field_id(1)
137 end
147 end
148
149 def custom_field_classes
150 files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') }
151 classes = files.map(&:classify).map(&:constantize)
152 assert classes.size > 0
153 classes
154 end
138 end
155 end
General Comments 0
You need to be logged in to leave comments. Login now