function EntityQueryTest::testCaseSensitivity

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testCaseSensitivity()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testCaseSensitivity()
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testCaseSensitivity()

Tests case sensitive and in-sensitive query conditions.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php, line 815

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testCaseSensitivity() : void {
    $bundle = $this->randomMachineName();
    entity_test_create_bundle($bundle, entity_type: 'entity_test_mulrev');
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'field_ci',
        'entity_type' => 'entity_test_mulrev',
        'type' => 'string',
        'cardinality' => 1,
        'translatable' => FALSE,
        'settings' => [
            'case_sensitive' => FALSE,
        ],
    ]);
    $field_storage->save();
    FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => $bundle,
    ])->save();
    $field_storage = FieldStorageConfig::create([
        'field_name' => 'field_cs',
        'entity_type' => 'entity_test_mulrev',
        'type' => 'string',
        'cardinality' => 1,
        'translatable' => FALSE,
        'settings' => [
            'case_sensitive' => TRUE,
        ],
    ]);
    $field_storage->save();
    FieldConfig::create([
        'field_storage' => $field_storage,
        'bundle' => $bundle,
    ])->save();
    $fixtures = [];
    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[] = [
            'original' => $string,
            'uppercase' => mb_strtoupper($string),
            'lowercase' => mb_strtolower($string),
        ];
    }
    EntityTestMulRev::create([
        '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 = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'])
        ->execute();
    $this->assertCount(1, $result, 'Case insensitive, lowercase');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'])
        ->execute();
    $this->assertCount(1, $result, 'Case insensitive, uppercase');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'])
        ->execute();
    $this->assertCount(1, $result, 'Case insensitive, mixed.');
    // Check the case sensitive field, = operator.
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'])
        ->execute();
    $this->assertCount(0, $result, 'Case sensitive, lowercase.');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'])
        ->execute();
    $this->assertCount(0, $result, 'Case sensitive, uppercase.');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'])
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, exact match.');
    // Check the case insensitive field, IN operator.
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', [
        $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'],
    ], 'IN')
        ->execute();
    $this->assertCount(1, $result, 'Case insensitive, lowercase');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', [
        $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'],
    ], 'IN')
        ->execute();
    $this->assertCount(1, $result, 'Case insensitive, uppercase');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', [
        $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'],
    ], 'IN')
        ->execute();
    $this->assertCount(1, $result, 'Case insensitive, mixed');
    // Check the case sensitive field, IN operator.
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', [
        $fixtures[0]['lowercase'] . $fixtures[1]['lowercase'],
    ], 'IN')
        ->execute();
    $this->assertCount(0, $result, 'Case sensitive, lowercase');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', [
        $fixtures[0]['uppercase'] . $fixtures[1]['uppercase'],
    ], 'IN')
        ->execute();
    $this->assertCount(0, $result, 'Case sensitive, uppercase');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', [
        $fixtures[0]['uppercase'] . $fixtures[1]['lowercase'],
    ], 'IN')
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, mixed');
    // Check the case insensitive field, STARTS_WITH operator.
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', $fixtures[0]['lowercase'], 'STARTS_WITH')
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, lowercase.');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', $fixtures[0]['uppercase'], 'STARTS_WITH')
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, exact match.');
    // Check the case sensitive field, STARTS_WITH operator.
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', $fixtures[0]['lowercase'], 'STARTS_WITH')
        ->execute();
    $this->assertCount(0, $result, 'Case sensitive, lowercase.');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', $fixtures[0]['uppercase'], 'STARTS_WITH')
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, exact match.');
    // Check the case insensitive field, ENDS_WITH operator.
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', $fixtures[1]['lowercase'], 'ENDS_WITH')
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, lowercase.');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', $fixtures[1]['uppercase'], 'ENDS_WITH')
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, exact match.');
    // Check the case sensitive field, ENDS_WITH operator.
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', $fixtures[1]['lowercase'], 'ENDS_WITH')
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, lowercase.');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', $fixtures[1]['uppercase'], 'ENDS_WITH')
        ->execute();
    $this->assertCount(0, $result, 'Case sensitive, exact match.');
    // Check the case insensitive field, CONTAINS operator, use the inner 8
    // characters of the uppercase and lowercase strings.
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS')
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, lowercase.');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_ci', mb_strtolower(mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS')
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, exact match.');
    // Check the case sensitive field, CONTAINS operator.
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8), 'CONTAINS')
        ->execute();
    $this->assertCount(1, $result, 'Case sensitive, lowercase.');
    $result = $this->storage
        ->getQuery()
        ->accessCheck(FALSE)
        ->condition('field_cs', mb_strtolower(mb_substr($fixtures[0]['uppercase'] . $fixtures[1]['lowercase'], 4, 8)), 'CONTAINS')
        ->execute();
    $this->assertCount(0, $result, 'Case sensitive, exact match.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.