##// END OF EJS Templates
Fixed that time report raises a SQL error if there are multiple CustomValue for a time entry (#11160)....
Jean-Philippe Lang -
r9648:76eeb64d1aa8
parent child
Show More
@@ -1,164 +1,164
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 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, :from, :to, :hours, :total_hours, :periods
21 attr_reader :criteria, :columns, :from, :to, :hours, :total_hours, :periods
22
22
23 def initialize(project, issue, criteria, columns, from, to)
23 def initialize(project, issue, criteria, columns, from, to)
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 @from = from
33 @from = from
34 @to = to
34 @to = to
35
35
36 run
36 run
37 end
37 end
38
38
39 def available_criteria
39 def available_criteria
40 @available_criteria || load_available_criteria
40 @available_criteria || load_available_criteria
41 end
41 end
42
42
43 private
43 private
44
44
45 def run
45 def run
46 unless @criteria.empty?
46 unless @criteria.empty?
47 scope = TimeEntry.visible.spent_between(@from, @to)
47 scope = TimeEntry.visible.spent_between(@from, @to)
48 if @issue
48 if @issue
49 scope = scope.on_issue(@issue)
49 scope = scope.on_issue(@issue)
50 elsif @project
50 elsif @project
51 scope = scope.on_project(@project, Setting.display_subprojects_issues?)
51 scope = scope.on_project(@project, Setting.display_subprojects_issues?)
52 end
52 end
53 time_columns = %w(tyear tmonth tweek spent_on)
53 time_columns = %w(tyear tmonth tweek spent_on)
54 @hours = []
54 @hours = []
55 scope.sum(:hours, :include => :issue, :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns).each do |hash, hours|
55 scope.sum(:hours, :include => :issue, :group => @criteria.collect{|criteria| @available_criteria[criteria][:sql]} + time_columns).each do |hash, hours|
56 h = {'hours' => hours}
56 h = {'hours' => hours}
57 (@criteria + time_columns).each_with_index do |name, i|
57 (@criteria + time_columns).each_with_index do |name, i|
58 h[name] = hash[i]
58 h[name] = hash[i]
59 end
59 end
60 @hours << h
60 @hours << h
61 end
61 end
62
62
63 @hours.each do |row|
63 @hours.each do |row|
64 case @columns
64 case @columns
65 when 'year'
65 when 'year'
66 row['year'] = row['tyear']
66 row['year'] = row['tyear']
67 when 'month'
67 when 'month'
68 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
68 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
69 when 'week'
69 when 'week'
70 row['week'] = "#{row['tyear']}-#{row['tweek']}"
70 row['week'] = "#{row['tyear']}-#{row['tweek']}"
71 when 'day'
71 when 'day'
72 row['day'] = "#{row['spent_on']}"
72 row['day'] = "#{row['spent_on']}"
73 end
73 end
74 end
74 end
75
75
76 if @from.nil?
76 if @from.nil?
77 min = @hours.collect {|row| row['spent_on']}.min
77 min = @hours.collect {|row| row['spent_on']}.min
78 @from = min ? min.to_date : Date.today
78 @from = min ? min.to_date : Date.today
79 end
79 end
80
80
81 if @to.nil?
81 if @to.nil?
82 max = @hours.collect {|row| row['spent_on']}.max
82 max = @hours.collect {|row| row['spent_on']}.max
83 @to = max ? max.to_date : Date.today
83 @to = max ? max.to_date : Date.today
84 end
84 end
85
85
86 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
86 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
87
87
88 @periods = []
88 @periods = []
89 # Date#at_beginning_of_ not supported in Rails 1.2.x
89 # Date#at_beginning_of_ not supported in Rails 1.2.x
90 date_from = @from.to_time
90 date_from = @from.to_time
91 # 100 columns max
91 # 100 columns max
92 while date_from <= @to.to_time && @periods.length < 100
92 while date_from <= @to.to_time && @periods.length < 100
93 case @columns
93 case @columns
94 when 'year'
94 when 'year'
95 @periods << "#{date_from.year}"
95 @periods << "#{date_from.year}"
96 date_from = (date_from + 1.year).at_beginning_of_year
96 date_from = (date_from + 1.year).at_beginning_of_year
97 when 'month'
97 when 'month'
98 @periods << "#{date_from.year}-#{date_from.month}"
98 @periods << "#{date_from.year}-#{date_from.month}"
99 date_from = (date_from + 1.month).at_beginning_of_month
99 date_from = (date_from + 1.month).at_beginning_of_month
100 when 'week'
100 when 'week'
101 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
101 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
102 date_from = (date_from + 7.day).at_beginning_of_week
102 date_from = (date_from + 7.day).at_beginning_of_week
103 when 'day'
103 when 'day'
104 @periods << "#{date_from.to_date}"
104 @periods << "#{date_from.to_date}"
105 date_from = date_from + 1.day
105 date_from = date_from + 1.day
106 end
106 end
107 end
107 end
108 end
108 end
109 end
109 end
110
110
111 def load_available_criteria
111 def load_available_criteria
112 @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
112 @available_criteria = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
113 :klass => Project,
113 :klass => Project,
114 :label => :label_project},
114 :label => :label_project},
115 'status' => {:sql => "#{Issue.table_name}.status_id",
115 'status' => {:sql => "#{Issue.table_name}.status_id",
116 :klass => IssueStatus,
116 :klass => IssueStatus,
117 :label => :field_status},
117 :label => :field_status},
118 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
118 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
119 :klass => Version,
119 :klass => Version,
120 :label => :label_version},
120 :label => :label_version},
121 'category' => {:sql => "#{Issue.table_name}.category_id",
121 'category' => {:sql => "#{Issue.table_name}.category_id",
122 :klass => IssueCategory,
122 :klass => IssueCategory,
123 :label => :field_category},
123 :label => :field_category},
124 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
124 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
125 :klass => User,
125 :klass => User,
126 :label => :label_member},
126 :label => :label_member},
127 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
127 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
128 :klass => Tracker,
128 :klass => Tracker,
129 :label => :label_tracker},
129 :label => :label_tracker},
130 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
130 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
131 :klass => TimeEntryActivity,
131 :klass => TimeEntryActivity,
132 :label => :label_activity},
132 :label => :label_activity},
133 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
133 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
134 :klass => Issue,
134 :klass => Issue,
135 :label => :label_issue}
135 :label => :label_issue}
136 }
136 }
137
137
138 # Add list and boolean custom fields as available criteria
138 # Add list and boolean custom fields as available criteria
139 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
139 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
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 => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)",
141 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id ORDER BY c.value LIMIT 1)",
142 :format => cf.field_format,
142 :format => cf.field_format,
143 :label => cf.name}
143 :label => cf.name}
144 end if @project
144 end if @project
145
145
146 # Add list and boolean time entry custom fields
146 # Add list and boolean time entry custom fields
147 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
147 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
148 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)",
148 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id ORDER BY c.value LIMIT 1)",
149 :format => cf.field_format,
149 :format => cf.field_format,
150 :label => cf.name}
150 :label => cf.name}
151 end
151 end
152
152
153 # Add list and boolean time entry activity custom fields
153 # Add list and boolean time entry activity custom fields
154 TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
154 TimeEntryActivityCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
155 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Enumeration' AND c.customized_id = #{TimeEntry.table_name}.activity_id)",
155 @available_criteria["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Enumeration' AND c.customized_id = #{TimeEntry.table_name}.activity_id ORDER BY c.value LIMIT 1)",
156 :format => cf.field_format,
156 :format => cf.field_format,
157 :label => cf.name}
157 :label => cf.name}
158 end
158 end
159
159
160 @available_criteria
160 @available_criteria
161 end
161 end
162 end
162 end
163 end
163 end
164 end
164 end
@@ -1,307 +1,317
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 require File.expand_path('../../test_helper', __FILE__)
2 require File.expand_path('../../test_helper', __FILE__)
3
3
4 class TimeEntryReportsControllerTest < ActionController::TestCase
4 class TimeEntryReportsControllerTest < ActionController::TestCase
5 tests TimelogController
5 tests TimelogController
6
6
7 fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
7 fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
8 :issues, :time_entries, :users, :trackers, :enumerations,
8 :issues, :time_entries, :users, :trackers, :enumerations,
9 :issue_statuses, :custom_fields, :custom_values
9 :issue_statuses, :custom_fields, :custom_values
10
10
11 include Redmine::I18n
11 include Redmine::I18n
12
12
13 def setup
13 def setup
14 Setting.default_language = "en"
14 Setting.default_language = "en"
15 end
15 end
16
16
17 def test_report_at_project_level
17 def test_report_at_project_level
18 get :report, :project_id => 'ecookbook'
18 get :report, :project_id => 'ecookbook'
19 assert_response :success
19 assert_response :success
20 assert_template 'report'
20 assert_template 'report'
21 assert_tag :form,
21 assert_tag :form,
22 :attributes => {:action => "/projects/ecookbook/time_entries/report", :id => 'query_form'}
22 :attributes => {:action => "/projects/ecookbook/time_entries/report", :id => 'query_form'}
23 end
23 end
24
24
25 def test_report_all_projects
25 def test_report_all_projects
26 get :report
26 get :report
27 assert_response :success
27 assert_response :success
28 assert_template 'report'
28 assert_template 'report'
29 assert_tag :form,
29 assert_tag :form,
30 :attributes => {:action => "/time_entries/report", :id => 'query_form'}
30 :attributes => {:action => "/time_entries/report", :id => 'query_form'}
31 end
31 end
32
32
33 def test_report_all_projects_denied
33 def test_report_all_projects_denied
34 r = Role.anonymous
34 r = Role.anonymous
35 r.permissions.delete(:view_time_entries)
35 r.permissions.delete(:view_time_entries)
36 r.permissions_will_change!
36 r.permissions_will_change!
37 r.save
37 r.save
38 get :report
38 get :report
39 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
39 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
40 end
40 end
41
41
42 def test_report_all_projects_one_criteria
42 def test_report_all_projects_one_criteria
43 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
43 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
44 assert_response :success
44 assert_response :success
45 assert_template 'report'
45 assert_template 'report'
46 assert_not_nil assigns(:report)
46 assert_not_nil assigns(:report)
47 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
47 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
48 end
48 end
49
49
50 def test_report_all_time
50 def test_report_all_time
51 get :report, :project_id => 1, :criteria => ['project', 'issue']
51 get :report, :project_id => 1, :criteria => ['project', 'issue']
52 assert_response :success
52 assert_response :success
53 assert_template 'report'
53 assert_template 'report'
54 assert_not_nil assigns(:report)
54 assert_not_nil assigns(:report)
55 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
55 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
56 end
56 end
57
57
58 def test_report_all_time_by_day
58 def test_report_all_time_by_day
59 get :report, :project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'
59 get :report, :project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'
60 assert_response :success
60 assert_response :success
61 assert_template 'report'
61 assert_template 'report'
62 assert_not_nil assigns(:report)
62 assert_not_nil assigns(:report)
63 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
63 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
64 assert_tag :tag => 'th', :content => '2007-03-12'
64 assert_tag :tag => 'th', :content => '2007-03-12'
65 end
65 end
66
66
67 def test_report_one_criteria
67 def test_report_one_criteria
68 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
68 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
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 "8.65", "%.2f" % assigns(:report).total_hours
72 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
73 end
73 end
74
74
75 def test_report_two_criteria
75 def test_report_two_criteria
76 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
76 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
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 end
81 end
82
82
83 def test_report_custom_field_criteria_with_multiple_values
84 field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2'])
85 entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today)
86 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1')
87 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2')
88
89 get :report, :project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"]
90 assert_response :success
91 end
92
83 def test_report_one_day
93 def test_report_one_day
84 get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["member", "activity"]
94 get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["member", "activity"]
85 assert_response :success
95 assert_response :success
86 assert_template 'report'
96 assert_template 'report'
87 assert_not_nil assigns(:report)
97 assert_not_nil assigns(:report)
88 assert_equal "4.25", "%.2f" % assigns(:report).total_hours
98 assert_equal "4.25", "%.2f" % assigns(:report).total_hours
89 end
99 end
90
100
91 def test_report_at_issue_level
101 def test_report_at_issue_level
92 get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
102 get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"]
93 assert_response :success
103 assert_response :success
94 assert_template 'report'
104 assert_template 'report'
95 assert_not_nil assigns(:report)
105 assert_not_nil assigns(:report)
96 assert_equal "154.25", "%.2f" % assigns(:report).total_hours
106 assert_equal "154.25", "%.2f" % assigns(:report).total_hours
97 assert_tag :form,
107 assert_tag :form,
98 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'}
108 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'}
99 end
109 end
100
110
101 def test_report_custom_field_criteria
111 def test_report_custom_field_criteria
102 get :report, :project_id => 1, :criteria => ['project', 'cf_1', 'cf_7']
112 get :report, :project_id => 1, :criteria => ['project', 'cf_1', 'cf_7']
103 assert_response :success
113 assert_response :success
104 assert_template 'report'
114 assert_template 'report'
105 assert_not_nil assigns(:report)
115 assert_not_nil assigns(:report)
106 assert_equal 3, assigns(:report).criteria.size
116 assert_equal 3, assigns(:report).criteria.size
107 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
117 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
108 # Custom field column
118 # Custom field column
109 assert_tag :tag => 'th', :content => 'Database'
119 assert_tag :tag => 'th', :content => 'Database'
110 # Custom field row
120 # Custom field row
111 assert_tag :tag => 'td', :content => 'MySQL',
121 assert_tag :tag => 'td', :content => 'MySQL',
112 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
122 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
113 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
123 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
114 :content => '1' }}
124 :content => '1' }}
115 # Second custom field column
125 # Second custom field column
116 assert_tag :tag => 'th', :content => 'Billable'
126 assert_tag :tag => 'th', :content => 'Billable'
117 end
127 end
118
128
119 def test_report_one_criteria_no_result
129 def test_report_one_criteria_no_result
120 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
130 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
121 assert_response :success
131 assert_response :success
122 assert_template 'report'
132 assert_template 'report'
123 assert_not_nil assigns(:report)
133 assert_not_nil assigns(:report)
124 assert_equal "0.00", "%.2f" % assigns(:report).total_hours
134 assert_equal "0.00", "%.2f" % assigns(:report).total_hours
125 end
135 end
126
136
127 def test_report_status_criterion
137 def test_report_status_criterion
128 get :report, :project_id => 1, :criteria => ['status']
138 get :report, :project_id => 1, :criteria => ['status']
129 assert_response :success
139 assert_response :success
130 assert_template 'report'
140 assert_template 'report'
131 assert_tag :tag => 'th', :content => 'Status'
141 assert_tag :tag => 'th', :content => 'Status'
132 assert_tag :tag => 'td', :content => 'New'
142 assert_tag :tag => 'td', :content => 'New'
133 end
143 end
134
144
135 def test_report_all_projects_csv_export
145 def test_report_all_projects_csv_export
136 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30",
146 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30",
137 :criteria => ["project", "member", "activity"], :format => "csv"
147 :criteria => ["project", "member", "activity"], :format => "csv"
138 assert_response :success
148 assert_response :success
139 assert_equal 'text/csv; header=present', @response.content_type
149 assert_equal 'text/csv; header=present', @response.content_type
140 lines = @response.body.chomp.split("\n")
150 lines = @response.body.chomp.split("\n")
141 # Headers
151 # Headers
142 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total',
152 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total',
143 lines.first
153 lines.first
144 # Total row
154 # Total row
145 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
155 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
146 end
156 end
147
157
148 def test_report_csv_export
158 def test_report_csv_export
149 get :report, :project_id => 1, :columns => 'month',
159 get :report, :project_id => 1, :columns => 'month',
150 :from => "2007-01-01", :to => "2007-06-30",
160 :from => "2007-01-01", :to => "2007-06-30",
151 :criteria => ["project", "member", "activity"], :format => "csv"
161 :criteria => ["project", "member", "activity"], :format => "csv"
152 assert_response :success
162 assert_response :success
153 assert_equal 'text/csv; header=present', @response.content_type
163 assert_equal 'text/csv; header=present', @response.content_type
154 lines = @response.body.chomp.split("\n")
164 lines = @response.body.chomp.split("\n")
155 # Headers
165 # Headers
156 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total',
166 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total',
157 lines.first
167 lines.first
158 # Total row
168 # Total row
159 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
169 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
160 end
170 end
161
171
162 def test_csv_big_5
172 def test_csv_big_5
163 Setting.default_language = "zh-TW"
173 Setting.default_language = "zh-TW"
164 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
174 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
165 str_big5 = "\xa4@\xa4\xeb"
175 str_big5 = "\xa4@\xa4\xeb"
166 if str_utf8.respond_to?(:force_encoding)
176 if str_utf8.respond_to?(:force_encoding)
167 str_utf8.force_encoding('UTF-8')
177 str_utf8.force_encoding('UTF-8')
168 str_big5.force_encoding('Big5')
178 str_big5.force_encoding('Big5')
169 end
179 end
170 user = User.find_by_id(3)
180 user = User.find_by_id(3)
171 user.firstname = str_utf8
181 user.firstname = str_utf8
172 user.lastname = "test-lastname"
182 user.lastname = "test-lastname"
173 assert user.save
183 assert user.save
174 comments = "test_csv_big_5"
184 comments = "test_csv_big_5"
175 te1 = TimeEntry.create(:spent_on => '2011-11-11',
185 te1 = TimeEntry.create(:spent_on => '2011-11-11',
176 :hours => 7.3,
186 :hours => 7.3,
177 :project => Project.find(1),
187 :project => Project.find(1),
178 :user => user,
188 :user => user,
179 :activity => TimeEntryActivity.find_by_name('Design'),
189 :activity => TimeEntryActivity.find_by_name('Design'),
180 :comments => comments)
190 :comments => comments)
181
191
182 te2 = TimeEntry.find_by_comments(comments)
192 te2 = TimeEntry.find_by_comments(comments)
183 assert_not_nil te2
193 assert_not_nil te2
184 assert_equal 7.3, te2.hours
194 assert_equal 7.3, te2.hours
185 assert_equal 3, te2.user_id
195 assert_equal 3, te2.user_id
186
196
187 get :report, :project_id => 1, :columns => 'day',
197 get :report, :project_id => 1, :columns => 'day',
188 :from => "2011-11-11", :to => "2011-11-11",
198 :from => "2011-11-11", :to => "2011-11-11",
189 :criteria => ["member"], :format => "csv"
199 :criteria => ["member"], :format => "csv"
190 assert_response :success
200 assert_response :success
191 assert_equal 'text/csv; header=present', @response.content_type
201 assert_equal 'text/csv; header=present', @response.content_type
192 lines = @response.body.chomp.split("\n")
202 lines = @response.body.chomp.split("\n")
193 # Headers
203 # Headers
194 s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp"
204 s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp"
195 s2 = "\xc1`\xadp"
205 s2 = "\xc1`\xadp"
196 if s1.respond_to?(:force_encoding)
206 if s1.respond_to?(:force_encoding)
197 s1.force_encoding('Big5')
207 s1.force_encoding('Big5')
198 s2.force_encoding('Big5')
208 s2.force_encoding('Big5')
199 end
209 end
200 assert_equal s1, lines.first
210 assert_equal s1, lines.first
201 # Total row
211 # Total row
202 assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
212 assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
203 assert_equal "#{s2},7.30,7.30", lines[2]
213 assert_equal "#{s2},7.30,7.30", lines[2]
204
214
205 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
215 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
206 if str_tw.respond_to?(:force_encoding)
216 if str_tw.respond_to?(:force_encoding)
207 str_tw.force_encoding('UTF-8')
217 str_tw.force_encoding('UTF-8')
208 end
218 end
209 assert_equal str_tw, l(:general_lang_name)
219 assert_equal str_tw, l(:general_lang_name)
210 assert_equal 'Big5', l(:general_csv_encoding)
220 assert_equal 'Big5', l(:general_csv_encoding)
211 assert_equal ',', l(:general_csv_separator)
221 assert_equal ',', l(:general_csv_separator)
212 assert_equal '.', l(:general_csv_decimal_separator)
222 assert_equal '.', l(:general_csv_decimal_separator)
213 end
223 end
214
224
215 def test_csv_cannot_convert_should_be_replaced_big_5
225 def test_csv_cannot_convert_should_be_replaced_big_5
216 Setting.default_language = "zh-TW"
226 Setting.default_language = "zh-TW"
217 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
227 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
218 if str_utf8.respond_to?(:force_encoding)
228 if str_utf8.respond_to?(:force_encoding)
219 str_utf8.force_encoding('UTF-8')
229 str_utf8.force_encoding('UTF-8')
220 end
230 end
221 user = User.find_by_id(3)
231 user = User.find_by_id(3)
222 user.firstname = str_utf8
232 user.firstname = str_utf8
223 user.lastname = "test-lastname"
233 user.lastname = "test-lastname"
224 assert user.save
234 assert user.save
225 comments = "test_replaced"
235 comments = "test_replaced"
226 te1 = TimeEntry.create(:spent_on => '2011-11-11',
236 te1 = TimeEntry.create(:spent_on => '2011-11-11',
227 :hours => 7.3,
237 :hours => 7.3,
228 :project => Project.find(1),
238 :project => Project.find(1),
229 :user => user,
239 :user => user,
230 :activity => TimeEntryActivity.find_by_name('Design'),
240 :activity => TimeEntryActivity.find_by_name('Design'),
231 :comments => comments)
241 :comments => comments)
232
242
233 te2 = TimeEntry.find_by_comments(comments)
243 te2 = TimeEntry.find_by_comments(comments)
234 assert_not_nil te2
244 assert_not_nil te2
235 assert_equal 7.3, te2.hours
245 assert_equal 7.3, te2.hours
236 assert_equal 3, te2.user_id
246 assert_equal 3, te2.user_id
237
247
238 get :report, :project_id => 1, :columns => 'day',
248 get :report, :project_id => 1, :columns => 'day',
239 :from => "2011-11-11", :to => "2011-11-11",
249 :from => "2011-11-11", :to => "2011-11-11",
240 :criteria => ["member"], :format => "csv"
250 :criteria => ["member"], :format => "csv"
241 assert_response :success
251 assert_response :success
242 assert_equal 'text/csv; header=present', @response.content_type
252 assert_equal 'text/csv; header=present', @response.content_type
243 lines = @response.body.chomp.split("\n")
253 lines = @response.body.chomp.split("\n")
244 # Headers
254 # Headers
245 s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp"
255 s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp"
246 if s1.respond_to?(:force_encoding)
256 if s1.respond_to?(:force_encoding)
247 s1.force_encoding('Big5')
257 s1.force_encoding('Big5')
248 end
258 end
249 assert_equal s1, lines.first
259 assert_equal s1, lines.first
250 # Total row
260 # Total row
251 s2 = ""
261 s2 = ""
252 if s2.respond_to?(:force_encoding)
262 if s2.respond_to?(:force_encoding)
253 s2 = "\xa5H?"
263 s2 = "\xa5H?"
254 s2.force_encoding('Big5')
264 s2.force_encoding('Big5')
255 elsif RUBY_PLATFORM == 'java'
265 elsif RUBY_PLATFORM == 'java'
256 s2 = "??"
266 s2 = "??"
257 else
267 else
258 s2 = "\xa5H???"
268 s2 = "\xa5H???"
259 end
269 end
260 assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
270 assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
261 end
271 end
262
272
263 def test_csv_fr
273 def test_csv_fr
264 with_settings :default_language => "fr" do
274 with_settings :default_language => "fr" do
265 str1 = "test_csv_fr"
275 str1 = "test_csv_fr"
266 user = User.find_by_id(3)
276 user = User.find_by_id(3)
267 te1 = TimeEntry.create(:spent_on => '2011-11-11',
277 te1 = TimeEntry.create(:spent_on => '2011-11-11',
268 :hours => 7.3,
278 :hours => 7.3,
269 :project => Project.find(1),
279 :project => Project.find(1),
270 :user => user,
280 :user => user,
271 :activity => TimeEntryActivity.find_by_name('Design'),
281 :activity => TimeEntryActivity.find_by_name('Design'),
272 :comments => str1)
282 :comments => str1)
273
283
274 te2 = TimeEntry.find_by_comments(str1)
284 te2 = TimeEntry.find_by_comments(str1)
275 assert_not_nil te2
285 assert_not_nil te2
276 assert_equal 7.3, te2.hours
286 assert_equal 7.3, te2.hours
277 assert_equal 3, te2.user_id
287 assert_equal 3, te2.user_id
278
288
279 get :report, :project_id => 1, :columns => 'day',
289 get :report, :project_id => 1, :columns => 'day',
280 :from => "2011-11-11", :to => "2011-11-11",
290 :from => "2011-11-11", :to => "2011-11-11",
281 :criteria => ["member"], :format => "csv"
291 :criteria => ["member"], :format => "csv"
282 assert_response :success
292 assert_response :success
283 assert_equal 'text/csv; header=present', @response.content_type
293 assert_equal 'text/csv; header=present', @response.content_type
284 lines = @response.body.chomp.split("\n")
294 lines = @response.body.chomp.split("\n")
285 # Headers
295 # Headers
286 s1 = "Membre;2011-11-11;Total"
296 s1 = "Membre;2011-11-11;Total"
287 s2 = "Total"
297 s2 = "Total"
288 if s1.respond_to?(:force_encoding)
298 if s1.respond_to?(:force_encoding)
289 s1.force_encoding('ISO-8859-1')
299 s1.force_encoding('ISO-8859-1')
290 s2.force_encoding('ISO-8859-1')
300 s2.force_encoding('ISO-8859-1')
291 end
301 end
292 assert_equal s1, lines.first
302 assert_equal s1, lines.first
293 # Total row
303 # Total row
294 assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
304 assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
295 assert_equal "#{s2};7,30;7,30", lines[2]
305 assert_equal "#{s2};7,30;7,30", lines[2]
296
306
297 str_fr = "Fran\xc3\xa7ais"
307 str_fr = "Fran\xc3\xa7ais"
298 if str_fr.respond_to?(:force_encoding)
308 if str_fr.respond_to?(:force_encoding)
299 str_fr.force_encoding('UTF-8')
309 str_fr.force_encoding('UTF-8')
300 end
310 end
301 assert_equal str_fr, l(:general_lang_name)
311 assert_equal str_fr, l(:general_lang_name)
302 assert_equal 'ISO-8859-1', l(:general_csv_encoding)
312 assert_equal 'ISO-8859-1', l(:general_csv_encoding)
303 assert_equal ';', l(:general_csv_separator)
313 assert_equal ';', l(:general_csv_separator)
304 assert_equal ',', l(:general_csv_decimal_separator)
314 assert_equal ',', l(:general_csv_decimal_separator)
305 end
315 end
306 end
316 end
307 end
317 end
General Comments 0
You need to be logged in to leave comments. Login now