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