You are here

public function EntityQueryTest::testCaseSensitivity in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityQueryTest.php \Drupal\system\Tests\Entity\EntityQueryTest::testCaseSensitivity()

Test case sensitive and in-sensitive query conditions.

File

core/modules/system/src/Tests/Entity/EntityQueryTest.php, line 580
Contains \Drupal\system\Tests\Entity\EntityQueryTest.

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testCaseSensitivity() {
  $bundle = $this
    ->randomMachineName();
  $field_storage = FieldStorageConfig::create(array(
    'field_name' => 'field_ci',
    'entity_type' => 'entity_test_mulrev',
    'type' => 'string',
    'cardinality' => 1,
    'translatable' => FALSE,
    'settings' => array(
      'case_sensitive' => FALSE,
    ),
  ));
  $field_storage
    ->save();
  FieldConfig::create(array(
    'field_storage' => $field_storage,
    'bundle' => $bundle,
  ))
    ->save();
  $field_storage = FieldStorageConfig::create(array(
    'field_name' => 'field_cs',
    'entity_type' => 'entity_test_mulrev',
    'type' => 'string',
    'cardinality' => 1,
    'translatable' => FALSE,
    'settings' => array(
      'case_sensitive' => TRUE,
    ),
  ));
  $field_storage
    ->save();
  FieldConfig::create(array(
    'field_storage' => $field_storage,
    'bundle' => $bundle,
  ))
    ->save();
  $fixtures = array();
  for ($i = 0; $i < 2; $i++) {

    // If the last 4 of the string are all numbers, then there is no
    // difference between upper and lowercase and the case sensitive CONTAINS
    // test will fail. Ensure that can not happen by appending a non-numeric
    // character. See https://www.drupal.org/node/2397297.
    $string = $this
      ->randomMachineName(7) . 'a';
    $fixtures[] = array(
      'original' => $string,
      'uppercase' => Unicode::strtoupper($string),
      'lowercase' => Unicode::strtolower($string),
    );
  }
  EntityTestMulRev::create(array(
    'type' => $bundle,
    'name' => $this
      ->randomMachineName(),
    'langcode' => 'en',
    'field_ci' => $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'],
    'field_cs' => $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'],
  ))
    ->save();

  // Check the case insensitive field, = operator.
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'])
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case insensitive, lowercase');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'])
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case insensitive, uppercase');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'])
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case insensitive, mixed.');

  // Check the case sensitive field, = operator.
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'])
    ->execute();
  $this
    ->assertIdentical(count($result), 0, 'Case sensitive, lowercase.');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'])
    ->execute();
  $this
    ->assertIdentical(count($result), 0, 'Case sensitive, uppercase.');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'])
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, exact match.');

  // Check the case insensitive field, IN operator.
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', array(
    $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'],
  ), 'IN')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case insensitive, lowercase');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', array(
    $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'],
  ), 'IN')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case insensitive, uppercase');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', array(
    $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'],
  ), 'IN')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case insensitive, mixed');

  // Check the case sensitive field, IN operator.
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', array(
    $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'],
  ), 'IN')
    ->execute();
  $this
    ->assertIdentical(count($result), 0, 'Case sensitive, lowercase');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', array(
    $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'],
  ), 'IN')
    ->execute();
  $this
    ->assertIdentical(count($result), 0, 'Case sensitive, uppercase');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', array(
    $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'],
  ), 'IN')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, mixed');

  // Check the case insensitive field, STARTS_WITH operator.
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', $fixtures[0]['lowercase'], 'STARTS_WITH')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, lowercase.');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', $fixtures[0]['uppercase'], 'STARTS_WITH')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, exact match.');

  // Check the case sensitive field, STARTS_WITH operator.
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', $fixtures[0]['lowercase'], 'STARTS_WITH')
    ->execute();
  $this
    ->assertIdentical(count($result), 0, 'Case sensitive, lowercase.');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', $fixtures[0]['uppercase'], 'STARTS_WITH')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, exact match.');

  // Check the case insensitive field, ENDS_WITH operator.
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', $fixtures[1]['lowercase'], 'ENDS_WITH')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, lowercase.');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', $fixtures[1]['uppercase'], 'ENDS_WITH')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, exact match.');

  // Check the case sensitive field, ENDS_WITH operator.
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', $fixtures[1]['lowercase'], 'ENDS_WITH')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, lowercase.');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', $fixtures[1]['uppercase'], 'ENDS_WITH')
    ->execute();
  $this
    ->assertIdentical(count($result), 0, 'Case sensitive, exact match.');

  // Check the case insensitive field, CONTAINS operator, use the inner 8
  // characters of the uppercase and lowercase strings.
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, lowercase.');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_ci', Unicode::strtolower(Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, exact match.');

  // Check the case sensitive field, CONTAINS operator.
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS')
    ->execute();
  $this
    ->assertIdentical(count($result), 1, 'Case sensitive, lowercase.');
  $result = \Drupal::entityQuery('entity_test_mulrev')
    ->condition('field_cs', Unicode::strtolower(Unicode::substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS')
    ->execute();
  $this
    ->assertIdentical(count($result), 0, 'Case sensitive, exact match.');
}