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