Commit 93dc7552 authored by catch's avatar catch
Browse files

Issue #3162603 by paulocs, daffie, mxr576, vadim.jin, sandeep_jangra,...

Issue #3162603 by paulocs, daffie, mxr576, vadim.jin, sandeep_jangra, naresh_bavaskar, longwave, amateescu, drunken monkey, larowlan: EntityStorageBase::loadByProperties() is broken on PostgreSQL when using two or more case insensitive properties

(cherry picked from commit 1cb136a1)
parent 5618ef16
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -18,18 +18,17 @@ public static function translateCondition(&$condition, SelectInterface $sql_quer
      $condition['where'] = 'LOWER(' . $sql_query->escapeField($condition['real_field']) . ') ' . $condition['operator'] . ' (';
      $condition['where_args'] = [];

      $n = 1;
      // Only use the array values in case an associative array is passed as an
      // argument following similar pattern in
      // \Drupal\Core\Database\Connection::expandArguments().
      foreach ($condition['value'] as $value) {
        $condition['where'] .= 'LOWER(:value' . $n . '),';
        $condition['where_args'][':value' . $n] = $value;
        $n++;
      $where_prefix = str_replace('.', '_', $condition['real_field']);
      foreach ($condition['value'] as $key => $value) {
        $where_id = $where_prefix . $key;
        $condition['where'] .= 'LOWER(:' . $where_id . '),';
        $condition['where_args'][':' . $where_id] = $value;
      }
      $condition['where'] = trim($condition['where'], ',');
      $condition['where'] .= ')';
      return;
    }
    parent::translateCondition($condition, $sql_query, $case_sensitive);
  }
+68 −0
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\Tests\node\Functional;

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;

/**
@@ -65,4 +67,70 @@ public function testNodeMultipleLoad() {
    }
  }

  /**
   * Creates four nodes with not case sensitive fields and load them.
   */
  public function testNodeMultipleLoadCaseSensitiveFalse() {
    $field_first_storage = FieldStorageConfig::create([
      'field_name' => 'field_first',
      'entity_type' => 'node',
      'type' => 'string',
      'settings' => [
        'case_sensitive' => FALSE,
      ],
    ]);
    $field_first_storage->save();

    FieldConfig::create([
      'field_storage' => $field_first_storage,
      'bundle' => 'page',
    ])->save();

    $field_second_storage = FieldStorageConfig::create([
      'field_name' => 'field_second',
      'entity_type' => 'node',
      'type' => 'string',
      'settings' => [
        'case_sensitive' => FALSE,
      ],
    ]);
    $field_second_storage->save();

    FieldConfig::create([
      'field_storage' => $field_second_storage,
      'bundle' => 'page',
    ])->save();

    // Test create nodes with values for field_first and field_second.
    $node1 = $this->drupalCreateNode([
      'type' => 'page',
      'field_first' => '1234',
      'field_second' => 'test_value_1',
    ]);
    $node2 = $this->drupalCreateNode([
      'type' => 'page',
      'field_first' => '1234',
      'field_second' => 'test_value_2',
    ]);
    $node3 = $this->drupalCreateNode([
      'type' => 'page',
      'field_first' => '5678',
      'field_second' => 'test_value_1',
    ]);
    $node4 = $this->drupalCreateNode([
      'type' => 'page',
      'field_first' => '5678',
      'field_second' => 'test_value_2',
    ]);

    // Load nodes by two properties (field_first and field_second).
    $nodes = $this->container->get('entity_type.manager')->getStorage('node')
      ->loadByProperties(['field_first' => ['1234', '5678'], 'field_second' => 'test_value_1']);
    $this->assertCount(2, $nodes);
    $this->assertEqual($node1->field_first->value, $nodes[$node1->id()]->field_first->value);
    $this->assertEqual($node1->field_second->value, $nodes[$node1->id()]->field_second->value);
    $this->assertEqual($node3->field_first->value, $nodes[$node3->id()]->field_first->value);
    $this->assertEqual($node3->field_second->value, $nodes[$node3->id()]->field_second->value);
  }

}