From 365e04b5f9ea2147ce3c005cb4c786e0e8ef3f23 Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 26 Dec 2018 17:30:07 -0600 Subject: [PATCH] implications: fix validation of circular implications. Fix bug where circular chains like `a -> b && b -> c && c -> a` were allowed. --- app/models/tag_implication.rb | 2 +- test/unit/tag_implication_test.rb | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index 9d9a851ad..b10a4c65a 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -77,7 +77,7 @@ class TagImplication < TagRelationship module ValidationMethods def absence_of_circular_relation # We don't want a -> b && b -> a chains - if TagImplication.active.exists?(["antecedent_name = ? and consequent_name = ?", consequent_name, antecedent_name]) + if descendants.include?(antecedent_name) errors[:base] << "Tag implication can not create a circular relation with another tag implication" end end diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index d1dd50880..d5352fcff 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -80,10 +80,13 @@ class TagImplicationTest < ActiveSupport::TestCase should "not validate when a circular relation is created" do ti1 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb") - ti2 = FactoryBot.build(:tag_implication, :antecedent_name => "bbb", :consequent_name => "aaa") - ti2.save - assert(ti2.errors.any?, "Tag implication should not have validated.") - assert_equal("Tag implication can not create a circular relation with another tag implication", ti2.errors.full_messages.join("")) + ti2 = FactoryBot.create(:tag_implication, :antecedent_name => "bbb", :consequent_name => "ccc") + ti3 = FactoryBot.build(:tag_implication, :antecedent_name => "bbb", :consequent_name => "aaa") + + assert(ti1.valid?) + assert(ti2.valid?) + refute(ti3.valid?) + assert_equal("Tag implication can not create a circular relation with another tag implication", ti3.errors.full_messages.join("")) end should "not validate when a transitive relation is created" do