##// END OF EJS Templates
Fixed that CSV Export of Spent Time ignores filters and columns selection (#13618)....
Jean-Philippe Lang -
r11466:25209273d33d
parent child
Show More
@@ -1,199 +1,223
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2013 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 module QueriesHelper
21 21 def filters_options_for_select(query)
22 22 options_for_select(filters_options(query))
23 23 end
24 24
25 25 def filters_options(query)
26 26 options = [[]]
27 27 options += query.available_filters.map do |field, field_options|
28 28 [field_options[:name], field]
29 29 end
30 30 end
31 31
32 def query_filters_hidden_tags(query)
33 tags = ''.html_safe
34 query.filters.each do |field, options|
35 tags << hidden_field_tag("f[]", field, :id => nil)
36 tags << hidden_field_tag("op[#{field}]", options[:operator], :id => nil)
37 options[:values].each do |value|
38 tags << hidden_field_tag("v[#{field}][]", value, :id => nil)
39 end
40 end
41 tags
42 end
43
44 def query_columns_hidden_tags(query)
45 tags = ''.html_safe
46 query.columns.each do |column|
47 tags << hidden_field_tag("c[]", column.name, :id => nil)
48 end
49 tags
50 end
51
52 def query_hidden_tags(query)
53 query_filters_hidden_tags(query) + query_columns_hidden_tags(query)
54 end
55
32 56 def available_block_columns_tags(query)
33 57 tags = ''.html_safe
34 58 query.available_block_columns.each do |column|
35 59 tags << content_tag('label', check_box_tag('c[]', column.name.to_s, query.has_column?(column)) + " #{column.caption}", :class => 'inline')
36 60 end
37 61 tags
38 62 end
39 63
40 64 def query_available_inline_columns_options(query)
41 65 (query.available_inline_columns - query.columns).reject(&:frozen?).collect {|column| [column.caption, column.name]}
42 66 end
43 67
44 68 def query_selected_inline_columns_options(query)
45 69 (query.inline_columns & query.available_inline_columns).reject(&:frozen?).collect {|column| [column.caption, column.name]}
46 70 end
47 71
48 72 def render_query_columns_selection(query, options={})
49 73 tag_name = (options[:name] || 'c') + '[]'
50 74 render :partial => 'queries/columns', :locals => {:query => query, :tag_name => tag_name}
51 75 end
52 76
53 77 def column_header(column)
54 78 column.sortable ? sort_header_tag(column.name.to_s, :caption => column.caption,
55 79 :default_order => column.default_order) :
56 80 content_tag('th', h(column.caption))
57 81 end
58 82
59 83 def column_content(column, issue)
60 84 value = column.value(issue)
61 85 if value.is_a?(Array)
62 86 value.collect {|v| column_value(column, issue, v)}.compact.join(', ').html_safe
63 87 else
64 88 column_value(column, issue, value)
65 89 end
66 90 end
67 91
68 92 def column_value(column, issue, value)
69 93 case value.class.name
70 94 when 'String'
71 95 if column.name == :subject
72 96 link_to(h(value), :controller => 'issues', :action => 'show', :id => issue)
73 97 elsif column.name == :description
74 98 issue.description? ? content_tag('div', textilizable(issue, :description), :class => "wiki") : ''
75 99 else
76 100 h(value)
77 101 end
78 102 when 'Time'
79 103 format_time(value)
80 104 when 'Date'
81 105 format_date(value)
82 106 when 'Fixnum'
83 107 if column.name == :id
84 108 link_to value, issue_path(issue)
85 109 elsif column.name == :done_ratio
86 110 progress_bar(value, :width => '80px')
87 111 else
88 112 value.to_s
89 113 end
90 114 when 'Float'
91 115 sprintf "%.2f", value
92 116 when 'User'
93 117 link_to_user value
94 118 when 'Project'
95 119 link_to_project value
96 120 when 'Version'
97 121 link_to(h(value), :controller => 'versions', :action => 'show', :id => value)
98 122 when 'TrueClass'
99 123 l(:general_text_Yes)
100 124 when 'FalseClass'
101 125 l(:general_text_No)
102 126 when 'Issue'
103 127 value.visible? ? link_to_issue(value) : "##{value.id}"
104 128 when 'IssueRelation'
105 129 other = value.other_issue(issue)
106 130 content_tag('span',
107 131 (l(value.label_for(issue)) + " " + link_to_issue(other, :subject => false, :tracker => false)).html_safe,
108 132 :class => value.css_classes_for(issue))
109 133 else
110 134 h(value)
111 135 end
112 136 end
113 137
114 138 def csv_content(column, issue)
115 139 value = column.value(issue)
116 140 if value.is_a?(Array)
117 141 value.collect {|v| csv_value(column, issue, v)}.compact.join(', ')
118 142 else
119 143 csv_value(column, issue, value)
120 144 end
121 145 end
122 146
123 147 def csv_value(column, issue, value)
124 148 case value.class.name
125 149 when 'Time'
126 150 format_time(value)
127 151 when 'Date'
128 152 format_date(value)
129 153 when 'Float'
130 154 sprintf("%.2f", value).gsub('.', l(:general_csv_decimal_separator))
131 155 when 'IssueRelation'
132 156 other = value.other_issue(issue)
133 157 l(value.label_for(issue)) + " ##{other.id}"
134 158 else
135 159 value.to_s
136 160 end
137 161 end
138 162
139 163 def query_to_csv(items, query, options={})
140 164 encoding = l(:general_csv_encoding)
141 165 columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
142 166 query.available_block_columns.each do |column|
143 167 if options[column.name].present?
144 168 columns << column
145 169 end
146 170 end
147 171
148 172 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
149 173 # csv header fields
150 174 csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) }
151 175 # csv lines
152 176 items.each do |item|
153 177 csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, item), encoding) }
154 178 end
155 179 end
156 180 export
157 181 end
158 182
159 183 # Retrieve query from session or build a new query
160 184 def retrieve_query
161 185 if !params[:query_id].blank?
162 186 cond = "project_id IS NULL"
163 187 cond << " OR project_id = #{@project.id}" if @project
164 188 @query = IssueQuery.find(params[:query_id], :conditions => cond)
165 189 raise ::Unauthorized unless @query.visible?
166 190 @query.project = @project
167 191 session[:query] = {:id => @query.id, :project_id => @query.project_id}
168 192 sort_clear
169 193 elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
170 194 # Give it a name, required to be valid
171 195 @query = IssueQuery.new(:name => "_")
172 196 @query.project = @project
173 197 @query.build_from_params(params)
174 198 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
175 199 else
176 200 # retrieve from session
177 201 @query = IssueQuery.find_by_id(session[:query][:id]) if session[:query][:id]
178 202 @query ||= IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
179 203 @query.project = @project
180 204 end
181 205 end
182 206
183 207 def retrieve_query_from_session
184 208 if session[:query]
185 209 if session[:query][:id]
186 210 @query = IssueQuery.find_by_id(session[:query][:id])
187 211 return unless @query
188 212 else
189 213 @query = IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
190 214 end
191 215 if session[:query].has_key?(:project_id)
192 216 @query.project_id = session[:query][:project_id]
193 217 else
194 218 @query.project = @project
195 219 end
196 220 @query
197 221 end
198 222 end
199 223 end
@@ -1,47 +1,48
1 1 <div class="contextual">
2 2 <%= link_to l(:button_log_time),
3 3 {:controller => 'timelog', :action => 'new', :project_id => @project, :issue_id => @issue},
4 4 :class => 'icon icon-time-add' if User.current.allowed_to?(:log_time, @project, :global => true) %>
5 5 </div>
6 6
7 7 <%= render_timelog_breadcrumb %>
8 8
9 9 <h2><%= l(:label_spent_time) %></h2>
10 10
11 11 <%= form_tag({:controller => 'timelog', :action => 'index', :project_id => @project, :issue_id => @issue}, :method => :get, :id => 'query_form') do %>
12 12 <%= render :partial => 'date_range' %>
13 13 <% end %>
14 14
15 15 <div class="total-hours">
16 16 <p><%= l(:label_total_time) %>: <%= html_hours(l_hours(@total_hours)) %></p>
17 17 </div>
18 18
19 19 <% unless @entries.empty? %>
20 20 <%= render :partial => 'list', :locals => { :entries => @entries }%>
21 21 <p class="pagination"><%= pagination_links_full @entry_pages, @entry_count %></p>
22 22
23 23 <% other_formats_links do |f| %>
24 24 <%= f.link_to 'Atom', :url => params.merge({:issue_id => @issue, :key => User.current.rss_key}) %>
25 25 <%= f.link_to 'CSV', :url => params, :onclick => "showModal('csv-export-options', '330px'); return false;" %>
26 26 <% end %>
27 27
28 28 <div id="csv-export-options" style="display:none;">
29 29 <h3 class="title"><%= l(:label_export_options, :export_format => 'CSV') %></h3>
30 <%= form_tag(params.merge({:format => 'csv',:page=>nil}), :method => :get, :id => 'csv-export-form') do %>
30 <%= form_tag(params.slice(:project_id, :issue_id).merge(:format => 'csv', :page=>nil), :method => :get, :id => 'csv-export-form') do %>
31 <%= query_hidden_tags @query %>
31 32 <p>
32 33 <label><%= radio_button_tag 'columns', '', true %> <%= l(:description_selected_columns) %></label><br />
33 34 <label><%= radio_button_tag 'columns', 'all' %> <%= l(:description_all_columns) %></label>
34 35 </p>
35 36 <p class="buttons">
36 37 <%= submit_tag l(:button_export), :name => nil, :onclick => "hideModal(this);" %>
37 38 <%= submit_tag l(:button_cancel), :name => nil, :onclick => "hideModal(this);", :type => 'button' %>
38 39 </p>
39 40 <% end %>
40 41 </div>
41 42 <% end %>
42 43
43 44 <% html_title l(:label_spent_time), l(:label_details) %>
44 45
45 46 <% content_for :header_tags do %>
46 47 <%= auto_discovery_link_tag(:atom, {:issue_id => @issue, :format => 'atom', :key => User.current.rss_key}, :title => l(:label_spent_time)) %>
47 48 <% end %>
@@ -1,564 +1,604
1 1 # -*- coding: utf-8 -*-
2 2 # Redmine - project management software
3 3 # Copyright (C) 2006-2013 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 TimelogControllerTest < ActionController::TestCase
22 22 fixtures :projects, :enabled_modules, :roles, :members,
23 23 :member_roles, :issues, :time_entries, :users,
24 24 :trackers, :enumerations, :issue_statuses,
25 25 :custom_fields, :custom_values,
26 26 :projects_trackers, :custom_fields_trackers,
27 27 :custom_fields_projects
28 28
29 29 include Redmine::I18n
30 30
31 31 def test_new_with_project_id
32 32 @request.session[:user_id] = 3
33 33 get :new, :project_id => 1
34 34 assert_response :success
35 35 assert_template 'new'
36 36 assert_select 'select[name=?]', 'time_entry[project_id]', 0
37 37 assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]'
38 38 end
39 39
40 40 def test_new_with_issue_id
41 41 @request.session[:user_id] = 3
42 42 get :new, :issue_id => 2
43 43 assert_response :success
44 44 assert_template 'new'
45 45 assert_select 'select[name=?]', 'time_entry[project_id]', 0
46 46 assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]'
47 47 end
48 48
49 49 def test_new_without_project
50 50 @request.session[:user_id] = 3
51 51 get :new
52 52 assert_response :success
53 53 assert_template 'new'
54 54 assert_select 'select[name=?]', 'time_entry[project_id]'
55 55 assert_select 'input[name=?]', 'time_entry[project_id]', 0
56 56 end
57 57
58 58 def test_new_without_project_should_prefill_the_form
59 59 @request.session[:user_id] = 3
60 60 get :new, :time_entry => {:project_id => '1'}
61 61 assert_response :success
62 62 assert_template 'new'
63 63 assert_select 'select[name=?]', 'time_entry[project_id]' do
64 64 assert_select 'option[value=1][selected=selected]'
65 65 end
66 66 assert_select 'input[name=?]', 'time_entry[project_id]', 0
67 67 end
68 68
69 69 def test_new_without_project_should_deny_without_permission
70 70 Role.all.each {|role| role.remove_permission! :log_time}
71 71 @request.session[:user_id] = 3
72 72
73 73 get :new
74 74 assert_response 403
75 75 end
76 76
77 77 def test_new_should_select_default_activity
78 78 @request.session[:user_id] = 3
79 79 get :new, :project_id => 1
80 80 assert_response :success
81 81 assert_select 'select[name=?]', 'time_entry[activity_id]' do
82 82 assert_select 'option[selected=selected]', :text => 'Development'
83 83 end
84 84 end
85 85
86 86 def test_new_should_only_show_active_time_entry_activities
87 87 @request.session[:user_id] = 3
88 88 get :new, :project_id => 1
89 89 assert_response :success
90 90 assert_no_tag 'option', :content => 'Inactive Activity'
91 91 end
92 92
93 93 def test_get_edit_existing_time
94 94 @request.session[:user_id] = 2
95 95 get :edit, :id => 2, :project_id => nil
96 96 assert_response :success
97 97 assert_template 'edit'
98 98 # Default activity selected
99 99 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
100 100 end
101 101
102 102 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
103 103 te = TimeEntry.find(1)
104 104 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
105 105 te.save!
106 106
107 107 @request.session[:user_id] = 1
108 108 get :edit, :project_id => 1, :id => 1
109 109 assert_response :success
110 110 assert_template 'edit'
111 111 # Blank option since nothing is pre-selected
112 112 assert_tag :tag => 'option', :content => '--- Please select ---'
113 113 end
114 114
115 115 def test_post_create
116 116 # TODO: should POST to issues’ time log instead of project. change form
117 117 # and routing
118 118 @request.session[:user_id] = 3
119 119 post :create, :project_id => 1,
120 120 :time_entry => {:comments => 'Some work on TimelogControllerTest',
121 121 # Not the default activity
122 122 :activity_id => '11',
123 123 :spent_on => '2008-03-14',
124 124 :issue_id => '1',
125 125 :hours => '7.3'}
126 126 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
127 127
128 128 i = Issue.find(1)
129 129 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
130 130 assert_not_nil t
131 131 assert_equal 11, t.activity_id
132 132 assert_equal 7.3, t.hours
133 133 assert_equal 3, t.user_id
134 134 assert_equal i, t.issue
135 135 assert_equal i.project, t.project
136 136 end
137 137
138 138 def test_post_create_with_blank_issue
139 139 # TODO: should POST to issues’ time log instead of project. change form
140 140 # and routing
141 141 @request.session[:user_id] = 3
142 142 post :create, :project_id => 1,
143 143 :time_entry => {:comments => 'Some work on TimelogControllerTest',
144 144 # Not the default activity
145 145 :activity_id => '11',
146 146 :issue_id => '',
147 147 :spent_on => '2008-03-14',
148 148 :hours => '7.3'}
149 149 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
150 150
151 151 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
152 152 assert_not_nil t
153 153 assert_equal 11, t.activity_id
154 154 assert_equal 7.3, t.hours
155 155 assert_equal 3, t.user_id
156 156 end
157 157
158 158 def test_create_and_continue
159 159 @request.session[:user_id] = 2
160 160 post :create, :project_id => 1,
161 161 :time_entry => {:activity_id => '11',
162 162 :issue_id => '',
163 163 :spent_on => '2008-03-14',
164 164 :hours => '7.3'},
165 165 :continue => '1'
166 166 assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D='
167 167 end
168 168
169 169 def test_create_and_continue_with_issue_id
170 170 @request.session[:user_id] = 2
171 171 post :create, :project_id => 1,
172 172 :time_entry => {:activity_id => '11',
173 173 :issue_id => '1',
174 174 :spent_on => '2008-03-14',
175 175 :hours => '7.3'},
176 176 :continue => '1'
177 177 assert_redirected_to '/projects/ecookbook/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1'
178 178 end
179 179
180 180 def test_create_and_continue_without_project
181 181 @request.session[:user_id] = 2
182 182 post :create, :time_entry => {:project_id => '1',
183 183 :activity_id => '11',
184 184 :issue_id => '',
185 185 :spent_on => '2008-03-14',
186 186 :hours => '7.3'},
187 187 :continue => '1'
188 188
189 189 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1'
190 190 end
191 191
192 192 def test_create_without_log_time_permission_should_be_denied
193 193 @request.session[:user_id] = 2
194 194 Role.find_by_name('Manager').remove_permission! :log_time
195 195 post :create, :project_id => 1,
196 196 :time_entry => {:activity_id => '11',
197 197 :issue_id => '',
198 198 :spent_on => '2008-03-14',
199 199 :hours => '7.3'}
200 200
201 201 assert_response 403
202 202 end
203 203
204 204 def test_create_with_failure
205 205 @request.session[:user_id] = 2
206 206 post :create, :project_id => 1,
207 207 :time_entry => {:activity_id => '',
208 208 :issue_id => '',
209 209 :spent_on => '2008-03-14',
210 210 :hours => '7.3'}
211 211
212 212 assert_response :success
213 213 assert_template 'new'
214 214 end
215 215
216 216 def test_create_without_project
217 217 @request.session[:user_id] = 2
218 218 assert_difference 'TimeEntry.count' do
219 219 post :create, :time_entry => {:project_id => '1',
220 220 :activity_id => '11',
221 221 :issue_id => '',
222 222 :spent_on => '2008-03-14',
223 223 :hours => '7.3'}
224 224 end
225 225
226 226 assert_redirected_to '/projects/ecookbook/time_entries'
227 227 time_entry = TimeEntry.first(:order => 'id DESC')
228 228 assert_equal 1, time_entry.project_id
229 229 end
230 230
231 231 def test_create_without_project_should_fail_with_issue_not_inside_project
232 232 @request.session[:user_id] = 2
233 233 assert_no_difference 'TimeEntry.count' do
234 234 post :create, :time_entry => {:project_id => '1',
235 235 :activity_id => '11',
236 236 :issue_id => '5',
237 237 :spent_on => '2008-03-14',
238 238 :hours => '7.3'}
239 239 end
240 240
241 241 assert_response :success
242 242 assert assigns(:time_entry).errors[:issue_id].present?
243 243 end
244 244
245 245 def test_create_without_project_should_deny_without_permission
246 246 @request.session[:user_id] = 2
247 247 Project.find(3).disable_module!(:time_tracking)
248 248
249 249 assert_no_difference 'TimeEntry.count' do
250 250 post :create, :time_entry => {:project_id => '3',
251 251 :activity_id => '11',
252 252 :issue_id => '',
253 253 :spent_on => '2008-03-14',
254 254 :hours => '7.3'}
255 255 end
256 256
257 257 assert_response 403
258 258 end
259 259
260 260 def test_create_without_project_with_failure
261 261 @request.session[:user_id] = 2
262 262 assert_no_difference 'TimeEntry.count' do
263 263 post :create, :time_entry => {:project_id => '1',
264 264 :activity_id => '11',
265 265 :issue_id => '',
266 266 :spent_on => '2008-03-14',
267 267 :hours => ''}
268 268 end
269 269
270 270 assert_response :success
271 271 assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'},
272 272 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
273 273 end
274 274
275 275 def test_update
276 276 entry = TimeEntry.find(1)
277 277 assert_equal 1, entry.issue_id
278 278 assert_equal 2, entry.user_id
279 279
280 280 @request.session[:user_id] = 1
281 281 put :update, :id => 1,
282 282 :time_entry => {:issue_id => '2',
283 283 :hours => '8'}
284 284 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
285 285 entry.reload
286 286
287 287 assert_equal 8, entry.hours
288 288 assert_equal 2, entry.issue_id
289 289 assert_equal 2, entry.user_id
290 290 end
291 291
292 292 def test_get_bulk_edit
293 293 @request.session[:user_id] = 2
294 294 get :bulk_edit, :ids => [1, 2]
295 295 assert_response :success
296 296 assert_template 'bulk_edit'
297 297
298 298 assert_select 'ul#bulk-selection' do
299 299 assert_select 'li', 2
300 300 assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours'
301 301 end
302 302
303 303 assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
304 304 # System wide custom field
305 305 assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
306 306
307 307 # Activities
308 308 assert_select 'select[name=?]', 'time_entry[activity_id]' do
309 309 assert_select 'option[value=]', :text => '(No change)'
310 310 assert_select 'option[value=9]', :text => 'Design'
311 311 end
312 312 end
313 313 end
314 314
315 315 def test_get_bulk_edit_on_different_projects
316 316 @request.session[:user_id] = 2
317 317 get :bulk_edit, :ids => [1, 2, 6]
318 318 assert_response :success
319 319 assert_template 'bulk_edit'
320 320 end
321 321
322 322 def test_bulk_update
323 323 @request.session[:user_id] = 2
324 324 # update time entry activity
325 325 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
326 326
327 327 assert_response 302
328 328 # check that the issues were updated
329 329 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
330 330 end
331 331
332 332 def test_bulk_update_with_failure
333 333 @request.session[:user_id] = 2
334 334 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
335 335
336 336 assert_response 302
337 337 assert_match /Failed to save 2 time entrie/, flash[:error]
338 338 end
339 339
340 340 def test_bulk_update_on_different_projects
341 341 @request.session[:user_id] = 2
342 342 # makes user a manager on the other project
343 343 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
344 344
345 345 # update time entry activity
346 346 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
347 347
348 348 assert_response 302
349 349 # check that the issues were updated
350 350 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
351 351 end
352 352
353 353 def test_bulk_update_on_different_projects_without_rights
354 354 @request.session[:user_id] = 3
355 355 user = User.find(3)
356 356 action = { :controller => "timelog", :action => "bulk_update" }
357 357 assert user.allowed_to?(action, TimeEntry.find(1).project)
358 358 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
359 359 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
360 360 assert_response 403
361 361 end
362 362
363 363 def test_bulk_update_custom_field
364 364 @request.session[:user_id] = 2
365 365 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
366 366
367 367 assert_response 302
368 368 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
369 369 end
370 370
371 371 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
372 372 @request.session[:user_id] = 2
373 373 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
374 374
375 375 assert_response :redirect
376 376 assert_redirected_to '/time_entries'
377 377 end
378 378
379 379 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
380 380 @request.session[:user_id] = 2
381 381 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
382 382
383 383 assert_response :redirect
384 384 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
385 385 end
386 386
387 387 def test_post_bulk_update_without_edit_permission_should_be_denied
388 388 @request.session[:user_id] = 2
389 389 Role.find_by_name('Manager').remove_permission! :edit_time_entries
390 390 post :bulk_update, :ids => [1,2]
391 391
392 392 assert_response 403
393 393 end
394 394
395 395 def test_destroy
396 396 @request.session[:user_id] = 2
397 397 delete :destroy, :id => 1
398 398 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
399 399 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
400 400 assert_nil TimeEntry.find_by_id(1)
401 401 end
402 402
403 403 def test_destroy_should_fail
404 404 # simulate that this fails (e.g. due to a plugin), see #5700
405 405 TimeEntry.any_instance.expects(:destroy).returns(false)
406 406
407 407 @request.session[:user_id] = 2
408 408 delete :destroy, :id => 1
409 409 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
410 410 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
411 411 assert_not_nil TimeEntry.find_by_id(1)
412 412 end
413 413
414 414 def test_index_all_projects
415 415 get :index
416 416 assert_response :success
417 417 assert_template 'index'
418 418 assert_not_nil assigns(:total_hours)
419 419 assert_equal "162.90", "%.2f" % assigns(:total_hours)
420 420 assert_tag :form,
421 421 :attributes => {:action => "/time_entries", :id => 'query_form'}
422 422 end
423 423
424 424 def test_index_all_projects_should_show_log_time_link
425 425 @request.session[:user_id] = 2
426 426 get :index
427 427 assert_response :success
428 428 assert_template 'index'
429 429 assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/
430 430 end
431 431
432 432 def test_index_at_project_level
433 433 get :index, :project_id => 'ecookbook'
434 434 assert_response :success
435 435 assert_template 'index'
436 436 assert_not_nil assigns(:entries)
437 437 assert_equal 4, assigns(:entries).size
438 438 # project and subproject
439 439 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
440 440 assert_not_nil assigns(:total_hours)
441 441 assert_equal "162.90", "%.2f" % assigns(:total_hours)
442 442 assert_tag :form,
443 443 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
444 444 end
445 445
446 446 def test_index_at_project_level_with_date_range
447 447 get :index, :project_id => 'ecookbook',
448 448 :f => ['spent_on'],
449 449 :op => {'spent_on' => '><'},
450 450 :v => {'spent_on' => ['2007-03-20', '2007-04-30']}
451 451 assert_response :success
452 452 assert_template 'index'
453 453 assert_not_nil assigns(:entries)
454 454 assert_equal 3, assigns(:entries).size
455 455 assert_not_nil assigns(:total_hours)
456 456 assert_equal "12.90", "%.2f" % assigns(:total_hours)
457 457 assert_tag :form,
458 458 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
459 459 end
460 460
461 461 def test_index_at_project_level_with_date_range_using_from_and_to_params
462 462 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
463 463 assert_response :success
464 464 assert_template 'index'
465 465 assert_not_nil assigns(:entries)
466 466 assert_equal 3, assigns(:entries).size
467 467 assert_not_nil assigns(:total_hours)
468 468 assert_equal "12.90", "%.2f" % assigns(:total_hours)
469 469 assert_tag :form,
470 470 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
471 471 end
472 472
473 473 def test_index_at_project_level_with_period
474 474 get :index, :project_id => 'ecookbook',
475 475 :f => ['spent_on'],
476 476 :op => {'spent_on' => '>t-'},
477 477 :v => {'spent_on' => ['7']}
478 478 assert_response :success
479 479 assert_template 'index'
480 480 assert_not_nil assigns(:entries)
481 481 assert_not_nil assigns(:total_hours)
482 482 assert_tag :form,
483 483 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
484 484 end
485 485
486 486 def test_index_at_issue_level
487 487 get :index, :issue_id => 1
488 488 assert_response :success
489 489 assert_template 'index'
490 490 assert_not_nil assigns(:entries)
491 491 assert_equal 2, assigns(:entries).size
492 492 assert_not_nil assigns(:total_hours)
493 493 assert_equal 154.25, assigns(:total_hours)
494 494 # display all time
495 495 assert_nil assigns(:from)
496 496 assert_nil assigns(:to)
497 497 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
498 498 # to use /issues/:issue_id/time_entries
499 499 assert_tag :form,
500 500 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
501 501 end
502 502
503 503 def test_index_should_sort_by_spent_on_and_created_on
504 504 t1 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:00:00', :activity_id => 10)
505 505 t2 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:05:00', :activity_id => 10)
506 506 t3 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-15', :created_on => '2012-06-16 20:10:00', :activity_id => 10)
507 507
508 508 get :index, :project_id => 1,
509 509 :f => ['spent_on'],
510 510 :op => {'spent_on' => '><'},
511 511 :v => {'spent_on' => ['2012-06-15', '2012-06-16']}
512 512 assert_response :success
513 513 assert_equal [t2, t1, t3], assigns(:entries)
514 514
515 515 get :index, :project_id => 1,
516 516 :f => ['spent_on'],
517 517 :op => {'spent_on' => '><'},
518 518 :v => {'spent_on' => ['2012-06-15', '2012-06-16']},
519 519 :sort => 'spent_on'
520 520 assert_response :success
521 521 assert_equal [t3, t1, t2], assigns(:entries)
522 522 end
523 523
524 524 def test_index_with_filter_on_issue_custom_field
525 525 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
526 526 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
527 527
528 528 get :index, :f => ['issue.cf_2'], :op => {'issue.cf_2' => '='}, :v => {'issue.cf_2' => ['filter_on_issue_custom_field']}
529 529 assert_response :success
530 530 assert_equal [entry], assigns(:entries)
531 531 end
532 532
533 533 def test_index_with_issue_custom_field_column
534 534 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
535 535 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
536 536
537 537 get :index, :c => %w(project spent_on issue comments hours issue.cf_2)
538 538 assert_response :success
539 539 assert_include :'issue.cf_2', assigns(:query).column_names
540 540 assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field'
541 541 end
542 542
543 543 def test_index_atom_feed
544 544 get :index, :project_id => 1, :format => 'atom'
545 545 assert_response :success
546 546 assert_equal 'application/atom+xml', @response.content_type
547 547 assert_not_nil assigns(:items)
548 548 assert assigns(:items).first.is_a?(TimeEntry)
549 549 end
550 550
551 def test_index_at_project_level_should_include_csv_export_dialog
552 get :index, :project_id => 'ecookbook',
553 :f => ['spent_on'],
554 :op => {'spent_on' => '>='},
555 :v => {'spent_on' => ['2007-04-01']},
556 :c => ['spent_on', 'user']
557 assert_response :success
558
559 assert_select '#csv-export-options' do
560 assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do
561 # filter
562 assert_select 'input[name=?][value=?]', 'f[]', 'spent_on'
563 assert_select 'input[name=?][value=?]', 'op[spent_on]', '&gt;='
564 assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01'
565 # columns
566 assert_select 'input[name=?][value=?]', 'c[]', 'spent_on'
567 assert_select 'input[name=?][value=?]', 'c[]', 'user'
568 assert_select 'input[name=?]', 'c[]', 2
569 end
570 end
571 end
572
573 def test_index_cross_project_should_include_csv_export_dialog
574 get :index
575 assert_response :success
576
577 assert_select '#csv-export-options' do
578 assert_select 'form[action=?][method=get]', '/time_entries.csv'
579 end
580 end
581
582 def test_index_at_issue_level_should_include_csv_export_dialog
583 get :index, :project_id => 'ecookbook', :issue_id => 3
584 assert_response :success
585
586 assert_select '#csv-export-options' do
587 assert_select 'form[action=?][method=get]', '/projects/ecookbook/issues/3/time_entries.csv'
588 end
589 end
590
551 591 def test_index_csv_all_projects
552 592 Setting.date_format = '%m/%d/%Y'
553 593 get :index, :format => 'csv'
554 594 assert_response :success
555 595 assert_equal 'text/csv; header=present', response.content_type
556 596 end
557 597
558 598 def test_index_csv
559 599 Setting.date_format = '%m/%d/%Y'
560 600 get :index, :project_id => 1, :format => 'csv'
561 601 assert_response :success
562 602 assert_equal 'text/csv; header=present', response.content_type
563 603 end
564 604 end
General Comments 0
You need to be logged in to leave comments. Login now