##// END OF EJS Templates
Actually block issues from closing when a blocking issue isn't closed (#1740)....
Jean-Philippe Lang -
r2700:6994d1c23b8d
parent child
Show More
@@ -0,0 +1,7
1 issue_relation_001:
2 id: 1
3 issue_from_id: 10
4 issue_to_id: 9
5 relation_type: blocks
6 delay:
7
@@ -1,316 +1,322
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 Issue < ActiveRecord::Base
19 19 belongs_to :project
20 20 belongs_to :tracker
21 21 belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id'
22 22 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
23 23 belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id'
24 24 belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id'
25 25 belongs_to :priority, :class_name => 'IssuePriority', :foreign_key => 'priority_id'
26 26 belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id'
27 27
28 28 has_many :journals, :as => :journalized, :dependent => :destroy
29 29 has_many :time_entries, :dependent => :delete_all
30 30 has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC"
31 31
32 32 has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
33 33 has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
34 34
35 35 acts_as_attachable :after_remove => :attachment_removed
36 36 acts_as_customizable
37 37 acts_as_watchable
38 38 acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"],
39 39 :include => [:project, :journals],
40 40 # sort by id so that limited eager loading doesn't break with postgresql
41 41 :order_column => "#{table_name}.id"
42 42 acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
43 43 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}},
44 44 :type => Proc.new {|o| 'issue' + (o.closed? ? ' closed' : '') }
45 45
46 46 acts_as_activity_provider :find_options => {:include => [:project, :author, :tracker]},
47 47 :author_key => :author_id
48 48
49 49 validates_presence_of :subject, :priority, :project, :tracker, :author, :status
50 50 validates_length_of :subject, :maximum => 255
51 51 validates_inclusion_of :done_ratio, :in => 0..100
52 52 validates_numericality_of :estimated_hours, :allow_nil => true
53 53
54 54 named_scope :visible, lambda {|*args| { :include => :project,
55 55 :conditions => Project.allowed_to_condition(args.first || User.current, :view_issues) } }
56 56
57 57 named_scope :open, :conditions => ["#{IssueStatus.table_name}.is_closed = ?", false], :include => :status
58 58
59 59 after_save :create_journal
60 60
61 61 # Returns true if usr or current user is allowed to view the issue
62 62 def visible?(usr=nil)
63 63 (usr || User.current).allowed_to?(:view_issues, self.project)
64 64 end
65 65
66 66 def after_initialize
67 67 if new_record?
68 68 # set default values for new records only
69 69 self.status ||= IssueStatus.default
70 70 self.priority ||= IssuePriority.default
71 71 end
72 72 end
73 73
74 74 # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
75 75 def available_custom_fields
76 76 (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
77 77 end
78 78
79 79 def copy_from(arg)
80 80 issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
81 81 self.attributes = issue.attributes.dup
82 82 self.custom_values = issue.custom_values.collect {|v| v.clone}
83 83 self
84 84 end
85 85
86 86 # Moves/copies an issue to a new project and tracker
87 87 # Returns the moved/copied issue on success, false on failure
88 88 def move_to(new_project, new_tracker = nil, options = {})
89 89 options ||= {}
90 90 issue = options[:copy] ? self.clone : self
91 91 transaction do
92 92 if new_project && issue.project_id != new_project.id
93 93 # delete issue relations
94 94 unless Setting.cross_project_issue_relations?
95 95 issue.relations_from.clear
96 96 issue.relations_to.clear
97 97 end
98 98 # issue is moved to another project
99 99 # reassign to the category with same name if any
100 100 new_category = issue.category.nil? ? nil : new_project.issue_categories.find_by_name(issue.category.name)
101 101 issue.category = new_category
102 102 issue.fixed_version = nil
103 103 issue.project = new_project
104 104 end
105 105 if new_tracker
106 106 issue.tracker = new_tracker
107 107 end
108 108 if options[:copy]
109 109 issue.custom_field_values = self.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
110 110 issue.status = self.status
111 111 end
112 112 if issue.save
113 113 unless options[:copy]
114 114 # Manually update project_id on related time entries
115 115 TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id})
116 116 end
117 117 else
118 118 Issue.connection.rollback_db_transaction
119 119 return false
120 120 end
121 121 end
122 122 return issue
123 123 end
124 124
125 125 def priority_id=(pid)
126 126 self.priority = nil
127 127 write_attribute(:priority_id, pid)
128 128 end
129 129
130 130 def estimated_hours=(h)
131 131 write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h)
132 132 end
133 133
134 134 def validate
135 135 if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
136 136 errors.add :due_date, :not_a_date
137 137 end
138 138
139 139 if self.due_date and self.start_date and self.due_date < self.start_date
140 140 errors.add :due_date, :greater_than_start_date
141 141 end
142 142
143 143 if start_date && soonest_start && start_date < soonest_start
144 144 errors.add :start_date, :invalid
145 145 end
146 146 end
147 147
148 148 def validate_on_create
149 149 errors.add :tracker_id, :invalid unless project.trackers.include?(tracker)
150 150 end
151 151
152 152 def before_create
153 153 # default assignment based on category
154 154 if assigned_to.nil? && category && category.assigned_to
155 155 self.assigned_to = category.assigned_to
156 156 end
157 157 end
158 158
159 159 def after_save
160 160 # Reload is needed in order to get the right status
161 161 reload
162 162
163 163 # Update start/due dates of following issues
164 164 relations_from.each(&:set_issue_to_dates)
165 165
166 166 # Close duplicates if the issue was closed
167 167 if @issue_before_change && !@issue_before_change.closed? && self.closed?
168 168 duplicates.each do |duplicate|
169 169 # Reload is need in case the duplicate was updated by a previous duplicate
170 170 duplicate.reload
171 171 # Don't re-close it if it's already closed
172 172 next if duplicate.closed?
173 173 # Same user and notes
174 174 duplicate.init_journal(@current_journal.user, @current_journal.notes)
175 175 duplicate.update_attribute :status, self.status
176 176 end
177 177 end
178 178 end
179 179
180 180 def init_journal(user, notes = "")
181 181 @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
182 182 @issue_before_change = self.clone
183 183 @issue_before_change.status = self.status
184 184 @custom_values_before_change = {}
185 185 self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value }
186 186 # Make sure updated_on is updated when adding a note.
187 187 updated_on_will_change!
188 188 @current_journal
189 189 end
190 190
191 191 # Return true if the issue is closed, otherwise false
192 192 def closed?
193 193 self.status.is_closed?
194 194 end
195 195
196 196 # Returns true if the issue is overdue
197 197 def overdue?
198 198 !due_date.nil? && (due_date < Date.today) && !status.is_closed?
199 199 end
200 200
201 201 # Users the issue can be assigned to
202 202 def assignable_users
203 203 project.assignable_users
204 204 end
205 205
206 # Returns true if this issue is blocked by another issue that is still open
207 def blocked?
208 !relations_to.detect {|ir| ir.relation_type == 'blocks' && !ir.issue_from.closed?}.nil?
209 end
210
206 211 # Returns an array of status that user is able to apply
207 212 def new_statuses_allowed_to(user)
208 213 statuses = status.find_new_statuses_allowed_to(user.roles_for_project(project), tracker)
209 214 statuses << status unless statuses.empty?
210 statuses.uniq.sort
215 statuses = statuses.uniq.sort
216 blocked? ? statuses.reject {|s| s.is_closed?} : statuses
211 217 end
212 218
213 219 # Returns the mail adresses of users that should be notified for the issue
214 220 def recipients
215 221 recipients = project.recipients
216 222 # Author and assignee are always notified unless they have been locked
217 223 recipients << author.mail if author && author.active?
218 224 recipients << assigned_to.mail if assigned_to && assigned_to.active?
219 225 recipients.compact.uniq
220 226 end
221 227
222 228 # Returns the total number of hours spent on this issue.
223 229 #
224 230 # Example:
225 231 # spent_hours => 0
226 232 # spent_hours => 50
227 233 def spent_hours
228 234 @spent_hours ||= time_entries.sum(:hours) || 0
229 235 end
230 236
231 237 def relations
232 238 (relations_from + relations_to).sort
233 239 end
234 240
235 241 def all_dependent_issues
236 242 dependencies = []
237 243 relations_from.each do |relation|
238 244 dependencies << relation.issue_to
239 245 dependencies += relation.issue_to.all_dependent_issues
240 246 end
241 247 dependencies
242 248 end
243 249
244 250 # Returns an array of issues that duplicate this one
245 251 def duplicates
246 252 relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from}
247 253 end
248 254
249 255 # Returns the due date or the target due date if any
250 256 # Used on gantt chart
251 257 def due_before
252 258 due_date || (fixed_version ? fixed_version.effective_date : nil)
253 259 end
254 260
255 261 # Returns the time scheduled for this issue.
256 262 #
257 263 # Example:
258 264 # Start Date: 2/26/09, End Date: 3/04/09
259 265 # duration => 6
260 266 def duration
261 267 (start_date && due_date) ? due_date - start_date : 0
262 268 end
263 269
264 270 def soonest_start
265 271 @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min
266 272 end
267 273
268 274 def to_s
269 275 "#{tracker} ##{id}: #{subject}"
270 276 end
271 277
272 278 # Returns a string of css classes that apply to the issue
273 279 def css_classes
274 280 s = "issue status-#{status.position} priority-#{priority.position}"
275 281 s << ' closed' if closed?
276 282 s << ' overdue' if overdue?
277 283 s << ' created-by-me' if User.current.logged? && author_id == User.current.id
278 284 s << ' assigned-to-me' if User.current.logged? && assigned_to_id == User.current.id
279 285 s
280 286 end
281 287
282 288 private
283 289
284 290 # Callback on attachment deletion
285 291 def attachment_removed(obj)
286 292 journal = init_journal(User.current)
287 293 journal.details << JournalDetail.new(:property => 'attachment',
288 294 :prop_key => obj.id,
289 295 :old_value => obj.filename)
290 296 journal.save
291 297 end
292 298
293 299 # Saves the changes in a Journal
294 300 # Called after_save
295 301 def create_journal
296 302 if @current_journal
297 303 # attributes changes
298 304 (Issue.column_names - %w(id description lock_version created_on updated_on)).each {|c|
299 305 @current_journal.details << JournalDetail.new(:property => 'attr',
300 306 :prop_key => c,
301 307 :old_value => @issue_before_change.send(c),
302 308 :value => send(c)) unless send(c)==@issue_before_change.send(c)
303 309 }
304 310 # custom fields changes
305 311 custom_values.each {|c|
306 312 next if (@custom_values_before_change[c.custom_field_id]==c.value ||
307 313 (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?))
308 314 @current_journal.details << JournalDetail.new(:property => 'cf',
309 315 :prop_key => c.custom_field_id,
310 316 :old_value => @custom_values_before_change[c.custom_field_id],
311 317 :value => c.value)
312 318 }
313 319 @current_journal.save
314 320 end
315 321 end
316 322 end
@@ -1,128 +1,159
1 1 ---
2 2 issues_001:
3 3 created_on: <%= 3.days.ago.to_date.to_s(:db) %>
4 4 project_id: 1
5 5 updated_on: <%= 1.day.ago.to_date.to_s(:db) %>
6 6 priority_id: 4
7 7 subject: Can't print recipes
8 8 id: 1
9 9 fixed_version_id:
10 10 category_id: 1
11 11 description: Unable to print recipes
12 12 tracker_id: 1
13 13 assigned_to_id:
14 14 author_id: 2
15 15 status_id: 1
16 16 start_date: <%= 1.day.ago.to_date.to_s(:db) %>
17 17 due_date: <%= 10.day.from_now.to_date.to_s(:db) %>
18 18 issues_002:
19 19 created_on: 2006-07-19 21:04:21 +02:00
20 20 project_id: 1
21 21 updated_on: 2006-07-19 21:09:50 +02:00
22 22 priority_id: 5
23 23 subject: Add ingredients categories
24 24 id: 2
25 25 fixed_version_id: 2
26 26 category_id:
27 27 description: Ingredients of the recipe should be classified by categories
28 28 tracker_id: 2
29 29 assigned_to_id: 3
30 30 author_id: 2
31 31 status_id: 2
32 32 start_date: <%= 2.day.ago.to_date.to_s(:db) %>
33 33 due_date:
34 34 issues_003:
35 35 created_on: 2006-07-19 21:07:27 +02:00
36 36 project_id: 1
37 37 updated_on: 2006-07-19 21:07:27 +02:00
38 38 priority_id: 4
39 39 subject: Error 281 when updating a recipe
40 40 id: 3
41 41 fixed_version_id:
42 42 category_id:
43 43 description: Error 281 is encountered when saving a recipe
44 44 tracker_id: 1
45 45 assigned_to_id: 3
46 46 author_id: 2
47 47 status_id: 1
48 48 start_date: <%= 1.day.from_now.to_date.to_s(:db) %>
49 49 due_date: <%= 40.day.ago.to_date.to_s(:db) %>
50 50 issues_004:
51 51 created_on: <%= 5.days.ago.to_date.to_s(:db) %>
52 52 project_id: 2
53 53 updated_on: <%= 2.days.ago.to_date.to_s(:db) %>
54 54 priority_id: 4
55 55 subject: Issue on project 2
56 56 id: 4
57 57 fixed_version_id:
58 58 category_id:
59 59 description: Issue on project 2
60 60 tracker_id: 1
61 61 assigned_to_id: 2
62 62 author_id: 2
63 63 status_id: 1
64 64 issues_005:
65 65 created_on: <%= 5.days.ago.to_date.to_s(:db) %>
66 66 project_id: 3
67 67 updated_on: <%= 2.days.ago.to_date.to_s(:db) %>
68 68 priority_id: 4
69 69 subject: Subproject issue
70 70 id: 5
71 71 fixed_version_id:
72 72 category_id:
73 73 description: This is an issue on a cookbook subproject
74 74 tracker_id: 1
75 75 assigned_to_id:
76 76 author_id: 2
77 77 status_id: 1
78 78 issues_006:
79 79 created_on: <%= 1.minute.ago.to_date.to_s(:db) %>
80 80 project_id: 5
81 81 updated_on: <%= 1.minute.ago.to_date.to_s(:db) %>
82 82 priority_id: 4
83 83 subject: Issue of a private subproject
84 84 id: 6
85 85 fixed_version_id:
86 86 category_id:
87 87 description: This is an issue of a private subproject of cookbook
88 88 tracker_id: 1
89 89 assigned_to_id:
90 90 author_id: 2
91 91 status_id: 1
92 92 start_date: <%= Date.today.to_s(:db) %>
93 93 due_date: <%= 1.days.from_now.to_date.to_s(:db) %>
94 94 issues_007:
95 95 created_on: <%= 10.days.ago.to_date.to_s(:db) %>
96 96 project_id: 1
97 97 updated_on: <%= 10.days.ago.to_date.to_s(:db) %>
98 98 priority_id: 5
99 99 subject: Issue due today
100 100 id: 7
101 101 fixed_version_id:
102 102 category_id:
103 103 description: This is an issue that is due today
104 104 tracker_id: 1
105 105 assigned_to_id:
106 106 author_id: 2
107 107 status_id: 1
108 108 start_date: <%= 10.days.ago.to_s(:db) %>
109 109 due_date: <%= Date.today.to_s(:db) %>
110 110 lock_version: 0
111 111 issues_008:
112 112 created_on: <%= 10.days.ago.to_date.to_s(:db) %>
113 113 project_id: 1
114 114 updated_on: <%= 10.days.ago.to_date.to_s(:db) %>
115 115 priority_id: 5
116 116 subject: Closed issue
117 117 id: 8
118 118 fixed_version_id:
119 119 category_id:
120 120 description: This is a closed issue.
121 121 tracker_id: 1
122 122 assigned_to_id:
123 123 author_id: 2
124 124 status_id: 5
125 125 start_date:
126 126 due_date:
127 127 lock_version: 0
128
128 issues_009:
129 created_on: <%= 1.minute.ago.to_date.to_s(:db) %>
130 project_id: 5
131 updated_on: <%= 1.minute.ago.to_date.to_s(:db) %>
132 priority_id: 5
133 subject: Blocked Issue
134 id: 9
135 fixed_version_id:
136 category_id:
137 description: This is an issue that is blocked by issue #10
138 tracker_id: 1
139 assigned_to_id:
140 author_id: 2
141 status_id: 1
142 start_date: <%= Date.today.to_s(:db) %>
143 due_date: <%= 1.days.from_now.to_date.to_s(:db) %>
144 issues_010:
145 created_on: <%= 1.minute.ago.to_date.to_s(:db) %>
146 project_id: 5
147 updated_on: <%= 1.minute.ago.to_date.to_s(:db) %>
148 priority_id: 5
149 subject: Issue Doing the Blocking
150 id: 10
151 fixed_version_id:
152 category_id:
153 description: This is an issue that blocks issue #9
154 tracker_id: 1
155 assigned_to_id:
156 author_id: 2
157 status_id: 1
158 start_date: <%= Date.today.to_s(:db) %>
159 due_date: <%= 1.days.from_now.to_date.to_s(:db) %>
@@ -1,275 +1,301
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 IssueTest < Test::Unit::TestCase
21 21 fixtures :projects, :users, :members, :member_roles,
22 22 :trackers, :projects_trackers,
23 :issue_statuses, :issue_categories,
23 :issue_statuses, :issue_categories, :issue_relations, :workflows,
24 24 :enumerations,
25 25 :issues,
26 26 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
27 27 :time_entries
28 28
29 29 def test_create
30 30 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30')
31 31 assert issue.save
32 32 issue.reload
33 33 assert_equal 1.5, issue.estimated_hours
34 34 end
35 35
36 36 def test_create_minimal
37 37 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'test_create')
38 38 assert issue.save
39 39 assert issue.description.nil?
40 40 end
41 41
42 42 def test_create_with_required_custom_field
43 43 field = IssueCustomField.find_by_name('Database')
44 44 field.update_attribute(:is_required, true)
45 45
46 46 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'test_create', :description => 'IssueTest#test_create_with_required_custom_field')
47 47 assert issue.available_custom_fields.include?(field)
48 48 # No value for the custom field
49 49 assert !issue.save
50 50 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
51 51 # Blank value
52 52 issue.custom_field_values = { field.id => '' }
53 53 assert !issue.save
54 54 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
55 55 # Invalid value
56 56 issue.custom_field_values = { field.id => 'SQLServer' }
57 57 assert !issue.save
58 58 assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
59 59 # Valid value
60 60 issue.custom_field_values = { field.id => 'PostgreSQL' }
61 61 assert issue.save
62 62 issue.reload
63 63 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
64 64 end
65 65
66 66 def test_errors_full_messages_should_include_custom_fields_errors
67 67 field = IssueCustomField.find_by_name('Database')
68 68
69 69 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'test_create', :description => 'IssueTest#test_create_with_required_custom_field')
70 70 assert issue.available_custom_fields.include?(field)
71 71 # Invalid value
72 72 issue.custom_field_values = { field.id => 'SQLServer' }
73 73
74 74 assert !issue.valid?
75 75 assert_equal 1, issue.errors.full_messages.size
76 76 assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}", issue.errors.full_messages.first
77 77 end
78 78
79 79 def test_update_issue_with_required_custom_field
80 80 field = IssueCustomField.find_by_name('Database')
81 81 field.update_attribute(:is_required, true)
82 82
83 83 issue = Issue.find(1)
84 84 assert_nil issue.custom_value_for(field)
85 85 assert issue.available_custom_fields.include?(field)
86 86 # No change to custom values, issue can be saved
87 87 assert issue.save
88 88 # Blank value
89 89 issue.custom_field_values = { field.id => '' }
90 90 assert !issue.save
91 91 # Valid value
92 92 issue.custom_field_values = { field.id => 'PostgreSQL' }
93 93 assert issue.save
94 94 issue.reload
95 95 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
96 96 end
97 97
98 98 def test_should_not_update_attributes_if_custom_fields_validation_fails
99 99 issue = Issue.find(1)
100 100 field = IssueCustomField.find_by_name('Database')
101 101 assert issue.available_custom_fields.include?(field)
102 102
103 103 issue.custom_field_values = { field.id => 'Invalid' }
104 104 issue.subject = 'Should be not be saved'
105 105 assert !issue.save
106 106
107 107 issue.reload
108 108 assert_equal "Can't print recipes", issue.subject
109 109 end
110 110
111 111 def test_should_not_recreate_custom_values_objects_on_update
112 112 field = IssueCustomField.find_by_name('Database')
113 113
114 114 issue = Issue.find(1)
115 115 issue.custom_field_values = { field.id => 'PostgreSQL' }
116 116 assert issue.save
117 117 custom_value = issue.custom_value_for(field)
118 118 issue.reload
119 119 issue.custom_field_values = { field.id => 'MySQL' }
120 120 assert issue.save
121 121 issue.reload
122 122 assert_equal custom_value.id, issue.custom_value_for(field).id
123 123 end
124 124
125 125 def test_category_based_assignment
126 126 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1)
127 127 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
128 128 end
129 129
130 130 def test_copy
131 131 issue = Issue.new.copy_from(1)
132 132 assert issue.save
133 133 issue.reload
134 134 orig = Issue.find(1)
135 135 assert_equal orig.subject, issue.subject
136 136 assert_equal orig.tracker, issue.tracker
137 137 assert_equal orig.custom_values.first.value, issue.custom_values.first.value
138 138 end
139 139
140 140 def test_should_close_duplicates
141 141 # Create 3 issues
142 142 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'Duplicates test', :description => 'Duplicates test')
143 143 assert issue1.save
144 144 issue2 = issue1.clone
145 145 assert issue2.save
146 146 issue3 = issue1.clone
147 147 assert issue3.save
148 148
149 149 # 2 is a dupe of 1
150 150 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
151 151 # And 3 is a dupe of 2
152 152 IssueRelation.create(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
153 153 # And 3 is a dupe of 1 (circular duplicates)
154 154 IssueRelation.create(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
155 155
156 156 assert issue1.reload.duplicates.include?(issue2)
157 157
158 158 # Closing issue 1
159 159 issue1.init_journal(User.find(:first), "Closing issue1")
160 160 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
161 161 assert issue1.save
162 162 # 2 and 3 should be also closed
163 163 assert issue2.reload.closed?
164 164 assert issue3.reload.closed?
165 165 end
166 166
167 167 def test_should_not_close_duplicated_issue
168 168 # Create 3 issues
169 169 issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'Duplicates test', :description => 'Duplicates test')
170 170 assert issue1.save
171 171 issue2 = issue1.clone
172 172 assert issue2.save
173 173
174 174 # 2 is a dupe of 1
175 175 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
176 176 # 2 is a dup of 1 but 1 is not a duplicate of 2
177 177 assert !issue2.reload.duplicates.include?(issue1)
178 178
179 179 # Closing issue 2
180 180 issue2.init_journal(User.find(:first), "Closing issue2")
181 181 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
182 182 assert issue2.save
183 183 # 1 should not be also closed
184 184 assert !issue1.reload.closed?
185 185 end
186 186
187 187 def test_move_to_another_project_with_same_category
188 188 issue = Issue.find(1)
189 189 assert issue.move_to(Project.find(2))
190 190 issue.reload
191 191 assert_equal 2, issue.project_id
192 192 # Category changes
193 193 assert_equal 4, issue.category_id
194 194 # Make sure time entries were move to the target project
195 195 assert_equal 2, issue.time_entries.first.project_id
196 196 end
197 197
198 198 def test_move_to_another_project_without_same_category
199 199 issue = Issue.find(2)
200 200 assert issue.move_to(Project.find(2))
201 201 issue.reload
202 202 assert_equal 2, issue.project_id
203 203 # Category cleared
204 204 assert_nil issue.category_id
205 205 end
206 206
207 207 def test_copy_to_the_same_project
208 208 issue = Issue.find(1)
209 209 copy = nil
210 210 assert_difference 'Issue.count' do
211 211 copy = issue.move_to(issue.project, nil, :copy => true)
212 212 end
213 213 assert_kind_of Issue, copy
214 214 assert_equal issue.project, copy.project
215 215 assert_equal "125", copy.custom_value_for(2).value
216 216 end
217 217
218 218 def test_copy_to_another_project_and_tracker
219 219 issue = Issue.find(1)
220 220 copy = nil
221 221 assert_difference 'Issue.count' do
222 222 copy = issue.move_to(Project.find(3), Tracker.find(2), :copy => true)
223 223 end
224 224 assert_kind_of Issue, copy
225 225 assert_equal Project.find(3), copy.project
226 226 assert_equal Tracker.find(2), copy.tracker
227 227 # Custom field #2 is not associated with target tracker
228 228 assert_nil copy.custom_value_for(2)
229 229 end
230 230
231 231 def test_issue_destroy
232 232 Issue.find(1).destroy
233 233 assert_nil Issue.find_by_id(1)
234 234 assert_nil TimeEntry.find_by_issue_id(1)
235 235 end
236 236
237 def test_blocked
238 blocked_issue = Issue.find(9)
239 blocking_issue = Issue.find(10)
240
241 assert blocked_issue.blocked?
242 assert !blocking_issue.blocked?
243 end
244
245 def test_blocked_issues_dont_allow_closed_statuses
246 blocked_issue = Issue.find(9)
247
248 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
249 assert !allowed_statuses.empty?
250 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
251 assert closed_statuses.empty?
252 end
253
254 def test_unblocked_issues_allow_closed_statuses
255 blocking_issue = Issue.find(10)
256
257 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
258 assert !allowed_statuses.empty?
259 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
260 assert !closed_statuses.empty?
261 end
262
237 263 def test_overdue
238 264 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
239 265 assert !Issue.new(:due_date => Date.today).overdue?
240 266 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
241 267 assert !Issue.new(:due_date => nil).overdue?
242 268 assert !Issue.new(:due_date => 1.day.ago.to_date, :status => IssueStatus.find(:first, :conditions => {:is_closed => true})).overdue?
243 269 end
244 270
245 271 def test_assignable_users
246 272 assert_kind_of User, Issue.find(1).assignable_users.first
247 273 end
248 274
249 275 def test_create_should_send_email_notification
250 276 ActionMailer::Base.deliveries.clear
251 277 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => IssuePriority.all.first, :subject => 'test_create', :estimated_hours => '1:30')
252 278
253 279 assert issue.save
254 280 assert_equal 1, ActionMailer::Base.deliveries.size
255 281 end
256 282
257 283 def test_stale_issue_should_not_send_email_notification
258 284 ActionMailer::Base.deliveries.clear
259 285 issue = Issue.find(1)
260 286 stale = Issue.find(1)
261 287
262 288 issue.init_journal(User.find(1))
263 289 issue.subject = 'Subjet update'
264 290 assert issue.save
265 291 assert_equal 1, ActionMailer::Base.deliveries.size
266 292 ActionMailer::Base.deliveries.clear
267 293
268 294 stale.init_journal(User.find(1))
269 295 stale.subject = 'Another subjet update'
270 296 assert_raise ActiveRecord::StaleObjectError do
271 297 stale.save
272 298 end
273 299 assert ActionMailer::Base.deliveries.empty?
274 300 end
275 301 end
General Comments 0
You need to be logged in to leave comments. Login now