From f6b3dd94810f62eb23034913ba624a072ef20b2d Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Thu, 3 Nov 2016 12:01:07 +0000
Subject: [PATCH] Issue #2809555 by jonathanjfshaw, alexpott, amateescu,
 Lendude, catch: ER views autocomplete fail to recognise exact matches typed
 in

---
 .../Plugin/views/display/EntityReference.php  |  9 ++-
 .../Plugin/DisplayEntityReferenceTest.php     | 57 +++++++++++++++++++
 2 files changed, 63 insertions(+), 3 deletions(-)

diff --git a/core/modules/views/src/Plugin/views/display/EntityReference.php b/core/modules/views/src/Plugin/views/display/EntityReference.php
index 22c8c8cf8d19..bf2d46034efb 100644
--- a/core/modules/views/src/Plugin/views/display/EntityReference.php
+++ b/core/modules/views/src/Plugin/views/display/EntityReference.php
@@ -122,9 +122,12 @@ public function query() {
     // Restrict the autocomplete options based on what's been typed already.
     if (isset($options['match'])) {
       $style_options = $this->getOption('style');
-      $value = db_like($options['match']) . '%';
-      if ($options['match_operator'] != 'STARTS_WITH') {
-        $value = '%' . $value;
+      $value = db_like($options['match']);
+      if ($options['match_operator'] !== '=') {
+        $value = $value . '%';
+        if ($options['match_operator'] != 'STARTS_WITH') {
+          $value = '%' . $value;
+        }
       }
 
       // Multiple search fields are OR'd together.
diff --git a/core/modules/views/src/Tests/Plugin/DisplayEntityReferenceTest.php b/core/modules/views/src/Tests/Plugin/DisplayEntityReferenceTest.php
index 67d4d451633d..96d0369263a8 100644
--- a/core/modules/views/src/Tests/Plugin/DisplayEntityReferenceTest.php
+++ b/core/modules/views/src/Tests/Plugin/DisplayEntityReferenceTest.php
@@ -103,6 +103,21 @@ protected function setUp() {
         $this->fieldName => 'text' . $i,
       ])->save();
     }
+    EntityTest::create([
+      'bundle' => 'entity_test',
+      'name' => 'name',
+      $this->fieldName => 'tex',
+    ])->save();
+    EntityTest::create([
+      'bundle' => 'entity_test',
+      'name' => 'name',
+      $this->fieldName => 'TEX',
+    ])->save();
+    EntityTest::create([
+      'bundle' => 'entity_test',
+      'name' => 'name',
+      $this->fieldName => 'sometext',
+    ])->save();
   }
 
   /**
@@ -140,6 +155,48 @@ public function testEntityReferenceDisplay() {
     $this->assertEqual(count($view->result), 2, 'Search returned two rows');
     $view->destroy();
 
+    // Test the 'CONTAINS' match_operator.
+    $view = Views::getView('test_display_entity_reference');
+    $view->setDisplay('entity_reference_1');
+    $options = [
+      'match' => 'tex',
+      'match_operator' => 'CONTAINS',
+      'limit' => 0,
+      'ids' => NULL,
+    ];
+    $view->display_handler->setOption('entity_reference_options', $options);
+    $this->executeView($view);
+    $this->assertEqual(count($view->result), 13, 'Search returned thirteen rows');
+    $view->destroy();
+
+    // Test the 'STARTS_WITH' match_operator.
+    $view = Views::getView('test_display_entity_reference');
+    $view->setDisplay('entity_reference_1');
+    $options = [
+      'match' => 'tex',
+      'match_operator' => 'STARTS_WITH',
+      'limit' => 0,
+      'ids' => NULL,
+    ];
+    $view->display_handler->setOption('entity_reference_options', $options);
+    $this->executeView($view);
+    $this->assertEqual(count($view->result), 12, 'Search returned twelve rows');
+    $view->destroy();
+
+    // Test the '=' match_operator.
+    $view = Views::getView('test_display_entity_reference');
+    $view->setDisplay('entity_reference_1');
+    $options = [
+      'match' => 'tex',
+      'match_operator' => '=',
+      'limit' => 0,
+      'ids' => NULL,
+    ];
+    $view->display_handler->setOption('entity_reference_options', $options);
+    $this->executeView($view);
+    $this->assertEqual(count($view->result), 2, 'Search returned two rows');
+    $view->destroy();
+
     // Add a relationship and a field using that relationship.
     $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/relationship', ['name[entity_test.user_id]' => TRUE], t('Add and configure relationships'));
     $this->drupalPostForm(NULL, [], t('Apply'));
-- 
GitLab