Commit 646b1d3c authored by maeda's avatar maeda

Disallow creating inverse relates issue relations (#27663).

Patch by Gregor Schmidt.


git-svn-id: https://svn.redmine.org/redmine/trunk@17054 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent d72be099
...@@ -207,13 +207,19 @@ class IssueRelation < ActiveRecord::Base ...@@ -207,13 +207,19 @@ class IssueRelation < ActiveRecord::Base
# Reverses the relation if needed so that it gets stored in the proper way # Reverses the relation if needed so that it gets stored in the proper way
# Should not be reversed before validation so that it can be displayed back # Should not be reversed before validation so that it can be displayed back
# as entered on new relation form # as entered on new relation form.
#
# Orders relates relations by ID, so that uniqueness index in DB is triggered
# on concurrent access.
def reverse_if_needed def reverse_if_needed
if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
issue_tmp = issue_to issue_tmp = issue_to
self.issue_to = issue_from self.issue_to = issue_from
self.issue_from = issue_tmp self.issue_from = issue_tmp
self.relation_type = TYPES[relation_type][:reverse] self.relation_type = TYPES[relation_type][:reverse]
elsif relation_type == TYPE_RELATES && issue_from_id > issue_to_id
self.issue_to, self.issue_from = issue_from, issue_to
end end
end end
...@@ -228,6 +234,8 @@ class IssueRelation < ActiveRecord::Base ...@@ -228,6 +234,8 @@ class IssueRelation < ActiveRecord::Base
issue_from.blocks? issue_to issue_from.blocks? issue_to
when 'blocks' when 'blocks'
issue_to.blocks? issue_from issue_to.blocks? issue_from
when 'relates'
self.class.where(issue_from_id: issue_to, issue_to_id: issue_from).present?
else else
false false
end end
......
...@@ -65,6 +65,20 @@ class IssueRelationTest < ActiveSupport::TestCase ...@@ -65,6 +65,20 @@ class IssueRelationTest < ActiveSupport::TestCase
assert_equal from, relation.issue_to assert_equal from, relation.issue_to
end end
def test_cannot_create_inverse_relates_relations
from = Issue.find(1)
to = Issue.find(2)
relation1 = IssueRelation.new :issue_from => from, :issue_to => to,
:relation_type => IssueRelation::TYPE_RELATES
assert relation1.save
relation2 = IssueRelation.new :issue_from => to, :issue_to => from,
:relation_type => IssueRelation::TYPE_RELATES
assert !relation2.save
assert_not_equal [], relation2.errors[:base]
end
def test_follows_relation_should_not_be_reversed_if_validation_fails def test_follows_relation_should_not_be_reversed_if_validation_fails
from = Issue.find(1) from = Issue.find(1)
to = Issue.find(2) to = Issue.find(2)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment