##// END OF EJS Templates
Merged r14795 and r14796 (#21150)....
Jean-Philippe Lang -
r14455:bff6ff9273cf
parent child
Show More
@@ -1,160 +1,160
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class TimeEntry < ActiveRecord::Base
19 19 include Redmine::SafeAttributes
20 20 # could have used polymorphic association
21 21 # project association here allows easy loading of time entries at project level with one database trip
22 22 belongs_to :project
23 23 belongs_to :issue
24 24 belongs_to :user
25 25 belongs_to :activity, :class_name => 'TimeEntryActivity'
26 26
27 27 attr_protected :user_id, :tyear, :tmonth, :tweek
28 28
29 29 acts_as_customizable
30 30 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
31 31 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
32 32 :author => :user,
33 33 :group => :issue,
34 34 :description => :comments
35 35
36 36 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
37 37 :author_key => :user_id,
38 38 :scope => joins(:project).preload(:project)
39 39
40 40 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
41 41 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
42 42 validates_length_of :comments, :maximum => 255, :allow_nil => true
43 43 validates :spent_on, :date => true
44 44 before_validation :set_project_if_nil
45 45 validate :validate_time_entry
46 46
47 47 scope :visible, lambda {|*args|
48 48 joins(:project).
49 49 where(TimeEntry.visible_condition(args.shift || User.current, *args))
50 50 }
51 51 scope :on_issue, lambda {|issue|
52 52 joins(:issue).
53 53 where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
54 54 }
55 55
56 56 safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
57 57
58 58 # Returns a SQL conditions string used to find all time entries visible by the specified user
59 59 def self.visible_condition(user, options={})
60 60 Project.allowed_to_condition(user, :view_time_entries, options) do |role, user|
61 61 if role.time_entries_visibility == 'all'
62 62 nil
63 63 elsif role.time_entries_visibility == 'own' && user.id && user.logged?
64 64 "#{table_name}.user_id = #{user.id}"
65 65 else
66 66 '1=0'
67 67 end
68 68 end
69 69 end
70 70
71 71 # Returns true if user or current user is allowed to view the time entry
72 72 def visible?(user=nil)
73 73 (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
74 74 if role.time_entries_visibility == 'all'
75 75 true
76 76 elsif role.time_entries_visibility == 'own'
77 77 self.user == user
78 78 else
79 79 false
80 80 end
81 81 end
82 82 end
83 83
84 84 def initialize(attributes=nil, *args)
85 85 super
86 86 if new_record? && self.activity.nil?
87 87 if default_activity = TimeEntryActivity.default
88 88 self.activity_id = default_activity.id
89 89 end
90 90 self.hours = nil if hours == 0
91 91 end
92 92 end
93 93
94 94 def safe_attributes=(attrs, user=User.current)
95 95 if attrs
96 96 attrs = super(attrs)
97 97 if issue_id_changed? && issue
98 if user.allowed_to?(:log_time, issue.project)
98 if issue.visible?(user) && user.allowed_to?(:log_time, issue.project)
99 99 if attrs[:project_id].blank? && issue.project_id != project_id
100 100 self.project_id = issue.project_id
101 101 end
102 102 @invalid_issue_id = nil
103 103 else
104 104 @invalid_issue_id = issue_id
105 105 end
106 106 end
107 107 end
108 108 attrs
109 109 end
110 110
111 111 def set_project_if_nil
112 112 self.project = issue.project if issue && project.nil?
113 113 end
114 114
115 115 def validate_time_entry
116 116 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
117 117 errors.add :project_id, :invalid if project.nil?
118 118 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id
119 119 errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity)
120 120 end
121 121
122 122 def hours=(h)
123 123 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
124 124 end
125 125
126 126 def hours
127 127 h = read_attribute(:hours)
128 128 if h.is_a?(Float)
129 129 h.round(2)
130 130 else
131 131 h
132 132 end
133 133 end
134 134
135 135 # tyear, tmonth, tweek assigned where setting spent_on attributes
136 136 # these attributes make time aggregations easier
137 137 def spent_on=(date)
138 138 super
139 139 self.tyear = spent_on ? spent_on.year : nil
140 140 self.tmonth = spent_on ? spent_on.month : nil
141 141 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
142 142 end
143 143
144 144 # Returns true if the time entry can be edited by usr, otherwise false
145 145 def editable_by?(usr)
146 146 visible?(usr) && (
147 147 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
148 148 )
149 149 end
150 150
151 151 # Returns the custom_field_values that can be edited by the given user
152 152 def editable_custom_field_values(user=nil)
153 153 visible_custom_field_values
154 154 end
155 155
156 156 # Returns the custom fields that can be edited by the given user
157 157 def editable_custom_fields(user=nil)
158 158 editable_custom_field_values(user).map(&:custom_field).uniq
159 159 end
160 160 end
@@ -1,47 +1,49
1 1 <%= error_messages_for 'time_entry' %>
2 2 <%= back_url_hidden_field_tag %>
3 3
4 4 <div class="box tabular">
5 5 <% if @time_entry.new_record? %>
6 6 <% if params[:project_id] %>
7 7 <%= hidden_field_tag 'project_id', params[:project_id] %>
8 8 <% elsif params[:issue_id] %>
9 9 <%= hidden_field_tag 'issue_id', params[:issue_id] %>
10 10 <% else %>
11 11 <p><%= f.select :project_id, project_tree_options_for_select(Project.allowed_to(:log_time).to_a, :selected => @time_entry.project, :include_blank => true) %></p>
12 12 <% end %>
13 13 <% end %>
14 14 <p>
15 15 <%= f.text_field :issue_id, :size => 6 %>
16 <span id="time_entry_issue"><%= "#{@time_entry.issue.tracker.name} ##{@time_entry.issue.id}: #{@time_entry.issue.subject}" if @time_entry.issue %></span>
16 <% if @time_entry.issue.try(:visible?) %>
17 <span id="time_entry_issue"><%= "#{@time_entry.issue.tracker.name} ##{@time_entry.issue.id}: #{@time_entry.issue.subject}" %></span>
18 <% end %>
17 19 </p>
18 20 <p><%= f.text_field :spent_on, :size => 10, :required => true %><%= calendar_for('time_entry_spent_on') %></p>
19 21 <p><%= f.text_field :hours, :size => 6, :required => true %></p>
20 22 <p><%= f.text_field :comments, :size => 100, :maxlength => 255 %></p>
21 23 <p><%= f.select :activity_id, activity_collection_for_select_options(@time_entry), :required => true %></p>
22 24 <% @time_entry.custom_field_values.each do |value| %>
23 25 <p><%= custom_field_tag_with_label :time_entry, value %></p>
24 26 <% end %>
25 27 <%= call_hook(:view_timelog_edit_form_bottom, { :time_entry => @time_entry, :form => f }) %>
26 28 </div>
27 29
28 30 <%= javascript_tag do %>
29 31 <% if @time_entry.new_record? %>
30 32 $(document).ready(function(){
31 33 $('#time_entry_project_id, #time_entry_issue_id').change(function(){
32 34 $.ajax({
33 35 url: '<%= escape_javascript new_time_entry_path(:format => 'js') %>',
34 36 type: 'post',
35 37 data: $('#new_time_entry').serialize()
36 38 });
37 39 });
38 40 });
39 41 <% end %>
40 42
41 43 observeAutocompleteField('time_entry_issue_id', '<%= escape_javascript auto_complete_issues_path(:project_id => @project, :scope => (@project ? nil : 'all'))%>', {
42 44 select: function(event, ui) {
43 45 $('#time_entry_issue').text(ui.item.label);
44 46 $('#time_entry_issue_id').blur();
45 47 }
46 48 });
47 49 <% end %>
@@ -1,809 +1,826
1 1 # -*- coding: utf-8 -*-
2 2 # Redmine - project management software
3 3 # Copyright (C) 2006-2015 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
32 32 @request.session[:user_id] = 3
33 33 get :new
34 34 assert_response :success
35 35 assert_template 'new'
36 36 assert_select 'input[name=?][type=hidden]', 'project_id', 0
37 37 assert_select 'input[name=?][type=hidden]', 'issue_id', 0
38 38 assert_select 'select[name=?]', 'time_entry[project_id]' do
39 39 # blank option for project
40 40 assert_select 'option[value=""]'
41 41 end
42 42 end
43 43
44 44 def test_new_with_project_id
45 45 @request.session[:user_id] = 3
46 46 get :new, :project_id => 1
47 47 assert_response :success
48 48 assert_template 'new'
49 49 assert_select 'input[name=?][type=hidden]', 'project_id'
50 50 assert_select 'input[name=?][type=hidden]', 'issue_id', 0
51 51 assert_select 'select[name=?]', 'time_entry[project_id]', 0
52 52 end
53 53
54 54 def test_new_with_issue_id
55 55 @request.session[:user_id] = 3
56 56 get :new, :issue_id => 2
57 57 assert_response :success
58 58 assert_template 'new'
59 59 assert_select 'input[name=?][type=hidden]', 'project_id', 0
60 60 assert_select 'input[name=?][type=hidden]', 'issue_id'
61 61 assert_select 'select[name=?]', 'time_entry[project_id]', 0
62 62 end
63 63
64 64 def test_new_without_project_should_prefill_the_form
65 65 @request.session[:user_id] = 3
66 66 get :new, :time_entry => {:project_id => '1'}
67 67 assert_response :success
68 68 assert_template 'new'
69 69 assert_select 'select[name=?]', 'time_entry[project_id]' do
70 70 assert_select 'option[value="1"][selected=selected]'
71 71 end
72 72 end
73 73
74 74 def test_new_without_project_should_deny_without_permission
75 75 Role.all.each {|role| role.remove_permission! :log_time}
76 76 @request.session[:user_id] = 3
77 77
78 78 get :new
79 79 assert_response 403
80 80 end
81 81
82 82 def test_new_should_select_default_activity
83 83 @request.session[:user_id] = 3
84 84 get :new, :project_id => 1
85 85 assert_response :success
86 86 assert_select 'select[name=?]', 'time_entry[activity_id]' do
87 87 assert_select 'option[selected=selected]', :text => 'Development'
88 88 end
89 89 end
90 90
91 91 def test_new_should_only_show_active_time_entry_activities
92 92 @request.session[:user_id] = 3
93 93 get :new, :project_id => 1
94 94 assert_response :success
95 95 assert_select 'option', :text => 'Inactive Activity', :count => 0
96 96 end
97 97
98 98 def test_post_new_as_js_should_update_activity_options
99 99 @request.session[:user_id] = 3
100 100 post :new, :time_entry => {:project_id => 1}, :format => 'js'
101 101 assert_response :success
102 102 assert_include '#time_entry_activity_id', response.body
103 103 end
104 104
105 105 def test_get_edit_existing_time
106 106 @request.session[:user_id] = 2
107 107 get :edit, :id => 2, :project_id => nil
108 108 assert_response :success
109 109 assert_template 'edit'
110 110 assert_select 'form[action=?]', '/time_entries/2'
111 111 end
112 112
113 113 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
114 114 te = TimeEntry.find(1)
115 115 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
116 116 te.save!(:validate => false)
117 117
118 118 @request.session[:user_id] = 1
119 119 get :edit, :project_id => 1, :id => 1
120 120 assert_response :success
121 121 assert_template 'edit'
122 122 # Blank option since nothing is pre-selected
123 123 assert_select 'option', :text => '--- Please select ---'
124 124 end
125 125
126 126 def test_post_create
127 127 @request.session[:user_id] = 3
128 128 assert_difference 'TimeEntry.count' do
129 129 post :create, :project_id => 1,
130 130 :time_entry => {:comments => 'Some work on TimelogControllerTest',
131 131 # Not the default activity
132 132 :activity_id => '11',
133 133 :spent_on => '2008-03-14',
134 134 :issue_id => '1',
135 135 :hours => '7.3'}
136 136 assert_redirected_to '/projects/ecookbook/time_entries'
137 137 end
138 138
139 139 t = TimeEntry.order('id DESC').first
140 140 assert_not_nil t
141 141 assert_equal 'Some work on TimelogControllerTest', t.comments
142 142 assert_equal 1, t.project_id
143 143 assert_equal 1, t.issue_id
144 144 assert_equal 11, t.activity_id
145 145 assert_equal 7.3, t.hours
146 146 assert_equal 3, t.user_id
147 147 end
148 148
149 149 def test_post_create_with_blank_issue
150 150 @request.session[:user_id] = 3
151 151 assert_difference 'TimeEntry.count' do
152 152 post :create, :project_id => 1,
153 153 :time_entry => {:comments => 'Some work on TimelogControllerTest',
154 154 # Not the default activity
155 155 :activity_id => '11',
156 156 :issue_id => '',
157 157 :spent_on => '2008-03-14',
158 158 :hours => '7.3'}
159 159 assert_redirected_to '/projects/ecookbook/time_entries'
160 160 end
161 161
162 162 t = TimeEntry.order('id DESC').first
163 163 assert_not_nil t
164 164 assert_equal 'Some work on TimelogControllerTest', t.comments
165 165 assert_equal 1, t.project_id
166 166 assert_nil t.issue_id
167 167 assert_equal 11, t.activity_id
168 168 assert_equal 7.3, t.hours
169 169 assert_equal 3, t.user_id
170 170 end
171 171
172 172 def test_create_on_project_with_time_tracking_disabled_should_fail
173 173 Project.find(1).disable_module! :time_tracking
174 174
175 175 @request.session[:user_id] = 2
176 176 assert_no_difference 'TimeEntry.count' do
177 177 post :create, :time_entry => {
178 178 :project_id => '1', :issue_id => '',
179 179 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
180 180 }
181 181 end
182 182 end
183 183
184 184 def test_create_on_project_without_permission_should_fail
185 185 Role.find(1).remove_permission! :log_time
186 186
187 187 @request.session[:user_id] = 2
188 188 assert_no_difference 'TimeEntry.count' do
189 189 post :create, :time_entry => {
190 190 :project_id => '1', :issue_id => '',
191 191 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
192 192 }
193 193 end
194 194 end
195 195
196 196 def test_create_on_issue_in_project_with_time_tracking_disabled_should_fail
197 197 Project.find(1).disable_module! :time_tracking
198 198
199 199 @request.session[:user_id] = 2
200 200 assert_no_difference 'TimeEntry.count' do
201 201 post :create, :time_entry => {
202 202 :project_id => '', :issue_id => '1',
203 203 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
204 204 }
205 205 assert_select_error /Issue is invalid/
206 206 end
207 207 end
208 208
209 209 def test_create_on_issue_in_project_without_permission_should_fail
210 210 Role.find(1).remove_permission! :log_time
211 211
212 212 @request.session[:user_id] = 2
213 213 assert_no_difference 'TimeEntry.count' do
214 214 post :create, :time_entry => {
215 215 :project_id => '', :issue_id => '1',
216 216 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
217 217 }
218 218 assert_select_error /Issue is invalid/
219 219 end
220 220 end
221 221
222 def test_create_on_issue_that_is_not_visible_should_not_disclose_subject
223 issue = Issue.generate!(:subject => "issue_that_is_not_visible", :is_private => true)
224 assert !issue.visible?(User.find(3))
225
226 @request.session[:user_id] = 3
227 assert_no_difference 'TimeEntry.count' do
228 post :create, :time_entry => {
229 :project_id => '', :issue_id => issue.id.to_s,
230 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
231 }
232 end
233 assert_select_error /Issue is invalid/
234 assert_select "input[name=?][value=?]", "time_entry[issue_id]", issue.id.to_s
235 assert_select "#time_entry_issue", 0
236 assert !response.body.include?('issue_that_is_not_visible')
237 end
238
222 239 def test_create_and_continue_at_project_level
223 240 @request.session[:user_id] = 2
224 241 assert_difference 'TimeEntry.count' do
225 242 post :create, :time_entry => {:project_id => '1',
226 243 :activity_id => '11',
227 244 :issue_id => '',
228 245 :spent_on => '2008-03-14',
229 246 :hours => '7.3'},
230 247 :continue => '1'
231 248 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1'
232 249 end
233 250 end
234 251
235 252 def test_create_and_continue_at_issue_level
236 253 @request.session[:user_id] = 2
237 254 assert_difference 'TimeEntry.count' do
238 255 post :create, :time_entry => {:project_id => '',
239 256 :activity_id => '11',
240 257 :issue_id => '1',
241 258 :spent_on => '2008-03-14',
242 259 :hours => '7.3'},
243 260 :continue => '1'
244 261 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D='
245 262 end
246 263 end
247 264
248 265 def test_create_and_continue_with_project_id
249 266 @request.session[:user_id] = 2
250 267 assert_difference 'TimeEntry.count' do
251 268 post :create, :project_id => 1,
252 269 :time_entry => {:activity_id => '11',
253 270 :issue_id => '',
254 271 :spent_on => '2008-03-14',
255 272 :hours => '7.3'},
256 273 :continue => '1'
257 274 assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D='
258 275 end
259 276 end
260 277
261 278 def test_create_and_continue_with_issue_id
262 279 @request.session[:user_id] = 2
263 280 assert_difference 'TimeEntry.count' do
264 281 post :create, :issue_id => 1,
265 282 :time_entry => {:activity_id => '11',
266 283 :issue_id => '1',
267 284 :spent_on => '2008-03-14',
268 285 :hours => '7.3'},
269 286 :continue => '1'
270 287 assert_redirected_to '/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D='
271 288 end
272 289 end
273 290
274 291 def test_create_without_log_time_permission_should_be_denied
275 292 @request.session[:user_id] = 2
276 293 Role.find_by_name('Manager').remove_permission! :log_time
277 294 post :create, :project_id => 1,
278 295 :time_entry => {:activity_id => '11',
279 296 :issue_id => '',
280 297 :spent_on => '2008-03-14',
281 298 :hours => '7.3'}
282 299
283 300 assert_response 403
284 301 end
285 302
286 303 def test_create_without_project_and_issue_should_fail
287 304 @request.session[:user_id] = 2
288 305 post :create, :time_entry => {:issue_id => ''}
289 306
290 307 assert_response :success
291 308 assert_template 'new'
292 309 end
293 310
294 311 def test_create_with_failure
295 312 @request.session[:user_id] = 2
296 313 post :create, :project_id => 1,
297 314 :time_entry => {:activity_id => '',
298 315 :issue_id => '',
299 316 :spent_on => '2008-03-14',
300 317 :hours => '7.3'}
301 318
302 319 assert_response :success
303 320 assert_template 'new'
304 321 end
305 322
306 323 def test_create_without_project
307 324 @request.session[:user_id] = 2
308 325 assert_difference 'TimeEntry.count' do
309 326 post :create, :time_entry => {:project_id => '1',
310 327 :activity_id => '11',
311 328 :issue_id => '',
312 329 :spent_on => '2008-03-14',
313 330 :hours => '7.3'}
314 331 end
315 332
316 333 assert_redirected_to '/projects/ecookbook/time_entries'
317 334 time_entry = TimeEntry.order('id DESC').first
318 335 assert_equal 1, time_entry.project_id
319 336 end
320 337
321 338 def test_create_without_project_should_fail_with_issue_not_inside_project
322 339 @request.session[:user_id] = 2
323 340 assert_no_difference 'TimeEntry.count' do
324 341 post :create, :time_entry => {:project_id => '1',
325 342 :activity_id => '11',
326 343 :issue_id => '5',
327 344 :spent_on => '2008-03-14',
328 345 :hours => '7.3'}
329 346 end
330 347
331 348 assert_response :success
332 349 assert assigns(:time_entry).errors[:issue_id].present?
333 350 end
334 351
335 352 def test_create_without_project_should_deny_without_permission
336 353 @request.session[:user_id] = 2
337 354 Project.find(3).disable_module!(:time_tracking)
338 355
339 356 assert_no_difference 'TimeEntry.count' do
340 357 post :create, :time_entry => {:project_id => '3',
341 358 :activity_id => '11',
342 359 :issue_id => '',
343 360 :spent_on => '2008-03-14',
344 361 :hours => '7.3'}
345 362 end
346 363
347 364 assert_response 403
348 365 end
349 366
350 367 def test_create_without_project_with_failure
351 368 @request.session[:user_id] = 2
352 369 assert_no_difference 'TimeEntry.count' do
353 370 post :create, :time_entry => {:project_id => '1',
354 371 :activity_id => '11',
355 372 :issue_id => '',
356 373 :spent_on => '2008-03-14',
357 374 :hours => ''}
358 375 end
359 376
360 377 assert_response :success
361 378 assert_select 'select[name=?]', 'time_entry[project_id]' do
362 379 assert_select 'option[value="1"][selected=selected]'
363 380 end
364 381 end
365 382
366 383 def test_update
367 384 entry = TimeEntry.find(1)
368 385 assert_equal 1, entry.issue_id
369 386 assert_equal 2, entry.user_id
370 387
371 388 @request.session[:user_id] = 1
372 389 put :update, :id => 1,
373 390 :time_entry => {:issue_id => '2',
374 391 :hours => '8'}
375 392 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
376 393 entry.reload
377 394
378 395 assert_equal 8, entry.hours
379 396 assert_equal 2, entry.issue_id
380 397 assert_equal 2, entry.user_id
381 398 end
382 399
383 400 def test_update_should_allow_to_change_issue_to_another_project
384 401 entry = TimeEntry.generate!(:issue_id => 1)
385 402
386 403 @request.session[:user_id] = 1
387 404 put :update, :id => entry.id, :time_entry => {:issue_id => '5'}
388 405 assert_response 302
389 406 entry.reload
390 407
391 408 assert_equal 5, entry.issue_id
392 409 assert_equal 3, entry.project_id
393 410 end
394 411
395 412 def test_update_should_not_allow_to_change_issue_to_an_invalid_project
396 413 entry = TimeEntry.generate!(:issue_id => 1)
397 414 Project.find(3).disable_module!(:time_tracking)
398 415
399 416 @request.session[:user_id] = 1
400 417 put :update, :id => entry.id, :time_entry => {:issue_id => '5'}
401 418 assert_response 200
402 419 assert_include "Issue is invalid", assigns(:time_entry).errors.full_messages
403 420 end
404 421
405 422 def test_get_bulk_edit
406 423 @request.session[:user_id] = 2
407 424 get :bulk_edit, :ids => [1, 2]
408 425 assert_response :success
409 426 assert_template 'bulk_edit'
410 427
411 428 assert_select 'ul#bulk-selection' do
412 429 assert_select 'li', 2
413 430 assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours'
414 431 end
415 432
416 433 assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
417 434 # System wide custom field
418 435 assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
419 436
420 437 # Activities
421 438 assert_select 'select[name=?]', 'time_entry[activity_id]' do
422 439 assert_select 'option[value=""]', :text => '(No change)'
423 440 assert_select 'option[value="9"]', :text => 'Design'
424 441 end
425 442 end
426 443 end
427 444
428 445 def test_get_bulk_edit_on_different_projects
429 446 @request.session[:user_id] = 2
430 447 get :bulk_edit, :ids => [1, 2, 6]
431 448 assert_response :success
432 449 assert_template 'bulk_edit'
433 450 end
434 451
435 452 def test_bulk_edit_with_edit_own_time_entries_permission
436 453 @request.session[:user_id] = 2
437 454 Role.find_by_name('Manager').remove_permission! :edit_time_entries
438 455 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
439 456 ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
440 457
441 458 get :bulk_edit, :ids => ids
442 459 assert_response :success
443 460 end
444 461
445 462 def test_bulk_update
446 463 @request.session[:user_id] = 2
447 464 # update time entry activity
448 465 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
449 466
450 467 assert_response 302
451 468 # check that the issues were updated
452 469 assert_equal [9, 9], TimeEntry.where(:id => [1, 2]).collect {|i| i.activity_id}
453 470 end
454 471
455 472 def test_bulk_update_with_failure
456 473 @request.session[:user_id] = 2
457 474 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
458 475
459 476 assert_response 302
460 477 assert_match /Failed to save 2 time entrie/, flash[:error]
461 478 end
462 479
463 480 def test_bulk_update_on_different_projects
464 481 @request.session[:user_id] = 2
465 482 # makes user a manager on the other project
466 483 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
467 484
468 485 # update time entry activity
469 486 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
470 487
471 488 assert_response 302
472 489 # check that the issues were updated
473 490 assert_equal [9, 9, 9], TimeEntry.where(:id => [1, 2, 4]).collect {|i| i.activity_id}
474 491 end
475 492
476 493 def test_bulk_update_on_different_projects_without_rights
477 494 @request.session[:user_id] = 3
478 495 user = User.find(3)
479 496 action = { :controller => "timelog", :action => "bulk_update" }
480 497 assert user.allowed_to?(action, TimeEntry.find(1).project)
481 498 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
482 499 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
483 500 assert_response 403
484 501 end
485 502
486 503 def test_bulk_update_with_edit_own_time_entries_permission
487 504 @request.session[:user_id] = 2
488 505 Role.find_by_name('Manager').remove_permission! :edit_time_entries
489 506 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
490 507 ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
491 508
492 509 post :bulk_update, :ids => ids, :time_entry => { :activity_id => 9 }
493 510 assert_response 302
494 511 end
495 512
496 513 def test_bulk_update_with_edit_own_time_entries_permissions_should_be_denied_for_time_entries_of_other_user
497 514 @request.session[:user_id] = 2
498 515 Role.find_by_name('Manager').remove_permission! :edit_time_entries
499 516 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
500 517
501 518 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9 }
502 519 assert_response 403
503 520 end
504 521
505 522 def test_bulk_update_custom_field
506 523 @request.session[:user_id] = 2
507 524 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
508 525
509 526 assert_response 302
510 527 assert_equal ["0", "0"], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(10).value}
511 528 end
512 529
513 530 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
514 531 @request.session[:user_id] = 2
515 532 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
516 533
517 534 assert_response :redirect
518 535 assert_redirected_to '/time_entries'
519 536 end
520 537
521 538 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
522 539 @request.session[:user_id] = 2
523 540 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
524 541
525 542 assert_response :redirect
526 543 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
527 544 end
528 545
529 546 def test_post_bulk_update_without_edit_permission_should_be_denied
530 547 @request.session[:user_id] = 2
531 548 Role.find_by_name('Manager').remove_permission! :edit_time_entries
532 549 post :bulk_update, :ids => [1,2]
533 550
534 551 assert_response 403
535 552 end
536 553
537 554 def test_destroy
538 555 @request.session[:user_id] = 2
539 556 delete :destroy, :id => 1
540 557 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
541 558 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
542 559 assert_nil TimeEntry.find_by_id(1)
543 560 end
544 561
545 562 def test_destroy_should_fail
546 563 # simulate that this fails (e.g. due to a plugin), see #5700
547 564 TimeEntry.any_instance.expects(:destroy).returns(false)
548 565
549 566 @request.session[:user_id] = 2
550 567 delete :destroy, :id => 1
551 568 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
552 569 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
553 570 assert_not_nil TimeEntry.find_by_id(1)
554 571 end
555 572
556 573 def test_index_all_projects
557 574 get :index
558 575 assert_response :success
559 576 assert_template 'index'
560 577 assert_not_nil assigns(:total_hours)
561 578 assert_equal "162.90", "%.2f" % assigns(:total_hours)
562 579 assert_select 'form#query_form[action=?]', '/time_entries'
563 580 end
564 581
565 582 def test_index_all_projects_should_show_log_time_link
566 583 @request.session[:user_id] = 2
567 584 get :index
568 585 assert_response :success
569 586 assert_template 'index'
570 587 assert_select 'a[href=?]', '/time_entries/new', :text => /Log time/
571 588 end
572 589
573 590 def test_index_my_spent_time
574 591 @request.session[:user_id] = 2
575 592 get :index, :user_id => 'me'
576 593 assert_response :success
577 594 assert_template 'index'
578 595 assert assigns(:entries).all? {|entry| entry.user_id == 2}
579 596 end
580 597
581 598 def test_index_at_project_level
582 599 get :index, :project_id => 'ecookbook'
583 600 assert_response :success
584 601 assert_template 'index'
585 602 assert_not_nil assigns(:entries)
586 603 assert_equal 4, assigns(:entries).size
587 604 # project and subproject
588 605 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
589 606 assert_not_nil assigns(:total_hours)
590 607 assert_equal "162.90", "%.2f" % assigns(:total_hours)
591 608 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
592 609 end
593 610
594 611 def test_index_with_display_subprojects_issues_to_false_should_not_include_subproject_entries
595 612 entry = TimeEntry.generate!(:project => Project.find(3))
596 613
597 614 with_settings :display_subprojects_issues => '0' do
598 615 get :index, :project_id => 'ecookbook'
599 616 assert_response :success
600 617 assert_template 'index'
601 618 assert_not_include entry, assigns(:entries)
602 619 end
603 620 end
604 621
605 622 def test_index_with_display_subprojects_issues_to_false_and_subproject_filter_should_include_subproject_entries
606 623 entry = TimeEntry.generate!(:project => Project.find(3))
607 624
608 625 with_settings :display_subprojects_issues => '0' do
609 626 get :index, :project_id => 'ecookbook', :subproject_id => 3
610 627 assert_response :success
611 628 assert_template 'index'
612 629 assert_include entry, assigns(:entries)
613 630 end
614 631 end
615 632
616 633 def test_index_at_project_level_with_date_range
617 634 get :index, :project_id => 'ecookbook',
618 635 :f => ['spent_on'],
619 636 :op => {'spent_on' => '><'},
620 637 :v => {'spent_on' => ['2007-03-20', '2007-04-30']}
621 638 assert_response :success
622 639 assert_template 'index'
623 640 assert_not_nil assigns(:entries)
624 641 assert_equal 3, assigns(:entries).size
625 642 assert_not_nil assigns(:total_hours)
626 643 assert_equal "12.90", "%.2f" % assigns(:total_hours)
627 644 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
628 645 end
629 646
630 647 def test_index_at_project_level_with_date_range_using_from_and_to_params
631 648 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
632 649 assert_response :success
633 650 assert_template 'index'
634 651 assert_not_nil assigns(:entries)
635 652 assert_equal 3, assigns(:entries).size
636 653 assert_not_nil assigns(:total_hours)
637 654 assert_equal "12.90", "%.2f" % assigns(:total_hours)
638 655 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
639 656 end
640 657
641 658 def test_index_at_project_level_with_period
642 659 get :index, :project_id => 'ecookbook',
643 660 :f => ['spent_on'],
644 661 :op => {'spent_on' => '>t-'},
645 662 :v => {'spent_on' => ['7']}
646 663 assert_response :success
647 664 assert_template 'index'
648 665 assert_not_nil assigns(:entries)
649 666 assert_not_nil assigns(:total_hours)
650 667 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
651 668 end
652 669
653 670 def test_index_at_issue_level
654 671 get :index, :issue_id => 1
655 672 assert_response :success
656 673 assert_template 'index'
657 674 assert_not_nil assigns(:entries)
658 675 assert_equal 2, assigns(:entries).size
659 676 assert_not_nil assigns(:total_hours)
660 677 assert_equal 154.25, assigns(:total_hours)
661 678 # display all time
662 679 assert_nil assigns(:from)
663 680 assert_nil assigns(:to)
664 681 assert_select 'form#query_form[action=?]', '/issues/1/time_entries'
665 682 end
666 683
667 684 def test_index_should_sort_by_spent_on_and_created_on
668 685 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)
669 686 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)
670 687 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)
671 688
672 689 get :index, :project_id => 1,
673 690 :f => ['spent_on'],
674 691 :op => {'spent_on' => '><'},
675 692 :v => {'spent_on' => ['2012-06-15', '2012-06-16']}
676 693 assert_response :success
677 694 assert_equal [t2, t1, t3], assigns(:entries)
678 695
679 696 get :index, :project_id => 1,
680 697 :f => ['spent_on'],
681 698 :op => {'spent_on' => '><'},
682 699 :v => {'spent_on' => ['2012-06-15', '2012-06-16']},
683 700 :sort => 'spent_on'
684 701 assert_response :success
685 702 assert_equal [t3, t1, t2], assigns(:entries)
686 703 end
687 704
688 705 def test_index_with_filter_on_issue_custom_field
689 706 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
690 707 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
691 708
692 709 get :index, :f => ['issue.cf_2'], :op => {'issue.cf_2' => '='}, :v => {'issue.cf_2' => ['filter_on_issue_custom_field']}
693 710 assert_response :success
694 711 assert_equal [entry], assigns(:entries)
695 712 end
696 713
697 714 def test_index_with_issue_custom_field_column
698 715 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
699 716 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
700 717
701 718 get :index, :c => %w(project spent_on issue comments hours issue.cf_2)
702 719 assert_response :success
703 720 assert_include :'issue.cf_2', assigns(:query).column_names
704 721 assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field'
705 722 end
706 723
707 724 def test_index_with_time_entry_custom_field_column
708 725 field = TimeEntryCustomField.generate!(:field_format => 'string')
709 726 entry = TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value'})
710 727 field_name = "cf_#{field.id}"
711 728
712 729 get :index, :c => ["hours", field_name]
713 730 assert_response :success
714 731 assert_include field_name.to_sym, assigns(:query).column_names
715 732 assert_select "td.#{field_name}", :text => 'CF Value'
716 733 end
717 734
718 735 def test_index_with_time_entry_custom_field_sorting
719 736 field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field')
720 737 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'})
721 738 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'})
722 739 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'})
723 740 field_name = "cf_#{field.id}"
724 741
725 742 get :index, :c => ["hours", field_name], :sort => field_name
726 743 assert_response :success
727 744 assert_include field_name.to_sym, assigns(:query).column_names
728 745 assert_select "th a.sort", :text => 'String Field'
729 746
730 747 # Make sure that values are properly sorted
731 748 values = assigns(:entries).map {|e| e.custom_field_value(field)}.compact
732 749 assert_equal 3, values.size
733 750 assert_equal values.sort, values
734 751 end
735 752
736 753 def test_index_atom_feed
737 754 get :index, :project_id => 1, :format => 'atom'
738 755 assert_response :success
739 756 assert_equal 'application/atom+xml', @response.content_type
740 757 assert_not_nil assigns(:items)
741 758 assert assigns(:items).first.is_a?(TimeEntry)
742 759 end
743 760
744 761 def test_index_at_project_level_should_include_csv_export_dialog
745 762 get :index, :project_id => 'ecookbook',
746 763 :f => ['spent_on'],
747 764 :op => {'spent_on' => '>='},
748 765 :v => {'spent_on' => ['2007-04-01']},
749 766 :c => ['spent_on', 'user']
750 767 assert_response :success
751 768
752 769 assert_select '#csv-export-options' do
753 770 assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do
754 771 # filter
755 772 assert_select 'input[name=?][value=?]', 'f[]', 'spent_on'
756 773 assert_select 'input[name=?][value=?]', 'op[spent_on]', '>='
757 774 assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01'
758 775 # columns
759 776 assert_select 'input[name=?][value=?]', 'c[]', 'spent_on'
760 777 assert_select 'input[name=?][value=?]', 'c[]', 'user'
761 778 assert_select 'input[name=?]', 'c[]', 2
762 779 end
763 780 end
764 781 end
765 782
766 783 def test_index_cross_project_should_include_csv_export_dialog
767 784 get :index
768 785 assert_response :success
769 786
770 787 assert_select '#csv-export-options' do
771 788 assert_select 'form[action=?][method=get]', '/time_entries.csv'
772 789 end
773 790 end
774 791
775 792 def test_index_at_issue_level_should_include_csv_export_dialog
776 793 get :index, :issue_id => 3
777 794 assert_response :success
778 795
779 796 assert_select '#csv-export-options' do
780 797 assert_select 'form[action=?][method=get]', '/issues/3/time_entries.csv'
781 798 end
782 799 end
783 800
784 801 def test_index_csv_all_projects
785 802 with_settings :date_format => '%m/%d/%Y' do
786 803 get :index, :format => 'csv'
787 804 assert_response :success
788 805 assert_equal 'text/csv; header=present', response.content_type
789 806 end
790 807 end
791 808
792 809 def test_index_csv
793 810 with_settings :date_format => '%m/%d/%Y' do
794 811 get :index, :project_id => 1, :format => 'csv'
795 812 assert_response :success
796 813 assert_equal 'text/csv; header=present', response.content_type
797 814 end
798 815 end
799 816
800 817 def test_index_csv_should_fill_issue_column_with_tracker_id_and_subject
801 818 issue = Issue.find(1)
802 819 entry = TimeEntry.generate!(:issue => issue, :comments => "Issue column content test")
803 820
804 821 get :index, :format => 'csv'
805 822 line = response.body.split("\n").detect {|l| l.include?(entry.comments)}
806 823 assert_not_nil line
807 824 assert_include "#{issue.tracker} #1: #{issue.subject}", line
808 825 end
809 826 end
General Comments 0
You need to be logged in to leave comments. Login now