##// END OF EJS Templates
Fixed: additional workflow transitions not available when set to both author and assignee (#8836)....
Jean-Philippe Lang -
r6180:5f79a6a19069
parent child
Show More
@@ -1,101 +1,103
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 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, :foreign_key => "old_status_id"
20 has_many :workflows, :foreign_key => "old_status_id"
21 acts_as_list
21 acts_as_list
22
22
23 before_destroy :delete_workflows
23 before_destroy :delete_workflows
24
24
25 validates_presence_of :name
25 validates_presence_of :name
26 validates_uniqueness_of :name
26 validates_uniqueness_of :name
27 validates_length_of :name, :maximum => 30
27 validates_length_of :name, :maximum => 30
28 validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
28 validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
29
29
30 named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
30 named_scope :named, lambda {|arg| { :conditions => ["LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip]}}
31
31
32 def after_save
32 def after_save
33 IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?
33 IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?
34 end
34 end
35
35
36 # Returns the default status for new issues
36 # Returns the default status for new issues
37 def self.default
37 def self.default
38 find(:first, :conditions =>["is_default=?", true])
38 find(:first, :conditions =>["is_default=?", true])
39 end
39 end
40
40
41 # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
41 # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
42 def self.update_issue_done_ratios
42 def self.update_issue_done_ratios
43 if Issue.use_status_for_done_ratio?
43 if Issue.use_status_for_done_ratio?
44 IssueStatus.find(:all, :conditions => ["default_done_ratio >= 0"]).each do |status|
44 IssueStatus.find(:all, :conditions => ["default_done_ratio >= 0"]).each do |status|
45 Issue.update_all(["done_ratio = ?", status.default_done_ratio],
45 Issue.update_all(["done_ratio = ?", status.default_done_ratio],
46 ["status_id = ?", status.id])
46 ["status_id = ?", status.id])
47 end
47 end
48 end
48 end
49
49
50 return Issue.use_status_for_done_ratio?
50 return Issue.use_status_for_done_ratio?
51 end
51 end
52
52
53 # Returns an array of all statuses the given role can switch to
53 # Returns an array of all statuses the given role can switch to
54 # Uses association cache when called more than one time
54 # Uses association cache when called more than one time
55 def new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
55 def new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
56 if roles && tracker
56 if roles && tracker
57 role_ids = roles.collect(&:id)
57 role_ids = roles.collect(&:id)
58 transitions = workflows.select do |w|
58 transitions = workflows.select do |w|
59 role_ids.include?(w.role_id) &&
59 role_ids.include?(w.role_id) &&
60 w.tracker_id == tracker.id &&
60 w.tracker_id == tracker.id &&
61 (author || !w.author) &&
61 ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee))
62 (assignee || !w.assignee)
63 end
62 end
64 transitions.collect{|w| w.new_status}.compact.sort
63 transitions.collect{|w| w.new_status}.compact.sort
65 else
64 else
66 []
65 []
67 end
66 end
68 end
67 end
69
68
70 # Same thing as above but uses a database query
69 # Same thing as above but uses a database query
71 # More efficient than the previous method if called just once
70 # More efficient than the previous method if called just once
72 def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
71 def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
73 if roles && tracker
72 if roles.present? && tracker
74 conditions = {:role_id => roles.collect(&:id), :tracker_id => tracker.id}
73 conditions = "(author = :false AND assignee = :false)"
75 conditions[:author] = false unless author
74 conditions << " OR author = :true" if author
76 conditions[:assignee] = false unless assignee
75 conditions << " OR assignee = :true" if assignee
77
76
78 workflows.find(:all,
77 workflows.find(:all,
79 :include => :new_status,
78 :include => :new_status,
80 :conditions => conditions).collect{|w| w.new_status}.compact.sort
79 :conditions => ["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})",
80 {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false}
81 ]
82 ).collect{|w| w.new_status}.compact.sort
81 else
83 else
82 []
84 []
83 end
85 end
84 end
86 end
85
87
86 def <=>(status)
88 def <=>(status)
87 position <=> status.position
89 position <=> status.position
88 end
90 end
89
91
90 def to_s; name end
92 def to_s; name end
91
93
92 private
94 private
93 def check_integrity
95 def check_integrity
94 raise "Can't delete status" if Issue.find(:first, :conditions => ["status_id=?", self.id])
96 raise "Can't delete status" if Issue.find(:first, :conditions => ["status_id=?", self.id])
95 end
97 end
96
98
97 # Deletes associated workflows
99 # Deletes associated workflows
98 def delete_workflows
100 def delete_workflows
99 Workflow.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
101 Workflow.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
100 end
102 end
101 end
103 end
@@ -1,131 +1,131
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.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 :issue_statuses, :issues, :roles, :trackers
22
22
23 def test_create
23 def test_create
24 status = IssueStatus.new :name => "Assigned"
24 status = IssueStatus.new :name => "Assigned"
25 assert !status.save
25 assert !status.save
26 # status name uniqueness
26 # status name uniqueness
27 assert_equal 1, status.errors.count
27 assert_equal 1, status.errors.count
28
28
29 status.name = "Test Status"
29 status.name = "Test Status"
30 assert status.save
30 assert status.save
31 assert !status.is_default
31 assert !status.is_default
32 end
32 end
33
33
34 def test_destroy
34 def test_destroy
35 status = IssueStatus.find(3)
35 status = IssueStatus.find(3)
36 assert_difference 'IssueStatus.count', -1 do
36 assert_difference 'IssueStatus.count', -1 do
37 assert status.destroy
37 assert status.destroy
38 end
38 end
39 assert_nil Workflow.first(:conditions => {:old_status_id => status.id})
39 assert_nil Workflow.first(:conditions => {:old_status_id => status.id})
40 assert_nil Workflow.first(:conditions => {:new_status_id => status.id})
40 assert_nil Workflow.first(:conditions => {:new_status_id => status.id})
41 end
41 end
42
42
43 def test_destroy_status_in_use
43 def test_destroy_status_in_use
44 # Status assigned to an Issue
44 # Status assigned to an Issue
45 status = Issue.find(1).status
45 status = Issue.find(1).status
46 assert_raise(RuntimeError, "Can't delete status") { status.destroy }
46 assert_raise(RuntimeError, "Can't delete status") { status.destroy }
47 end
47 end
48
48
49 def test_default
49 def test_default
50 status = IssueStatus.default
50 status = IssueStatus.default
51 assert_kind_of IssueStatus, status
51 assert_kind_of IssueStatus, status
52 end
52 end
53
53
54 def test_change_default
54 def test_change_default
55 status = IssueStatus.find(2)
55 status = IssueStatus.find(2)
56 assert !status.is_default
56 assert !status.is_default
57 status.is_default = true
57 status.is_default = true
58 assert status.save
58 assert status.save
59 status.reload
59 status.reload
60
60
61 assert_equal status, IssueStatus.default
61 assert_equal status, IssueStatus.default
62 assert !IssueStatus.find(1).is_default
62 assert !IssueStatus.find(1).is_default
63 end
63 end
64
64
65 def test_reorder_should_not_clear_default_status
65 def test_reorder_should_not_clear_default_status
66 status = IssueStatus.default
66 status = IssueStatus.default
67 status.move_to_bottom
67 status.move_to_bottom
68 status.reload
68 status.reload
69 assert status.is_default?
69 assert status.is_default?
70 end
70 end
71
71
72 def test_new_statuses_allowed_to
72 def test_new_statuses_allowed_to
73 Workflow.delete_all
73 Workflow.delete_all
74
74
75 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false)
75 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false)
76 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false)
76 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false)
77 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true)
77 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true)
78 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true)
78 Workflow.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true)
79 status = IssueStatus.find(1)
79 status = IssueStatus.find(1)
80 role = Role.find(1)
80 role = Role.find(1)
81 tracker = Tracker.find(1)
81 tracker = Tracker.find(1)
82
82
83 assert_equal [2], status.new_statuses_allowed_to([role], tracker, false, false).map(&:id)
83 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)
84 assert_equal [2], status.find_new_statuses_allowed_to([role], tracker, false, false).map(&:id)
85
85
86 assert_equal [2, 3], status.new_statuses_allowed_to([role], tracker, true, false).map(&:id)
86 assert_equal [2, 3, 5], status.new_statuses_allowed_to([role], tracker, true, false).map(&:id)
87 assert_equal [2, 3], status.find_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)
88
88
89 assert_equal [2, 4], status.new_statuses_allowed_to([role], tracker, false, true).map(&:id)
89 assert_equal [2, 4, 5], status.new_statuses_allowed_to([role], tracker, false, true).map(&:id)
90 assert_equal [2, 4], status.find_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)
91
91
92 assert_equal [2, 3, 4, 5], status.new_statuses_allowed_to([role], tracker, true, true).map(&:id)
92 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)
93 assert_equal [2, 3, 4, 5], status.find_new_statuses_allowed_to([role], tracker, true, true).map(&:id)
94 end
94 end
95
95
96 context "#update_done_ratios" do
96 context "#update_done_ratios" do
97 setup do
97 setup do
98 @issue = Issue.find(1)
98 @issue = Issue.find(1)
99 @issue_status = IssueStatus.find(1)
99 @issue_status = IssueStatus.find(1)
100 @issue_status.update_attribute(:default_done_ratio, 50)
100 @issue_status.update_attribute(:default_done_ratio, 50)
101 end
101 end
102
102
103 context "with Setting.issue_done_ratio using the issue_field" do
103 context "with Setting.issue_done_ratio using the issue_field" do
104 setup do
104 setup do
105 Setting.issue_done_ratio = 'issue_field'
105 Setting.issue_done_ratio = 'issue_field'
106 end
106 end
107
107
108 should "change nothing" do
108 should "change nothing" do
109 IssueStatus.update_issue_done_ratios
109 IssueStatus.update_issue_done_ratios
110
110
111 assert_equal 0, Issue.count(:conditions => {:done_ratio => 50})
111 assert_equal 0, Issue.count(:conditions => {:done_ratio => 50})
112 end
112 end
113 end
113 end
114
114
115 context "with Setting.issue_done_ratio using the issue_status" do
115 context "with Setting.issue_done_ratio using the issue_status" do
116 setup do
116 setup do
117 Setting.issue_done_ratio = 'issue_status'
117 Setting.issue_done_ratio = 'issue_status'
118 end
118 end
119
119
120 should "update all of the issue's done_ratios to match their Issue Status" do
120 should "update all of the issue's done_ratios to match their Issue Status" do
121 IssueStatus.update_issue_done_ratios
121 IssueStatus.update_issue_done_ratios
122
122
123 issues = Issue.find([1,3,4,5,6,7,9,10])
123 issues = Issue.find([1,3,4,5,6,7,9,10])
124 issues.each do |issue|
124 issues.each do |issue|
125 assert_equal @issue_status, issue.status
125 assert_equal @issue_status, issue.status
126 assert_equal 50, issue.read_attribute(:done_ratio)
126 assert_equal 50, issue.read_attribute(:done_ratio)
127 end
127 end
128 end
128 end
129 end
129 end
130 end
130 end
131 end
131 end
General Comments 0
You need to be logged in to leave comments. Login now