"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/test/unit/time_entry_import_test.rb" (6 Apr 2020, 6312 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 "time_entry_import_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 TimeEntryImportTest < ActiveSupport::TestCase
   23   fixtures :projects, :enabled_modules,
   24            :users, :email_addresses,
   25            :roles, :members, :member_roles,
   26            :issues, :issue_statuses,
   27            :trackers, :projects_trackers,
   28            :versions,
   29            :issue_categories,
   30            :enumerations,
   31            :workflows,
   32            :custom_fields,
   33            :custom_values
   34 
   35   include Redmine::I18n
   36 
   37   def setup
   38     set_language_if_valid 'en'
   39     User.current = nil
   40   end
   41 
   42   def test_authorized
   43     assert  TimeEntryImport.authorized?(User.find(1)) # admins
   44     assert  TimeEntryImport.authorized?(User.find(2)) # has log_time permission
   45     assert !TimeEntryImport.authorized?(User.find(6)) # anonymous does not have log_time permission
   46   end
   47 
   48   def test_maps_issue_id
   49     import = generate_import_with_mapping
   50     first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
   51 
   52     assert_nil first.issue_id
   53     assert_nil second.issue_id
   54     assert_equal 1, third.issue_id
   55     assert_equal 2, fourth.issue_id
   56   end
   57 
   58   def test_maps_date
   59     import = generate_import_with_mapping
   60     first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
   61 
   62     assert_equal Date.new(2020, 1, 1), first.spent_on
   63     assert_equal Date.new(2020, 1, 2), second.spent_on
   64     assert_equal Date.new(2020, 1, 3), third.spent_on
   65     assert_equal Date.new(2020, 1, 4), fourth.spent_on
   66   end
   67 
   68   def test_maps_hours
   69     import = generate_import_with_mapping
   70     first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
   71 
   72     assert_equal 1, first.hours
   73     assert_equal 2, second.hours
   74     assert_equal 3, third.hours
   75     assert_equal 4, fourth.hours
   76   end
   77 
   78   def test_maps_comments
   79     import = generate_import_with_mapping
   80     first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
   81 
   82     assert_equal 'Some Design',      first.comments
   83     assert_equal 'Some Development', second.comments
   84     assert_equal 'Some QA',          third.comments
   85     assert_equal 'Some Inactivity',  fourth.comments
   86   end
   87 
   88   def test_maps_activity_to_column_value
   89     import = generate_import_with_mapping
   90     import.mapping.merge!('activity' => '5')
   91     import.save!
   92 
   93     # N.B. last row is not imported due to the usage of a disabled activity
   94     first, second, third = new_records(TimeEntry, 3) { import.run }
   95 
   96     assert_equal 9,  first.activity_id
   97     assert_equal 10, second.activity_id
   98     assert_equal 11, third.activity_id
   99 
  100     last = import.items.last
  101     assert_equal 'Activity cannot be blank', last.message
  102     assert_nil last.obj_id
  103   end
  104 
  105   def test_maps_activity_to_fixed_value
  106     import = generate_import_with_mapping
  107     first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  108 
  109     assert_equal 10, first.activity_id
  110     assert_equal 10, second.activity_id
  111     assert_equal 10, third.activity_id
  112     assert_equal 10, fourth.activity_id
  113   end
  114 
  115   def test_maps_custom_fields
  116     overtime_cf = CustomField.find(10)
  117 
  118     import = generate_import_with_mapping
  119     import.mapping.merge!('cf_10' => '6')
  120     import.save!
  121     first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  122 
  123     assert_equal '1', first.custom_field_value(overtime_cf)
  124     assert_equal '1', second.custom_field_value(overtime_cf)
  125     assert_equal '0', third.custom_field_value(overtime_cf)
  126     assert_equal '0', fourth.custom_field_value(overtime_cf)
  127   end
  128 
  129   def test_maps_user_id_for_user_with_permissions
  130     Role.find_by_name('Manager').add_permission! :log_time_for_other_users
  131 
  132     import = generate_import_with_mapping
  133     first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  134 
  135     assert_equal 2, first.user_id
  136     assert_equal 2, second.user_id
  137     assert_equal 3, third.user_id
  138     assert_equal 2, fourth.user_id
  139   end
  140 
  141   def test_maps_user_to_column_value
  142     Role.find_by_name('Manager').add_permission! :log_time_for_other_users
  143 
  144     import = generate_import_with_mapping
  145     import.mapping.merge!('user_id' => 'value:3')
  146     import.save!
  147     first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  148 
  149     assert_equal 3, first.user_id
  150     assert_equal 3, second.user_id
  151     assert_equal 3, third.user_id
  152     assert_equal 3, fourth.user_id
  153   end
  154 
  155   def test_maps_user_id_for_user_without_permissions
  156     # User 2 doesn't have log_time_for_other_users permission
  157     User.current = User.find(2)
  158     import = generate_import_with_mapping
  159     first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  160 
  161     assert_equal 2, first.user_id
  162     assert_equal 2, second.user_id
  163     # user_id value from CSV should be ignored
  164     assert_equal 2, third.user_id
  165     assert_equal 2, fourth.user_id
  166   end
  167 
  168   protected
  169 
  170   def generate_import(fixture_name='import_time_entries.csv')
  171     import = TimeEntryImport.new
  172     import.user_id = 2
  173     import.file = uploaded_test_file(fixture_name, 'text/csv')
  174     import.save!
  175     import
  176   end
  177 
  178   def generate_import_with_mapping(fixture_name='import_time_entries.csv')
  179     import = generate_import(fixture_name)
  180 
  181     import.settings = {
  182       'separator' => ';', 'wrapper' => '"', 'encoding' => 'UTF-8',
  183       'mapping' => {
  184         'project_id' => '1',
  185         'activity'   => 'value:10',
  186         'issue_id'   => '1',
  187         'spent_on'   => '2',
  188         'hours'      => '3',
  189         'comments'   => '4',
  190         'user_id'    => '7'
  191       }
  192     }
  193     import.save!
  194     import
  195   end
  196 end