Browse Source

Follow up a81a25f - Fixes #4948 - Trigger Condition "contains not" doesn't...

Mantas Masalskis 1 year ago
parent
commit
b6f796fd04
2 changed files with 99 additions and 1 deletions
  1. 1 1
      lib/selector/sql.rb
  2. 98 0
      spec/lib/selector/sql_spec.rb

+ 1 - 1
lib/selector/sql.rb

@@ -466,7 +466,7 @@ class Selector::Sql < Selector::Base
     elsif block_condition[:operator] == 'contains not'
       # NOT LIKE is always false on NULL values
       # https://github.com/zammad/zammad/issues/4948
-      query << "#{attribute} NOT #{like} (?) OR #{attribute} IS NULL"
+      query << "(#{attribute} NOT #{like} (?) OR #{attribute} IS NULL)"
       bind_params.push "%#{SqlHelper.quote_like(block_condition[:value])}%"
     elsif block_condition[:operator] == 'matches regex'
       query << sql_helper.regex_match(attribute, negated: false)

+ 98 - 0
spec/lib/selector/sql_spec.rb

@@ -713,6 +713,104 @@ RSpec.describe Selector::Sql do
 
     end
 
+    describe 'complex conditions' do
+      context "when 'contains not' operator is after negative operator" do
+        let(:condition) do
+          { operator: 'AND', conditions: [
+            {
+              name:     'ticket.title',
+              operator: 'is not',
+              value:    'title',
+            }, {
+              name:     'ticket.note',
+              operator: 'contains not',
+              value:    'some',
+            },
+          ] }
+        end
+
+        let(:additional_ticket_attributes) { { title: 'title' } }
+
+        before do
+          ticket
+        end
+
+        include_examples 'does not find the ticket'
+      end
+
+      context "when 'contains not' operator is before negative operator" do
+        let(:condition) do
+          { operator: 'AND', conditions: [
+            {
+              name:     'ticket.note',
+              operator: 'contains not',
+              value:    'some',
+            }, {
+              name:     'ticket.title',
+              operator: 'is not',
+              value:    'title',
+            }
+          ] }
+        end
+
+        let(:additional_ticket_attributes) { { title: 'title' } }
+
+        before do
+          ticket
+        end
+
+        include_examples 'does not find the ticket'
+      end
+
+      context "when 'contains not' operator on a related table is after negative operator" do
+        let(:condition) do
+          { operator: 'AND', conditions: [
+            {
+              name:     'ticket.title',
+              operator: 'is not',
+              value:    'title',
+            }, {
+              name:     'customer.email',
+              operator: 'contains not',
+              value:    'some',
+            },
+          ] }
+        end
+
+        let(:additional_ticket_attributes) { { title: 'title' } }
+
+        before do
+          ticket
+        end
+
+        include_examples 'does not find the ticket'
+      end
+
+      context "when 'contains not' operator on a related table is before negative operator" do
+        let(:condition) do
+          { operator: 'AND', conditions: [
+            {
+              name:     'customer.email',
+              operator: 'contains not',
+              value:    'some',
+            }, {
+              name:     'ticket.title',
+              operator: 'is not',
+              value:    'title',
+            }
+          ] }
+        end
+
+        let(:additional_ticket_attributes) { { title: 'title' } }
+
+        before do
+          ticket
+        end
+
+        include_examples 'does not find the ticket'
+      end
+    end
+
     describe 'external data source field', db_adapter: :postgresql, db_strategy: :reset do
       let(:external_data_source_attribute) do
         create(:object_manager_attribute_autocompletion_ajax_external_data_source,