##// END OF EJS Templates
fix malformed time log csv encoding in case of unable to convert (#8549)...
Toshi MARUYAMA -
r7699:4b5d50e40a49
parent child
Show More
@@ -1,190 +1,193
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 TimelogHelper
18 module TimelogHelper
19 include ApplicationHelper
19 include ApplicationHelper
20
20
21 def render_timelog_breadcrumb
21 def render_timelog_breadcrumb
22 links = []
22 links = []
23 links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil})
23 links << link_to(l(:label_project_all), {:project_id => nil, :issue_id => nil})
24 links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project
24 links << link_to(h(@project), {:project_id => @project, :issue_id => nil}) if @project
25 if @issue
25 if @issue
26 if @issue.visible?
26 if @issue.visible?
27 links << link_to_issue(@issue, :subject => false)
27 links << link_to_issue(@issue, :subject => false)
28 else
28 else
29 links << "##{@issue.id}"
29 links << "##{@issue.id}"
30 end
30 end
31 end
31 end
32 breadcrumb links
32 breadcrumb links
33 end
33 end
34
34
35 # Returns a collection of activities for a select field. time_entry
35 # Returns a collection of activities for a select field. time_entry
36 # is optional and will be used to check if the selected TimeEntryActivity
36 # is optional and will be used to check if the selected TimeEntryActivity
37 # is active.
37 # is active.
38 def activity_collection_for_select_options(time_entry=nil, project=nil)
38 def activity_collection_for_select_options(time_entry=nil, project=nil)
39 project ||= @project
39 project ||= @project
40 if project.nil?
40 if project.nil?
41 activities = TimeEntryActivity.shared.active
41 activities = TimeEntryActivity.shared.active
42 else
42 else
43 activities = project.activities
43 activities = project.activities
44 end
44 end
45
45
46 collection = []
46 collection = []
47 if time_entry && time_entry.activity && !time_entry.activity.active?
47 if time_entry && time_entry.activity && !time_entry.activity.active?
48 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ]
48 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ]
49 else
49 else
50 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
50 collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
51 end
51 end
52 activities.each { |a| collection << [a.name, a.id] }
52 activities.each { |a| collection << [a.name, a.id] }
53 collection
53 collection
54 end
54 end
55
55
56 def select_hours(data, criteria, value)
56 def select_hours(data, criteria, value)
57 if value.to_s.empty?
57 if value.to_s.empty?
58 data.select {|row| row[criteria].blank? }
58 data.select {|row| row[criteria].blank? }
59 else
59 else
60 data.select {|row| row[criteria].to_s == value.to_s}
60 data.select {|row| row[criteria].to_s == value.to_s}
61 end
61 end
62 end
62 end
63
63
64 def sum_hours(data)
64 def sum_hours(data)
65 sum = 0
65 sum = 0
66 data.each do |row|
66 data.each do |row|
67 sum += row['hours'].to_f
67 sum += row['hours'].to_f
68 end
68 end
69 sum
69 sum
70 end
70 end
71
71
72 def options_for_period_select(value)
72 def options_for_period_select(value)
73 options_for_select([[l(:label_all_time), 'all'],
73 options_for_select([[l(:label_all_time), 'all'],
74 [l(:label_today), 'today'],
74 [l(:label_today), 'today'],
75 [l(:label_yesterday), 'yesterday'],
75 [l(:label_yesterday), 'yesterday'],
76 [l(:label_this_week), 'current_week'],
76 [l(:label_this_week), 'current_week'],
77 [l(:label_last_week), 'last_week'],
77 [l(:label_last_week), 'last_week'],
78 [l(:label_last_n_days, 7), '7_days'],
78 [l(:label_last_n_days, 7), '7_days'],
79 [l(:label_this_month), 'current_month'],
79 [l(:label_this_month), 'current_month'],
80 [l(:label_last_month), 'last_month'],
80 [l(:label_last_month), 'last_month'],
81 [l(:label_last_n_days, 30), '30_days'],
81 [l(:label_last_n_days, 30), '30_days'],
82 [l(:label_this_year), 'current_year']],
82 [l(:label_this_year), 'current_year']],
83 value)
83 value)
84 end
84 end
85
85
86 def entries_to_csv(entries)
86 def entries_to_csv(entries)
87 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
88 decimal_separator = l(:general_csv_decimal_separator)
87 decimal_separator = l(:general_csv_decimal_separator)
89 custom_fields = TimeEntryCustomField.find(:all)
88 custom_fields = TimeEntryCustomField.find(:all)
90 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
89 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
91 # csv header fields
90 # csv header fields
92 headers = [l(:field_spent_on),
91 headers = [l(:field_spent_on),
93 l(:field_user),
92 l(:field_user),
94 l(:field_activity),
93 l(:field_activity),
95 l(:field_project),
94 l(:field_project),
96 l(:field_issue),
95 l(:field_issue),
97 l(:field_tracker),
96 l(:field_tracker),
98 l(:field_subject),
97 l(:field_subject),
99 l(:field_hours),
98 l(:field_hours),
100 l(:field_comments)
99 l(:field_comments)
101 ]
100 ]
102 # Export custom fields
101 # Export custom fields
103 headers += custom_fields.collect(&:name)
102 headers += custom_fields.collect(&:name)
104
103
105 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
104 csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
105 c.to_s,
106 l(:general_csv_encoding) ) }
106 # csv lines
107 # csv lines
107 entries.each do |entry|
108 entries.each do |entry|
108 fields = [format_date(entry.spent_on),
109 fields = [format_date(entry.spent_on),
109 entry.user,
110 entry.user,
110 entry.activity,
111 entry.activity,
111 entry.project,
112 entry.project,
112 (entry.issue ? entry.issue.id : nil),
113 (entry.issue ? entry.issue.id : nil),
113 (entry.issue ? entry.issue.tracker : nil),
114 (entry.issue ? entry.issue.tracker : nil),
114 (entry.issue ? entry.issue.subject : nil),
115 (entry.issue ? entry.issue.subject : nil),
115 entry.hours.to_s.gsub('.', decimal_separator),
116 entry.hours.to_s.gsub('.', decimal_separator),
116 entry.comments
117 entry.comments
117 ]
118 ]
118 fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) }
119 fields += custom_fields.collect {|f| show_value(entry.custom_value_for(f)) }
119
120
120 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
121 csv << fields.collect {|c| Redmine::CodesetUtil.from_utf8(
122 c.to_s,
123 l(:general_csv_encoding) ) }
121 end
124 end
122 end
125 end
123 export
126 export
124 end
127 end
125
128
126 def format_criteria_value(criteria, value)
129 def format_criteria_value(criteria, value)
127 if value.blank?
130 if value.blank?
128 l(:label_none)
131 l(:label_none)
129 elsif k = @available_criterias[criteria][:klass]
132 elsif k = @available_criterias[criteria][:klass]
130 obj = k.find_by_id(value.to_i)
133 obj = k.find_by_id(value.to_i)
131 if obj.is_a?(Issue)
134 if obj.is_a?(Issue)
132 obj.visible? ? h("#{obj.tracker} ##{obj.id}: #{obj.subject}") : h("##{obj.id}")
135 obj.visible? ? h("#{obj.tracker} ##{obj.id}: #{obj.subject}") : h("##{obj.id}")
133 else
136 else
134 obj
137 obj
135 end
138 end
136 else
139 else
137 format_value(value, @available_criterias[criteria][:format])
140 format_value(value, @available_criterias[criteria][:format])
138 end
141 end
139 end
142 end
140
143
141 def report_to_csv(criterias, periods, hours)
144 def report_to_csv(criterias, periods, hours)
142 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
145 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
143 # Column headers
146 # Column headers
144 headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
147 headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
145 headers += periods
148 headers += periods
146 headers << l(:label_total)
149 headers << l(:label_total)
147 csv << headers.collect {|c| to_utf8(c) }
150 csv << headers.collect {|c| to_utf8(c) }
148 # Content
151 # Content
149 report_criteria_to_csv(csv, criterias, periods, hours)
152 report_criteria_to_csv(csv, criterias, periods, hours)
150 # Total row
153 # Total row
151 row = [ l(:label_total) ] + [''] * (criterias.size - 1)
154 row = [ l(:label_total) ] + [''] * (criterias.size - 1)
152 total = 0
155 total = 0
153 periods.each do |period|
156 periods.each do |period|
154 sum = sum_hours(select_hours(hours, @columns, period.to_s))
157 sum = sum_hours(select_hours(hours, @columns, period.to_s))
155 total += sum
158 total += sum
156 row << (sum > 0 ? "%.2f" % sum : '')
159 row << (sum > 0 ? "%.2f" % sum : '')
157 end
160 end
158 row << "%.2f" %total
161 row << "%.2f" %total
159 csv << row
162 csv << row
160 end
163 end
161 export
164 export
162 end
165 end
163
166
164 def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
167 def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
165 hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value|
168 hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value|
166 hours_for_value = select_hours(hours, criterias[level], value)
169 hours_for_value = select_hours(hours, criterias[level], value)
167 next if hours_for_value.empty?
170 next if hours_for_value.empty?
168 row = [''] * level
171 row = [''] * level
169 row << to_utf8(format_criteria_value(criterias[level], value))
172 row << to_utf8(format_criteria_value(criterias[level], value))
170 row += [''] * (criterias.length - level - 1)
173 row += [''] * (criterias.length - level - 1)
171 total = 0
174 total = 0
172 periods.each do |period|
175 periods.each do |period|
173 sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
176 sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
174 total += sum
177 total += sum
175 row << (sum > 0 ? "%.2f" % sum : '')
178 row << (sum > 0 ? "%.2f" % sum : '')
176 end
179 end
177 row << "%.2f" %total
180 row << "%.2f" %total
178 csv << row
181 csv << row
179
182
180 if criterias.length > level + 1
183 if criterias.length > level + 1
181 report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
184 report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
182 end
185 end
183 end
186 end
184 end
187 end
185
188
186 def to_utf8(s)
189 def to_utf8(s)
187 @ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
190 @ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
188 begin; @ic.iconv(s.to_s); rescue; s.to_s; end
191 begin; @ic.iconv(s.to_s); rescue; s.to_s; end
189 end
192 end
190 end
193 end
@@ -1,371 +1,417
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 # Redmine - project management software
2 # Redmine - project management software
3 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # Copyright (C) 2006-2011 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 require 'timelog_controller'
20 require 'timelog_controller'
21
21
22 # Re-raise errors caught by the controller.
22 # Re-raise errors caught by the controller.
23 class TimelogController; def rescue_action(e) raise e end; end
23 class TimelogController; def rescue_action(e) raise e end; end
24
24
25 class TimelogControllerTest < ActionController::TestCase
25 class TimelogControllerTest < ActionController::TestCase
26 fixtures :projects, :enabled_modules, :roles, :members,
26 fixtures :projects, :enabled_modules, :roles, :members,
27 :member_roles, :issues, :time_entries, :users,
27 :member_roles, :issues, :time_entries, :users,
28 :trackers, :enumerations, :issue_statuses,
28 :trackers, :enumerations, :issue_statuses,
29 :custom_fields, :custom_values
29 :custom_fields, :custom_values
30
30
31 def setup
31 def setup
32 @controller = TimelogController.new
32 @controller = TimelogController.new
33 @request = ActionController::TestRequest.new
33 @request = ActionController::TestRequest.new
34 @response = ActionController::TestResponse.new
34 @response = ActionController::TestResponse.new
35 end
35 end
36
36
37 def test_get_new
37 def test_get_new
38 @request.session[:user_id] = 3
38 @request.session[:user_id] = 3
39 get :new, :project_id => 1
39 get :new, :project_id => 1
40 assert_response :success
40 assert_response :success
41 assert_template 'edit'
41 assert_template 'edit'
42 # Default activity selected
42 # Default activity selected
43 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
43 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
44 :content => 'Development'
44 :content => 'Development'
45 end
45 end
46
46
47 def test_get_new_should_only_show_active_time_entry_activities
47 def test_get_new_should_only_show_active_time_entry_activities
48 @request.session[:user_id] = 3
48 @request.session[:user_id] = 3
49 get :new, :project_id => 1
49 get :new, :project_id => 1
50 assert_response :success
50 assert_response :success
51 assert_template 'edit'
51 assert_template 'edit'
52 assert_no_tag :tag => 'option', :content => 'Inactive Activity'
52 assert_no_tag :tag => 'option', :content => 'Inactive Activity'
53 end
53 end
54
54
55 def test_get_edit_existing_time
55 def test_get_edit_existing_time
56 @request.session[:user_id] = 2
56 @request.session[:user_id] = 2
57 get :edit, :id => 2, :project_id => nil
57 get :edit, :id => 2, :project_id => nil
58 assert_response :success
58 assert_response :success
59 assert_template 'edit'
59 assert_template 'edit'
60 # Default activity selected
60 # Default activity selected
61 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
61 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
62 end
62 end
63
63
64 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
64 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
65 te = TimeEntry.find(1)
65 te = TimeEntry.find(1)
66 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
66 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
67 te.save!
67 te.save!
68
68
69 @request.session[:user_id] = 1
69 @request.session[:user_id] = 1
70 get :edit, :project_id => 1, :id => 1
70 get :edit, :project_id => 1, :id => 1
71 assert_response :success
71 assert_response :success
72 assert_template 'edit'
72 assert_template 'edit'
73 # Blank option since nothing is pre-selected
73 # Blank option since nothing is pre-selected
74 assert_tag :tag => 'option', :content => '--- Please select ---'
74 assert_tag :tag => 'option', :content => '--- Please select ---'
75 end
75 end
76
76
77 def test_post_create
77 def test_post_create
78 # TODO: should POST to issues’ time log instead of project. change form
78 # TODO: should POST to issues’ time log instead of project. change form
79 # and routing
79 # and routing
80 @request.session[:user_id] = 3
80 @request.session[:user_id] = 3
81 post :create, :project_id => 1,
81 post :create, :project_id => 1,
82 :time_entry => {:comments => 'Some work on TimelogControllerTest',
82 :time_entry => {:comments => 'Some work on TimelogControllerTest',
83 # Not the default activity
83 # Not the default activity
84 :activity_id => '11',
84 :activity_id => '11',
85 :spent_on => '2008-03-14',
85 :spent_on => '2008-03-14',
86 :issue_id => '1',
86 :issue_id => '1',
87 :hours => '7.3'}
87 :hours => '7.3'}
88 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
88 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
89
89
90 i = Issue.find(1)
90 i = Issue.find(1)
91 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
91 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
92 assert_not_nil t
92 assert_not_nil t
93 assert_equal 11, t.activity_id
93 assert_equal 11, t.activity_id
94 assert_equal 7.3, t.hours
94 assert_equal 7.3, t.hours
95 assert_equal 3, t.user_id
95 assert_equal 3, t.user_id
96 assert_equal i, t.issue
96 assert_equal i, t.issue
97 assert_equal i.project, t.project
97 assert_equal i.project, t.project
98 end
98 end
99
99
100 def test_post_create_with_blank_issue
100 def test_post_create_with_blank_issue
101 # TODO: should POST to issues’ time log instead of project. change form
101 # TODO: should POST to issues’ time log instead of project. change form
102 # and routing
102 # and routing
103 @request.session[:user_id] = 3
103 @request.session[:user_id] = 3
104 post :create, :project_id => 1,
104 post :create, :project_id => 1,
105 :time_entry => {:comments => 'Some work on TimelogControllerTest',
105 :time_entry => {:comments => 'Some work on TimelogControllerTest',
106 # Not the default activity
106 # Not the default activity
107 :activity_id => '11',
107 :activity_id => '11',
108 :issue_id => '',
108 :issue_id => '',
109 :spent_on => '2008-03-14',
109 :spent_on => '2008-03-14',
110 :hours => '7.3'}
110 :hours => '7.3'}
111 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
111 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
112
112
113 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
113 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
114 assert_not_nil t
114 assert_not_nil t
115 assert_equal 11, t.activity_id
115 assert_equal 11, t.activity_id
116 assert_equal 7.3, t.hours
116 assert_equal 7.3, t.hours
117 assert_equal 3, t.user_id
117 assert_equal 3, t.user_id
118 end
118 end
119
119
120 def test_update
120 def test_update
121 entry = TimeEntry.find(1)
121 entry = TimeEntry.find(1)
122 assert_equal 1, entry.issue_id
122 assert_equal 1, entry.issue_id
123 assert_equal 2, entry.user_id
123 assert_equal 2, entry.user_id
124
124
125 @request.session[:user_id] = 1
125 @request.session[:user_id] = 1
126 put :update, :id => 1,
126 put :update, :id => 1,
127 :time_entry => {:issue_id => '2',
127 :time_entry => {:issue_id => '2',
128 :hours => '8'}
128 :hours => '8'}
129 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
129 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
130 entry.reload
130 entry.reload
131
131
132 assert_equal 8, entry.hours
132 assert_equal 8, entry.hours
133 assert_equal 2, entry.issue_id
133 assert_equal 2, entry.issue_id
134 assert_equal 2, entry.user_id
134 assert_equal 2, entry.user_id
135 end
135 end
136
136
137 def test_get_bulk_edit
137 def test_get_bulk_edit
138 @request.session[:user_id] = 2
138 @request.session[:user_id] = 2
139 get :bulk_edit, :ids => [1, 2]
139 get :bulk_edit, :ids => [1, 2]
140 assert_response :success
140 assert_response :success
141 assert_template 'bulk_edit'
141 assert_template 'bulk_edit'
142
142
143 # System wide custom field
143 # System wide custom field
144 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
144 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
145 end
145 end
146
146
147 def test_get_bulk_edit_on_different_projects
147 def test_get_bulk_edit_on_different_projects
148 @request.session[:user_id] = 2
148 @request.session[:user_id] = 2
149 get :bulk_edit, :ids => [1, 2, 6]
149 get :bulk_edit, :ids => [1, 2, 6]
150 assert_response :success
150 assert_response :success
151 assert_template 'bulk_edit'
151 assert_template 'bulk_edit'
152 end
152 end
153
153
154 def test_bulk_update
154 def test_bulk_update
155 @request.session[:user_id] = 2
155 @request.session[:user_id] = 2
156 # update time entry activity
156 # update time entry activity
157 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
157 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
158
158
159 assert_response 302
159 assert_response 302
160 # check that the issues were updated
160 # check that the issues were updated
161 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
161 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
162 end
162 end
163
163
164 def test_bulk_update_on_different_projects
164 def test_bulk_update_on_different_projects
165 @request.session[:user_id] = 2
165 @request.session[:user_id] = 2
166 # update time entry activity
166 # update time entry activity
167 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
167 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
168
168
169 assert_response 302
169 assert_response 302
170 # check that the issues were updated
170 # check that the issues were updated
171 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
171 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
172 end
172 end
173
173
174 def test_bulk_update_on_different_projects_without_rights
174 def test_bulk_update_on_different_projects_without_rights
175 @request.session[:user_id] = 3
175 @request.session[:user_id] = 3
176 user = User.find(3)
176 user = User.find(3)
177 action = { :controller => "timelog", :action => "bulk_update" }
177 action = { :controller => "timelog", :action => "bulk_update" }
178 assert user.allowed_to?(action, TimeEntry.find(1).project)
178 assert user.allowed_to?(action, TimeEntry.find(1).project)
179 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
179 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
180 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
180 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
181 assert_response 403
181 assert_response 403
182 end
182 end
183
183
184 def test_bulk_update_custom_field
184 def test_bulk_update_custom_field
185 @request.session[:user_id] = 2
185 @request.session[:user_id] = 2
186 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
186 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
187
187
188 assert_response 302
188 assert_response 302
189 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
189 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
190 end
190 end
191
191
192 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
192 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
193 @request.session[:user_id] = 2
193 @request.session[:user_id] = 2
194 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
194 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
195
195
196 assert_response :redirect
196 assert_response :redirect
197 assert_redirected_to '/time_entries'
197 assert_redirected_to '/time_entries'
198 end
198 end
199
199
200 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
200 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
201 @request.session[:user_id] = 2
201 @request.session[:user_id] = 2
202 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
202 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
203
203
204 assert_response :redirect
204 assert_response :redirect
205 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
205 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
206 end
206 end
207
207
208 def test_destroy
208 def test_destroy
209 @request.session[:user_id] = 2
209 @request.session[:user_id] = 2
210 delete :destroy, :id => 1
210 delete :destroy, :id => 1
211 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
211 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
212 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
212 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
213 assert_nil TimeEntry.find_by_id(1)
213 assert_nil TimeEntry.find_by_id(1)
214 end
214 end
215
215
216 def test_destroy_should_fail
216 def test_destroy_should_fail
217 # simulate that this fails (e.g. due to a plugin), see #5700
217 # simulate that this fails (e.g. due to a plugin), see #5700
218 TimeEntry.any_instance.expects(:destroy).returns(false)
218 TimeEntry.any_instance.expects(:destroy).returns(false)
219
219
220 @request.session[:user_id] = 2
220 @request.session[:user_id] = 2
221 delete :destroy, :id => 1
221 delete :destroy, :id => 1
222 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
222 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
223 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
223 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
224 assert_not_nil TimeEntry.find_by_id(1)
224 assert_not_nil TimeEntry.find_by_id(1)
225 end
225 end
226
226
227 def test_index_all_projects
227 def test_index_all_projects
228 get :index
228 get :index
229 assert_response :success
229 assert_response :success
230 assert_template 'index'
230 assert_template 'index'
231 assert_not_nil assigns(:total_hours)
231 assert_not_nil assigns(:total_hours)
232 assert_equal "162.90", "%.2f" % assigns(:total_hours)
232 assert_equal "162.90", "%.2f" % assigns(:total_hours)
233 assert_tag :form,
233 assert_tag :form,
234 :attributes => {:action => "/time_entries", :id => 'query_form'}
234 :attributes => {:action => "/time_entries", :id => 'query_form'}
235 end
235 end
236
236
237 def test_index_at_project_level
237 def test_index_at_project_level
238 get :index, :project_id => 'ecookbook'
238 get :index, :project_id => 'ecookbook'
239 assert_response :success
239 assert_response :success
240 assert_template 'index'
240 assert_template 'index'
241 assert_not_nil assigns(:entries)
241 assert_not_nil assigns(:entries)
242 assert_equal 4, assigns(:entries).size
242 assert_equal 4, assigns(:entries).size
243 # project and subproject
243 # project and subproject
244 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
244 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
245 assert_not_nil assigns(:total_hours)
245 assert_not_nil assigns(:total_hours)
246 assert_equal "162.90", "%.2f" % assigns(:total_hours)
246 assert_equal "162.90", "%.2f" % assigns(:total_hours)
247 # display all time by default
247 # display all time by default
248 assert_equal '2007-03-12'.to_date, assigns(:from)
248 assert_equal '2007-03-12'.to_date, assigns(:from)
249 assert_equal '2007-04-22'.to_date, assigns(:to)
249 assert_equal '2007-04-22'.to_date, assigns(:to)
250 assert_tag :form,
250 assert_tag :form,
251 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
251 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
252 end
252 end
253
253
254 def test_index_at_project_level_with_date_range
254 def test_index_at_project_level_with_date_range
255 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
255 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
256 assert_response :success
256 assert_response :success
257 assert_template 'index'
257 assert_template 'index'
258 assert_not_nil assigns(:entries)
258 assert_not_nil assigns(:entries)
259 assert_equal 3, assigns(:entries).size
259 assert_equal 3, assigns(:entries).size
260 assert_not_nil assigns(:total_hours)
260 assert_not_nil assigns(:total_hours)
261 assert_equal "12.90", "%.2f" % assigns(:total_hours)
261 assert_equal "12.90", "%.2f" % assigns(:total_hours)
262 assert_equal '2007-03-20'.to_date, assigns(:from)
262 assert_equal '2007-03-20'.to_date, assigns(:from)
263 assert_equal '2007-04-30'.to_date, assigns(:to)
263 assert_equal '2007-04-30'.to_date, assigns(:to)
264 assert_tag :form,
264 assert_tag :form,
265 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
265 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
266 end
266 end
267
267
268 def test_index_at_project_level_with_period
268 def test_index_at_project_level_with_period
269 get :index, :project_id => 'ecookbook', :period => '7_days'
269 get :index, :project_id => 'ecookbook', :period => '7_days'
270 assert_response :success
270 assert_response :success
271 assert_template 'index'
271 assert_template 'index'
272 assert_not_nil assigns(:entries)
272 assert_not_nil assigns(:entries)
273 assert_not_nil assigns(:total_hours)
273 assert_not_nil assigns(:total_hours)
274 assert_equal Date.today - 7, assigns(:from)
274 assert_equal Date.today - 7, assigns(:from)
275 assert_equal Date.today, assigns(:to)
275 assert_equal Date.today, assigns(:to)
276 assert_tag :form,
276 assert_tag :form,
277 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
277 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
278 end
278 end
279
279
280 def test_index_one_day
280 def test_index_one_day
281 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
281 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
282 assert_response :success
282 assert_response :success
283 assert_template 'index'
283 assert_template 'index'
284 assert_not_nil assigns(:total_hours)
284 assert_not_nil assigns(:total_hours)
285 assert_equal "4.25", "%.2f" % assigns(:total_hours)
285 assert_equal "4.25", "%.2f" % assigns(:total_hours)
286 assert_tag :form,
286 assert_tag :form,
287 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
287 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
288 end
288 end
289
289
290 def test_index_at_issue_level
290 def test_index_at_issue_level
291 get :index, :issue_id => 1
291 get :index, :issue_id => 1
292 assert_response :success
292 assert_response :success
293 assert_template 'index'
293 assert_template 'index'
294 assert_not_nil assigns(:entries)
294 assert_not_nil assigns(:entries)
295 assert_equal 2, assigns(:entries).size
295 assert_equal 2, assigns(:entries).size
296 assert_not_nil assigns(:total_hours)
296 assert_not_nil assigns(:total_hours)
297 assert_equal 154.25, assigns(:total_hours)
297 assert_equal 154.25, assigns(:total_hours)
298 # display all time based on what's been logged
298 # display all time based on what's been logged
299 assert_equal '2007-03-12'.to_date, assigns(:from)
299 assert_equal '2007-03-12'.to_date, assigns(:from)
300 assert_equal '2007-04-22'.to_date, assigns(:to)
300 assert_equal '2007-04-22'.to_date, assigns(:to)
301 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
301 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
302 # to use /issues/:issue_id/time_entries
302 # to use /issues/:issue_id/time_entries
303 assert_tag :form,
303 assert_tag :form,
304 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
304 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
305 end
305 end
306
306
307 def test_index_atom_feed
307 def test_index_atom_feed
308 get :index, :project_id => 1, :format => 'atom'
308 get :index, :project_id => 1, :format => 'atom'
309 assert_response :success
309 assert_response :success
310 assert_equal 'application/atom+xml', @response.content_type
310 assert_equal 'application/atom+xml', @response.content_type
311 assert_not_nil assigns(:items)
311 assert_not_nil assigns(:items)
312 assert assigns(:items).first.is_a?(TimeEntry)
312 assert assigns(:items).first.is_a?(TimeEntry)
313 end
313 end
314
314
315 def test_index_all_projects_csv_export
315 def test_index_all_projects_csv_export
316 Setting.date_format = '%m/%d/%Y'
316 Setting.date_format = '%m/%d/%Y'
317 get :index, :format => 'csv'
317 get :index, :format => 'csv'
318 assert_response :success
318 assert_response :success
319 assert_equal 'text/csv', @response.content_type
319 assert_equal 'text/csv', @response.content_type
320 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
320 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
321 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
321 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
322 end
322 end
323
323
324 def test_index_csv_export
324 def test_index_csv_export
325 Setting.date_format = '%m/%d/%Y'
325 Setting.date_format = '%m/%d/%Y'
326 get :index, :project_id => 1, :format => 'csv'
326 get :index, :project_id => 1, :format => 'csv'
327 assert_response :success
327 assert_response :success
328 assert_equal 'text/csv', @response.content_type
328 assert_equal 'text/csv', @response.content_type
329 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
329 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
330 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
330 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
331 end
331 end
332
332
333 def test_csv_big_5
333 def test_csv_big_5
334 user = User.find_by_id(3)
334 user = User.find_by_id(3)
335 user.language = "zh-TW"
335 user.language = "zh-TW"
336 assert user.save
336 assert user.save
337 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
337 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
338 str_big5 = "\xa4@\xa4\xeb"
338 str_big5 = "\xa4@\xa4\xeb"
339 if str_utf8.respond_to?(:force_encoding)
339 if str_utf8.respond_to?(:force_encoding)
340 str_utf8.force_encoding('UTF-8')
340 str_utf8.force_encoding('UTF-8')
341 str_big5.force_encoding('Big5')
341 str_big5.force_encoding('Big5')
342 end
342 end
343 @request.session[:user_id] = 3
343 @request.session[:user_id] = 3
344 post :create, :project_id => 1,
344 post :create, :project_id => 1,
345 :time_entry => {:comments => str_utf8,
345 :time_entry => {:comments => str_utf8,
346 # Not the default activity
346 # Not the default activity
347 :activity_id => '11',
347 :activity_id => '11',
348 :issue_id => '',
348 :issue_id => '',
349 :spent_on => '2011-11-10',
349 :spent_on => '2011-11-10',
350 :hours => '7.3'}
350 :hours => '7.3'}
351 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
351 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
352
352
353 t = TimeEntry.find_by_comments(str_utf8)
353 t = TimeEntry.find_by_comments(str_utf8)
354 assert_not_nil t
354 assert_not_nil t
355 assert_equal 11, t.activity_id
355 assert_equal 11, t.activity_id
356 assert_equal 7.3, t.hours
356 assert_equal 7.3, t.hours
357 assert_equal 3, t.user_id
357 assert_equal 3, t.user_id
358
358
359 get :index, :project_id => 1, :format => 'csv',
359 get :index, :project_id => 1, :format => 'csv',
360 :from => '2011-11-10', :to => '2011-11-10'
360 :from => '2011-11-10', :to => '2011-11-10'
361 assert_response :success
361 assert_response :success
362 assert_equal 'text/csv', @response.content_type
362 assert_equal 'text/csv', @response.content_type
363 ar = @response.body.chomp.split("\n")
363 ar = @response.body.chomp.split("\n")
364 s1 = "\xa4\xe9\xb4\xc1"
364 s1 = "\xa4\xe9\xb4\xc1"
365 if str_utf8.respond_to?(:force_encoding)
365 if str_utf8.respond_to?(:force_encoding)
366 s1.force_encoding('Big5')
366 s1.force_encoding('Big5')
367 end
367 end
368 assert ar[0].include?(s1)
368 assert ar[0].include?(s1)
369 assert ar[1].include?(str_big5)
369 assert ar[1].include?(str_big5)
370 end
370 end
371
372 def test_csv_cannot_convert_should_be_replaced_big_5
373 user = User.find_by_id(3)
374 user.language = "zh-TW"
375 assert user.save
376 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
377 if str_utf8.respond_to?(:force_encoding)
378 str_utf8.force_encoding('UTF-8')
379 end
380 @request.session[:user_id] = 3
381 post :create, :project_id => 1,
382 :time_entry => {:comments => str_utf8,
383 # Not the default activity
384 :activity_id => '11',
385 :issue_id => '',
386 :spent_on => '2011-11-10',
387 :hours => '7.3'}
388 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
389
390 t = TimeEntry.find_by_comments(str_utf8)
391 assert_not_nil t
392 assert_equal 11, t.activity_id
393 assert_equal 7.3, t.hours
394 assert_equal 3, t.user_id
395
396 get :index, :project_id => 1, :format => 'csv',
397 :from => '2011-11-10', :to => '2011-11-10'
398 assert_response :success
399 assert_equal 'text/csv', @response.content_type
400 ar = @response.body.chomp.split("\n")
401 s1 = "\xa4\xe9\xb4\xc1"
402 if str_utf8.respond_to?(:force_encoding)
403 s1.force_encoding('Big5')
404 end
405 assert ar[0].include?(s1)
406 s2 = ar[1].split(",")[8]
407 if s2.respond_to?(:force_encoding)
408 s3 = "\xa5H?"
409 s3.force_encoding('Big5')
410 assert_equal s3, s2
411 elsif RUBY_PLATFORM == 'java'
412 assert_equal "??", s2
413 else
414 assert_equal "\xa5H???", s2
415 end
416 end
371 end
417 end
General Comments 0
You need to be logged in to leave comments. Login now