"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/test/functional/enumerations_controller_test.rb" (6 Apr 2020, 6652 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "enumerations_controller_test.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    6 # This program is free software; you can redistribute it and/or
    7 # modify it under the terms of the GNU General Public License
    8 # as published by the Free Software Foundation; either version 2
    9 # of the License, or (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19 
   20 require File.expand_path('../../test_helper', __FILE__)
   21 
   22 class EnumerationsControllerTest < Redmine::ControllerTest
   23   fixtures :enumerations, :issues, :users
   24 
   25   def setup
   26     @request.session[:user_id] = 1 # admin
   27   end
   28 
   29   def test_index
   30     get :index
   31     assert_response :success
   32     assert_select 'table.enumerations'
   33   end
   34 
   35   def test_index_should_require_admin
   36     @request.session[:user_id] = nil
   37     get :index
   38     assert_response 302
   39   end
   40 
   41   def test_new
   42     get :new, :params => {
   43         :type => 'IssuePriority'
   44       }
   45     assert_response :success
   46 
   47     assert_select 'input[name=?][value=?]', 'enumeration[type]', 'IssuePriority'
   48     assert_select 'input[name=?]', 'enumeration[name]'
   49   end
   50 
   51   def test_new_with_invalid_type_should_respond_with_404
   52     get :new, :params => {
   53         :type => 'UnknownType'
   54       }
   55     assert_response 404
   56   end
   57 
   58   def test_create
   59     assert_difference 'IssuePriority.count' do
   60       post :create, :params => {
   61           :enumeration => {
   62             :type => 'IssuePriority',
   63             :name => 'Lowest'
   64           }
   65         }
   66     end
   67     assert_redirected_to '/enumerations'
   68     e = IssuePriority.find_by_name('Lowest')
   69     assert_not_nil e
   70   end
   71 
   72   def test_create_with_custom_field_values
   73     custom_field = TimeEntryActivityCustomField.generate!
   74     assert_difference 'TimeEntryActivity.count' do
   75       post :create, :params => {
   76           :enumeration => {
   77             :type => 'TimeEntryActivity',
   78             :name => 'Sample',
   79             :custom_field_values => {custom_field.id.to_s => "sample"}
   80           }
   81         }
   82     end
   83     assert_redirected_to '/enumerations'
   84     assert_equal "sample", Enumeration.find_by(:name => 'Sample').custom_field_values.last.value
   85   end
   86 
   87   def test_create_with_multiple_select_list_custom_fields
   88     custom_field = IssuePriorityCustomField.generate!(:field_format => 'list', :multiple => true, :possible_values => ['1', '2', '3', '4'])
   89     assert_difference 'IssuePriority.count' do
   90       post :create, :params => {
   91           :enumeration => {
   92             :type => 'IssuePriority',
   93             :name => 'Sample',
   94             :custom_field_values => {custom_field.id.to_s => ['1', '2']}
   95           }
   96         }
   97     end
   98     assert_redirected_to '/enumerations'
   99     assert_equal ['1', '2'].sort, Enumeration.find_by(:name => 'Sample').custom_field_values.last.value.sort
  100   end
  101 
  102   def test_create_with_failure
  103     assert_no_difference 'IssuePriority.count' do
  104       post :create, :params => {
  105           :enumeration => {
  106             :type => 'IssuePriority',
  107             :name => ''
  108           }
  109         }
  110     end
  111     assert_response :success
  112     assert_select_error /name cannot be blank/i
  113   end
  114 
  115   def test_edit
  116     get :edit, :params => {
  117         :id => 6
  118       }
  119     assert_response :success
  120     assert_select 'input[name=?][value=?]', 'enumeration[name]', 'High'
  121   end
  122 
  123   def test_edit_invalid_should_respond_with_404
  124     get :edit, :params => {
  125         :id => 999
  126       }
  127     assert_response 404
  128   end
  129 
  130   def test_update
  131     assert_no_difference 'IssuePriority.count' do
  132       put :update, :params => {
  133           :id => 6,
  134           :enumeration => {
  135             :type => 'IssuePriority',
  136             :name => 'New name'
  137           }
  138         }
  139     end
  140     assert_redirected_to '/enumerations'
  141     e = IssuePriority.find(6)
  142     assert_equal 'New name', e.name
  143   end
  144 
  145   def test_update_with_failure
  146     assert_no_difference 'IssuePriority.count' do
  147       put :update, :params => {
  148           :id => 6,
  149           :enumeration => {
  150             :type => 'IssuePriority',
  151             :name => ''
  152           }
  153         }
  154     end
  155     assert_response :success
  156     assert_select_error /name cannot be blank/i
  157   end
  158 
  159   def test_update_position
  160     assert_equal 2, Enumeration.find(2).position
  161     put :update, :params => {
  162           :id => 2,
  163           :enumeration => {
  164             :position => 1,
  165         }
  166       }
  167     assert_response 302
  168     assert_equal 1, Enumeration.find(2).position
  169   end
  170 
  171   def test_update_custom_field_values
  172     custom_field = TimeEntryActivityCustomField.generate!
  173     enumeration = Enumeration.find(9)
  174     assert_nil enumeration.custom_field_values.last.value
  175     put :update, :params => {
  176           :id => enumeration.id,
  177           :enumeration => {
  178             :custom_field_values => {custom_field.id.to_s => "sample"}
  179         }
  180       }
  181     assert_response 302
  182     assert_equal "sample", enumeration.reload.custom_field_values.last.value
  183   end
  184 
  185   def test_destroy_enumeration_not_in_use
  186     assert_difference 'IssuePriority.count', -1 do
  187       delete :destroy, :params => {
  188           :id => 7
  189         }
  190     end
  191     assert_redirected_to :controller => 'enumerations', :action => 'index'
  192     assert_nil Enumeration.find_by_id(7)
  193   end
  194 
  195   def test_destroy_enumeration_in_use
  196     assert_no_difference 'IssuePriority.count' do
  197       delete :destroy, :params => {
  198           :id => 4
  199         }
  200     end
  201     assert_response :success
  202 
  203     assert_not_nil Enumeration.find_by_id(4)
  204     assert_select 'select[name=reassign_to_id]' do
  205       assert_select 'option[value="6"]', :text => 'High'
  206     end
  207   end
  208 
  209   def test_destroy_enumeration_in_use_with_reassignment
  210     issue = Issue.where(:priority_id => 4).first
  211     assert_difference 'IssuePriority.count', -1 do
  212       delete :destroy, :params => {
  213           :id => 4,
  214           :reassign_to_id => 6
  215         }
  216     end
  217     assert_redirected_to :controller => 'enumerations', :action => 'index'
  218     assert_nil Enumeration.find_by_id(4)
  219     # check that the issue was reassign
  220     assert_equal 6, issue.reload.priority_id
  221   end
  222 
  223   def test_destroy_enumeration_in_use_with_blank_reassignment
  224     assert_no_difference 'IssuePriority.count' do
  225       delete :destroy, :params => {
  226           :id => 4,
  227           :reassign_to_id => ''
  228         }
  229     end
  230     assert_response :success
  231   end
  232 end