To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / e1 / e107366c1a6751ac8fc0593f4fc17da1868e7064.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (18.2 KB)

1
# -*- coding: utf-8 -*-
2
# Redmine - project management software
3
# Copyright (C) 2006-2011  Jean-Philippe Lang
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
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
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18

    
19
require File.expand_path('../../test_helper', __FILE__)
20
require 'timelog_controller'
21

    
22
# Re-raise errors caught by the controller.
23
class TimelogController; def rescue_action(e) raise e end; end
24

    
25
class TimelogControllerTest < ActionController::TestCase
26
  fixtures :projects, :enabled_modules, :roles, :members,
27
           :member_roles, :issues, :time_entries, :users,
28
           :trackers, :enumerations, :issue_statuses,
29
           :custom_fields, :custom_values
30

    
31
  include Redmine::I18n
32

    
33
  def setup
34
    @controller = TimelogController.new
35
    @request    = ActionController::TestRequest.new
36
    @response   = ActionController::TestResponse.new
37
  end
38

    
39
  def test_get_new
40
    @request.session[:user_id] = 3
41
    get :new, :project_id => 1
42
    assert_response :success
43
    assert_template 'edit'
44
    # Default activity selected
45
    assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
46
                                 :content => 'Development'
47
  end
48

    
49
  def test_get_new_should_only_show_active_time_entry_activities
50
    @request.session[:user_id] = 3
51
    get :new, :project_id => 1
52
    assert_response :success
53
    assert_template 'edit'
54
    assert_no_tag :tag => 'option', :content => 'Inactive Activity'
55
  end
56

    
57
  def test_get_edit_existing_time
58
    @request.session[:user_id] = 2
59
    get :edit, :id => 2, :project_id => nil
60
    assert_response :success
61
    assert_template 'edit'
62
    # Default activity selected
63
    assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
64
  end
65

    
66
  def test_get_edit_with_an_existing_time_entry_with_inactive_activity
67
    te = TimeEntry.find(1)
68
    te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
69
    te.save!
70

    
71
    @request.session[:user_id] = 1
72
    get :edit, :project_id => 1, :id => 1
73
    assert_response :success
74
    assert_template 'edit'
75
    # Blank option since nothing is pre-selected
76
    assert_tag :tag => 'option', :content => '--- Please select ---'
77
  end
78

    
79
  def test_post_create
80
    # TODO: should POST to issues’ time log instead of project. change form
81
    # and routing
82
    @request.session[:user_id] = 3
83
    post :create, :project_id => 1,
84
                :time_entry => {:comments => 'Some work on TimelogControllerTest',
85
                                # Not the default activity
86
                                :activity_id => '11',
87
                                :spent_on => '2008-03-14',
88
                                :issue_id => '1',
89
                                :hours => '7.3'}
90
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
91

    
92
    i = Issue.find(1)
93
    t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
94
    assert_not_nil t
95
    assert_equal 11, t.activity_id
96
    assert_equal 7.3, t.hours
97
    assert_equal 3, t.user_id
98
    assert_equal i, t.issue
99
    assert_equal i.project, t.project
100
  end
101

    
102
  def test_post_create_with_blank_issue
103
    # TODO: should POST to issues’ time log instead of project. change form
104
    # and routing
105
    @request.session[:user_id] = 3
106
    post :create, :project_id => 1,
107
                :time_entry => {:comments => 'Some work on TimelogControllerTest',
108
                                # Not the default activity
109
                                :activity_id => '11',
110
                                :issue_id => '',
111
                                :spent_on => '2008-03-14',
112
                                :hours => '7.3'}
113
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
114

    
115
    t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
116
    assert_not_nil t
117
    assert_equal 11, t.activity_id
118
    assert_equal 7.3, t.hours
119
    assert_equal 3, t.user_id
120
  end
121

    
122
  def test_create_without_log_time_permission_should_be_denied
123
    @request.session[:user_id] = 2
124
    Role.find_by_name('Manager').remove_permission! :log_time
125
    post :create, :project_id => 1,
126
                :time_entry => {:activity_id => '11',
127
                                :issue_id => '',
128
                                :spent_on => '2008-03-14',
129
                                :hours => '7.3'}
130

    
131
    assert_response 403
132
  end
133

    
134
  def test_update
135
    entry = TimeEntry.find(1)
136
    assert_equal 1, entry.issue_id
137
    assert_equal 2, entry.user_id
138

    
139
    @request.session[:user_id] = 1
140
    put :update, :id => 1,
141
                :time_entry => {:issue_id => '2',
142
                                :hours => '8'}
143
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
144
    entry.reload
145

    
146
    assert_equal 8, entry.hours
147
    assert_equal 2, entry.issue_id
148
    assert_equal 2, entry.user_id
149
  end
150

    
151
  def test_get_bulk_edit
152
    @request.session[:user_id] = 2
153
    get :bulk_edit, :ids => [1, 2]
154
    assert_response :success
155
    assert_template 'bulk_edit'
156

    
157
    # System wide custom field
158
    assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
159
  end
160

    
161
  def test_get_bulk_edit_on_different_projects
162
    @request.session[:user_id] = 2
163
    get :bulk_edit, :ids => [1, 2, 6]
164
    assert_response :success
165
    assert_template 'bulk_edit'
166
  end
167

    
168
  def test_bulk_update
169
    @request.session[:user_id] = 2
170
    # update time entry activity
171
    post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
172

    
173
    assert_response 302
174
    # check that the issues were updated
175
    assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
176
  end
177

    
178
  def test_bulk_update_on_different_projects
179
    @request.session[:user_id] = 2
180
    # makes user a manager on the other project
181
    Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
182
    
183
    # update time entry activity
184
    post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
185

    
186
    assert_response 302
187
    # check that the issues were updated
188
    assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
189
  end
190

    
191
  def test_bulk_update_on_different_projects_without_rights
192
    @request.session[:user_id] = 3
193
    user = User.find(3)
194
    action = { :controller => "timelog", :action => "bulk_update" }
195
    assert user.allowed_to?(action, TimeEntry.find(1).project)
196
    assert ! user.allowed_to?(action, TimeEntry.find(5).project)
197
    post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
198
    assert_response 403
199
  end
200

    
201
  def test_bulk_update_custom_field
202
    @request.session[:user_id] = 2
203
    post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
204

    
205
    assert_response 302
206
    assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
207
  end
208

    
209
  def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
210
    @request.session[:user_id] = 2
211
    post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
212

    
213
    assert_response :redirect
214
    assert_redirected_to '/time_entries'
215
  end
216

    
217
  def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
218
    @request.session[:user_id] = 2
219
    post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
220

    
221
    assert_response :redirect
222
    assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
223
  end
224

    
225
  def test_post_bulk_update_without_edit_permission_should_be_denied
226
    @request.session[:user_id] = 2
227
    Role.find_by_name('Manager').remove_permission! :edit_time_entries
228
    post :bulk_update, :ids => [1,2]
229

    
230
    assert_response 403
231
  end
232

    
233
  def test_destroy
234
    @request.session[:user_id] = 2
235
    delete :destroy, :id => 1
236
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
237
    assert_equal I18n.t(:notice_successful_delete), flash[:notice]
238
    assert_nil TimeEntry.find_by_id(1)
239
  end
240

    
241
  def test_destroy_should_fail
242
    # simulate that this fails (e.g. due to a plugin), see #5700
243
    TimeEntry.any_instance.expects(:destroy).returns(false)
244

    
245
    @request.session[:user_id] = 2
246
    delete :destroy, :id => 1
247
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
248
    assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
249
    assert_not_nil TimeEntry.find_by_id(1)
250
  end
251

    
252
  def test_index_all_projects
253
    get :index
254
    assert_response :success
255
    assert_template 'index'
256
    assert_not_nil assigns(:total_hours)
257
    assert_equal "162.90", "%.2f" % assigns(:total_hours)
258
    assert_tag :form,
259
      :attributes => {:action => "/time_entries", :id => 'query_form'}
260
  end
261

    
262
  def test_index_at_project_level
263
    get :index, :project_id => 'ecookbook'
264
    assert_response :success
265
    assert_template 'index'
266
    assert_not_nil assigns(:entries)
267
    assert_equal 4, assigns(:entries).size
268
    # project and subproject
269
    assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
270
    assert_not_nil assigns(:total_hours)
271
    assert_equal "162.90", "%.2f" % assigns(:total_hours)
272
    # display all time by default
273
    assert_equal '2007-03-12'.to_date, assigns(:from)
274
    assert_equal '2007-04-22'.to_date, assigns(:to)
275
    assert_tag :form,
276
      :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
277
  end
278

    
279
  def test_index_at_project_level_with_date_range
280
    get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
281
    assert_response :success
282
    assert_template 'index'
283
    assert_not_nil assigns(:entries)
284
    assert_equal 3, assigns(:entries).size
285
    assert_not_nil assigns(:total_hours)
286
    assert_equal "12.90", "%.2f" % assigns(:total_hours)
287
    assert_equal '2007-03-20'.to_date, assigns(:from)
288
    assert_equal '2007-04-30'.to_date, assigns(:to)
289
    assert_tag :form,
290
      :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
291
  end
292

    
293
  def test_index_at_project_level_with_period
294
    get :index, :project_id => 'ecookbook', :period => '7_days'
295
    assert_response :success
296
    assert_template 'index'
297
    assert_not_nil assigns(:entries)
298
    assert_not_nil assigns(:total_hours)
299
    assert_equal Date.today - 7, assigns(:from)
300
    assert_equal Date.today, assigns(:to)
301
    assert_tag :form,
302
      :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
303
  end
304

    
305
  def test_index_one_day
306
    get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
307
    assert_response :success
308
    assert_template 'index'
309
    assert_not_nil assigns(:total_hours)
310
    assert_equal "4.25", "%.2f" % assigns(:total_hours)
311
    assert_tag :form,
312
      :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
313
  end
314

    
315
  def test_index_at_issue_level
316
    get :index, :issue_id => 1
317
    assert_response :success
318
    assert_template 'index'
319
    assert_not_nil assigns(:entries)
320
    assert_equal 2, assigns(:entries).size
321
    assert_not_nil assigns(:total_hours)
322
    assert_equal 154.25, assigns(:total_hours)
323
    # display all time based on what's been logged
324
    assert_equal '2007-03-12'.to_date, assigns(:from)
325
    assert_equal '2007-04-22'.to_date, assigns(:to)
326
    # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
327
    # to use /issues/:issue_id/time_entries
328
    assert_tag :form,
329
      :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
330
  end
331

    
332
  def test_index_atom_feed
333
    get :index, :project_id => 1, :format => 'atom'
334
    assert_response :success
335
    assert_equal 'application/atom+xml', @response.content_type
336
    assert_not_nil assigns(:items)
337
    assert assigns(:items).first.is_a?(TimeEntry)
338
  end
339

    
340
  def test_index_all_projects_csv_export
341
    Setting.date_format = '%m/%d/%Y'
342
    get :index, :format => 'csv'
343
    assert_response :success
344
    assert_equal 'text/csv', @response.content_type
345
    assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
346
    assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
347
  end
348

    
349
  def test_index_csv_export
350
    Setting.date_format = '%m/%d/%Y'
351
    get :index, :project_id => 1, :format => 'csv'
352
    assert_response :success
353
    assert_equal 'text/csv', @response.content_type
354
    assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
355
    assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
356
  end
357

    
358
  def test_csv_big_5
359
    user = User.find_by_id(3)
360
    user.language = "zh-TW"
361
    assert user.save
362
    str_utf8  = "\xe4\xb8\x80\xe6\x9c\x88"
363
    str_big5  = "\xa4@\xa4\xeb"
364
    if str_utf8.respond_to?(:force_encoding)
365
      str_utf8.force_encoding('UTF-8')
366
      str_big5.force_encoding('Big5')
367
    end
368
    @request.session[:user_id] = 3
369
    post :create, :project_id => 1,
370
                :time_entry => {:comments => str_utf8,
371
                                # Not the default activity
372
                                :activity_id => '11',
373
                                :issue_id => '',
374
                                :spent_on => '2011-11-10',
375
                                :hours => '7.3'}
376
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
377

    
378
    t = TimeEntry.find_by_comments(str_utf8)
379
    assert_not_nil t
380
    assert_equal 11, t.activity_id
381
    assert_equal 7.3, t.hours
382
    assert_equal 3, t.user_id
383

    
384
    get :index, :project_id => 1, :format => 'csv',
385
        :from => '2011-11-10', :to => '2011-11-10'
386
    assert_response :success
387
    assert_equal 'text/csv', @response.content_type
388
    ar = @response.body.chomp.split("\n")
389
    s1 = "\xa4\xe9\xb4\xc1"
390
    if str_utf8.respond_to?(:force_encoding)
391
      s1.force_encoding('Big5')
392
    end
393
    assert ar[0].include?(s1)
394
    assert ar[1].include?(str_big5)
395
  end
396

    
397
  def test_csv_cannot_convert_should_be_replaced_big_5
398
    user = User.find_by_id(3)
399
    user.language = "zh-TW"
400
    assert user.save
401
    str_utf8  = "\xe4\xbb\xa5\xe5\x86\x85"
402
    if str_utf8.respond_to?(:force_encoding)
403
      str_utf8.force_encoding('UTF-8')
404
    end
405
    @request.session[:user_id] = 3
406
    post :create, :project_id => 1,
407
                :time_entry => {:comments => str_utf8,
408
                                # Not the default activity
409
                                :activity_id => '11',
410
                                :issue_id => '',
411
                                :spent_on => '2011-11-10',
412
                                :hours => '7.3'}
413
    assert_redirected_to :action => 'index', :project_id => 'ecookbook'
414

    
415
    t = TimeEntry.find_by_comments(str_utf8)
416
    assert_not_nil t
417
    assert_equal 11, t.activity_id
418
    assert_equal 7.3, t.hours
419
    assert_equal 3, t.user_id
420

    
421
    get :index, :project_id => 1, :format => 'csv',
422
        :from => '2011-11-10', :to => '2011-11-10'
423
    assert_response :success
424
    assert_equal 'text/csv', @response.content_type
425
    ar = @response.body.chomp.split("\n")
426
    s1 = "\xa4\xe9\xb4\xc1"
427
    if str_utf8.respond_to?(:force_encoding)
428
      s1.force_encoding('Big5')
429
    end
430
    assert ar[0].include?(s1)
431
    s2 = ar[1].split(",")[8]
432
    if s2.respond_to?(:force_encoding)
433
      s3 = "\xa5H?"
434
      s3.force_encoding('Big5')
435
      assert_equal s3, s2
436
    elsif RUBY_PLATFORM == 'java'
437
      assert_equal "??", s2
438
    else
439
      assert_equal "\xa5H???", s2
440
    end
441
  end
442

    
443
  def test_csv_tw
444
    with_settings :default_language => "zh-TW" do
445
      str1  = "test_csv_tw"
446
      user = User.find_by_id(3)
447
      te1 = TimeEntry.create(:spent_on => '2011-11-10',
448
                             :hours    => 999.9,
449
                             :project  => Project.find(1),
450
                             :user     => user,
451
                             :activity => TimeEntryActivity.find_by_name('Design'),
452
                             :comments => str1)
453
      te2 = TimeEntry.find_by_comments(str1)
454
      assert_not_nil te2
455
      assert_equal 999.9, te2.hours
456
      assert_equal 3, te2.user_id
457

    
458
      get :index, :project_id => 1, :format => 'csv',
459
          :from => '2011-11-10', :to => '2011-11-10'
460
      assert_response :success
461
      assert_equal 'text/csv', @response.content_type
462

    
463
      ar = @response.body.chomp.split("\n")
464
      s2 = ar[1].split(",")[7]
465
      assert_equal '999.9', s2
466

    
467
      str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
468
      if str_tw.respond_to?(:force_encoding)
469
        str_tw.force_encoding('UTF-8')
470
      end
471
      assert_equal str_tw, l(:general_lang_name)
472
      assert_equal ',', l(:general_csv_separator)
473
      assert_equal '.', l(:general_csv_decimal_separator)
474
    end
475
  end
476

    
477
  def test_csv_fr
478
    with_settings :default_language => "fr" do
479
      str1  = "test_csv_fr"
480
      user = User.find_by_id(3)
481
      te1 = TimeEntry.create(:spent_on => '2011-11-10',
482
                             :hours    => 999.9,
483
                             :project  => Project.find(1),
484
                             :user     => user,
485
                             :activity => TimeEntryActivity.find_by_name('Design'),
486
                             :comments => str1)
487
      te2 = TimeEntry.find_by_comments(str1)
488
      assert_not_nil te2
489
      assert_equal 999.9, te2.hours
490
      assert_equal 3, te2.user_id
491

    
492
      get :index, :project_id => 1, :format => 'csv',
493
          :from => '2011-11-10', :to => '2011-11-10'
494
      assert_response :success
495
      assert_equal 'text/csv', @response.content_type
496

    
497
      ar = @response.body.chomp.split("\n")
498
      s2 = ar[1].split(";")[7]
499
      assert_equal '999,9', s2
500

    
501
      str_fr = "Fran\xc3\xa7ais"
502
      if str_fr.respond_to?(:force_encoding)
503
        str_fr.force_encoding('UTF-8')
504
      end
505
      assert_equal str_fr, l(:general_lang_name)
506
      assert_equal ';', l(:general_csv_separator)
507
      assert_equal ',', l(:general_csv_decimal_separator)
508
    end
509
  end
510
end