"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/test/functional/timelog_report_test.rb" (6 Apr 2020, 14995 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 "timelog_report_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 TimelogReportTest < Redmine::ControllerTest
   23   tests TimelogController
   24 
   25   fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
   26            :email_addresses,
   27            :issues, :time_entries, :users, :trackers, :enumerations,
   28            :issue_statuses, :custom_fields, :custom_values,
   29            :projects_trackers, :custom_fields_trackers,
   30            :custom_fields_projects
   31 
   32   include Redmine::I18n
   33 
   34   def setup
   35     Setting.default_language = "en"
   36   end
   37 
   38   def test_report_at_project_level
   39     get :report, :params => {:project_id => 'ecookbook'}
   40     assert_response :success
   41 
   42     # query form
   43     assert_select 'form#query_form' do
   44       assert_select 'div#query_form_with_buttons.hide-when-print' do
   45         assert_select 'div#query_form_content' do
   46           assert_select 'fieldset#filters.collapsible'
   47           assert_select 'fieldset#options'
   48         end
   49         assert_select 'p.buttons'
   50       end
   51     end
   52 
   53     assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries/report'
   54   end
   55 
   56   def test_report_all_projects
   57     get :report
   58     assert_response :success
   59     assert_select 'form#query_form[action=?]', '/time_entries/report'
   60   end
   61 
   62   def test_report_all_projects_denied
   63     r = Role.anonymous
   64     r.permissions.delete(:view_time_entries)
   65     r.permissions_will_change!
   66     r.save
   67     get :report
   68     assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
   69   end
   70 
   71   def test_report_all_projects_one_criteria
   72     get :report, :params => {:columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']}
   73     assert_response :success
   74     assert_select 'tr.total td:last', :text => '8.65'
   75     assert_select 'tr td.name a[href=?]', '/projects/ecookbook', :text => 'eCookbook'
   76   end
   77 
   78   def test_report_all_time
   79     get :report, :params => {:project_id => 1, :criteria => ['project', 'issue']}
   80     assert_response :success
   81     assert_select 'tr.total td:last', :text => '162.90'
   82   end
   83 
   84   def test_report_all_time_by_day
   85     get :report, :params => {:project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'}
   86     assert_response :success
   87     assert_select 'tr.total td:last', :text => '162.90'
   88     assert_select 'th', :text => '2007-03-12'
   89   end
   90 
   91   def test_report_one_criteria
   92     get :report, :params => {:project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']}
   93     assert_response :success
   94     assert_select 'tr.total td:last', :text => '8.65'
   95   end
   96 
   97   def test_report_two_criteria
   98     get :report, :params => {:project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]}
   99     assert_response :success
  100     assert_select 'tr.total td:last', :text => '162.90'
  101   end
  102 
  103   def test_report_should_show_locked_users
  104     @request.session[:user_id] = 1
  105 
  106     user = User.find(2)
  107     user.status = User::STATUS_LOCKED
  108     user.save
  109 
  110     get :report, :params => {:project_id => 1, :columns => 'month', :criteria => ["user", "activity"]}
  111     assert_response :success
  112 
  113     assert_select 'td.name a.user.active[href=?]', '/users/1', 1, :text => 'Redmine Admin'
  114     assert_select 'td.name a.user.locked[href=?]', '/users/2', 1, :text => 'John Smith'
  115   end
  116 
  117   def test_report_custom_field_criteria_with_multiple_values_on_single_value_custom_field_should_not_fail
  118     field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2'])
  119     entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today)
  120     CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1')
  121     CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2')
  122 
  123     get :report, :params => {:project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"]}
  124     assert_response :success
  125   end
  126 
  127   def test_report_multiple_values_custom_fields_should_not_be_proposed
  128     TimeEntryCustomField.create!(:name => 'Single', :field_format => 'list', :possible_values => ['value1', 'value2'])
  129     TimeEntryCustomField.create!(:name => 'Multi', :field_format => 'list', :multiple => true, :possible_values => ['value1', 'value2'])
  130 
  131     get :report, :params => {:project_id => 1}
  132     assert_response :success
  133     assert_select 'select[name=?]', 'criteria[]' do
  134       assert_select 'option', :text => 'Single'
  135       assert_select 'option', :text => 'Multi', :count => 0
  136     end
  137   end
  138 
  139   def test_hidden_custom_fields_should_not_be_proposed
  140     TimeEntryCustomField.create!(name: 'shown', field_format: 'list', possible_values: ['value1', 'value2'], visible: true)
  141     TimeEntryCustomField.create!(name: 'Hidden', field_format: 'list', possible_values: ['value1', 'value2'], visible: false, role_ids: [3])
  142 
  143     get :report, :params => {:project_id => 1}
  144     assert_response :success
  145     assert_select 'select[name=?]', 'criteria[]' do
  146       assert_select 'option', :text => 'Shown'
  147       assert_select 'option', :text => 'Hidden', :count => 0
  148     end
  149   end
  150 
  151   def test_report_one_day
  152     get :report, :params => {:project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["user", "activity"]}
  153     assert_response :success
  154     assert_select 'tr.total td:last', :text => '4.25'
  155   end
  156 
  157   def test_report_by_week_should_use_commercial_year
  158     TimeEntry.delete_all
  159     TimeEntry.generate!(:hours => '2', :spent_on => '2009-12-25') # 2009-52
  160     TimeEntry.generate!(:hours => '4', :spent_on => '2009-12-31') # 2009-53
  161     TimeEntry.generate!(:hours => '8', :spent_on => '2010-01-01') # 2009-53
  162     TimeEntry.generate!(:hours => '16', :spent_on => '2010-01-05') # 2010-1
  163 
  164     get :report, :params => {:columns => 'week', :from => "2009-12-25", :to => "2010-01-05", :criteria => ["project"]}
  165     assert_response :success
  166 
  167     assert_select '#time-report thead tr' do
  168       assert_select 'th:nth-child(1)', :text => 'Project'
  169       assert_select 'th:nth-child(2)', :text => '2009-52'
  170       assert_select 'th:nth-child(3)', :text => '2009-53'
  171       assert_select 'th:nth-child(4)', :text => '2010-1'
  172       assert_select 'th:nth-child(5)', :text => 'Total time'
  173     end
  174     assert_select '#time-report tbody tr' do
  175       assert_select 'td:nth-child(1)', :text => 'eCookbook'
  176       assert_select 'td:nth-child(2)', :text => '2.00'
  177       assert_select 'td:nth-child(3)', :text => '12.00'
  178       assert_select 'td:nth-child(4)', :text => '16.00'
  179       assert_select 'td:nth-child(5)', :text => '30.00' # Total
  180     end
  181   end
  182 
  183   def test_report_should_propose_association_custom_fields
  184     get :report
  185     assert_response :success
  186 
  187     assert_select 'select[name=?]', 'criteria[]' do
  188       assert_select 'option[value=cf_1]', {:text => 'Database'}, 'Issue custom field not found'
  189       assert_select 'option[value=cf_3]', {:text => 'Development status'}, 'Project custom field not found'
  190       assert_select 'option[value=cf_7]', {:text => 'Billable'}, 'TimeEntryActivity custom field not found'
  191     end
  192   end
  193 
  194   def test_report_with_association_custom_fields
  195     get :report, :params => {:criteria => ['cf_1', 'cf_3', 'cf_7']}
  196     assert_response :success
  197 
  198     assert_select 'tr.total td:last', :text => '162.90'
  199 
  200     # Custom fields columns
  201     assert_select 'th', :text => 'Database'
  202     assert_select 'th', :text => 'Development status'
  203     assert_select 'th', :text => 'Billable'
  204 
  205     # Custom field row
  206     assert_select 'tr' do
  207       assert_select 'td', :text => 'MySQL'
  208       assert_select 'td.hours', :text => '1.00'
  209     end
  210   end
  211 
  212   def test_report_one_criteria_no_result
  213     get :report, :params => {:project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']}
  214     assert_response :success
  215 
  216     assert_select '.nodata'
  217   end
  218 
  219   def test_report_status_criterion
  220     get :report, :params => {:project_id => 1, :criteria => ['status']}
  221     assert_response :success
  222 
  223     assert_select 'th', :text => 'Status'
  224     assert_select 'td', :text => 'New'
  225   end
  226 
  227   def test_report_all_projects_csv_export
  228     get :report, :params => {
  229       :columns => 'month',
  230       :from => "2007-01-01",
  231       :to => "2007-06-30",
  232       :criteria => ["project", "user", "activity"],
  233       :format => "csv"
  234     }
  235     assert_response :success
  236     assert_equal 'text/csv', @response.media_type
  237     lines = @response.body.chomp.split("\n")
  238     # Headers
  239     assert_equal 'Project,User,Activity,2007-3,2007-4,Total time', lines.first
  240     # Total row
  241     assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
  242   end
  243 
  244   def test_report_csv_export
  245     get :report, :params => {
  246       :project_id => 1,
  247       :columns => 'month',
  248       :from => "2007-01-01",
  249       :to => "2007-06-30",
  250       :criteria => ["project", "user", "cf_10"],
  251       :format => "csv"
  252     }
  253     assert_response :success
  254     assert_equal 'text/csv', @response.media_type
  255     lines = @response.body.chomp.split("\n")
  256     # Headers
  257     assert_equal 'Project,User,Overtime,2007-3,2007-4,Total time', lines.first
  258     # Total row
  259     assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
  260   end
  261 
  262   def test_report_csv_should_fill_issue_criteria_with_tracker_id_and_subject
  263     get :report, :params => {
  264       :project_id => 1,
  265       :columns => 'month',
  266       :from => "2007-01-01",
  267       :to => "2007-06-30",
  268       :criteria => ["issue"],
  269       :format => "csv"
  270     }
  271 
  272     assert_response :success
  273     lines = @response.body.chomp.split("\n")
  274     assert lines.detect {|line| line.include?('Bug #1: Cannot print recipes')}
  275   end
  276 
  277   def test_csv_big_5
  278     str_big5  = (+"\xa4@\xa4\xeb").force_encoding('Big5')
  279     user = User.find_by_id(3)
  280     user.firstname = "一月"
  281     user.lastname  = "test-lastname"
  282     assert user.save
  283     comments = "test_csv_big_5"
  284     te1 = TimeEntry.create(:spent_on => '2011-11-11',
  285                            :hours    => 7.3,
  286                            :project  => Project.find(1),
  287                            :user     => user,
  288                            :activity => TimeEntryActivity.find_by_name('Design'),
  289                            :comments => comments)
  290 
  291     te2 = TimeEntry.find_by_comments(comments)
  292     assert_not_nil te2
  293     assert_equal 7.3, te2.hours
  294     assert_equal 3, te2.user_id
  295 
  296     with_settings :default_language => "zh-TW" do
  297       get :report, :params => {
  298         :project_id => 1,
  299         :columns => 'day',
  300         :from => "2011-11-11",
  301         :to => "2011-11-11",
  302         :criteria => ["user"],
  303         :format => "csv"
  304       }
  305     end
  306     assert_response :success
  307     assert_equal 'text/csv', @response.media_type
  308     lines = @response.body.chomp.split("\n")
  309     # Headers
  310     s1 = (+"\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp").force_encoding('Big5')
  311     s2 = (+"\xa4u\xae\xc9\xc1`\xadp").force_encoding('Big5')
  312     assert_equal s1, lines.first
  313     # Total row
  314     assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
  315     assert_equal "#{s2},7.30,7.30", lines[2]
  316 
  317     assert_equal 'Chinese/Traditional (繁體中文)', l(:general_lang_name)
  318     assert_equal 'Big5', l(:general_csv_encoding)
  319     assert_equal ',', l(:general_csv_separator)
  320     assert_equal '.', l(:general_csv_decimal_separator)
  321   end
  322 
  323   def test_csv_cannot_convert_should_be_replaced_big_5
  324     user = User.find_by_id(3)
  325     user.firstname = "以内"
  326     user.lastname  = "test-lastname"
  327     assert user.save
  328     comments = "test_replaced"
  329     te1 = TimeEntry.create(:spent_on => '2011-11-11',
  330                            :hours    => 7.3,
  331                            :project  => Project.find(1),
  332                            :user     => user,
  333                            :activity => TimeEntryActivity.find_by_name('Design'),
  334                            :comments => comments)
  335 
  336     te2 = TimeEntry.find_by_comments(comments)
  337     assert_not_nil te2
  338     assert_equal 7.3, te2.hours
  339     assert_equal 3, te2.user_id
  340 
  341     with_settings :default_language => "zh-TW" do
  342       get :report, :params => {
  343         :project_id => 1,
  344         :columns => 'day',
  345         :from => "2011-11-11",
  346         :to => "2011-11-11",
  347         :criteria => ["user"],
  348         :format => "csv"
  349       }
  350     end
  351     assert_response :success
  352     assert_equal 'text/csv', @response.media_type
  353     lines = @response.body.chomp.split("\n")
  354     # Headers
  355     s1 = (+"\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp").force_encoding('Big5')
  356     assert_equal s1, lines.first
  357     # Total row
  358     s2 = (+"\xa5H?").force_encoding('Big5')
  359     assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
  360   end
  361 
  362   def test_csv_fr
  363     with_settings :default_language => "fr" do
  364       str1  = "test_csv_fr"
  365       user = User.find_by_id(3)
  366       te1 = TimeEntry.create(:spent_on => '2011-11-11',
  367                              :hours    => 7.3,
  368                              :project  => Project.find(1),
  369                              :user     => user,
  370                              :activity => TimeEntryActivity.find_by_name('Design'),
  371                              :comments => str1)
  372 
  373       te2 = TimeEntry.find_by_comments(str1)
  374       assert_not_nil te2
  375       assert_equal 7.3, te2.hours
  376       assert_equal 3, te2.user_id
  377 
  378       get :report, :params => {
  379         :project_id => 1,
  380         :columns => 'day',
  381         :from => "2011-11-11",
  382         :to => "2011-11-11",
  383         :criteria => ["user"],
  384         :format => "csv"
  385       }
  386       assert_response :success
  387       assert_equal 'text/csv', @response.media_type
  388       lines = @response.body.chomp.split("\n")
  389       # Headers
  390       s1 = (+"Utilisateur;2011-11-11;Temps total").force_encoding('ISO-8859-1')
  391       s2 = (+"Temps total").force_encoding('ISO-8859-1')
  392       assert_equal s1, lines.first
  393       # Total row
  394       assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
  395       assert_equal "#{s2};7,30;7,30", lines[2]
  396 
  397       assert_equal 'French (Français)', l(:general_lang_name)
  398       assert_equal 'ISO-8859-1', l(:general_csv_encoding)
  399       assert_equal ';', l(:general_csv_separator)
  400       assert_equal ',', l(:general_csv_decimal_separator)
  401     end
  402   end
  403 end