##// END OF EJS Templates
Fixed version date validation (#12359)....
Jean-Philippe Lang -
r10583:8b527ce24783
parent child
Show More
@@ -1,278 +1,285
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 Version < ActiveRecord::Base
19 19 include Redmine::SafeAttributes
20 20 after_update :update_issues_from_sharing_change
21 21 belongs_to :project
22 22 has_many :fixed_issues, :class_name => 'Issue', :foreign_key => 'fixed_version_id', :dependent => :nullify
23 23 acts_as_customizable
24 24 acts_as_attachable :view_permission => :view_files,
25 25 :delete_permission => :manage_files
26 26
27 27 VERSION_STATUSES = %w(open locked closed)
28 28 VERSION_SHARINGS = %w(none descendants hierarchy tree system)
29 29
30 30 validates_presence_of :name
31 31 validates_uniqueness_of :name, :scope => [:project_id]
32 32 validates_length_of :name, :maximum => 60
33 33 validates_format_of :effective_date, :with => /^\d{4}-\d{2}-\d{2}$/, :message => :not_a_date, :allow_nil => true
34 34 validates_inclusion_of :status, :in => VERSION_STATUSES
35 35 validates_inclusion_of :sharing, :in => VERSION_SHARINGS
36 validate :validate_version
36 37
37 38 scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
38 39 scope :open, :conditions => {:status => 'open'}
39 40 scope :visible, lambda {|*args| { :include => :project,
40 41 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
41 42
42 43 safe_attributes 'name',
43 44 'description',
44 45 'effective_date',
45 46 'due_date',
46 47 'wiki_page_title',
47 48 'status',
48 49 'sharing',
49 50 'custom_field_values'
50 51
51 52 # Returns true if +user+ or current user is allowed to view the version
52 53 def visible?(user=User.current)
53 54 user.allowed_to?(:view_issues, self.project)
54 55 end
55 56
56 57 # Version files have same visibility as project files
57 58 def attachments_visible?(*args)
58 59 project.present? && project.attachments_visible?(*args)
59 60 end
60 61
61 62 def start_date
62 63 @start_date ||= fixed_issues.minimum('start_date')
63 64 end
64 65
65 66 def due_date
66 67 effective_date
67 68 end
68 69
69 70 def due_date=(arg)
70 71 self.effective_date=(arg)
71 72 end
72 73
73 74 # Returns the total estimated time for this version
74 75 # (sum of leaves estimated_hours)
75 76 def estimated_hours
76 77 @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f
77 78 end
78 79
79 80 # Returns the total reported time for this version
80 81 def spent_hours
81 82 @spent_hours ||= TimeEntry.sum(:hours, :joins => :issue, :conditions => ["#{Issue.table_name}.fixed_version_id = ?", id]).to_f
82 83 end
83 84
84 85 def closed?
85 86 status == 'closed'
86 87 end
87 88
88 89 def open?
89 90 status == 'open'
90 91 end
91 92
92 93 # Returns true if the version is completed: due date reached and no open issues
93 94 def completed?
94 95 effective_date && (effective_date < Date.today) && (open_issues_count == 0)
95 96 end
96 97
97 98 def behind_schedule?
98 99 if completed_pourcent == 100
99 100 return false
100 101 elsif due_date && start_date
101 102 done_date = start_date + ((due_date - start_date+1)* completed_pourcent/100).floor
102 103 return done_date <= Date.today
103 104 else
104 105 false # No issues so it's not late
105 106 end
106 107 end
107 108
108 109 # Returns the completion percentage of this version based on the amount of open/closed issues
109 110 # and the time spent on the open issues.
110 111 def completed_pourcent
111 112 if issues_count == 0
112 113 0
113 114 elsif open_issues_count == 0
114 115 100
115 116 else
116 117 issues_progress(false) + issues_progress(true)
117 118 end
118 119 end
119 120
120 121 # Returns the percentage of issues that have been marked as 'closed'.
121 122 def closed_pourcent
122 123 if issues_count == 0
123 124 0
124 125 else
125 126 issues_progress(false)
126 127 end
127 128 end
128 129
129 130 # Returns true if the version is overdue: due date reached and some open issues
130 131 def overdue?
131 132 effective_date && (effective_date < Date.today) && (open_issues_count > 0)
132 133 end
133 134
134 135 # Returns assigned issues count
135 136 def issues_count
136 137 load_issue_counts
137 138 @issue_count
138 139 end
139 140
140 141 # Returns the total amount of open issues for this version.
141 142 def open_issues_count
142 143 load_issue_counts
143 144 @open_issues_count
144 145 end
145 146
146 147 # Returns the total amount of closed issues for this version.
147 148 def closed_issues_count
148 149 load_issue_counts
149 150 @closed_issues_count
150 151 end
151 152
152 153 def wiki_page
153 154 if project.wiki && !wiki_page_title.blank?
154 155 @wiki_page ||= project.wiki.find_page(wiki_page_title)
155 156 end
156 157 @wiki_page
157 158 end
158 159
159 160 def to_s; name end
160 161
161 162 def to_s_with_project
162 163 "#{project} - #{name}"
163 164 end
164 165
165 166 # Versions are sorted by effective_date and name
166 167 # Those with no effective_date are at the end, sorted by name
167 168 def <=>(version)
168 169 if self.effective_date
169 170 if version.effective_date
170 171 if self.effective_date == version.effective_date
171 172 name == version.name ? id <=> version.id : name <=> version.name
172 173 else
173 174 self.effective_date <=> version.effective_date
174 175 end
175 176 else
176 177 -1
177 178 end
178 179 else
179 180 if version.effective_date
180 181 1
181 182 else
182 183 name == version.name ? id <=> version.id : name <=> version.name
183 184 end
184 185 end
185 186 end
186 187
187 188 def self.fields_for_order_statement(table=nil)
188 189 table ||= table_name
189 190 ["(CASE WHEN #{table}.effective_date IS NULL THEN 1 ELSE 0 END)", "#{table}.effective_date", "#{table}.name", "#{table}.id"]
190 191 end
191 192
192 193 scope :sorted, order(fields_for_order_statement)
193 194
194 195 # Returns the sharings that +user+ can set the version to
195 196 def allowed_sharings(user = User.current)
196 197 VERSION_SHARINGS.select do |s|
197 198 if sharing == s
198 199 true
199 200 else
200 201 case s
201 202 when 'system'
202 203 # Only admin users can set a systemwide sharing
203 204 user.admin?
204 205 when 'hierarchy', 'tree'
205 206 # Only users allowed to manage versions of the root project can
206 207 # set sharing to hierarchy or tree
207 208 project.nil? || user.allowed_to?(:manage_versions, project.root)
208 209 else
209 210 true
210 211 end
211 212 end
212 213 end
213 214 end
214 215
215 216 private
216 217
217 218 def load_issue_counts
218 219 unless @issue_count
219 220 @open_issues_count = 0
220 221 @closed_issues_count = 0
221 222 fixed_issues.count(:all, :group => :status).each do |status, count|
222 223 if status.is_closed?
223 224 @closed_issues_count += count
224 225 else
225 226 @open_issues_count += count
226 227 end
227 228 end
228 229 @issue_count = @open_issues_count + @closed_issues_count
229 230 end
230 231 end
231 232
232 233 # Update the issue's fixed versions. Used if a version's sharing changes.
233 234 def update_issues_from_sharing_change
234 235 if sharing_changed?
235 236 if VERSION_SHARINGS.index(sharing_was).nil? ||
236 237 VERSION_SHARINGS.index(sharing).nil? ||
237 238 VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing)
238 239 Issue.update_versions_from_sharing_change self
239 240 end
240 241 end
241 242 end
242 243
243 244 # Returns the average estimated time of assigned issues
244 245 # or 1 if no issue has an estimated time
245 246 # Used to weigth unestimated issues in progress calculation
246 247 def estimated_average
247 248 if @estimated_average.nil?
248 249 average = fixed_issues.average(:estimated_hours).to_f
249 250 if average == 0
250 251 average = 1
251 252 end
252 253 @estimated_average = average
253 254 end
254 255 @estimated_average
255 256 end
256 257
257 258 # Returns the total progress of open or closed issues. The returned percentage takes into account
258 259 # the amount of estimated time set for this version.
259 260 #
260 261 # Examples:
261 262 # issues_progress(true) => returns the progress percentage for open issues.
262 263 # issues_progress(false) => returns the progress percentage for closed issues.
263 264 def issues_progress(open)
264 265 @issues_progress ||= {}
265 266 @issues_progress[open] ||= begin
266 267 progress = 0
267 268 if issues_count > 0
268 269 ratio = open ? 'done_ratio' : 100
269 270
270 271 done = fixed_issues.sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}",
271 272 :joins => :status,
272 273 :conditions => ["#{IssueStatus.table_name}.is_closed = ?", !open]).to_f
273 274 progress = done / (estimated_average * issues_count)
274 275 end
275 276 progress
276 277 end
277 278 end
279
280 def validate_version
281 if effective_date.nil? && @attributes['effective_date'].present?
282 errors.add :effective_date, :not_a_date
283 end
284 end
278 285 end
@@ -1,246 +1,252
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class VersionTest < ActiveSupport::TestCase
21 21 fixtures :projects, :users, :issues, :issue_statuses, :trackers, :enumerations, :versions, :projects_trackers
22 22
23 23 def setup
24 24 end
25 25
26 26 def test_create
27 27 v = Version.new(:project => Project.find(1), :name => '1.1', :effective_date => '2011-03-25')
28 28 assert v.save
29 29 assert_equal 'open', v.status
30 30 assert_equal 'none', v.sharing
31 31 end
32 32
33 33 def test_invalid_effective_date_validation
34 34 v = Version.new(:project => Project.find(1), :name => '1.1', :effective_date => '99999-01-01')
35 assert !v.save
35 assert !v.valid?
36 v.effective_date = '2012-11-33'
37 assert !v.valid?
38 v.effective_date = '2012-31-11'
39 assert !v.valid?
40 v.effective_date = 'ABC'
41 assert !v.valid?
36 42 assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
37 43 v.errors[:effective_date]
38 44 end
39 45
40 46 def test_progress_should_be_0_with_no_assigned_issues
41 47 project = Project.find(1)
42 48 v = Version.create!(:project => project, :name => 'Progress')
43 49 assert_equal 0, v.completed_pourcent
44 50 assert_equal 0, v.closed_pourcent
45 51 end
46 52
47 53 def test_progress_should_be_0_with_unbegun_assigned_issues
48 54 project = Project.find(1)
49 55 v = Version.create!(:project => project, :name => 'Progress')
50 56 add_issue(v)
51 57 add_issue(v, :done_ratio => 0)
52 58 assert_progress_equal 0, v.completed_pourcent
53 59 assert_progress_equal 0, v.closed_pourcent
54 60 end
55 61
56 62 def test_progress_should_be_100_with_closed_assigned_issues
57 63 project = Project.find(1)
58 64 status = IssueStatus.find(:first, :conditions => {:is_closed => true})
59 65 v = Version.create!(:project => project, :name => 'Progress')
60 66 add_issue(v, :status => status)
61 67 add_issue(v, :status => status, :done_ratio => 20)
62 68 add_issue(v, :status => status, :done_ratio => 70, :estimated_hours => 25)
63 69 add_issue(v, :status => status, :estimated_hours => 15)
64 70 assert_progress_equal 100.0, v.completed_pourcent
65 71 assert_progress_equal 100.0, v.closed_pourcent
66 72 end
67 73
68 74 def test_progress_should_consider_done_ratio_of_open_assigned_issues
69 75 project = Project.find(1)
70 76 v = Version.create!(:project => project, :name => 'Progress')
71 77 add_issue(v)
72 78 add_issue(v, :done_ratio => 20)
73 79 add_issue(v, :done_ratio => 70)
74 80 assert_progress_equal (0.0 + 20.0 + 70.0)/3, v.completed_pourcent
75 81 assert_progress_equal 0, v.closed_pourcent
76 82 end
77 83
78 84 def test_progress_should_consider_closed_issues_as_completed
79 85 project = Project.find(1)
80 86 v = Version.create!(:project => project, :name => 'Progress')
81 87 add_issue(v)
82 88 add_issue(v, :done_ratio => 20)
83 89 add_issue(v, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
84 90 assert_progress_equal (0.0 + 20.0 + 100.0)/3, v.completed_pourcent
85 91 assert_progress_equal (100.0)/3, v.closed_pourcent
86 92 end
87 93
88 94 def test_progress_should_consider_estimated_hours_to_weigth_issues
89 95 project = Project.find(1)
90 96 v = Version.create!(:project => project, :name => 'Progress')
91 97 add_issue(v, :estimated_hours => 10)
92 98 add_issue(v, :estimated_hours => 20, :done_ratio => 30)
93 99 add_issue(v, :estimated_hours => 40, :done_ratio => 10)
94 100 add_issue(v, :estimated_hours => 25, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
95 101 assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_pourcent
96 102 assert_progress_equal 25.0/95.0*100, v.closed_pourcent
97 103 end
98 104
99 105 def test_progress_should_consider_average_estimated_hours_to_weigth_unestimated_issues
100 106 project = Project.find(1)
101 107 v = Version.create!(:project => project, :name => 'Progress')
102 108 add_issue(v, :done_ratio => 20)
103 109 add_issue(v, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
104 110 add_issue(v, :estimated_hours => 10, :done_ratio => 30)
105 111 add_issue(v, :estimated_hours => 40, :done_ratio => 10)
106 112 assert_progress_equal (25.0*0.2 + 25.0*1 + 10.0*0.3 + 40.0*0.1)/100.0*100, v.completed_pourcent
107 113 assert_progress_equal 25.0/100.0*100, v.closed_pourcent
108 114 end
109 115
110 116 def test_should_sort_scheduled_then_unscheduled_versions
111 117 Version.delete_all
112 118 v4 = Version.create!(:project_id => 1, :name => 'v4')
113 119 v3 = Version.create!(:project_id => 1, :name => 'v2', :effective_date => '2012-07-14')
114 120 v2 = Version.create!(:project_id => 1, :name => 'v1')
115 121 v1 = Version.create!(:project_id => 1, :name => 'v3', :effective_date => '2012-08-02')
116 122 v5 = Version.create!(:project_id => 1, :name => 'v5', :effective_date => '2012-07-02')
117 123
118 124 assert_equal [v5, v3, v1, v2, v4], [v1, v2, v3, v4, v5].sort
119 125 assert_equal [v5, v3, v1, v2, v4], Version.sorted.all
120 126 end
121 127
122 128 def test_completed_should_be_false_when_due_today
123 129 version = Version.create!(:project_id => 1, :effective_date => Date.today, :name => 'Due today')
124 130 assert_equal false, version.completed?
125 131 end
126 132
127 133 context "#behind_schedule?" do
128 134 setup do
129 135 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
130 136 @project = Project.create!(:name => 'test0', :identifier => 'test0')
131 137 @project.trackers << Tracker.create!(:name => 'track')
132 138
133 139 @version = Version.create!(:project => @project, :effective_date => nil, :name => 'version')
134 140 end
135 141
136 142 should "be false if there are no issues assigned" do
137 143 @version.update_attribute(:effective_date, Date.yesterday)
138 144 assert_equal false, @version.behind_schedule?
139 145 end
140 146
141 147 should "be false if there is no effective_date" do
142 148 assert_equal false, @version.behind_schedule?
143 149 end
144 150
145 151 should "be false if all of the issues are ahead of schedule" do
146 152 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
147 153 add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
148 154 add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
149 155 assert_equal 60, @version.completed_pourcent
150 156 assert_equal false, @version.behind_schedule?
151 157 end
152 158
153 159 should "be true if any of the issues are behind schedule" do
154 160 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
155 161 add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
156 162 add_issue(@version, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left
157 163 assert_equal 40, @version.completed_pourcent
158 164 assert_equal true, @version.behind_schedule?
159 165 end
160 166
161 167 should "be false if all of the issues are complete" do
162 168 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
163 169 add_issue(@version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
164 170 add_issue(@version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
165 171 assert_equal 100, @version.completed_pourcent
166 172 assert_equal false, @version.behind_schedule?
167 173 end
168 174 end
169 175
170 176 context "#estimated_hours" do
171 177 setup do
172 178 @version = Version.create!(:project_id => 1, :name => '#estimated_hours')
173 179 end
174 180
175 181 should "return 0 with no assigned issues" do
176 182 assert_equal 0, @version.estimated_hours
177 183 end
178 184
179 185 should "return 0 with no estimated hours" do
180 186 add_issue(@version)
181 187 assert_equal 0, @version.estimated_hours
182 188 end
183 189
184 190 should "return the sum of estimated hours" do
185 191 add_issue(@version, :estimated_hours => 2.5)
186 192 add_issue(@version, :estimated_hours => 5)
187 193 assert_equal 7.5, @version.estimated_hours
188 194 end
189 195
190 196 should "return the sum of leaves estimated hours" do
191 197 parent = add_issue(@version)
192 198 add_issue(@version, :estimated_hours => 2.5, :parent_issue_id => parent.id)
193 199 add_issue(@version, :estimated_hours => 5, :parent_issue_id => parent.id)
194 200 assert_equal 7.5, @version.estimated_hours
195 201 end
196 202 end
197 203
198 204 test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do
199 205 User.current = User.find(1) # Need the admin's permissions
200 206
201 207 @version = Version.find(7)
202 208 # Separate hierarchy
203 209 project_1_issue = Issue.find(1)
204 210 project_1_issue.fixed_version = @version
205 211 assert project_1_issue.save, project_1_issue.errors.full_messages.to_s
206 212
207 213 project_5_issue = Issue.find(6)
208 214 project_5_issue.fixed_version = @version
209 215 assert project_5_issue.save
210 216
211 217 # Project
212 218 project_2_issue = Issue.find(4)
213 219 project_2_issue.fixed_version = @version
214 220 assert project_2_issue.save
215 221
216 222 # Update the sharing
217 223 @version.sharing = 'none'
218 224 assert @version.save
219 225
220 226 # Project 1 now out of the shared scope
221 227 project_1_issue.reload
222 228 assert_equal nil, project_1_issue.fixed_version, "Fixed version is still set after changing the Version's sharing"
223 229
224 230 # Project 5 now out of the shared scope
225 231 project_5_issue.reload
226 232 assert_equal nil, project_5_issue.fixed_version, "Fixed version is still set after changing the Version's sharing"
227 233
228 234 # Project 2 issue remains
229 235 project_2_issue.reload
230 236 assert_equal @version, project_2_issue.fixed_version
231 237 end
232 238
233 239 private
234 240
235 241 def add_issue(version, attributes={})
236 242 Issue.create!({:project => version.project,
237 243 :fixed_version => version,
238 244 :subject => 'Test',
239 245 :author => User.find(:first),
240 246 :tracker => version.project.trackers.find(:first)}.merge(attributes))
241 247 end
242 248
243 249 def assert_progress_equal(expected_float, actual_float, message="")
244 250 assert_in_delta(expected_float, actual_float, 0.000001, message="")
245 251 end
246 252 end
General Comments 0
You need to be logged in to leave comments. Login now