@@ -1,209 +1,210 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006 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 | before_destroy :check_integrity |
|
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' |
|
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 | named_scope :open, :conditions => {:status => 'open'} |
|
38 | 38 | named_scope :visible, lambda {|*args| { :include => :project, |
|
39 | 39 | :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } } |
|
40 | 40 | |
|
41 | 41 | # Returns true if +user+ or current user is allowed to view the version |
|
42 | 42 | def visible?(user=User.current) |
|
43 | 43 | user.allowed_to?(:view_issues, self.project) |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | def start_date |
|
47 | 47 | effective_date |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def due_date |
|
51 | 51 | effective_date |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | # Returns the total estimated time for this version |
|
55 | # (sum of leaves estimated_hours) | |
|
55 | 56 | def estimated_hours |
|
56 | @estimated_hours ||= fixed_issues.sum(:estimated_hours).to_f | |
|
57 | @estimated_hours ||= fixed_issues.leaves.sum(:estimated_hours).to_f | |
|
57 | 58 | end |
|
58 | 59 | |
|
59 | 60 | # Returns the total reported time for this version |
|
60 | 61 | def spent_hours |
|
61 | 62 | @spent_hours ||= TimeEntry.sum(:hours, :include => :issue, :conditions => ["#{Issue.table_name}.fixed_version_id = ?", id]).to_f |
|
62 | 63 | end |
|
63 | 64 | |
|
64 | 65 | def closed? |
|
65 | 66 | status == 'closed' |
|
66 | 67 | end |
|
67 | 68 | |
|
68 | 69 | def open? |
|
69 | 70 | status == 'open' |
|
70 | 71 | end |
|
71 | 72 | |
|
72 | 73 | # Returns true if the version is completed: due date reached and no open issues |
|
73 | 74 | def completed? |
|
74 | 75 | effective_date && (effective_date <= Date.today) && (open_issues_count == 0) |
|
75 | 76 | end |
|
76 | 77 | |
|
77 | 78 | # Returns the completion percentage of this version based on the amount of open/closed issues |
|
78 | 79 | # and the time spent on the open issues. |
|
79 | 80 | def completed_pourcent |
|
80 | 81 | if issues_count == 0 |
|
81 | 82 | 0 |
|
82 | 83 | elsif open_issues_count == 0 |
|
83 | 84 | 100 |
|
84 | 85 | else |
|
85 | 86 | issues_progress(false) + issues_progress(true) |
|
86 | 87 | end |
|
87 | 88 | end |
|
88 | 89 | |
|
89 | 90 | # Returns the percentage of issues that have been marked as 'closed'. |
|
90 | 91 | def closed_pourcent |
|
91 | 92 | if issues_count == 0 |
|
92 | 93 | 0 |
|
93 | 94 | else |
|
94 | 95 | issues_progress(false) |
|
95 | 96 | end |
|
96 | 97 | end |
|
97 | 98 | |
|
98 | 99 | # Returns true if the version is overdue: due date reached and some open issues |
|
99 | 100 | def overdue? |
|
100 | 101 | effective_date && (effective_date < Date.today) && (open_issues_count > 0) |
|
101 | 102 | end |
|
102 | 103 | |
|
103 | 104 | # Returns assigned issues count |
|
104 | 105 | def issues_count |
|
105 | 106 | @issue_count ||= fixed_issues.count |
|
106 | 107 | end |
|
107 | 108 | |
|
108 | 109 | # Returns the total amount of open issues for this version. |
|
109 | 110 | def open_issues_count |
|
110 | 111 | @open_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, false], :include => :status) |
|
111 | 112 | end |
|
112 | 113 | |
|
113 | 114 | # Returns the total amount of closed issues for this version. |
|
114 | 115 | def closed_issues_count |
|
115 | 116 | @closed_issues_count ||= Issue.count(:all, :conditions => ["fixed_version_id = ? AND is_closed = ?", self.id, true], :include => :status) |
|
116 | 117 | end |
|
117 | 118 | |
|
118 | 119 | def wiki_page |
|
119 | 120 | if project.wiki && !wiki_page_title.blank? |
|
120 | 121 | @wiki_page ||= project.wiki.find_page(wiki_page_title) |
|
121 | 122 | end |
|
122 | 123 | @wiki_page |
|
123 | 124 | end |
|
124 | 125 | |
|
125 | 126 | def to_s; name end |
|
126 | 127 | |
|
127 | 128 | # Versions are sorted by effective_date and name |
|
128 | 129 | # Those with no effective_date are at the end, sorted by name |
|
129 | 130 | def <=>(version) |
|
130 | 131 | if self.effective_date |
|
131 | 132 | version.effective_date ? (self.effective_date == version.effective_date ? self.name <=> version.name : self.effective_date <=> version.effective_date) : -1 |
|
132 | 133 | else |
|
133 | 134 | version.effective_date ? 1 : (self.name <=> version.name) |
|
134 | 135 | end |
|
135 | 136 | end |
|
136 | 137 | |
|
137 | 138 | # Returns the sharings that +user+ can set the version to |
|
138 | 139 | def allowed_sharings(user = User.current) |
|
139 | 140 | VERSION_SHARINGS.select do |s| |
|
140 | 141 | if sharing == s |
|
141 | 142 | true |
|
142 | 143 | else |
|
143 | 144 | case s |
|
144 | 145 | when 'system' |
|
145 | 146 | # Only admin users can set a systemwide sharing |
|
146 | 147 | user.admin? |
|
147 | 148 | when 'hierarchy', 'tree' |
|
148 | 149 | # Only users allowed to manage versions of the root project can |
|
149 | 150 | # set sharing to hierarchy or tree |
|
150 | 151 | project.nil? || user.allowed_to?(:manage_versions, project.root) |
|
151 | 152 | else |
|
152 | 153 | true |
|
153 | 154 | end |
|
154 | 155 | end |
|
155 | 156 | end |
|
156 | 157 | end |
|
157 | 158 | |
|
158 | 159 | private |
|
159 | 160 | def check_integrity |
|
160 | 161 | raise "Can't delete version" if self.fixed_issues.find(:first) |
|
161 | 162 | end |
|
162 | 163 | |
|
163 | 164 | # Update the issue's fixed versions. Used if a version's sharing changes. |
|
164 | 165 | def update_issues_from_sharing_change |
|
165 | 166 | if sharing_changed? |
|
166 | 167 | if VERSION_SHARINGS.index(sharing_was).nil? || |
|
167 | 168 | VERSION_SHARINGS.index(sharing).nil? || |
|
168 | 169 | VERSION_SHARINGS.index(sharing_was) > VERSION_SHARINGS.index(sharing) |
|
169 | 170 | Issue.update_versions_from_sharing_change self |
|
170 | 171 | end |
|
171 | 172 | end |
|
172 | 173 | end |
|
173 | 174 | |
|
174 | 175 | # Returns the average estimated time of assigned issues |
|
175 | 176 | # or 1 if no issue has an estimated time |
|
176 | 177 | # Used to weigth unestimated issues in progress calculation |
|
177 | 178 | def estimated_average |
|
178 | 179 | if @estimated_average.nil? |
|
179 | 180 | average = fixed_issues.average(:estimated_hours).to_f |
|
180 | 181 | if average == 0 |
|
181 | 182 | average = 1 |
|
182 | 183 | end |
|
183 | 184 | @estimated_average = average |
|
184 | 185 | end |
|
185 | 186 | @estimated_average |
|
186 | 187 | end |
|
187 | 188 | |
|
188 | 189 | # Returns the total progress of open or closed issues. The returned percentage takes into account |
|
189 | 190 | # the amount of estimated time set for this version. |
|
190 | 191 | # |
|
191 | 192 | # Examples: |
|
192 | 193 | # issues_progress(true) => returns the progress percentage for open issues. |
|
193 | 194 | # issues_progress(false) => returns the progress percentage for closed issues. |
|
194 | 195 | def issues_progress(open) |
|
195 | 196 | @issues_progress ||= {} |
|
196 | 197 | @issues_progress[open] ||= begin |
|
197 | 198 | progress = 0 |
|
198 | 199 | if issues_count > 0 |
|
199 | 200 | ratio = open ? 'done_ratio' : 100 |
|
200 | 201 | |
|
201 | 202 | done = fixed_issues.sum("COALESCE(estimated_hours, #{estimated_average}) * #{ratio}", |
|
202 | 203 | :include => :status, |
|
203 | 204 | :conditions => ["is_closed = ?", !open]).to_f |
|
204 | 205 | progress = done / (estimated_average * issues_count) |
|
205 | 206 | end |
|
206 | 207 | progress |
|
207 | 208 | end |
|
208 | 209 | end |
|
209 | 210 | end |
@@ -1,156 +1,184 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2008 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.dirname(__FILE__) + '/../test_helper' |
|
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 | end |
|
31 | 31 | |
|
32 | 32 | def test_invalid_effective_date_validation |
|
33 | 33 | v = Version.new(:project => Project.find(1), :name => '1.1', :effective_date => '99999-01-01') |
|
34 | 34 | assert !v.save |
|
35 | 35 | assert_equal I18n.translate('activerecord.errors.messages.not_a_date'), v.errors.on(:effective_date) |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | def test_progress_should_be_0_with_no_assigned_issues |
|
39 | 39 | project = Project.find(1) |
|
40 | 40 | v = Version.create!(:project => project, :name => 'Progress') |
|
41 | 41 | assert_equal 0, v.completed_pourcent |
|
42 | 42 | assert_equal 0, v.closed_pourcent |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def test_progress_should_be_0_with_unbegun_assigned_issues |
|
46 | 46 | project = Project.find(1) |
|
47 | 47 | v = Version.create!(:project => project, :name => 'Progress') |
|
48 | 48 | add_issue(v) |
|
49 | 49 | add_issue(v, :done_ratio => 0) |
|
50 | 50 | assert_progress_equal 0, v.completed_pourcent |
|
51 | 51 | assert_progress_equal 0, v.closed_pourcent |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | def test_progress_should_be_100_with_closed_assigned_issues |
|
55 | 55 | project = Project.find(1) |
|
56 | 56 | status = IssueStatus.find(:first, :conditions => {:is_closed => true}) |
|
57 | 57 | v = Version.create!(:project => project, :name => 'Progress') |
|
58 | 58 | add_issue(v, :status => status) |
|
59 | 59 | add_issue(v, :status => status, :done_ratio => 20) |
|
60 | 60 | add_issue(v, :status => status, :done_ratio => 70, :estimated_hours => 25) |
|
61 | 61 | add_issue(v, :status => status, :estimated_hours => 15) |
|
62 | 62 | assert_progress_equal 100.0, v.completed_pourcent |
|
63 | 63 | assert_progress_equal 100.0, v.closed_pourcent |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def test_progress_should_consider_done_ratio_of_open_assigned_issues |
|
67 | 67 | project = Project.find(1) |
|
68 | 68 | v = Version.create!(:project => project, :name => 'Progress') |
|
69 | 69 | add_issue(v) |
|
70 | 70 | add_issue(v, :done_ratio => 20) |
|
71 | 71 | add_issue(v, :done_ratio => 70) |
|
72 | 72 | assert_progress_equal (0.0 + 20.0 + 70.0)/3, v.completed_pourcent |
|
73 | 73 | assert_progress_equal 0, v.closed_pourcent |
|
74 | 74 | end |
|
75 | 75 | |
|
76 | 76 | def test_progress_should_consider_closed_issues_as_completed |
|
77 | 77 | project = Project.find(1) |
|
78 | 78 | v = Version.create!(:project => project, :name => 'Progress') |
|
79 | 79 | add_issue(v) |
|
80 | 80 | add_issue(v, :done_ratio => 20) |
|
81 | 81 | add_issue(v, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})) |
|
82 | 82 | assert_progress_equal (0.0 + 20.0 + 100.0)/3, v.completed_pourcent |
|
83 | 83 | assert_progress_equal (100.0)/3, v.closed_pourcent |
|
84 | 84 | end |
|
85 | 85 | |
|
86 | 86 | def test_progress_should_consider_estimated_hours_to_weigth_issues |
|
87 | 87 | project = Project.find(1) |
|
88 | 88 | v = Version.create!(:project => project, :name => 'Progress') |
|
89 | 89 | add_issue(v, :estimated_hours => 10) |
|
90 | 90 | add_issue(v, :estimated_hours => 20, :done_ratio => 30) |
|
91 | 91 | add_issue(v, :estimated_hours => 40, :done_ratio => 10) |
|
92 | 92 | add_issue(v, :estimated_hours => 25, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})) |
|
93 | 93 | assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_pourcent |
|
94 | 94 | assert_progress_equal 25.0/95.0*100, v.closed_pourcent |
|
95 | 95 | end |
|
96 | 96 | |
|
97 | 97 | def test_progress_should_consider_average_estimated_hours_to_weigth_unestimated_issues |
|
98 | 98 | project = Project.find(1) |
|
99 | 99 | v = Version.create!(:project => project, :name => 'Progress') |
|
100 | 100 | add_issue(v, :done_ratio => 20) |
|
101 | 101 | add_issue(v, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})) |
|
102 | 102 | add_issue(v, :estimated_hours => 10, :done_ratio => 30) |
|
103 | 103 | add_issue(v, :estimated_hours => 40, :done_ratio => 10) |
|
104 | 104 | assert_progress_equal (25.0*0.2 + 25.0*1 + 10.0*0.3 + 40.0*0.1)/100.0*100, v.completed_pourcent |
|
105 | 105 | assert_progress_equal 25.0/100.0*100, v.closed_pourcent |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | context "#estimated_hours" do | |
|
109 | setup do | |
|
110 | @version = Version.create!(:project_id => 1, :name => '#estimated_hours') | |
|
111 | end | |
|
112 | ||
|
113 | should "return 0 with no assigned issues" do | |
|
114 | assert_equal 0, @version.estimated_hours | |
|
115 | end | |
|
116 | ||
|
117 | should "return 0 with no estimated hours" do | |
|
118 | add_issue(@version) | |
|
119 | assert_equal 0, @version.estimated_hours | |
|
120 | end | |
|
121 | ||
|
122 | should "return the sum of estimated hours" do | |
|
123 | add_issue(@version, :estimated_hours => 2.5) | |
|
124 | add_issue(@version, :estimated_hours => 5) | |
|
125 | assert_equal 7.5, @version.estimated_hours | |
|
126 | end | |
|
127 | ||
|
128 | should "return the sum of leaves estimated hours" do | |
|
129 | parent = add_issue(@version) | |
|
130 | add_issue(@version, :estimated_hours => 2.5, :parent_issue_id => parent.id) | |
|
131 | add_issue(@version, :estimated_hours => 5, :parent_issue_id => parent.id) | |
|
132 | assert_equal 7.5, @version.estimated_hours | |
|
133 | end | |
|
134 | end | |
|
135 | ||
|
108 | 136 | test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do |
|
109 | 137 | User.current = User.find(1) # Need the admin's permissions |
|
110 | 138 | |
|
111 | 139 | @version = Version.find(7) |
|
112 | 140 | # Separate hierarchy |
|
113 | 141 | project_1_issue = Issue.find(1) |
|
114 | 142 | project_1_issue.fixed_version = @version |
|
115 | 143 | assert project_1_issue.save, project_1_issue.errors.full_messages |
|
116 | 144 | |
|
117 | 145 | project_5_issue = Issue.find(6) |
|
118 | 146 | project_5_issue.fixed_version = @version |
|
119 | 147 | assert project_5_issue.save |
|
120 | 148 | |
|
121 | 149 | # Project |
|
122 | 150 | project_2_issue = Issue.find(4) |
|
123 | 151 | project_2_issue.fixed_version = @version |
|
124 | 152 | assert project_2_issue.save |
|
125 | 153 | |
|
126 | 154 | # Update the sharing |
|
127 | 155 | @version.sharing = 'none' |
|
128 | 156 | assert @version.save |
|
129 | 157 | |
|
130 | 158 | # Project 1 now out of the shared scope |
|
131 | 159 | project_1_issue.reload |
|
132 | 160 | assert_equal nil, project_1_issue.fixed_version, "Fixed version is still set after changing the Version's sharing" |
|
133 | 161 | |
|
134 | 162 | # Project 5 now out of the shared scope |
|
135 | 163 | project_5_issue.reload |
|
136 | 164 | assert_equal nil, project_5_issue.fixed_version, "Fixed version is still set after changing the Version's sharing" |
|
137 | 165 | |
|
138 | 166 | # Project 2 issue remains |
|
139 | 167 | project_2_issue.reload |
|
140 | 168 | assert_equal @version, project_2_issue.fixed_version |
|
141 | 169 | end |
|
142 | 170 | |
|
143 | 171 | private |
|
144 | 172 | |
|
145 | 173 | def add_issue(version, attributes={}) |
|
146 | 174 | Issue.create!({:project => version.project, |
|
147 | 175 | :fixed_version => version, |
|
148 | 176 | :subject => 'Test', |
|
149 | 177 | :author => User.find(:first), |
|
150 | 178 | :tracker => version.project.trackers.find(:first)}.merge(attributes)) |
|
151 | 179 | end |
|
152 | 180 | |
|
153 | 181 | def assert_progress_equal(expected_float, actual_float, message="") |
|
154 | 182 | assert_in_delta(expected_float, actual_float, 0.000001, message="") |
|
155 | 183 | end |
|
156 | 184 | end |
General Comments 0
You need to be logged in to leave comments.
Login now