##// END OF EJS Templates
Fixed: time entries created with the default activity even if a different one is specified (#1302)....
Jean-Philippe Lang -
r1519:846045fd0590
parent child
Show More
@@ -1,71 +1,73
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class TimeEntry < ActiveRecord::Base
18 class TimeEntry < ActiveRecord::Base
19 # could have used polymorphic association
19 # could have used polymorphic association
20 # project association here allows easy loading of time entries at project level with one database trip
20 # project association here allows easy loading of time entries at project level with one database trip
21 belongs_to :project
21 belongs_to :project
22 belongs_to :issue
22 belongs_to :issue
23 belongs_to :user
23 belongs_to :user
24 belongs_to :activity, :class_name => 'Enumeration', :foreign_key => :activity_id
24 belongs_to :activity, :class_name => 'Enumeration', :foreign_key => :activity_id
25
25
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
27
27
28 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
28 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
29 validates_numericality_of :hours, :allow_nil => true
29 validates_numericality_of :hours, :allow_nil => true
30 validates_length_of :comments, :maximum => 255
30 validates_length_of :comments, :maximum => 255
31
31
32 def after_initialize
32 def after_initialize
33 if new_record?
33 if new_record? && self.activity.nil?
34 self.activity ||= Enumeration.default('ACTI')
34 if default_activity = Enumeration.default('ACTI')
35 self.activity_id = default_activity.id
36 end
35 end
37 end
36 end
38 end
37
39
38 def before_validation
40 def before_validation
39 self.project = issue.project if issue && project.nil?
41 self.project = issue.project if issue && project.nil?
40 end
42 end
41
43
42 def validate
44 def validate
43 errors.add :hours, :activerecord_error_invalid if hours && (hours < 0 || hours >= 1000)
45 errors.add :hours, :activerecord_error_invalid if hours && (hours < 0 || hours >= 1000)
44 errors.add :project_id, :activerecord_error_invalid if project.nil?
46 errors.add :project_id, :activerecord_error_invalid if project.nil?
45 errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project)
47 errors.add :issue_id, :activerecord_error_invalid if (issue_id && !issue) || (issue && project!=issue.project)
46 end
48 end
47
49
48 def hours=(h)
50 def hours=(h)
49 write_attribute :hours, (h.is_a?(String) ? h.to_hours : h)
51 write_attribute :hours, (h.is_a?(String) ? h.to_hours : h)
50 end
52 end
51
53
52 # tyear, tmonth, tweek assigned where setting spent_on attributes
54 # tyear, tmonth, tweek assigned where setting spent_on attributes
53 # these attributes make time aggregations easier
55 # these attributes make time aggregations easier
54 def spent_on=(date)
56 def spent_on=(date)
55 super
57 super
56 self.tyear = spent_on ? spent_on.year : nil
58 self.tyear = spent_on ? spent_on.year : nil
57 self.tmonth = spent_on ? spent_on.month : nil
59 self.tmonth = spent_on ? spent_on.month : nil
58 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
60 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
59 end
61 end
60
62
61 # Returns true if the time entry can be edited by usr, otherwise false
63 # Returns true if the time entry can be edited by usr, otherwise false
62 def editable_by?(usr)
64 def editable_by?(usr)
63 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
65 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
64 end
66 end
65
67
66 def self.visible_by(usr)
68 def self.visible_by(usr)
67 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
69 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
68 yield
70 yield
69 end
71 end
70 end
72 end
71 end
73 end
@@ -1,218 +1,220
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'timelog_controller'
19 require 'timelog_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class TimelogController; def rescue_action(e) raise e end; end
22 class TimelogController; def rescue_action(e) raise e end; end
23
23
24 class TimelogControllerTest < Test::Unit::TestCase
24 class TimelogControllerTest < Test::Unit::TestCase
25 fixtures :projects, :enabled_modules, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
25 fixtures :projects, :enabled_modules, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
26
26
27 def setup
27 def setup
28 @controller = TimelogController.new
28 @controller = TimelogController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 end
31 end
32
32
33 def test_get_edit
33 def test_get_edit
34 @request.session[:user_id] = 3
34 @request.session[:user_id] = 3
35 get :edit, :project_id => 1
35 get :edit, :project_id => 1
36 assert_response :success
36 assert_response :success
37 assert_template 'edit'
37 assert_template 'edit'
38 # Default activity selected
38 # Default activity selected
39 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
39 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
40 :content => 'Development'
40 :content => 'Development'
41 end
41 end
42
42
43 def test_post_edit
43 def test_post_edit
44 @request.session[:user_id] = 3
44 @request.session[:user_id] = 3
45 post :edit, :project_id => 1,
45 post :edit, :project_id => 1,
46 :time_entry => {:comments => 'Some work on TimelogControllerTest',
46 :time_entry => {:comments => 'Some work on TimelogControllerTest',
47 :activity_id => '10',
47 # Not the default activity
48 :activity_id => '11',
48 :spent_on => '2008-03-14',
49 :spent_on => '2008-03-14',
49 :issue_id => '1',
50 :issue_id => '1',
50 :hours => '7.3'}
51 :hours => '7.3'}
51 assert_redirected_to 'projects/ecookbook/timelog/details'
52 assert_redirected_to 'projects/ecookbook/timelog/details'
52
53
53 i = Issue.find(1)
54 i = Issue.find(1)
54 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
55 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
55 assert_not_nil t
56 assert_not_nil t
57 assert_equal 11, t.activity_id
56 assert_equal 7.3, t.hours
58 assert_equal 7.3, t.hours
57 assert_equal 3, t.user_id
59 assert_equal 3, t.user_id
58 assert_equal i, t.issue
60 assert_equal i, t.issue
59 assert_equal i.project, t.project
61 assert_equal i.project, t.project
60 end
62 end
61
63
62 def test_update
64 def test_update
63 entry = TimeEntry.find(1)
65 entry = TimeEntry.find(1)
64 assert_equal 1, entry.issue_id
66 assert_equal 1, entry.issue_id
65 assert_equal 2, entry.user_id
67 assert_equal 2, entry.user_id
66
68
67 @request.session[:user_id] = 1
69 @request.session[:user_id] = 1
68 post :edit, :id => 1,
70 post :edit, :id => 1,
69 :time_entry => {:issue_id => '2',
71 :time_entry => {:issue_id => '2',
70 :hours => '8'}
72 :hours => '8'}
71 assert_redirected_to 'projects/ecookbook/timelog/details'
73 assert_redirected_to 'projects/ecookbook/timelog/details'
72 entry.reload
74 entry.reload
73
75
74 assert_equal 8, entry.hours
76 assert_equal 8, entry.hours
75 assert_equal 2, entry.issue_id
77 assert_equal 2, entry.issue_id
76 assert_equal 2, entry.user_id
78 assert_equal 2, entry.user_id
77 end
79 end
78
80
79 def destroy
81 def destroy
80 @request.session[:user_id] = 2
82 @request.session[:user_id] = 2
81 post :destroy, :id => 1
83 post :destroy, :id => 1
82 assert_redirected_to 'projects/ecookbook/timelog/details'
84 assert_redirected_to 'projects/ecookbook/timelog/details'
83 assert_nil TimeEntry.find_by_id(1)
85 assert_nil TimeEntry.find_by_id(1)
84 end
86 end
85
87
86 def test_report_no_criteria
88 def test_report_no_criteria
87 get :report, :project_id => 1
89 get :report, :project_id => 1
88 assert_response :success
90 assert_response :success
89 assert_template 'report'
91 assert_template 'report'
90 end
92 end
91
93
92 def test_report_all_time
94 def test_report_all_time
93 get :report, :project_id => 1, :criterias => ['project', 'issue']
95 get :report, :project_id => 1, :criterias => ['project', 'issue']
94 assert_response :success
96 assert_response :success
95 assert_template 'report'
97 assert_template 'report'
96 assert_not_nil assigns(:total_hours)
98 assert_not_nil assigns(:total_hours)
97 assert_equal "162.90", "%.2f" % assigns(:total_hours)
99 assert_equal "162.90", "%.2f" % assigns(:total_hours)
98 end
100 end
99
101
100 def test_report_all_time_by_day
102 def test_report_all_time_by_day
101 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
103 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
102 assert_response :success
104 assert_response :success
103 assert_template 'report'
105 assert_template 'report'
104 assert_not_nil assigns(:total_hours)
106 assert_not_nil assigns(:total_hours)
105 assert_equal "162.90", "%.2f" % assigns(:total_hours)
107 assert_equal "162.90", "%.2f" % assigns(:total_hours)
106 assert_tag :tag => 'th', :content => '2007-03-12'
108 assert_tag :tag => 'th', :content => '2007-03-12'
107 end
109 end
108
110
109 def test_report_one_criteria
111 def test_report_one_criteria
110 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
112 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
111 assert_response :success
113 assert_response :success
112 assert_template 'report'
114 assert_template 'report'
113 assert_not_nil assigns(:total_hours)
115 assert_not_nil assigns(:total_hours)
114 assert_equal "8.65", "%.2f" % assigns(:total_hours)
116 assert_equal "8.65", "%.2f" % assigns(:total_hours)
115 end
117 end
116
118
117 def test_report_two_criterias
119 def test_report_two_criterias
118 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
120 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
119 assert_response :success
121 assert_response :success
120 assert_template 'report'
122 assert_template 'report'
121 assert_not_nil assigns(:total_hours)
123 assert_not_nil assigns(:total_hours)
122 assert_equal "162.90", "%.2f" % assigns(:total_hours)
124 assert_equal "162.90", "%.2f" % assigns(:total_hours)
123 end
125 end
124
126
125 def test_report_custom_field_criteria
127 def test_report_custom_field_criteria
126 get :report, :project_id => 1, :criterias => ['project', 'cf_1']
128 get :report, :project_id => 1, :criterias => ['project', 'cf_1']
127 assert_response :success
129 assert_response :success
128 assert_template 'report'
130 assert_template 'report'
129 assert_not_nil assigns(:total_hours)
131 assert_not_nil assigns(:total_hours)
130 assert_not_nil assigns(:criterias)
132 assert_not_nil assigns(:criterias)
131 assert_equal 2, assigns(:criterias).size
133 assert_equal 2, assigns(:criterias).size
132 assert_equal "162.90", "%.2f" % assigns(:total_hours)
134 assert_equal "162.90", "%.2f" % assigns(:total_hours)
133 # Custom field column
135 # Custom field column
134 assert_tag :tag => 'th', :content => 'Database'
136 assert_tag :tag => 'th', :content => 'Database'
135 # Custom field row
137 # Custom field row
136 assert_tag :tag => 'td', :content => 'MySQL',
138 assert_tag :tag => 'td', :content => 'MySQL',
137 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
139 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
138 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
140 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
139 :content => '1' }}
141 :content => '1' }}
140 end
142 end
141
143
142 def test_report_one_criteria_no_result
144 def test_report_one_criteria_no_result
143 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
145 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
144 assert_response :success
146 assert_response :success
145 assert_template 'report'
147 assert_template 'report'
146 assert_not_nil assigns(:total_hours)
148 assert_not_nil assigns(:total_hours)
147 assert_equal "0.00", "%.2f" % assigns(:total_hours)
149 assert_equal "0.00", "%.2f" % assigns(:total_hours)
148 end
150 end
149
151
150 def test_report_csv_export
152 def test_report_csv_export
151 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
153 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
152 assert_response :success
154 assert_response :success
153 assert_equal 'text/csv', @response.content_type
155 assert_equal 'text/csv', @response.content_type
154 lines = @response.body.chomp.split("\n")
156 lines = @response.body.chomp.split("\n")
155 # Headers
157 # Headers
156 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
158 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
157 # Total row
159 # Total row
158 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
160 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
159 end
161 end
160
162
161 def test_details_at_project_level
163 def test_details_at_project_level
162 get :details, :project_id => 1
164 get :details, :project_id => 1
163 assert_response :success
165 assert_response :success
164 assert_template 'details'
166 assert_template 'details'
165 assert_not_nil assigns(:entries)
167 assert_not_nil assigns(:entries)
166 assert_equal 4, assigns(:entries).size
168 assert_equal 4, assigns(:entries).size
167 # project and subproject
169 # project and subproject
168 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
170 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
169 assert_not_nil assigns(:total_hours)
171 assert_not_nil assigns(:total_hours)
170 assert_equal "162.90", "%.2f" % assigns(:total_hours)
172 assert_equal "162.90", "%.2f" % assigns(:total_hours)
171 # display all time by default
173 # display all time by default
172 assert_equal '2007-03-11'.to_date, assigns(:from)
174 assert_equal '2007-03-11'.to_date, assigns(:from)
173 assert_equal '2007-04-22'.to_date, assigns(:to)
175 assert_equal '2007-04-22'.to_date, assigns(:to)
174 end
176 end
175
177
176 def test_details_at_project_level_with_date_range
178 def test_details_at_project_level_with_date_range
177 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
179 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
178 assert_response :success
180 assert_response :success
179 assert_template 'details'
181 assert_template 'details'
180 assert_not_nil assigns(:entries)
182 assert_not_nil assigns(:entries)
181 assert_equal 3, assigns(:entries).size
183 assert_equal 3, assigns(:entries).size
182 assert_not_nil assigns(:total_hours)
184 assert_not_nil assigns(:total_hours)
183 assert_equal "12.90", "%.2f" % assigns(:total_hours)
185 assert_equal "12.90", "%.2f" % assigns(:total_hours)
184 assert_equal '2007-03-20'.to_date, assigns(:from)
186 assert_equal '2007-03-20'.to_date, assigns(:from)
185 assert_equal '2007-04-30'.to_date, assigns(:to)
187 assert_equal '2007-04-30'.to_date, assigns(:to)
186 end
188 end
187
189
188 def test_details_at_project_level_with_period
190 def test_details_at_project_level_with_period
189 get :details, :project_id => 1, :period => '7_days'
191 get :details, :project_id => 1, :period => '7_days'
190 assert_response :success
192 assert_response :success
191 assert_template 'details'
193 assert_template 'details'
192 assert_not_nil assigns(:entries)
194 assert_not_nil assigns(:entries)
193 assert_not_nil assigns(:total_hours)
195 assert_not_nil assigns(:total_hours)
194 assert_equal Date.today - 7, assigns(:from)
196 assert_equal Date.today - 7, assigns(:from)
195 assert_equal Date.today, assigns(:to)
197 assert_equal Date.today, assigns(:to)
196 end
198 end
197
199
198 def test_details_at_issue_level
200 def test_details_at_issue_level
199 get :details, :issue_id => 1
201 get :details, :issue_id => 1
200 assert_response :success
202 assert_response :success
201 assert_template 'details'
203 assert_template 'details'
202 assert_not_nil assigns(:entries)
204 assert_not_nil assigns(:entries)
203 assert_equal 2, assigns(:entries).size
205 assert_equal 2, assigns(:entries).size
204 assert_not_nil assigns(:total_hours)
206 assert_not_nil assigns(:total_hours)
205 assert_equal 154.25, assigns(:total_hours)
207 assert_equal 154.25, assigns(:total_hours)
206 # display all time by default
208 # display all time by default
207 assert_equal '2007-03-11'.to_date, assigns(:from)
209 assert_equal '2007-03-11'.to_date, assigns(:from)
208 assert_equal '2007-04-22'.to_date, assigns(:to)
210 assert_equal '2007-04-22'.to_date, assigns(:to)
209 end
211 end
210
212
211 def test_details_csv_export
213 def test_details_csv_export
212 get :details, :project_id => 1, :format => 'csv'
214 get :details, :project_id => 1, :format => 'csv'
213 assert_response :success
215 assert_response :success
214 assert_equal 'text/csv', @response.content_type
216 assert_equal 'text/csv', @response.content_type
215 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
217 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
216 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
218 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
217 end
219 end
218 end
220 end
General Comments 0
You need to be logged in to leave comments. Login now