You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

custom_fields_controller_test.rb 6.5 kB

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

软件技术创新是当今国家整体创新能力的重要基础和发展引擎。近年来,大众参与的软件创新与创业活动已经成为网络时代软件开发和应用的新形态,正在快速改变着全球软件创新模式和软件产业格局。 ...