@@ -1,138 +1,155 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | require 'custom_fields_controller' |
|
20 | 20 | |
|
21 | 21 | # Re-raise errors caught by the controller. |
|
22 | 22 | class CustomFieldsController; def rescue_action(e) raise e end; end |
|
23 | 23 | |
|
24 | 24 | class CustomFieldsControllerTest < ActionController::TestCase |
|
25 | 25 | fixtures :custom_fields, :custom_values, :trackers, :users |
|
26 | 26 | |
|
27 | 27 | def setup |
|
28 | 28 | @controller = CustomFieldsController.new |
|
29 | 29 | @request = ActionController::TestRequest.new |
|
30 | 30 | @response = ActionController::TestResponse.new |
|
31 | 31 | @request.session[:user_id] = 1 |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_index |
|
35 | 35 | get :index |
|
36 | 36 | assert_response :success |
|
37 | 37 | assert_template 'index' |
|
38 | 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 | 50 | def test_new_issue_custom_field |
|
41 | 51 | get :new, :type => 'IssueCustomField' |
|
42 | 52 | assert_response :success |
|
43 | 53 | assert_template 'new' |
|
44 | 54 | assert_tag :input, :attributes => {:name => 'custom_field[name]'} |
|
45 | 55 | assert_tag :select, |
|
46 | 56 | :attributes => {:name => 'custom_field[field_format]'}, |
|
47 | 57 | :child => { |
|
48 | 58 | :tag => 'option', |
|
49 | 59 | :attributes => {:value => 'user'}, |
|
50 | 60 | :content => 'User' |
|
51 | 61 | } |
|
52 | 62 | assert_tag :select, |
|
53 | 63 | :attributes => {:name => 'custom_field[field_format]'}, |
|
54 | 64 | :child => { |
|
55 | 65 | :tag => 'option', |
|
56 | 66 | :attributes => {:value => 'version'}, |
|
57 | 67 | :content => 'Version' |
|
58 | 68 | } |
|
59 | 69 | assert_tag :input, :attributes => {:name => 'type', :value => 'IssueCustomField'} |
|
60 | 70 | end |
|
61 | 71 | |
|
62 | 72 | def test_new_with_invalid_custom_field_class_should_render_404 |
|
63 | 73 | get :new, :type => 'UnknownCustomField' |
|
64 | 74 | assert_response 404 |
|
65 | 75 | end |
|
66 | 76 | |
|
67 | 77 | def test_create_list_custom_field |
|
68 | 78 | assert_difference 'CustomField.count' do |
|
69 | 79 | post :create, :type => "IssueCustomField", |
|
70 | 80 | :custom_field => {:name => "test_post_new_list", |
|
71 | 81 | :default_value => "", |
|
72 | 82 | :min_length => "0", |
|
73 | 83 | :searchable => "0", |
|
74 | 84 | :regexp => "", |
|
75 | 85 | :is_for_all => "1", |
|
76 | 86 | :possible_values => "0.1\n0.2\n", |
|
77 | 87 | :max_length => "0", |
|
78 | 88 | :is_filter => "0", |
|
79 | 89 | :is_required =>"0", |
|
80 | 90 | :field_format => "list", |
|
81 | 91 | :tracker_ids => ["1", ""]} |
|
82 | 92 | end |
|
83 | 93 | assert_redirected_to '/custom_fields?tab=IssueCustomField' |
|
84 | 94 | field = IssueCustomField.find_by_name('test_post_new_list') |
|
85 | 95 | assert_not_nil field |
|
86 | 96 | assert_equal ["0.1", "0.2"], field.possible_values |
|
87 | 97 | assert_equal 1, field.trackers.size |
|
88 | 98 | end |
|
89 | 99 | |
|
90 | 100 | def test_create_with_failure |
|
91 | 101 | assert_no_difference 'CustomField.count' do |
|
92 | 102 | post :create, :type => "IssueCustomField", :custom_field => {:name => ''} |
|
93 | 103 | end |
|
94 | 104 | assert_response :success |
|
95 | 105 | assert_template 'new' |
|
96 | 106 | end |
|
97 | 107 | |
|
98 | 108 | def test_edit |
|
99 | 109 | get :edit, :id => 1 |
|
100 | 110 | assert_response :success |
|
101 | 111 | assert_template 'edit' |
|
102 | 112 | assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'} |
|
103 | 113 | end |
|
104 | 114 | |
|
105 | 115 | def test_edit_invalid_custom_field_should_render_404 |
|
106 | 116 | get :edit, :id => 99 |
|
107 | 117 | assert_response 404 |
|
108 | 118 | end |
|
109 | 119 | |
|
110 | 120 | def test_update |
|
111 | 121 | put :update, :id => 1, :custom_field => {:name => 'New name'} |
|
112 | 122 | assert_redirected_to '/custom_fields?tab=IssueCustomField' |
|
113 | 123 | |
|
114 | 124 | field = CustomField.find(1) |
|
115 | 125 | assert_equal 'New name', field.name |
|
116 | 126 | end |
|
117 | 127 | |
|
118 | 128 | def test_update_with_failure |
|
119 | 129 | put :update, :id => 1, :custom_field => {:name => ''} |
|
120 | 130 | assert_response :success |
|
121 | 131 | assert_template 'edit' |
|
122 | 132 | end |
|
123 | 133 | |
|
124 | 134 | def test_destroy |
|
125 | 135 | custom_values_count = CustomValue.count(:conditions => {:custom_field_id => 1}) |
|
126 | 136 | assert custom_values_count > 0 |
|
127 | 137 | |
|
128 | 138 | assert_difference 'CustomField.count', -1 do |
|
129 | 139 | assert_difference 'CustomValue.count', - custom_values_count do |
|
130 | 140 | delete :destroy, :id => 1 |
|
131 | 141 | end |
|
132 | 142 | end |
|
133 | 143 | |
|
134 | 144 | assert_redirected_to '/custom_fields?tab=IssueCustomField' |
|
135 | 145 | assert_nil CustomField.find_by_id(1) |
|
136 | 146 | assert_nil CustomValue.find_by_custom_field_id(1) |
|
137 | 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 | 155 | end |
General Comments 0
You need to be logged in to leave comments.
Login now