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