##// END OF EJS Templates
Merged r11471 from trunk (#5329)....
Jean-Philippe Lang -
r11335:83430dacd948
parent child
Show More
@@ -1,151 +1,151
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 module Redmine
18 module Redmine
19 module Helpers
19 module Helpers
20 class TimeReport
20 class TimeReport
21 attr_reader :criteria, :columns, :hours, :total_hours, :periods
21 attr_reader :criteria, :columns, :hours, :total_hours, :periods
22
22
23 def initialize(project, issue, criteria, columns, time_entry_scope)
23 def initialize(project, issue, criteria, columns, time_entry_scope)
24 @project = project
24 @project = project
25 @issue = issue
25 @issue = issue
26
26
27 @criteria = criteria || []
27 @criteria = criteria || []
28 @criteria = @criteria.select{|criteria| available_criteria.has_key? criteria}
28 @criteria = @criteria.select{|criteria| available_criteria.has_key? criteria}
29 @criteria.uniq!
29 @criteria.uniq!
30 @criteria = @criteria[0,3]
30 @criteria = @criteria[0,3]
31
31
32 @columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month'
32 @columns = (columns && %w(year month week day).include?(columns)) ? columns : 'month'
33 @scope = time_entry_scope
33 @scope = time_entry_scope
34
34
35 run
35 run
36 end
36 end
37
37
38 def available_criteria
38 def available_criteria
39 @available_criteria || load_available_criteria
39 @available_criteria || load_available_criteria
40 end
40 end
41
41
42 private
42 private
43
43
44 def run
44 def run
45 unless @criteria.empty?
45 unless @criteria.empty?
46 time_columns = %w(tyear tmonth tweek spent_on)
46 time_columns = %w(tyear tmonth tweek spent_on)
47 @hours = []
47 @hours = []
48 @scope.sum(:hours,
48 @scope.sum(:hours,
49 :include => [:issue, :activity],
49 :include => [:issue, :activity],
50 :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns,
50 :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns,
51 :joins => @criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact).each do |hash, hours|
51 :joins => @criteria.collect{|criteria| @available_criteria[criteria][:joins]}.compact).each do |hash, hours|
52 h = {'hours' => hours}
52 h = {'hours' => hours}
53 (@criteria + time_columns).each_with_index do |name, i|
53 (@criteria + time_columns).each_with_index do |name, i|
54 h[name] = hash[i]
54 h[name] = hash[i]
55 end
55 end
56 @hours << h
56 @hours << h
57 end
57 end
58
58
59 @hours.each do |row|
59 @hours.each do |row|
60 case @columns
60 case @columns
61 when 'year'
61 when 'year'
62 row['year'] = row['tyear']
62 row['year'] = row['tyear']
63 when 'month'
63 when 'month'
64 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
64 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
65 when 'week'
65 when 'week'
66 row['week'] = "#{row['tyear']}-#{row['tweek']}"
66 row['week'] = "#{row['spent_on'].cwyear}-#{row['tweek']}"
67 when 'day'
67 when 'day'
68 row['day'] = "#{row['spent_on']}"
68 row['day'] = "#{row['spent_on']}"
69 end
69 end
70 end
70 end
71
71
72 min = @hours.collect {|row| row['spent_on']}.min
72 min = @hours.collect {|row| row['spent_on']}.min
73 @from = min ? min.to_date : Date.today
73 @from = min ? min.to_date : Date.today
74
74
75 max = @hours.collect {|row| row['spent_on']}.max
75 max = @hours.collect {|row| row['spent_on']}.max
76 @to = max ? max.to_date : Date.today
76 @to = max ? max.to_date : Date.today
77
77
78 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
78 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
79
79
80 @periods = []
80 @periods = []
81 # Date#at_beginning_of_ not supported in Rails 1.2.x
81 # Date#at_beginning_of_ not supported in Rails 1.2.x
82 date_from = @from.to_time
82 date_from = @from.to_time
83 # 100 columns max
83 # 100 columns max
84 while date_from <= @to.to_time && @periods.length < 100
84 while date_from <= @to.to_time && @periods.length < 100
85 case @columns
85 case @columns
86 when 'year'
86 when 'year'
87 @periods << "#{date_from.year}"
87 @periods << "#{date_from.year}"
88 date_from = (date_from + 1.year).at_beginning_of_year
88 date_from = (date_from + 1.year).at_beginning_of_year
89 when 'month'
89 when 'month'
90 @periods << "#{date_from.year}-#{date_from.month}"
90 @periods << "#{date_from.year}-#{date_from.month}"
91 date_from = (date_from + 1.month).at_beginning_of_month
91 date_from = (date_from + 1.month).at_beginning_of_month
92 when 'week'
92 when 'week'
93 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
93 @periods << "#{date_from.to_date.cwyear}-#{date_from.to_date.cweek}"
94 date_from = (date_from + 7.day).at_beginning_of_week
94 date_from = (date_from + 7.day).at_beginning_of_week
95 when 'day'
95 when 'day'
96 @periods << "#{date_from.to_date}"
96 @periods << "#{date_from.to_date}"
97 date_from = date_from + 1.day
97 date_from = date_from + 1.day
98 end
98 end
99 end
99 end
100 end
100 end
101 end
101 end
102
102
103 def load_available_criteria
103 def load_available_criteria
104 @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
104 @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
105 :klass => Project,
105 :klass => Project,
106 :label => :label_project},
106 :label => :label_project},
107 'status' => {:sql => "#{Issue.table_name}.status_id",
107 'status' => {:sql => "#{Issue.table_name}.status_id",
108 :klass => IssueStatus,
108 :klass => IssueStatus,
109 :label => :field_status},
109 :label => :field_status},
110 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
110 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
111 :klass => Version,
111 :klass => Version,
112 :label => :label_version},
112 :label => :label_version},
113 'category' => {:sql => "#{Issue.table_name}.category_id",
113 'category' => {:sql => "#{Issue.table_name}.category_id",
114 :klass => IssueCategory,
114 :klass => IssueCategory,
115 :label => :field_category},
115 :label => :field_category},
116 'user' => {:sql => "#{TimeEntry.table_name}.user_id",
116 'user' => {:sql => "#{TimeEntry.table_name}.user_id",
117 :klass => User,
117 :klass => User,
118 :label => :label_user},
118 :label => :label_user},
119 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
119 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
120 :klass => Tracker,
120 :klass => Tracker,
121 :label => :label_tracker},
121 :label => :label_tracker},
122 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
122 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
123 :klass => TimeEntryActivity,
123 :klass => TimeEntryActivity,
124 :label => :label_activity},
124 :label => :label_activity},
125 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
125 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
126 :klass => Issue,
126 :klass => Issue,
127 :label => :label_issue}
127 :label => :label_issue}
128 }
128 }
129
129
130 # Add time entry custom fields
130 # Add time entry custom fields
131 custom_fields = TimeEntryCustomField.all
131 custom_fields = TimeEntryCustomField.all
132 # Add project custom fields
132 # Add project custom fields
133 custom_fields += ProjectCustomField.all
133 custom_fields += ProjectCustomField.all
134 # Add issue custom fields
134 # Add issue custom fields
135 custom_fields += (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
135 custom_fields += (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
136 # Add time entry activity custom fields
136 # Add time entry activity custom fields
137 custom_fields += TimeEntryActivityCustomField.all
137 custom_fields += TimeEntryActivityCustomField.all
138
138
139 # Add list and boolean custom fields as available criteria
139 # Add list and boolean custom fields as available criteria
140 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
140 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
141 @available_criteria["cf_#{cf.id}"] = {:sql => "#{cf.join_alias}.value",
141 @available_criteria["cf_#{cf.id}"] = {:sql => "#{cf.join_alias}.value",
142 :joins => cf.join_for_order_statement,
142 :joins => cf.join_for_order_statement,
143 :format => cf.field_format,
143 :format => cf.field_format,
144 :label => cf.name}
144 :label => cf.name}
145 end
145 end
146
146
147 @available_criteria
147 @available_criteria
148 end
148 end
149 end
149 end
150 end
150 end
151 end
151 end
@@ -1,346 +1,372
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 # Redmine - project management software
2 # Redmine - project management software
3 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # Copyright (C) 2006-2013 Jean-Philippe Lang
4 #
4 #
5 # This program is free software; you can redistribute it and/or
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
8 # of the License, or (at your option) any later version.
9 #
9 #
10 # This program is distributed in the hope that it will be useful,
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
13 # GNU General Public License for more details.
14 #
14 #
15 # You should have received a copy of the GNU General Public License
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
18
19 require File.expand_path('../../test_helper', __FILE__)
19 require File.expand_path('../../test_helper', __FILE__)
20
20
21 class TimeEntryReportsControllerTest < ActionController::TestCase
21 class TimeEntryReportsControllerTest < ActionController::TestCase
22 tests TimelogController
22 tests TimelogController
23
23
24 fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
24 fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
25 :issues, :time_entries, :users, :trackers, :enumerations,
25 :issues, :time_entries, :users, :trackers, :enumerations,
26 :issue_statuses, :custom_fields, :custom_values
26 :issue_statuses, :custom_fields, :custom_values
27
27
28 include Redmine::I18n
28 include Redmine::I18n
29
29
30 def setup
30 def setup
31 Setting.default_language = "en"
31 Setting.default_language = "en"
32 end
32 end
33
33
34 def test_report_at_project_level
34 def test_report_at_project_level
35 get :report, :project_id => 'ecookbook'
35 get :report, :project_id => 'ecookbook'
36 assert_response :success
36 assert_response :success
37 assert_template 'report'
37 assert_template 'report'
38 assert_tag :form,
38 assert_tag :form,
39 :attributes => {:action => "/projects/ecookbook/time_entries/report", :id => 'query_form'}
39 :attributes => {:action => "/projects/ecookbook/time_entries/report", :id => 'query_form'}
40 end
40 end
41
41
42 def test_report_all_projects
42 def test_report_all_projects
43 get :report
43 get :report
44 assert_response :success
44 assert_response :success
45 assert_template 'report'
45 assert_template 'report'
46 assert_tag :form,
46 assert_tag :form,
47 :attributes => {:action => "/time_entries/report", :id => 'query_form'}
47 :attributes => {:action => "/time_entries/report", :id => 'query_form'}
48 end
48 end
49
49
50 def test_report_all_projects_denied
50 def test_report_all_projects_denied
51 r = Role.anonymous
51 r = Role.anonymous
52 r.permissions.delete(:view_time_entries)
52 r.permissions.delete(:view_time_entries)
53 r.permissions_will_change!
53 r.permissions_will_change!
54 r.save
54 r.save
55 get :report
55 get :report
56 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
56 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
57 end
57 end
58
58
59 def test_report_all_projects_one_criteria
59 def test_report_all_projects_one_criteria
60 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
60 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
61 assert_response :success
61 assert_response :success
62 assert_template 'report'
62 assert_template 'report'
63 assert_not_nil assigns(:report)
63 assert_not_nil assigns(:report)
64 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
64 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
65 end
65 end
66
66
67 def test_report_all_time
67 def test_report_all_time
68 get :report, :project_id => 1, :criteria => ['project', 'issue']
68 get :report, :project_id => 1, :criteria => ['project', 'issue']
69 assert_response :success
69 assert_response :success
70 assert_template 'report'
70 assert_template 'report'
71 assert_not_nil assigns(:report)
71 assert_not_nil assigns(:report)
72 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
72 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
73 end
73 end
74
74
75 def test_report_all_time_by_day
75 def test_report_all_time_by_day
76 get :report, :project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'
76 get :report, :project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'
77 assert_response :success
77 assert_response :success
78 assert_template 'report'
78 assert_template 'report'
79 assert_not_nil assigns(:report)
79 assert_not_nil assigns(:report)
80 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
80 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
81 assert_tag :tag => 'th', :content => '2007-03-12'
81 assert_tag :tag => 'th', :content => '2007-03-12'
82 end
82 end
83
83
84 def test_report_one_criteria
84 def test_report_one_criteria
85 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
85 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
86 assert_response :success
86 assert_response :success
87 assert_template 'report'
87 assert_template 'report'
88 assert_not_nil assigns(:report)
88 assert_not_nil assigns(:report)
89 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
89 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
90 end
90 end
91
91
92 def test_report_two_criteria
92 def test_report_two_criteria
93 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]
93 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]
94 assert_response :success
94 assert_response :success
95 assert_template 'report'
95 assert_template 'report'
96 assert_not_nil assigns(:report)
96 assert_not_nil assigns(:report)
97 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
97 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
98 end
98 end
99
99
100 def test_report_custom_field_criteria_with_multiple_values
100 def test_report_custom_field_criteria_with_multiple_values
101 field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2'])
101 field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2'])
102 entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today)
102 entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today)
103 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1')
103 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1')
104 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2')
104 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2')
105
105
106 get :report, :project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"]
106 get :report, :project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"]
107 assert_response :success
107 assert_response :success
108 end
108 end
109
109
110 def test_report_one_day
110 def test_report_one_day
111 get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["user", "activity"]
111 get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["user", "activity"]
112 assert_response :success
112 assert_response :success
113 assert_template 'report'
113 assert_template 'report'
114 assert_not_nil assigns(:report)
114 assert_not_nil assigns(:report)
115 assert_equal "4.25", "%.2f" % assigns(:report).total_hours
115 assert_equal "4.25", "%.2f" % assigns(:report).total_hours
116 end
116 end
117
117
118 def test_report_at_issue_level
118 def test_report_at_issue_level
119 get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]
119 get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]
120 assert_response :success
120 assert_response :success
121 assert_template 'report'
121 assert_template 'report'
122 assert_not_nil assigns(:report)
122 assert_not_nil assigns(:report)
123 assert_equal "154.25", "%.2f" % assigns(:report).total_hours
123 assert_equal "154.25", "%.2f" % assigns(:report).total_hours
124 assert_tag :form,
124 assert_tag :form,
125 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'}
125 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'}
126 end
126 end
127
127
128 def test_report_by_week_should_use_commercial_year
129 TimeEntry.delete_all
130 TimeEntry.generate!(:hours => '2', :spent_on => '2009-12-25') # 2009-52
131 TimeEntry.generate!(:hours => '4', :spent_on => '2009-12-31') # 2009-53
132 TimeEntry.generate!(:hours => '8', :spent_on => '2010-01-01') # 2009-53
133 TimeEntry.generate!(:hours => '16', :spent_on => '2010-01-05') # 2010-1
134
135 get :report, :columns => 'week', :from => "2009-12-25", :to => "2010-01-05", :criteria => ["project"]
136 assert_response :success
137
138 assert_select '#time-report thead tr' do
139 assert_select 'th:nth-child(1)', :text => 'Project'
140 assert_select 'th:nth-child(2)', :text => '2009-52'
141 assert_select 'th:nth-child(3)', :text => '2009-53'
142 assert_select 'th:nth-child(4)', :text => '2010-1'
143 assert_select 'th:nth-child(5)', :text => 'Total'
144 end
145 assert_select '#time-report tbody tr' do
146 assert_select 'td:nth-child(1)', :text => 'eCookbook'
147 assert_select 'td:nth-child(2)', :text => '2.00'
148 assert_select 'td:nth-child(3)', :text => '12.00'
149 assert_select 'td:nth-child(4)', :text => '16.00'
150 assert_select 'td:nth-child(5)', :text => '30.00' # Total
151 end
152 end
153
128 def test_report_should_propose_association_custom_fields
154 def test_report_should_propose_association_custom_fields
129 get :report
155 get :report
130 assert_response :success
156 assert_response :success
131 assert_template 'report'
157 assert_template 'report'
132
158
133 assert_select 'select[name=?]', 'criteria[]' do
159 assert_select 'select[name=?]', 'criteria[]' do
134 assert_select 'option[value=cf_1]', {:text => 'Database'}, 'Issue custom field not found'
160 assert_select 'option[value=cf_1]', {:text => 'Database'}, 'Issue custom field not found'
135 assert_select 'option[value=cf_3]', {:text => 'Development status'}, 'Project custom field not found'
161 assert_select 'option[value=cf_3]', {:text => 'Development status'}, 'Project custom field not found'
136 assert_select 'option[value=cf_7]', {:text => 'Billable'}, 'TimeEntryActivity custom field not found'
162 assert_select 'option[value=cf_7]', {:text => 'Billable'}, 'TimeEntryActivity custom field not found'
137 end
163 end
138 end
164 end
139
165
140 def test_report_with_association_custom_fields
166 def test_report_with_association_custom_fields
141 get :report, :criteria => ['cf_1', 'cf_3', 'cf_7']
167 get :report, :criteria => ['cf_1', 'cf_3', 'cf_7']
142 assert_response :success
168 assert_response :success
143 assert_template 'report'
169 assert_template 'report'
144 assert_not_nil assigns(:report)
170 assert_not_nil assigns(:report)
145 assert_equal 3, assigns(:report).criteria.size
171 assert_equal 3, assigns(:report).criteria.size
146 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
172 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
147
173
148 # Custom fields columns
174 # Custom fields columns
149 assert_select 'th', :text => 'Database'
175 assert_select 'th', :text => 'Database'
150 assert_select 'th', :text => 'Development status'
176 assert_select 'th', :text => 'Development status'
151 assert_select 'th', :text => 'Billable'
177 assert_select 'th', :text => 'Billable'
152
178
153 # Custom field row
179 # Custom field row
154 assert_select 'tr' do
180 assert_select 'tr' do
155 assert_select 'td', :text => 'MySQL'
181 assert_select 'td', :text => 'MySQL'
156 assert_select 'td.hours', :text => '1.00'
182 assert_select 'td.hours', :text => '1.00'
157 end
183 end
158 end
184 end
159
185
160 def test_report_one_criteria_no_result
186 def test_report_one_criteria_no_result
161 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
187 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
162 assert_response :success
188 assert_response :success
163 assert_template 'report'
189 assert_template 'report'
164 assert_not_nil assigns(:report)
190 assert_not_nil assigns(:report)
165 assert_equal "0.00", "%.2f" % assigns(:report).total_hours
191 assert_equal "0.00", "%.2f" % assigns(:report).total_hours
166 end
192 end
167
193
168 def test_report_status_criterion
194 def test_report_status_criterion
169 get :report, :project_id => 1, :criteria => ['status']
195 get :report, :project_id => 1, :criteria => ['status']
170 assert_response :success
196 assert_response :success
171 assert_template 'report'
197 assert_template 'report'
172 assert_tag :tag => 'th', :content => 'Status'
198 assert_tag :tag => 'th', :content => 'Status'
173 assert_tag :tag => 'td', :content => 'New'
199 assert_tag :tag => 'td', :content => 'New'
174 end
200 end
175
201
176 def test_report_all_projects_csv_export
202 def test_report_all_projects_csv_export
177 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30",
203 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30",
178 :criteria => ["project", "user", "activity"], :format => "csv"
204 :criteria => ["project", "user", "activity"], :format => "csv"
179 assert_response :success
205 assert_response :success
180 assert_equal 'text/csv; header=present', @response.content_type
206 assert_equal 'text/csv; header=present', @response.content_type
181 lines = @response.body.chomp.split("\n")
207 lines = @response.body.chomp.split("\n")
182 # Headers
208 # Headers
183 assert_equal 'Project,User,Activity,2007-3,2007-4,Total', lines.first
209 assert_equal 'Project,User,Activity,2007-3,2007-4,Total', lines.first
184 # Total row
210 # Total row
185 assert_equal 'Total,"","",154.25,8.65,162.90', lines.last
211 assert_equal 'Total,"","",154.25,8.65,162.90', lines.last
186 end
212 end
187
213
188 def test_report_csv_export
214 def test_report_csv_export
189 get :report, :project_id => 1, :columns => 'month',
215 get :report, :project_id => 1, :columns => 'month',
190 :from => "2007-01-01", :to => "2007-06-30",
216 :from => "2007-01-01", :to => "2007-06-30",
191 :criteria => ["project", "user", "activity"], :format => "csv"
217 :criteria => ["project", "user", "activity"], :format => "csv"
192 assert_response :success
218 assert_response :success
193 assert_equal 'text/csv; header=present', @response.content_type
219 assert_equal 'text/csv; header=present', @response.content_type
194 lines = @response.body.chomp.split("\n")
220 lines = @response.body.chomp.split("\n")
195 # Headers
221 # Headers
196 assert_equal 'Project,User,Activity,2007-3,2007-4,Total', lines.first
222 assert_equal 'Project,User,Activity,2007-3,2007-4,Total', lines.first
197 # Total row
223 # Total row
198 assert_equal 'Total,"","",154.25,8.65,162.90', lines.last
224 assert_equal 'Total,"","",154.25,8.65,162.90', lines.last
199 end
225 end
200
226
201 def test_csv_big_5
227 def test_csv_big_5
202 Setting.default_language = "zh-TW"
228 Setting.default_language = "zh-TW"
203 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
229 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
204 str_big5 = "\xa4@\xa4\xeb"
230 str_big5 = "\xa4@\xa4\xeb"
205 if str_utf8.respond_to?(:force_encoding)
231 if str_utf8.respond_to?(:force_encoding)
206 str_utf8.force_encoding('UTF-8')
232 str_utf8.force_encoding('UTF-8')
207 str_big5.force_encoding('Big5')
233 str_big5.force_encoding('Big5')
208 end
234 end
209 user = User.find_by_id(3)
235 user = User.find_by_id(3)
210 user.firstname = str_utf8
236 user.firstname = str_utf8
211 user.lastname = "test-lastname"
237 user.lastname = "test-lastname"
212 assert user.save
238 assert user.save
213 comments = "test_csv_big_5"
239 comments = "test_csv_big_5"
214 te1 = TimeEntry.create(:spent_on => '2011-11-11',
240 te1 = TimeEntry.create(:spent_on => '2011-11-11',
215 :hours => 7.3,
241 :hours => 7.3,
216 :project => Project.find(1),
242 :project => Project.find(1),
217 :user => user,
243 :user => user,
218 :activity => TimeEntryActivity.find_by_name('Design'),
244 :activity => TimeEntryActivity.find_by_name('Design'),
219 :comments => comments)
245 :comments => comments)
220
246
221 te2 = TimeEntry.find_by_comments(comments)
247 te2 = TimeEntry.find_by_comments(comments)
222 assert_not_nil te2
248 assert_not_nil te2
223 assert_equal 7.3, te2.hours
249 assert_equal 7.3, te2.hours
224 assert_equal 3, te2.user_id
250 assert_equal 3, te2.user_id
225
251
226 get :report, :project_id => 1, :columns => 'day',
252 get :report, :project_id => 1, :columns => 'day',
227 :from => "2011-11-11", :to => "2011-11-11",
253 :from => "2011-11-11", :to => "2011-11-11",
228 :criteria => ["user"], :format => "csv"
254 :criteria => ["user"], :format => "csv"
229 assert_response :success
255 assert_response :success
230 assert_equal 'text/csv; header=present', @response.content_type
256 assert_equal 'text/csv; header=present', @response.content_type
231 lines = @response.body.chomp.split("\n")
257 lines = @response.body.chomp.split("\n")
232 # Headers
258 # Headers
233 s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xc1`\xadp"
259 s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xc1`\xadp"
234 s2 = "\xc1`\xadp"
260 s2 = "\xc1`\xadp"
235 if s1.respond_to?(:force_encoding)
261 if s1.respond_to?(:force_encoding)
236 s1.force_encoding('Big5')
262 s1.force_encoding('Big5')
237 s2.force_encoding('Big5')
263 s2.force_encoding('Big5')
238 end
264 end
239 assert_equal s1, lines.first
265 assert_equal s1, lines.first
240 # Total row
266 # Total row
241 assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
267 assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
242 assert_equal "#{s2},7.30,7.30", lines[2]
268 assert_equal "#{s2},7.30,7.30", lines[2]
243
269
244 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
270 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
245 if str_tw.respond_to?(:force_encoding)
271 if str_tw.respond_to?(:force_encoding)
246 str_tw.force_encoding('UTF-8')
272 str_tw.force_encoding('UTF-8')
247 end
273 end
248 assert_equal str_tw, l(:general_lang_name)
274 assert_equal str_tw, l(:general_lang_name)
249 assert_equal 'Big5', l(:general_csv_encoding)
275 assert_equal 'Big5', l(:general_csv_encoding)
250 assert_equal ',', l(:general_csv_separator)
276 assert_equal ',', l(:general_csv_separator)
251 assert_equal '.', l(:general_csv_decimal_separator)
277 assert_equal '.', l(:general_csv_decimal_separator)
252 end
278 end
253
279
254 def test_csv_cannot_convert_should_be_replaced_big_5
280 def test_csv_cannot_convert_should_be_replaced_big_5
255 Setting.default_language = "zh-TW"
281 Setting.default_language = "zh-TW"
256 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
282 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
257 if str_utf8.respond_to?(:force_encoding)
283 if str_utf8.respond_to?(:force_encoding)
258 str_utf8.force_encoding('UTF-8')
284 str_utf8.force_encoding('UTF-8')
259 end
285 end
260 user = User.find_by_id(3)
286 user = User.find_by_id(3)
261 user.firstname = str_utf8
287 user.firstname = str_utf8
262 user.lastname = "test-lastname"
288 user.lastname = "test-lastname"
263 assert user.save
289 assert user.save
264 comments = "test_replaced"
290 comments = "test_replaced"
265 te1 = TimeEntry.create(:spent_on => '2011-11-11',
291 te1 = TimeEntry.create(:spent_on => '2011-11-11',
266 :hours => 7.3,
292 :hours => 7.3,
267 :project => Project.find(1),
293 :project => Project.find(1),
268 :user => user,
294 :user => user,
269 :activity => TimeEntryActivity.find_by_name('Design'),
295 :activity => TimeEntryActivity.find_by_name('Design'),
270 :comments => comments)
296 :comments => comments)
271
297
272 te2 = TimeEntry.find_by_comments(comments)
298 te2 = TimeEntry.find_by_comments(comments)
273 assert_not_nil te2
299 assert_not_nil te2
274 assert_equal 7.3, te2.hours
300 assert_equal 7.3, te2.hours
275 assert_equal 3, te2.user_id
301 assert_equal 3, te2.user_id
276
302
277 get :report, :project_id => 1, :columns => 'day',
303 get :report, :project_id => 1, :columns => 'day',
278 :from => "2011-11-11", :to => "2011-11-11",
304 :from => "2011-11-11", :to => "2011-11-11",
279 :criteria => ["user"], :format => "csv"
305 :criteria => ["user"], :format => "csv"
280 assert_response :success
306 assert_response :success
281 assert_equal 'text/csv; header=present', @response.content_type
307 assert_equal 'text/csv; header=present', @response.content_type
282 lines = @response.body.chomp.split("\n")
308 lines = @response.body.chomp.split("\n")
283 # Headers
309 # Headers
284 s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xc1`\xadp"
310 s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xc1`\xadp"
285 if s1.respond_to?(:force_encoding)
311 if s1.respond_to?(:force_encoding)
286 s1.force_encoding('Big5')
312 s1.force_encoding('Big5')
287 end
313 end
288 assert_equal s1, lines.first
314 assert_equal s1, lines.first
289 # Total row
315 # Total row
290 s2 = ""
316 s2 = ""
291 if s2.respond_to?(:force_encoding)
317 if s2.respond_to?(:force_encoding)
292 s2 = "\xa5H?"
318 s2 = "\xa5H?"
293 s2.force_encoding('Big5')
319 s2.force_encoding('Big5')
294 elsif RUBY_PLATFORM == 'java'
320 elsif RUBY_PLATFORM == 'java'
295 s2 = "??"
321 s2 = "??"
296 else
322 else
297 s2 = "\xa5H???"
323 s2 = "\xa5H???"
298 end
324 end
299 assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
325 assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
300 end
326 end
301
327
302 def test_csv_fr
328 def test_csv_fr
303 with_settings :default_language => "fr" do
329 with_settings :default_language => "fr" do
304 str1 = "test_csv_fr"
330 str1 = "test_csv_fr"
305 user = User.find_by_id(3)
331 user = User.find_by_id(3)
306 te1 = TimeEntry.create(:spent_on => '2011-11-11',
332 te1 = TimeEntry.create(:spent_on => '2011-11-11',
307 :hours => 7.3,
333 :hours => 7.3,
308 :project => Project.find(1),
334 :project => Project.find(1),
309 :user => user,
335 :user => user,
310 :activity => TimeEntryActivity.find_by_name('Design'),
336 :activity => TimeEntryActivity.find_by_name('Design'),
311 :comments => str1)
337 :comments => str1)
312
338
313 te2 = TimeEntry.find_by_comments(str1)
339 te2 = TimeEntry.find_by_comments(str1)
314 assert_not_nil te2
340 assert_not_nil te2
315 assert_equal 7.3, te2.hours
341 assert_equal 7.3, te2.hours
316 assert_equal 3, te2.user_id
342 assert_equal 3, te2.user_id
317
343
318 get :report, :project_id => 1, :columns => 'day',
344 get :report, :project_id => 1, :columns => 'day',
319 :from => "2011-11-11", :to => "2011-11-11",
345 :from => "2011-11-11", :to => "2011-11-11",
320 :criteria => ["user"], :format => "csv"
346 :criteria => ["user"], :format => "csv"
321 assert_response :success
347 assert_response :success
322 assert_equal 'text/csv; header=present', @response.content_type
348 assert_equal 'text/csv; header=present', @response.content_type
323 lines = @response.body.chomp.split("\n")
349 lines = @response.body.chomp.split("\n")
324 # Headers
350 # Headers
325 s1 = "Utilisateur;2011-11-11;Total"
351 s1 = "Utilisateur;2011-11-11;Total"
326 s2 = "Total"
352 s2 = "Total"
327 if s1.respond_to?(:force_encoding)
353 if s1.respond_to?(:force_encoding)
328 s1.force_encoding('ISO-8859-1')
354 s1.force_encoding('ISO-8859-1')
329 s2.force_encoding('ISO-8859-1')
355 s2.force_encoding('ISO-8859-1')
330 end
356 end
331 assert_equal s1, lines.first
357 assert_equal s1, lines.first
332 # Total row
358 # Total row
333 assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
359 assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
334 assert_equal "#{s2};7,30;7,30", lines[2]
360 assert_equal "#{s2};7,30;7,30", lines[2]
335
361
336 str_fr = "Fran\xc3\xa7ais"
362 str_fr = "Fran\xc3\xa7ais"
337 if str_fr.respond_to?(:force_encoding)
363 if str_fr.respond_to?(:force_encoding)
338 str_fr.force_encoding('UTF-8')
364 str_fr.force_encoding('UTF-8')
339 end
365 end
340 assert_equal str_fr, l(:general_lang_name)
366 assert_equal str_fr, l(:general_lang_name)
341 assert_equal 'ISO-8859-1', l(:general_csv_encoding)
367 assert_equal 'ISO-8859-1', l(:general_csv_encoding)
342 assert_equal ';', l(:general_csv_separator)
368 assert_equal ';', l(:general_csv_separator)
343 assert_equal ',', l(:general_csv_decimal_separator)
369 assert_equal ',', l(:general_csv_decimal_separator)
344 end
370 end
345 end
371 end
346 end
372 end
General Comments 0
You need to be logged in to leave comments. Login now