Project

General

Profile

Patch #27663 » 0001-Disallow-creating-inverse-relates-issue-relations.patch

Gregor Schmidt, 2017-11-28 10:22

View differences:

app/models/issue_relation.rb
207 207

  
208 208
  # Reverses the relation if needed so that it gets stored in the proper way
209 209
  # Should not be reversed before validation so that it can be displayed back
210
  # as entered on new relation form
210
  # as entered on new relation form.
211
  #
212
  # Orders relates relations by ID, so that uniqueness index in DB is triggered
213
  # on concurrent access.
211 214
  def reverse_if_needed
212 215
    if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
213 216
      issue_tmp = issue_to
214 217
      self.issue_to = issue_from
215 218
      self.issue_from = issue_tmp
216 219
      self.relation_type = TYPES[relation_type][:reverse]
220

  
221
    elsif relation_type == TYPE_RELATES && issue_from_id > issue_to_id
222
      self.issue_to, self.issue_from = issue_from, issue_to
217 223
    end
218 224
  end
219 225

  
......
228 234
      issue_from.blocks? issue_to
229 235
    when 'blocks'
230 236
      issue_to.blocks? issue_from
237
    when 'relates'
238
      self.class.where(issue_from_id: issue_to, issue_to_id: issue_from).present?
231 239
    else
232 240
      false
233 241
    end
test/unit/issue_relation_test.rb
65 65
    assert_equal from, relation.issue_to
66 66
  end
67 67

  
68
  def test_cannot_create_inverse_relates_relations
69
    from = Issue.find(1)
70
    to = Issue.find(2)
71

  
72
    relation1 = IssueRelation.new :issue_from => from, :issue_to => to,
73
                                  :relation_type => IssueRelation::TYPE_RELATES
74
    assert relation1.save
75

  
76
    relation2 = IssueRelation.new :issue_from => to, :issue_to => from,
77
                                  :relation_type => IssueRelation::TYPE_RELATES
78
    assert !relation2.save
79
    assert_not_equal [], relation2.errors[:base]
80
  end
81

  
68 82
  def test_follows_relation_should_not_be_reversed_if_validation_fails
69 83
    from = Issue.find(1)
70 84
    to = Issue.find(2)
(1-1/2)