@@ -1,105 +1,123 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 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 IssueStatus < ActiveRecord::Base |
|
19 | 19 | before_destroy :check_integrity |
|
20 | 20 | has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id" |
|
21 | 21 | acts_as_list |
|
22 | 22 | |
|
23 | after_update :handle_is_closed_change | |
|
23 | 24 | before_destroy :delete_workflow_rules |
|
24 | 25 | after_save :update_default |
|
25 | 26 | |
|
26 | 27 | validates_presence_of :name |
|
27 | 28 | validates_uniqueness_of :name |
|
28 | 29 | validates_length_of :name, :maximum => 30 |
|
29 | 30 | validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true |
|
30 | 31 | |
|
31 | 32 | scope :sorted, lambda { order("#{table_name}.position ASC") } |
|
32 | 33 | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} |
|
33 | 34 | |
|
34 | 35 | def update_default |
|
35 | 36 | IssueStatus.where(['id <> ?', id]).update_all({:is_default => false}) if self.is_default? |
|
36 | 37 | end |
|
37 | 38 | |
|
38 | 39 | # Returns the default status for new issues |
|
39 | 40 | def self.default |
|
40 | 41 | where(:is_default => true).first |
|
41 | 42 | end |
|
42 | 43 | |
|
43 | 44 | # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+ |
|
44 | 45 | def self.update_issue_done_ratios |
|
45 | 46 | if Issue.use_status_for_done_ratio? |
|
46 | 47 | IssueStatus.where("default_done_ratio >= 0").each do |status| |
|
47 | 48 | Issue.where({:status_id => status.id}).update_all({:done_ratio => status.default_done_ratio}) |
|
48 | 49 | end |
|
49 | 50 | end |
|
50 | 51 | |
|
51 | 52 | return Issue.use_status_for_done_ratio? |
|
52 | 53 | end |
|
53 | 54 | |
|
54 | 55 | # Returns an array of all statuses the given role can switch to |
|
55 | 56 | # Uses association cache when called more than one time |
|
56 | 57 | def new_statuses_allowed_to(roles, tracker, author=false, assignee=false) |
|
57 | 58 | if roles && tracker |
|
58 | 59 | role_ids = roles.collect(&:id) |
|
59 | 60 | transitions = workflows.select do |w| |
|
60 | 61 | role_ids.include?(w.role_id) && |
|
61 | 62 | w.tracker_id == tracker.id && |
|
62 | 63 | ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee)) |
|
63 | 64 | end |
|
64 | 65 | transitions.map(&:new_status).compact.sort |
|
65 | 66 | else |
|
66 | 67 | [] |
|
67 | 68 | end |
|
68 | 69 | end |
|
69 | 70 | |
|
70 | 71 | # Same thing as above but uses a database query |
|
71 | 72 | # More efficient than the previous method if called just once |
|
72 | 73 | def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false) |
|
73 | 74 | if roles.present? && tracker |
|
74 | 75 | conditions = "(author = :false AND assignee = :false)" |
|
75 | 76 | conditions << " OR author = :true" if author |
|
76 | 77 | conditions << " OR assignee = :true" if assignee |
|
77 | 78 | |
|
78 | 79 | workflows. |
|
79 | 80 | includes(:new_status). |
|
80 | 81 | where(["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})", |
|
81 | 82 | {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false} |
|
82 | 83 | ]).all. |
|
83 | 84 | map(&:new_status).compact.sort |
|
84 | 85 | else |
|
85 | 86 | [] |
|
86 | 87 | end |
|
87 | 88 | end |
|
88 | 89 | |
|
89 | 90 | def <=>(status) |
|
90 | 91 | position <=> status.position |
|
91 | 92 | end |
|
92 | 93 | |
|
93 | 94 | def to_s; name end |
|
94 | 95 | |
|
95 | 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 | 115 | def check_integrity |
|
98 | 116 | raise "Can't delete status" if Issue.where(:status_id => id).any? |
|
99 | 117 | end |
|
100 | 118 | |
|
101 | 119 | # Deletes associated workflows |
|
102 | 120 | def delete_workflow_rules |
|
103 | 121 | WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}]) |
|
104 | 122 | end |
|
105 | 123 | end |
@@ -1,123 +1,165 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 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.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 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 | 31 | def test_create |
|
24 | 32 | status = IssueStatus.new :name => "Assigned" |
|
25 | 33 | assert !status.save |
|
26 | 34 | # status name uniqueness |
|
27 | 35 | assert_equal 1, status.errors.count |
|
28 | 36 | |
|
29 | 37 | status.name = "Test Status" |
|
30 | 38 | assert status.save |
|
31 | 39 | assert !status.is_default |
|
32 | 40 | end |
|
33 | 41 | |
|
34 | 42 | def test_destroy |
|
35 | 43 | status = IssueStatus.find(3) |
|
36 | 44 | assert_difference 'IssueStatus.count', -1 do |
|
37 | 45 | assert status.destroy |
|
38 | 46 | end |
|
39 | 47 | assert_nil WorkflowTransition.where(:old_status_id => status.id).first |
|
40 | 48 | assert_nil WorkflowTransition.where(:new_status_id => status.id).first |
|
41 | 49 | end |
|
42 | 50 | |
|
43 | 51 | def test_destroy_status_in_use |
|
44 | 52 | # Status assigned to an Issue |
|
45 | 53 | status = Issue.find(1).status |
|
46 | 54 | assert_raise(RuntimeError, "Can't delete status") { status.destroy } |
|
47 | 55 | end |
|
48 | 56 | |
|
49 | 57 | def test_default |
|
50 | 58 | status = IssueStatus.default |
|
51 | 59 | assert_kind_of IssueStatus, status |
|
52 | 60 | end |
|
53 | 61 | |
|
54 | 62 | def test_change_default |
|
55 | 63 | status = IssueStatus.find(2) |
|
56 | 64 | assert !status.is_default |
|
57 | 65 | status.is_default = true |
|
58 | 66 | assert status.save |
|
59 | 67 | status.reload |
|
60 | 68 | |
|
61 | 69 | assert_equal status, IssueStatus.default |
|
62 | 70 | assert !IssueStatus.find(1).is_default |
|
63 | 71 | end |
|
64 | 72 | |
|
65 | 73 | def test_reorder_should_not_clear_default_status |
|
66 | 74 | status = IssueStatus.default |
|
67 | 75 | status.move_to_bottom |
|
68 | 76 | status.reload |
|
69 | 77 | assert status.is_default? |
|
70 | 78 | end |
|
71 | 79 | |
|
72 | 80 | def test_new_statuses_allowed_to |
|
73 | 81 | WorkflowTransition.delete_all |
|
74 | 82 | |
|
75 | 83 | WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false) |
|
76 | 84 | WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false) |
|
77 | 85 | WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true) |
|
78 | 86 | WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true) |
|
79 | 87 | status = IssueStatus.find(1) |
|
80 | 88 | role = Role.find(1) |
|
81 | 89 | tracker = Tracker.find(1) |
|
82 | 90 | |
|
83 | 91 | assert_equal [2], status.new_statuses_allowed_to([role], tracker, false, false).map(&:id) |
|
84 | 92 | assert_equal [2], status.find_new_statuses_allowed_to([role], tracker, false, false).map(&:id) |
|
85 | 93 | |
|
86 | 94 | assert_equal [2, 3, 5], status.new_statuses_allowed_to([role], tracker, true, false).map(&:id) |
|
87 | 95 | assert_equal [2, 3, 5], status.find_new_statuses_allowed_to([role], tracker, true, false).map(&:id) |
|
88 | 96 | |
|
89 | 97 | assert_equal [2, 4, 5], status.new_statuses_allowed_to([role], tracker, false, true).map(&:id) |
|
90 | 98 | assert_equal [2, 4, 5], status.find_new_statuses_allowed_to([role], tracker, false, true).map(&:id) |
|
91 | 99 | |
|
92 | 100 | assert_equal [2, 3, 4, 5], status.new_statuses_allowed_to([role], tracker, true, true).map(&:id) |
|
93 | 101 | assert_equal [2, 3, 4, 5], status.find_new_statuses_allowed_to([role], tracker, true, true).map(&:id) |
|
94 | 102 | end |
|
95 | 103 | |
|
96 | 104 | def test_update_done_ratios_with_issue_done_ratio_set_to_issue_field_should_change_nothing |
|
97 | 105 | IssueStatus.find(1).update_attribute(:default_done_ratio, 50) |
|
98 | 106 | |
|
99 | 107 | with_settings :issue_done_ratio => 'issue_field' do |
|
100 | 108 | IssueStatus.update_issue_done_ratios |
|
101 | 109 | assert_equal 0, Issue.where(:done_ratio => 50).count |
|
102 | 110 | end |
|
103 | 111 | end |
|
104 | 112 | |
|
105 | 113 | def test_update_done_ratios_with_issue_done_ratio_set_to_issue_status_should_update_issues |
|
106 | 114 | IssueStatus.find(1).update_attribute(:default_done_ratio, 50) |
|
107 | 115 | with_settings :issue_done_ratio => 'issue_status' do |
|
108 | 116 | IssueStatus.update_issue_done_ratios |
|
109 | 117 | issues = Issue.where(:status_id => 1) |
|
110 | 118 | assert_equal [50], issues.map {|issue| issue.read_attribute(:done_ratio)}.uniq |
|
111 | 119 | end |
|
112 | 120 | end |
|
113 | 121 | |
|
114 | 122 | def test_sorted_scope |
|
115 | 123 | assert_equal IssueStatus.all.sort, IssueStatus.sorted.all |
|
116 | 124 | end |
|
117 | 125 | |
|
118 | 126 | def test_named_scope |
|
119 | 127 | status = IssueStatus.named("resolved").first |
|
120 | 128 | assert_not_nil status |
|
121 | 129 | assert_equal "Resolved", status.name |
|
122 | 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 | 165 | end |
General Comments 0
You need to be logged in to leave comments.
Login now