@@ -1,257 +1,259 | |||||
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 | class Issue < ActiveRecord::Base |
|
18 | class Issue < ActiveRecord::Base | |
19 | belongs_to :project |
|
19 | belongs_to :project | |
20 | belongs_to :tracker |
|
20 | belongs_to :tracker | |
21 | belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id' |
|
21 | belongs_to :status, :class_name => 'IssueStatus', :foreign_key => 'status_id' | |
22 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' |
|
22 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' | |
23 | belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id' |
|
23 | belongs_to :assigned_to, :class_name => 'User', :foreign_key => 'assigned_to_id' | |
24 | belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id' |
|
24 | belongs_to :fixed_version, :class_name => 'Version', :foreign_key => 'fixed_version_id' | |
25 | belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id' |
|
25 | belongs_to :priority, :class_name => 'Enumeration', :foreign_key => 'priority_id' | |
26 | belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id' |
|
26 | belongs_to :category, :class_name => 'IssueCategory', :foreign_key => 'category_id' | |
27 |
|
27 | |||
28 | has_many :journals, :as => :journalized, :dependent => :destroy |
|
28 | has_many :journals, :as => :journalized, :dependent => :destroy | |
29 | has_many :attachments, :as => :container, :dependent => :destroy |
|
29 | has_many :attachments, :as => :container, :dependent => :destroy | |
30 | has_many :time_entries, :dependent => :delete_all |
|
30 | has_many :time_entries, :dependent => :delete_all | |
31 | has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC" |
|
31 | has_and_belongs_to_many :changesets, :order => "#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC" | |
32 |
|
32 | |||
33 | has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all |
|
33 | has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all | |
34 | has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all |
|
34 | has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all | |
35 |
|
35 | |||
36 | acts_as_customizable |
|
36 | acts_as_customizable | |
37 | acts_as_watchable |
|
37 | acts_as_watchable | |
38 | acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"], |
|
38 | acts_as_searchable :columns => ['subject', "#{table_name}.description", "#{Journal.table_name}.notes"], | |
39 | :include => [:project, :journals], |
|
39 | :include => [:project, :journals], | |
40 | # sort by id so that limited eager loading doesn't break with postgresql |
|
40 | # sort by id so that limited eager loading doesn't break with postgresql | |
41 | :order_column => "#{table_name}.id" |
|
41 | :order_column => "#{table_name}.id" | |
42 | acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"}, |
|
42 | acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"}, | |
43 | :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}} |
|
43 | :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.id}} | |
44 |
|
44 | |||
45 | validates_presence_of :subject, :description, :priority, :project, :tracker, :author, :status |
|
45 | validates_presence_of :subject, :description, :priority, :project, :tracker, :author, :status | |
46 | validates_length_of :subject, :maximum => 255 |
|
46 | validates_length_of :subject, :maximum => 255 | |
47 | validates_inclusion_of :done_ratio, :in => 0..100 |
|
47 | validates_inclusion_of :done_ratio, :in => 0..100 | |
48 | validates_numericality_of :estimated_hours, :allow_nil => true |
|
48 | validates_numericality_of :estimated_hours, :allow_nil => true | |
49 |
|
49 | |||
50 | def after_initialize |
|
50 | def after_initialize | |
51 | if new_record? |
|
51 | if new_record? | |
52 | # set default values for new records only |
|
52 | # set default values for new records only | |
53 | self.status ||= IssueStatus.default |
|
53 | self.status ||= IssueStatus.default | |
54 | self.priority ||= Enumeration.default('IPRI') |
|
54 | self.priority ||= Enumeration.default('IPRI') | |
55 | end |
|
55 | end | |
56 | end |
|
56 | end | |
57 |
|
57 | |||
58 | # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields |
|
58 | # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields | |
59 | def available_custom_fields |
|
59 | def available_custom_fields | |
60 | (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : [] |
|
60 | (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : [] | |
61 | end |
|
61 | end | |
62 |
|
62 | |||
63 | def copy_from(arg) |
|
63 | def copy_from(arg) | |
64 | issue = arg.is_a?(Issue) ? arg : Issue.find(arg) |
|
64 | issue = arg.is_a?(Issue) ? arg : Issue.find(arg) | |
65 | self.attributes = issue.attributes.dup |
|
65 | self.attributes = issue.attributes.dup | |
66 | self.custom_values = issue.custom_values.collect {|v| v.clone} |
|
66 | self.custom_values = issue.custom_values.collect {|v| v.clone} | |
67 | self |
|
67 | self | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | # Move an issue to a new project and tracker |
|
70 | # Move an issue to a new project and tracker | |
71 | def move_to(new_project, new_tracker = nil) |
|
71 | def move_to(new_project, new_tracker = nil) | |
72 | transaction do |
|
72 | transaction do | |
73 | if new_project && project_id != new_project.id |
|
73 | if new_project && project_id != new_project.id | |
74 | # delete issue relations |
|
74 | # delete issue relations | |
75 | unless Setting.cross_project_issue_relations? |
|
75 | unless Setting.cross_project_issue_relations? | |
76 | self.relations_from.clear |
|
76 | self.relations_from.clear | |
77 | self.relations_to.clear |
|
77 | self.relations_to.clear | |
78 | end |
|
78 | end | |
79 | # issue is moved to another project |
|
79 | # issue is moved to another project | |
80 | self.category = nil |
|
80 | # reassign to the category with same name if any | |
|
81 | new_category = category.nil? ? nil : new_project.issue_categories.find_by_name(category.name) | |||
|
82 | self.category = new_category | |||
81 | self.fixed_version = nil |
|
83 | self.fixed_version = nil | |
82 | self.project = new_project |
|
84 | self.project = new_project | |
83 | end |
|
85 | end | |
84 | if new_tracker |
|
86 | if new_tracker | |
85 | self.tracker = new_tracker |
|
87 | self.tracker = new_tracker | |
86 | end |
|
88 | end | |
87 | if save |
|
89 | if save | |
88 | # Manually update project_id on related time entries |
|
90 | # Manually update project_id on related time entries | |
89 | TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id}) |
|
91 | TimeEntry.update_all("project_id = #{new_project.id}", {:issue_id => id}) | |
90 | else |
|
92 | else | |
91 | rollback_db_transaction |
|
93 | rollback_db_transaction | |
92 | return false |
|
94 | return false | |
93 | end |
|
95 | end | |
94 | end |
|
96 | end | |
95 | return true |
|
97 | return true | |
96 | end |
|
98 | end | |
97 |
|
99 | |||
98 | def priority_id=(pid) |
|
100 | def priority_id=(pid) | |
99 | self.priority = nil |
|
101 | self.priority = nil | |
100 | write_attribute(:priority_id, pid) |
|
102 | write_attribute(:priority_id, pid) | |
101 | end |
|
103 | end | |
102 |
|
104 | |||
103 | def estimated_hours=(h) |
|
105 | def estimated_hours=(h) | |
104 | write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h) |
|
106 | write_attribute :estimated_hours, (h.is_a?(String) ? h.to_hours : h) | |
105 | end |
|
107 | end | |
106 |
|
108 | |||
107 | def validate |
|
109 | def validate | |
108 | if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty? |
|
110 | if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty? | |
109 | errors.add :due_date, :activerecord_error_not_a_date |
|
111 | errors.add :due_date, :activerecord_error_not_a_date | |
110 | end |
|
112 | end | |
111 |
|
113 | |||
112 | if self.due_date and self.start_date and self.due_date < self.start_date |
|
114 | if self.due_date and self.start_date and self.due_date < self.start_date | |
113 | errors.add :due_date, :activerecord_error_greater_than_start_date |
|
115 | errors.add :due_date, :activerecord_error_greater_than_start_date | |
114 | end |
|
116 | end | |
115 |
|
117 | |||
116 | if start_date && soonest_start && start_date < soonest_start |
|
118 | if start_date && soonest_start && start_date < soonest_start | |
117 | errors.add :start_date, :activerecord_error_invalid |
|
119 | errors.add :start_date, :activerecord_error_invalid | |
118 | end |
|
120 | end | |
119 | end |
|
121 | end | |
120 |
|
122 | |||
121 | def validate_on_create |
|
123 | def validate_on_create | |
122 | errors.add :tracker_id, :activerecord_error_invalid unless project.trackers.include?(tracker) |
|
124 | errors.add :tracker_id, :activerecord_error_invalid unless project.trackers.include?(tracker) | |
123 | end |
|
125 | end | |
124 |
|
126 | |||
125 | def before_create |
|
127 | def before_create | |
126 | # default assignment based on category |
|
128 | # default assignment based on category | |
127 | if assigned_to.nil? && category && category.assigned_to |
|
129 | if assigned_to.nil? && category && category.assigned_to | |
128 | self.assigned_to = category.assigned_to |
|
130 | self.assigned_to = category.assigned_to | |
129 | end |
|
131 | end | |
130 | end |
|
132 | end | |
131 |
|
133 | |||
132 | def before_save |
|
134 | def before_save | |
133 | if @current_journal |
|
135 | if @current_journal | |
134 | # attributes changes |
|
136 | # attributes changes | |
135 | (Issue.column_names - %w(id description)).each {|c| |
|
137 | (Issue.column_names - %w(id description)).each {|c| | |
136 | @current_journal.details << JournalDetail.new(:property => 'attr', |
|
138 | @current_journal.details << JournalDetail.new(:property => 'attr', | |
137 | :prop_key => c, |
|
139 | :prop_key => c, | |
138 | :old_value => @issue_before_change.send(c), |
|
140 | :old_value => @issue_before_change.send(c), | |
139 | :value => send(c)) unless send(c)==@issue_before_change.send(c) |
|
141 | :value => send(c)) unless send(c)==@issue_before_change.send(c) | |
140 | } |
|
142 | } | |
141 | # custom fields changes |
|
143 | # custom fields changes | |
142 | custom_values.each {|c| |
|
144 | custom_values.each {|c| | |
143 | next if (@custom_values_before_change[c.custom_field_id]==c.value || |
|
145 | next if (@custom_values_before_change[c.custom_field_id]==c.value || | |
144 | (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?)) |
|
146 | (@custom_values_before_change[c.custom_field_id].blank? && c.value.blank?)) | |
145 | @current_journal.details << JournalDetail.new(:property => 'cf', |
|
147 | @current_journal.details << JournalDetail.new(:property => 'cf', | |
146 | :prop_key => c.custom_field_id, |
|
148 | :prop_key => c.custom_field_id, | |
147 | :old_value => @custom_values_before_change[c.custom_field_id], |
|
149 | :old_value => @custom_values_before_change[c.custom_field_id], | |
148 | :value => c.value) |
|
150 | :value => c.value) | |
149 | } |
|
151 | } | |
150 | @current_journal.save |
|
152 | @current_journal.save | |
151 | end |
|
153 | end | |
152 | # Save the issue even if the journal is not saved (because empty) |
|
154 | # Save the issue even if the journal is not saved (because empty) | |
153 | true |
|
155 | true | |
154 | end |
|
156 | end | |
155 |
|
157 | |||
156 | def after_save |
|
158 | def after_save | |
157 | # Reload is needed in order to get the right status |
|
159 | # Reload is needed in order to get the right status | |
158 | reload |
|
160 | reload | |
159 |
|
161 | |||
160 | # Update start/due dates of following issues |
|
162 | # Update start/due dates of following issues | |
161 | relations_from.each(&:set_issue_to_dates) |
|
163 | relations_from.each(&:set_issue_to_dates) | |
162 |
|
164 | |||
163 | # Close duplicates if the issue was closed |
|
165 | # Close duplicates if the issue was closed | |
164 | if @issue_before_change && !@issue_before_change.closed? && self.closed? |
|
166 | if @issue_before_change && !@issue_before_change.closed? && self.closed? | |
165 | duplicates.each do |duplicate| |
|
167 | duplicates.each do |duplicate| | |
166 | # Reload is need in case the duplicate was updated by a previous duplicate |
|
168 | # Reload is need in case the duplicate was updated by a previous duplicate | |
167 | duplicate.reload |
|
169 | duplicate.reload | |
168 | # Don't re-close it if it's already closed |
|
170 | # Don't re-close it if it's already closed | |
169 | next if duplicate.closed? |
|
171 | next if duplicate.closed? | |
170 | # Same user and notes |
|
172 | # Same user and notes | |
171 | duplicate.init_journal(@current_journal.user, @current_journal.notes) |
|
173 | duplicate.init_journal(@current_journal.user, @current_journal.notes) | |
172 | duplicate.update_attribute :status, self.status |
|
174 | duplicate.update_attribute :status, self.status | |
173 | end |
|
175 | end | |
174 | end |
|
176 | end | |
175 | end |
|
177 | end | |
176 |
|
178 | |||
177 | def init_journal(user, notes = "") |
|
179 | def init_journal(user, notes = "") | |
178 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) |
|
180 | @current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes) | |
179 | @issue_before_change = self.clone |
|
181 | @issue_before_change = self.clone | |
180 | @issue_before_change.status = self.status |
|
182 | @issue_before_change.status = self.status | |
181 | @custom_values_before_change = {} |
|
183 | @custom_values_before_change = {} | |
182 | self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value } |
|
184 | self.custom_values.each {|c| @custom_values_before_change.store c.custom_field_id, c.value } | |
183 | @current_journal |
|
185 | @current_journal | |
184 | end |
|
186 | end | |
185 |
|
187 | |||
186 | # Return true if the issue is closed, otherwise false |
|
188 | # Return true if the issue is closed, otherwise false | |
187 | def closed? |
|
189 | def closed? | |
188 | self.status.is_closed? |
|
190 | self.status.is_closed? | |
189 | end |
|
191 | end | |
190 |
|
192 | |||
191 | # Users the issue can be assigned to |
|
193 | # Users the issue can be assigned to | |
192 | def assignable_users |
|
194 | def assignable_users | |
193 | project.assignable_users |
|
195 | project.assignable_users | |
194 | end |
|
196 | end | |
195 |
|
197 | |||
196 | # Returns an array of status that user is able to apply |
|
198 | # Returns an array of status that user is able to apply | |
197 | def new_statuses_allowed_to(user) |
|
199 | def new_statuses_allowed_to(user) | |
198 | statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker) |
|
200 | statuses = status.find_new_statuses_allowed_to(user.role_for_project(project), tracker) | |
199 | statuses << status unless statuses.empty? |
|
201 | statuses << status unless statuses.empty? | |
200 | statuses.uniq.sort |
|
202 | statuses.uniq.sort | |
201 | end |
|
203 | end | |
202 |
|
204 | |||
203 | # Returns the mail adresses of users that should be notified for the issue |
|
205 | # Returns the mail adresses of users that should be notified for the issue | |
204 | def recipients |
|
206 | def recipients | |
205 | recipients = project.recipients |
|
207 | recipients = project.recipients | |
206 | # Author and assignee are always notified unless they have been locked |
|
208 | # Author and assignee are always notified unless they have been locked | |
207 | recipients << author.mail if author && author.active? |
|
209 | recipients << author.mail if author && author.active? | |
208 | recipients << assigned_to.mail if assigned_to && assigned_to.active? |
|
210 | recipients << assigned_to.mail if assigned_to && assigned_to.active? | |
209 | recipients.compact.uniq |
|
211 | recipients.compact.uniq | |
210 | end |
|
212 | end | |
211 |
|
213 | |||
212 | def spent_hours |
|
214 | def spent_hours | |
213 | @spent_hours ||= time_entries.sum(:hours) || 0 |
|
215 | @spent_hours ||= time_entries.sum(:hours) || 0 | |
214 | end |
|
216 | end | |
215 |
|
217 | |||
216 | def relations |
|
218 | def relations | |
217 | (relations_from + relations_to).sort |
|
219 | (relations_from + relations_to).sort | |
218 | end |
|
220 | end | |
219 |
|
221 | |||
220 | def all_dependent_issues |
|
222 | def all_dependent_issues | |
221 | dependencies = [] |
|
223 | dependencies = [] | |
222 | relations_from.each do |relation| |
|
224 | relations_from.each do |relation| | |
223 | dependencies << relation.issue_to |
|
225 | dependencies << relation.issue_to | |
224 | dependencies += relation.issue_to.all_dependent_issues |
|
226 | dependencies += relation.issue_to.all_dependent_issues | |
225 | end |
|
227 | end | |
226 | dependencies |
|
228 | dependencies | |
227 | end |
|
229 | end | |
228 |
|
230 | |||
229 | # Returns an array of issues that duplicate this one |
|
231 | # Returns an array of issues that duplicate this one | |
230 | def duplicates |
|
232 | def duplicates | |
231 | relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from} |
|
233 | relations_to.select {|r| r.relation_type == IssueRelation::TYPE_DUPLICATES}.collect {|r| r.issue_from} | |
232 | end |
|
234 | end | |
233 |
|
235 | |||
234 | # Returns the due date or the target due date if any |
|
236 | # Returns the due date or the target due date if any | |
235 | # Used on gantt chart |
|
237 | # Used on gantt chart | |
236 | def due_before |
|
238 | def due_before | |
237 | due_date || (fixed_version ? fixed_version.effective_date : nil) |
|
239 | due_date || (fixed_version ? fixed_version.effective_date : nil) | |
238 | end |
|
240 | end | |
239 |
|
241 | |||
240 | def duration |
|
242 | def duration | |
241 | (start_date && due_date) ? due_date - start_date : 0 |
|
243 | (start_date && due_date) ? due_date - start_date : 0 | |
242 | end |
|
244 | end | |
243 |
|
245 | |||
244 | def soonest_start |
|
246 | def soonest_start | |
245 | @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min |
|
247 | @soonest_start ||= relations_to.collect{|relation| relation.successor_soonest_start}.compact.min | |
246 | end |
|
248 | end | |
247 |
|
249 | |||
248 | def self.visible_by(usr) |
|
250 | def self.visible_by(usr) | |
249 | with_scope(:find => { :conditions => Project.visible_by(usr) }) do |
|
251 | with_scope(:find => { :conditions => Project.visible_by(usr) }) do | |
250 | yield |
|
252 | yield | |
251 | end |
|
253 | end | |
252 | end |
|
254 | end | |
253 |
|
255 | |||
254 | def to_s |
|
256 | def to_s | |
255 | "#{tracker} ##{id}: #{subject}" |
|
257 | "#{tracker} ##{id}: #{subject}" | |
256 | end |
|
258 | end | |
257 | end |
|
259 | end |
@@ -1,17 +1,22 | |||||
1 | --- |
|
1 | --- | |
2 | issue_categories_001: |
|
2 | issue_categories_001: | |
3 | name: Printing |
|
3 | name: Printing | |
4 | project_id: 1 |
|
4 | project_id: 1 | |
5 | assigned_to_id: 2 |
|
5 | assigned_to_id: 2 | |
6 | id: 1 |
|
6 | id: 1 | |
7 | issue_categories_002: |
|
7 | issue_categories_002: | |
8 | name: Recipes |
|
8 | name: Recipes | |
9 | project_id: 1 |
|
9 | project_id: 1 | |
10 | assigned_to_id: |
|
10 | assigned_to_id: | |
11 | id: 2 |
|
11 | id: 2 | |
12 | issue_categories_003: |
|
12 | issue_categories_003: | |
13 | name: Stock management |
|
13 | name: Stock management | |
14 | project_id: 2 |
|
14 | project_id: 2 | |
15 | assigned_to_id: |
|
15 | assigned_to_id: | |
16 | id: 3 |
|
16 | id: 3 | |
|
17 | issue_categories_004: | |||
|
18 | name: Printing | |||
|
19 | project_id: 2 | |||
|
20 | assigned_to_id: | |||
|
21 | id: 4 | |||
17 | No newline at end of file |
|
22 |
@@ -1,184 +1,193 | |||||
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 |
|
19 | |||
20 | class IssueTest < Test::Unit::TestCase |
|
20 | class IssueTest < Test::Unit::TestCase | |
21 | fixtures :projects, :users, :members, |
|
21 | fixtures :projects, :users, :members, | |
22 | :trackers, :projects_trackers, |
|
22 | :trackers, :projects_trackers, | |
23 | :issue_statuses, :issue_categories, |
|
23 | :issue_statuses, :issue_categories, | |
24 | :enumerations, |
|
24 | :enumerations, | |
25 | :issues, |
|
25 | :issues, | |
26 | :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values, |
|
26 | :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values, | |
27 | :time_entries |
|
27 | :time_entries | |
28 |
|
28 | |||
29 | def test_create |
|
29 | def test_create | |
30 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30') |
|
30 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30') | |
31 | assert issue.save |
|
31 | assert issue.save | |
32 | issue.reload |
|
32 | issue.reload | |
33 | assert_equal 1.5, issue.estimated_hours |
|
33 | assert_equal 1.5, issue.estimated_hours | |
34 | end |
|
34 | end | |
35 |
|
35 | |||
36 | def test_create_with_required_custom_field |
|
36 | def test_create_with_required_custom_field | |
37 | field = IssueCustomField.find_by_name('Database') |
|
37 | field = IssueCustomField.find_by_name('Database') | |
38 | field.update_attribute(:is_required, true) |
|
38 | field.update_attribute(:is_required, true) | |
39 |
|
39 | |||
40 | 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') |
|
40 | 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') | |
41 | assert issue.available_custom_fields.include?(field) |
|
41 | assert issue.available_custom_fields.include?(field) | |
42 | # No value for the custom field |
|
42 | # No value for the custom field | |
43 | assert !issue.save |
|
43 | assert !issue.save | |
44 | assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values) |
|
44 | assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values) | |
45 | # Blank value |
|
45 | # Blank value | |
46 | issue.custom_field_values = { field.id => '' } |
|
46 | issue.custom_field_values = { field.id => '' } | |
47 | assert !issue.save |
|
47 | assert !issue.save | |
48 | assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values) |
|
48 | assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values) | |
49 | # Invalid value |
|
49 | # Invalid value | |
50 | issue.custom_field_values = { field.id => 'SQLServer' } |
|
50 | issue.custom_field_values = { field.id => 'SQLServer' } | |
51 | assert !issue.save |
|
51 | assert !issue.save | |
52 | assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values) |
|
52 | assert_equal 'activerecord_error_invalid', issue.errors.on(:custom_values) | |
53 | # Valid value |
|
53 | # Valid value | |
54 | issue.custom_field_values = { field.id => 'PostgreSQL' } |
|
54 | issue.custom_field_values = { field.id => 'PostgreSQL' } | |
55 | assert issue.save |
|
55 | assert issue.save | |
56 | issue.reload |
|
56 | issue.reload | |
57 | assert_equal 'PostgreSQL', issue.custom_value_for(field).value |
|
57 | assert_equal 'PostgreSQL', issue.custom_value_for(field).value | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | def test_update_issue_with_required_custom_field |
|
60 | def test_update_issue_with_required_custom_field | |
61 | field = IssueCustomField.find_by_name('Database') |
|
61 | field = IssueCustomField.find_by_name('Database') | |
62 | field.update_attribute(:is_required, true) |
|
62 | field.update_attribute(:is_required, true) | |
63 |
|
63 | |||
64 | issue = Issue.find(1) |
|
64 | issue = Issue.find(1) | |
65 | assert_nil issue.custom_value_for(field) |
|
65 | assert_nil issue.custom_value_for(field) | |
66 | assert issue.available_custom_fields.include?(field) |
|
66 | assert issue.available_custom_fields.include?(field) | |
67 | # No change to custom values, issue can be saved |
|
67 | # No change to custom values, issue can be saved | |
68 | assert issue.save |
|
68 | assert issue.save | |
69 | # Blank value |
|
69 | # Blank value | |
70 | issue.custom_field_values = { field.id => '' } |
|
70 | issue.custom_field_values = { field.id => '' } | |
71 | assert !issue.save |
|
71 | assert !issue.save | |
72 | # Valid value |
|
72 | # Valid value | |
73 | issue.custom_field_values = { field.id => 'PostgreSQL' } |
|
73 | issue.custom_field_values = { field.id => 'PostgreSQL' } | |
74 | assert issue.save |
|
74 | assert issue.save | |
75 | issue.reload |
|
75 | issue.reload | |
76 | assert_equal 'PostgreSQL', issue.custom_value_for(field).value |
|
76 | assert_equal 'PostgreSQL', issue.custom_value_for(field).value | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | def test_should_not_update_attributes_if_custom_fields_validation_fails |
|
79 | def test_should_not_update_attributes_if_custom_fields_validation_fails | |
80 | issue = Issue.find(1) |
|
80 | issue = Issue.find(1) | |
81 | field = IssueCustomField.find_by_name('Database') |
|
81 | field = IssueCustomField.find_by_name('Database') | |
82 | assert issue.available_custom_fields.include?(field) |
|
82 | assert issue.available_custom_fields.include?(field) | |
83 |
|
83 | |||
84 | issue.custom_field_values = { field.id => 'Invalid' } |
|
84 | issue.custom_field_values = { field.id => 'Invalid' } | |
85 | issue.subject = 'Should be not be saved' |
|
85 | issue.subject = 'Should be not be saved' | |
86 | assert !issue.save |
|
86 | assert !issue.save | |
87 |
|
87 | |||
88 | issue.reload |
|
88 | issue.reload | |
89 | assert_equal "Can't print recipes", issue.subject |
|
89 | assert_equal "Can't print recipes", issue.subject | |
90 | end |
|
90 | end | |
91 |
|
91 | |||
92 | def test_should_not_recreate_custom_values_objects_on_update |
|
92 | def test_should_not_recreate_custom_values_objects_on_update | |
93 | field = IssueCustomField.find_by_name('Database') |
|
93 | field = IssueCustomField.find_by_name('Database') | |
94 |
|
94 | |||
95 | issue = Issue.find(1) |
|
95 | issue = Issue.find(1) | |
96 | issue.custom_field_values = { field.id => 'PostgreSQL' } |
|
96 | issue.custom_field_values = { field.id => 'PostgreSQL' } | |
97 | assert issue.save |
|
97 | assert issue.save | |
98 | custom_value = issue.custom_value_for(field) |
|
98 | custom_value = issue.custom_value_for(field) | |
99 | issue.reload |
|
99 | issue.reload | |
100 | issue.custom_field_values = { field.id => 'MySQL' } |
|
100 | issue.custom_field_values = { field.id => 'MySQL' } | |
101 | assert issue.save |
|
101 | assert issue.save | |
102 | issue.reload |
|
102 | issue.reload | |
103 | assert_equal custom_value.id, issue.custom_value_for(field).id |
|
103 | assert_equal custom_value.id, issue.custom_value_for(field).id | |
104 | end |
|
104 | end | |
105 |
|
105 | |||
106 | def test_category_based_assignment |
|
106 | def test_category_based_assignment | |
107 | issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1) |
|
107 | issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1) | |
108 | assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to |
|
108 | assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to | |
109 | end |
|
109 | end | |
110 |
|
110 | |||
111 | def test_copy |
|
111 | def test_copy | |
112 | issue = Issue.new.copy_from(1) |
|
112 | issue = Issue.new.copy_from(1) | |
113 | assert issue.save |
|
113 | assert issue.save | |
114 | issue.reload |
|
114 | issue.reload | |
115 | orig = Issue.find(1) |
|
115 | orig = Issue.find(1) | |
116 | assert_equal orig.subject, issue.subject |
|
116 | assert_equal orig.subject, issue.subject | |
117 | assert_equal orig.tracker, issue.tracker |
|
117 | assert_equal orig.tracker, issue.tracker | |
118 | assert_equal orig.custom_values.first.value, issue.custom_values.first.value |
|
118 | assert_equal orig.custom_values.first.value, issue.custom_values.first.value | |
119 | end |
|
119 | end | |
120 |
|
120 | |||
121 | def test_should_close_duplicates |
|
121 | def test_should_close_duplicates | |
122 | # Create 3 issues |
|
122 | # Create 3 issues | |
123 | issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test') |
|
123 | issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test') | |
124 | assert issue1.save |
|
124 | assert issue1.save | |
125 | issue2 = issue1.clone |
|
125 | issue2 = issue1.clone | |
126 | assert issue2.save |
|
126 | assert issue2.save | |
127 | issue3 = issue1.clone |
|
127 | issue3 = issue1.clone | |
128 | assert issue3.save |
|
128 | assert issue3.save | |
129 |
|
129 | |||
130 | # 2 is a dupe of 1 |
|
130 | # 2 is a dupe of 1 | |
131 | IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES) |
|
131 | IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES) | |
132 | # And 3 is a dupe of 2 |
|
132 | # And 3 is a dupe of 2 | |
133 | IssueRelation.create(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES) |
|
133 | IssueRelation.create(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES) | |
134 | # And 3 is a dupe of 1 (circular duplicates) |
|
134 | # And 3 is a dupe of 1 (circular duplicates) | |
135 | IssueRelation.create(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES) |
|
135 | IssueRelation.create(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES) | |
136 |
|
136 | |||
137 | assert issue1.reload.duplicates.include?(issue2) |
|
137 | assert issue1.reload.duplicates.include?(issue2) | |
138 |
|
138 | |||
139 | # Closing issue 1 |
|
139 | # Closing issue 1 | |
140 | issue1.init_journal(User.find(:first), "Closing issue1") |
|
140 | issue1.init_journal(User.find(:first), "Closing issue1") | |
141 | issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true} |
|
141 | issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true} | |
142 | assert issue1.save |
|
142 | assert issue1.save | |
143 | # 2 and 3 should be also closed |
|
143 | # 2 and 3 should be also closed | |
144 | assert issue2.reload.closed? |
|
144 | assert issue2.reload.closed? | |
145 | assert issue3.reload.closed? |
|
145 | assert issue3.reload.closed? | |
146 | end |
|
146 | end | |
147 |
|
147 | |||
148 | def test_should_not_close_duplicated_issue |
|
148 | def test_should_not_close_duplicated_issue | |
149 | # Create 3 issues |
|
149 | # Create 3 issues | |
150 | issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test') |
|
150 | issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test') | |
151 | assert issue1.save |
|
151 | assert issue1.save | |
152 | issue2 = issue1.clone |
|
152 | issue2 = issue1.clone | |
153 | assert issue2.save |
|
153 | assert issue2.save | |
154 |
|
154 | |||
155 | # 2 is a dupe of 1 |
|
155 | # 2 is a dupe of 1 | |
156 | IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES) |
|
156 | IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES) | |
157 | # 2 is a dup of 1 but 1 is not a duplicate of 2 |
|
157 | # 2 is a dup of 1 but 1 is not a duplicate of 2 | |
158 | assert !issue2.reload.duplicates.include?(issue1) |
|
158 | assert !issue2.reload.duplicates.include?(issue1) | |
159 |
|
159 | |||
160 | # Closing issue 2 |
|
160 | # Closing issue 2 | |
161 | issue2.init_journal(User.find(:first), "Closing issue2") |
|
161 | issue2.init_journal(User.find(:first), "Closing issue2") | |
162 | issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true} |
|
162 | issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true} | |
163 | assert issue2.save |
|
163 | assert issue2.save | |
164 | # 1 should not be also closed |
|
164 | # 1 should not be also closed | |
165 | assert !issue1.reload.closed? |
|
165 | assert !issue1.reload.closed? | |
166 | end |
|
166 | end | |
167 |
|
167 | |||
168 | def test_move_to_another_project |
|
168 | def test_move_to_another_project_with_same_category | |
169 | issue = Issue.find(1) |
|
169 | issue = Issue.find(1) | |
170 | assert issue.move_to(Project.find(2)) |
|
170 | assert issue.move_to(Project.find(2)) | |
171 | issue.reload |
|
171 | issue.reload | |
172 | assert_equal 2, issue.project_id |
|
172 | assert_equal 2, issue.project_id | |
173 |
# Category |
|
173 | # Category changes | |
174 |
assert_ |
|
174 | assert_equal 4, issue.category_id | |
175 | # Make sure time entries were move to the target project |
|
175 | # Make sure time entries were move to the target project | |
176 | assert_equal 2, issue.time_entries.first.project_id |
|
176 | assert_equal 2, issue.time_entries.first.project_id | |
177 | end |
|
177 | end | |
178 |
|
178 | |||
|
179 | def test_move_to_another_project_without_same_category | |||
|
180 | issue = Issue.find(2) | |||
|
181 | assert issue.move_to(Project.find(2)) | |||
|
182 | issue.reload | |||
|
183 | assert_equal 2, issue.project_id | |||
|
184 | # Category cleared | |||
|
185 | assert_nil issue.category_id | |||
|
186 | end | |||
|
187 | ||||
179 | def test_issue_destroy |
|
188 | def test_issue_destroy | |
180 | Issue.find(1).destroy |
|
189 | Issue.find(1).destroy | |
181 | assert_nil Issue.find_by_id(1) |
|
190 | assert_nil Issue.find_by_id(1) | |
182 | assert_nil TimeEntry.find_by_issue_id(1) |
|
191 | assert_nil TimeEntry.find_by_issue_id(1) | |
183 | end |
|
192 | end | |
184 | end |
|
193 | end |
General Comments 0
You need to be logged in to leave comments.
Login now