From b6f796fd04ebb1c123473f2e153d610cf8af14e0 Mon Sep 17 00:00:00 2001 From: Mantas Masalskis Date: Fri, 29 Dec 2023 11:26:03 +0100 Subject: [PATCH] Follow up a81a25f - Fixes #4948 - Trigger Condition "contains not" doesn't... --- lib/selector/sql.rb | 2 +- spec/lib/selector/sql_spec.rb | 98 +++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/lib/selector/sql.rb b/lib/selector/sql.rb index 851f43f9799a..fa7fff1295ce 100644 --- a/lib/selector/sql.rb +++ b/lib/selector/sql.rb @@ -466,7 +466,7 @@ def condition_sql(block_condition) 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) diff --git a/spec/lib/selector/sql_spec.rb b/spec/lib/selector/sql_spec.rb index 52b726b15646..36e3b4306005 100644 --- a/spec/lib/selector/sql_spec.rb +++ b/spec/lib/selector/sql_spec.rb @@ -713,6 +713,104 @@ def get_condition(operator, range) 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,