##// END OF EJS Templates
Backported r13560 and r13569 (#18280)....
Jean-Philippe Lang -
r13249:cc2d5f61ab06
parent child
Show More
@@ -1,105 +1,123
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 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 IssueStatus < ActiveRecord::Base
18 class IssueStatus < ActiveRecord::Base
19 before_destroy :check_integrity
19 before_destroy :check_integrity
20 has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id"
20 has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id"
21 acts_as_list
21 acts_as_list
22
22
23 after_update :handle_is_closed_change
23 before_destroy :delete_workflow_rules
24 before_destroy :delete_workflow_rules
24 after_save :update_default
25 after_save :update_default
25
26
26 validates_presence_of :name
27 validates_presence_of :name
27 validates_uniqueness_of :name
28 validates_uniqueness_of :name
28 validates_length_of :name, :maximum => 30
29 validates_length_of :name, :maximum => 30
29 validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
30 validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
30
31
31 scope :sorted, lambda { order("#{table_name}.position ASC") }
32 scope :sorted, lambda { order("#{table_name}.position ASC") }
32 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
33 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
33
34
34 def update_default
35 def update_default
35 IssueStatus.where(['id <> ?', id]).update_all({:is_default => false}) if self.is_default?
36 IssueStatus.where(['id <> ?', id]).update_all({:is_default => false}) if self.is_default?
36 end
37 end
37
38
38 # Returns the default status for new issues
39 # Returns the default status for new issues
39 def self.default
40 def self.default
40 where(:is_default => true).first
41 where(:is_default => true).first
41 end
42 end
42
43
43 # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
44 # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
44 def self.update_issue_done_ratios
45 def self.update_issue_done_ratios
45 if Issue.use_status_for_done_ratio?
46 if Issue.use_status_for_done_ratio?
46 IssueStatus.where("default_done_ratio >= 0").each do |status|
47 IssueStatus.where("default_done_ratio >= 0").each do |status|
47 Issue.where({:status_id => status.id}).update_all({:done_ratio => status.default_done_ratio})
48 Issue.where({:status_id => status.id}).update_all({:done_ratio => status.default_done_ratio})
48 end
49 end
49 end
50 end
50
51
51 return Issue.use_status_for_done_ratio?
52 return Issue.use_status_for_done_ratio?
52 end
53 end
53
54
54 # Returns an array of all statuses the given role can switch to
55 # Returns an array of all statuses the given role can switch to
55 # Uses association cache when called more than one time
56 # Uses association cache when called more than one time
56 def new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
57 def new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
57 if roles && tracker
58 if roles && tracker
58 role_ids = roles.collect(&:id)
59 role_ids = roles.collect(&:id)
59 transitions = workflows.select do |w|
60 transitions = workflows.select do |w|
60 role_ids.include?(w.role_id) &&
61 role_ids.include?(w.role_id) &&
61 w.tracker_id == tracker.id &&
62 w.tracker_id == tracker.id &&
62 ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee))
63 ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee))
63 end
64 end
64 transitions.map(&:new_status).compact.sort
65 transitions.map(&:new_status).compact.sort
65 else
66 else
66 []
67 []
67 end
68 end
68 end
69 end
69
70
70 # Same thing as above but uses a database query
71 # Same thing as above but uses a database query
71 # More efficient than the previous method if called just once
72 # More efficient than the previous method if called just once
72 def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
73 def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
73 if roles.present? && tracker
74 if roles.present? && tracker
74 conditions = "(author = :false AND assignee = :false)"
75 conditions = "(author = :false AND assignee = :false)"
75 conditions << " OR author = :true" if author
76 conditions << " OR author = :true" if author
76 conditions << " OR assignee = :true" if assignee
77 conditions << " OR assignee = :true" if assignee
77
78
78 workflows.
79 workflows.
79 includes(:new_status).
80 includes(:new_status).
80 where(["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})",
81 where(["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})",
81 {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false}
82 {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false}
82 ]).all.
83 ]).all.
83 map(&:new_status).compact.sort
84 map(&:new_status).compact.sort
84 else
85 else
85 []
86 []
86 end
87 end
87 end
88 end
88
89
89 def <=>(status)
90 def <=>(status)
90 position <=> status.position
91 position <=> status.position
91 end
92 end
92
93
93 def to_s; name end
94 def to_s; name end
94
95
95 private
96 private
96
97
98 # Updates issues closed_on attribute when an existing status is set as closed.
99 def handle_is_closed_change
100 if is_closed_changed? && is_closed == true
101 # First we update issues that have a journal for when the current status was set,
102 # a subselect is used to update all issues with a single query
103 subselect = "SELECT MAX(j.created_on) FROM #{Journal.table_name} j" +
104 " JOIN #{JournalDetail.table_name} d ON d.journal_id = j.id" +
105 " WHERE j.journalized_type = 'Issue' AND j.journalized_id = #{Issue.table_name}.id" +
106 " AND d.property = 'attr' AND d.prop_key = 'status_id' AND d.value = :status_id"
107 Issue.where(:status_id => id, :closed_on => nil).update_all(["closed_on = (#{subselect})", :status_id => id.to_s])
108
109 # Then we update issues that don't have a journal which means the
110 # current status was set on creation
111 Issue.where(:status_id => id, :closed_on => nil).update_all("closed_on = created_on")
112 end
113 end
114
97 def check_integrity
115 def check_integrity
98 raise "Can't delete status" if Issue.where(:status_id => id).any?
116 raise "Can't delete status" if Issue.where(:status_id => id).any?
99 end
117 end
100
118
101 # Deletes associated workflows
119 # Deletes associated workflows
102 def delete_workflow_rules
120 def delete_workflow_rules
103 WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
121 WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
104 end
122 end
105 end
123 end
@@ -1,123 +1,165
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 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.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class IssueStatusTest < ActiveSupport::TestCase
20 class IssueStatusTest < ActiveSupport::TestCase
21 fixtures :issue_statuses, :issues, :roles, :trackers
21 fixtures :projects, :users, :members, :member_roles, :roles,
22 :groups_users,
23 :trackers, :projects_trackers,
24 :enabled_modules,
25 :versions,
26 :issue_statuses, :issue_categories, :issue_relations, :workflows,
27 :enumerations,
28 :issues, :journals, :journal_details,
29 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values
22
30
23 def test_create
31 def test_create
24 status = IssueStatus.new :name => "Assigned"
32 status = IssueStatus.new :name => "Assigned"
25 assert !status.save
33 assert !status.save
26 # status name uniqueness
34 # status name uniqueness
27 assert_equal 1, status.errors.count
35 assert_equal 1, status.errors.count
28
36
29 status.name = "Test Status"
37 status.name = "Test Status"
30 assert status.save
38 assert status.save
31 assert !status.is_default
39 assert !status.is_default
32 end
40 end
33
41
34 def test_destroy
42 def test_destroy
35 status = IssueStatus.find(3)
43 status = IssueStatus.find(3)
36 assert_difference 'IssueStatus.count', -1 do
44 assert_difference 'IssueStatus.count', -1 do
37 assert status.destroy
45 assert status.destroy
38 end
46 end
39 assert_nil WorkflowTransition.where(:old_status_id => status.id).first
47 assert_nil WorkflowTransition.where(:old_status_id => status.id).first
40 assert_nil WorkflowTransition.where(:new_status_id => status.id).first
48 assert_nil WorkflowTransition.where(:new_status_id => status.id).first
41 end
49 end
42
50
43 def test_destroy_status_in_use
51 def test_destroy_status_in_use
44 # Status assigned to an Issue
52 # Status assigned to an Issue
45 status = Issue.find(1).status
53 status = Issue.find(1).status
46 assert_raise(RuntimeError, "Can't delete status") { status.destroy }
54 assert_raise(RuntimeError, "Can't delete status") { status.destroy }
47 end
55 end
48
56
49 def test_default
57 def test_default
50 status = IssueStatus.default
58 status = IssueStatus.default
51 assert_kind_of IssueStatus, status
59 assert_kind_of IssueStatus, status
52 end
60 end
53
61
54 def test_change_default
62 def test_change_default
55 status = IssueStatus.find(2)
63 status = IssueStatus.find(2)
56 assert !status.is_default
64 assert !status.is_default
57 status.is_default = true
65 status.is_default = true
58 assert status.save
66 assert status.save
59 status.reload
67 status.reload
60
68
61 assert_equal status, IssueStatus.default
69 assert_equal status, IssueStatus.default
62 assert !IssueStatus.find(1).is_default
70 assert !IssueStatus.find(1).is_default
63 end
71 end
64
72
65 def test_reorder_should_not_clear_default_status
73 def test_reorder_should_not_clear_default_status
66 status = IssueStatus.default
74 status = IssueStatus.default
67 status.move_to_bottom
75 status.move_to_bottom
68 status.reload
76 status.reload
69 assert status.is_default?
77 assert status.is_default?
70 end
78 end
71
79
72 def test_new_statuses_allowed_to
80 def test_new_statuses_allowed_to
73 WorkflowTransition.delete_all
81 WorkflowTransition.delete_all
74
82
75 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false)
83 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false)
76 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false)
84 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false)
77 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true)
85 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true)
78 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true)
86 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true)
79 status = IssueStatus.find(1)
87 status = IssueStatus.find(1)
80 role = Role.find(1)
88 role = Role.find(1)
81 tracker = Tracker.find(1)
89 tracker = Tracker.find(1)
82
90
83 assert_equal [2], status.new_statuses_allowed_to([role], tracker, false, false).map(&:id)
91 assert_equal [2], status.new_statuses_allowed_to([role], tracker, false, false).map(&:id)
84 assert_equal [2], status.find_new_statuses_allowed_to([role], tracker, false, false).map(&:id)
92 assert_equal [2], status.find_new_statuses_allowed_to([role], tracker, false, false).map(&:id)
85
93
86 assert_equal [2, 3, 5], status.new_statuses_allowed_to([role], tracker, true, false).map(&:id)
94 assert_equal [2, 3, 5], status.new_statuses_allowed_to([role], tracker, true, false).map(&:id)
87 assert_equal [2, 3, 5], status.find_new_statuses_allowed_to([role], tracker, true, false).map(&:id)
95 assert_equal [2, 3, 5], status.find_new_statuses_allowed_to([role], tracker, true, false).map(&:id)
88
96
89 assert_equal [2, 4, 5], status.new_statuses_allowed_to([role], tracker, false, true).map(&:id)
97 assert_equal [2, 4, 5], status.new_statuses_allowed_to([role], tracker, false, true).map(&:id)
90 assert_equal [2, 4, 5], status.find_new_statuses_allowed_to([role], tracker, false, true).map(&:id)
98 assert_equal [2, 4, 5], status.find_new_statuses_allowed_to([role], tracker, false, true).map(&:id)
91
99
92 assert_equal [2, 3, 4, 5], status.new_statuses_allowed_to([role], tracker, true, true).map(&:id)
100 assert_equal [2, 3, 4, 5], status.new_statuses_allowed_to([role], tracker, true, true).map(&:id)
93 assert_equal [2, 3, 4, 5], status.find_new_statuses_allowed_to([role], tracker, true, true).map(&:id)
101 assert_equal [2, 3, 4, 5], status.find_new_statuses_allowed_to([role], tracker, true, true).map(&:id)
94 end
102 end
95
103
96 def test_update_done_ratios_with_issue_done_ratio_set_to_issue_field_should_change_nothing
104 def test_update_done_ratios_with_issue_done_ratio_set_to_issue_field_should_change_nothing
97 IssueStatus.find(1).update_attribute(:default_done_ratio, 50)
105 IssueStatus.find(1).update_attribute(:default_done_ratio, 50)
98
106
99 with_settings :issue_done_ratio => 'issue_field' do
107 with_settings :issue_done_ratio => 'issue_field' do
100 IssueStatus.update_issue_done_ratios
108 IssueStatus.update_issue_done_ratios
101 assert_equal 0, Issue.where(:done_ratio => 50).count
109 assert_equal 0, Issue.where(:done_ratio => 50).count
102 end
110 end
103 end
111 end
104
112
105 def test_update_done_ratios_with_issue_done_ratio_set_to_issue_status_should_update_issues
113 def test_update_done_ratios_with_issue_done_ratio_set_to_issue_status_should_update_issues
106 IssueStatus.find(1).update_attribute(:default_done_ratio, 50)
114 IssueStatus.find(1).update_attribute(:default_done_ratio, 50)
107 with_settings :issue_done_ratio => 'issue_status' do
115 with_settings :issue_done_ratio => 'issue_status' do
108 IssueStatus.update_issue_done_ratios
116 IssueStatus.update_issue_done_ratios
109 issues = Issue.where(:status_id => 1)
117 issues = Issue.where(:status_id => 1)
110 assert_equal [50], issues.map {|issue| issue.read_attribute(:done_ratio)}.uniq
118 assert_equal [50], issues.map {|issue| issue.read_attribute(:done_ratio)}.uniq
111 end
119 end
112 end
120 end
113
121
114 def test_sorted_scope
122 def test_sorted_scope
115 assert_equal IssueStatus.all.sort, IssueStatus.sorted.all
123 assert_equal IssueStatus.all.sort, IssueStatus.sorted.all
116 end
124 end
117
125
118 def test_named_scope
126 def test_named_scope
119 status = IssueStatus.named("resolved").first
127 status = IssueStatus.named("resolved").first
120 assert_not_nil status
128 assert_not_nil status
121 assert_equal "Resolved", status.name
129 assert_equal "Resolved", status.name
122 end
130 end
131
132 def test_setting_status_as_closed_should_set_closed_on_for_issues_without_status_journal
133 issue = Issue.generate!(:status_id => 1, :created_on => 2.days.ago)
134 assert_nil issue.closed_on
135
136 issue.status.update_attribute :is_closed, true
137
138 issue.reload
139 assert issue.closed?
140 assert_equal issue.created_on, issue.closed_on
141 end
142
143 def test_setting_status_as_closed_should_set_closed_on_for_issues_with_status_journal
144 issue = Issue.generate!(:status_id => 1, :created_on => 2.days.ago)
145 issue.init_journal(User.find(1))
146 issue.status_id = 2
147 issue.save!
148
149 issue.status.update_attribute :is_closed, true
150
151 issue.reload
152 assert issue.closed?
153 assert_equal issue.journals.first.created_on, issue.closed_on
154 end
155
156 def test_setting_status_as_closed_should_not_set_closed_on_for_issues_with_other_status
157 issue = Issue.generate!(:status_id => 2)
158
159 IssueStatus.find(1).update_attribute :is_closed, true
160
161 issue.reload
162 assert !issue.closed?
163 assert_nil issue.closed_on
164 end
123 end
165 end
General Comments 0
You need to be logged in to leave comments. Login now