custom_fields_controller_test.rb
138 lines
| 4.6 KiB
| text/x-ruby
|
RubyLexer
|
r2781 | # Redmine - project management software | ||
|
r5152 | # Copyright (C) 2006-2011 Jean-Philippe Lang | ||
|
r2781 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r6699 | # | ||
|
r2781 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r6699 | # | ||
|
r2781 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
|
r4395 | require File.expand_path('../../test_helper', __FILE__) | ||
|
r2781 | require 'custom_fields_controller' | ||
# Re-raise errors caught by the controller. | ||||
class CustomFieldsController; def rescue_action(e) raise e end; end | ||||
class CustomFieldsControllerTest < ActionController::TestCase | ||||
|
r7840 | fixtures :custom_fields, :custom_values, :trackers, :users | ||
|
r6699 | |||
|
r2781 | def setup | ||
@controller = CustomFieldsController.new | ||||
@request = ActionController::TestRequest.new | ||||
@response = ActionController::TestResponse.new | ||||
@request.session[:user_id] = 1 | ||||
end | ||||
|
r6699 | |||
|
r7840 | def test_index | ||
get :index | ||||
assert_response :success | ||||
assert_template 'index' | ||||
end | ||||
|
r8024 | def test_new_issue_custom_field | ||
|
r5152 | get :new, :type => 'IssueCustomField' | ||
assert_response :success | ||||
assert_template 'new' | ||||
|
r8024 | assert_tag :input, :attributes => {:name => 'custom_field[name]'} | ||
|
r5152 | assert_tag :select, | ||
:attributes => {:name => 'custom_field[field_format]'}, | ||||
:child => { | ||||
:tag => 'option', | ||||
:attributes => {:value => 'user'}, | ||||
:content => 'User' | ||||
} | ||||
assert_tag :select, | ||||
:attributes => {:name => 'custom_field[field_format]'}, | ||||
:child => { | ||||
:tag => 'option', | ||||
:attributes => {:value => 'version'}, | ||||
:content => 'Version' | ||||
} | ||||
|
r8024 | assert_tag :input, :attributes => {:name => 'type', :value => 'IssueCustomField'} | ||
|
r5152 | end | ||
|
r6699 | |||
|
r8024 | def test_new_with_invalid_custom_field_class_should_render_404 | ||
|
r5152 | get :new, :type => 'UnknownCustomField' | ||
|
r8024 | assert_response 404 | ||
|
r5152 | end | ||
|
r6699 | |||
|
r8024 | def test_create_list_custom_field | ||
|
r2781 | assert_difference 'CustomField.count' do | ||
|
r8024 | post :create, :type => "IssueCustomField", | ||
|
r2781 | :custom_field => {:name => "test_post_new_list", | ||
:default_value => "", | ||||
:min_length => "0", | ||||
:searchable => "0", | ||||
:regexp => "", | ||||
:is_for_all => "1", | ||||
:possible_values => "0.1\n0.2\n", | ||||
:max_length => "0", | ||||
:is_filter => "0", | ||||
:is_required =>"0", | ||||
:field_format => "list", | ||||
:tracker_ids => ["1", ""]} | ||||
|
r6699 | end | ||
|
r2781 | assert_redirected_to '/custom_fields?tab=IssueCustomField' | ||
field = IssueCustomField.find_by_name('test_post_new_list') | ||||
assert_not_nil field | ||||
assert_equal ["0.1", "0.2"], field.possible_values | ||||
assert_equal 1, field.trackers.size | ||||
end | ||||
|
r7840 | |||
|
r8024 | def test_create_with_failure | ||
assert_no_difference 'CustomField.count' do | ||||
post :create, :type => "IssueCustomField", :custom_field => {:name => ''} | ||||
end | ||||
assert_response :success | ||||
assert_template 'new' | ||||
end | ||||
def test_edit | ||||
|
r7840 | get :edit, :id => 1 | ||
assert_response :success | ||||
assert_template 'edit' | ||||
assert_tag 'input', :attributes => {:name => 'custom_field[name]', :value => 'Database'} | ||||
end | ||||
|
r8024 | def test_edit_invalid_custom_field_should_render_404 | ||
get :edit, :id => 99 | ||||
assert_response 404 | ||||
end | ||||
def test_update | ||||
put :update, :id => 1, :custom_field => {:name => 'New name'} | ||||
|
r7840 | assert_redirected_to '/custom_fields?tab=IssueCustomField' | ||
field = CustomField.find(1) | ||||
assert_equal 'New name', field.name | ||||
end | ||||
|
r8024 | def test_update_with_failure | ||
put :update, :id => 1, :custom_field => {:name => ''} | ||||
assert_response :success | ||||
assert_template 'edit' | ||||
end | ||||
|
r7840 | def test_destroy | ||
custom_values_count = CustomValue.count(:conditions => {:custom_field_id => 1}) | ||||
assert custom_values_count > 0 | ||||
assert_difference 'CustomField.count', -1 do | ||||
assert_difference 'CustomValue.count', - custom_values_count do | ||||
|
r8024 | delete :destroy, :id => 1 | ||
|
r7840 | end | ||
end | ||||
assert_redirected_to '/custom_fields?tab=IssueCustomField' | ||||
assert_nil CustomField.find_by_id(1) | ||||
assert_nil CustomValue.find_by_custom_field_id(1) | ||||
end | ||||
|
r2781 | end | ||