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