You are here

public function ConfigEntityQueryTest::testLookupKeys in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityQueryTest::testLookupKeys()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityQueryTest::testLookupKeys()

Tests lookup keys are added to the key value store.

File

core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php, line 669

Class

ConfigEntityQueryTest
Tests Config Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testLookupKeys() {
  \Drupal::service('state')
    ->set('config_test.lookup_keys', TRUE);
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();
  $key_value = $this->container
    ->get('keyvalue')
    ->get(QueryFactory::CONFIG_LOOKUP_PREFIX . 'config_test');
  $test_entities = [];
  $storage = \Drupal::entityTypeManager()
    ->getStorage('config_test');
  $entity = $storage
    ->create([
    'label' => 'entity_1',
    'id' => '1',
    'style' => 'test',
  ]);
  $test_entities[$entity
    ->getConfigDependencyName()] = $entity;
  $entity
    ->enforceIsNew();
  $entity
    ->save();
  $expected[] = $entity
    ->getConfigDependencyName();
  $this
    ->assertEquals($expected, $key_value
    ->get('style:test'));
  $entity = $storage
    ->create([
    'label' => 'entity_2',
    'id' => '2',
    'style' => 'test',
  ]);
  $test_entities[$entity
    ->getConfigDependencyName()] = $entity;
  $entity
    ->enforceIsNew();
  $entity
    ->save();
  $expected[] = $entity
    ->getConfigDependencyName();
  $this
    ->assertEquals($expected, $key_value
    ->get('style:test'));
  $entity = $storage
    ->create([
    'label' => 'entity_3',
    'id' => '3',
    'style' => 'blah',
  ]);
  $entity
    ->enforceIsNew();
  $entity
    ->save();

  // Do not add this entity to the list of expected result as it has a
  // different value.
  $this
    ->assertEquals($expected, $key_value
    ->get('style:test'));
  $this
    ->assertEquals([
    $entity
      ->getConfigDependencyName(),
  ], $key_value
    ->get('style:blah'));

  // Ensure that a delete clears a key.
  $entity
    ->delete();
  $this
    ->assertNull($key_value
    ->get('style:blah'));

  // Ensure that delete only clears one key.
  $entity_id = array_pop($expected);
  $test_entities[$entity_id]
    ->delete();
  $this
    ->assertEquals($expected, $key_value
    ->get('style:test'));
  $entity_id = array_pop($expected);
  $test_entities[$entity_id]
    ->delete();
  $this
    ->assertNull($key_value
    ->get('style:test'));
}