You are here

public function ConfigEntityQueryTest::testLookupKeys in Zircon Profile 8

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

Tests lookup keys are added to the key value store.

File

core/modules/system/src/Tests/Entity/ConfigEntityQueryTest.php, line 600
Contains \Drupal\system\Tests\Entity\ConfigEntityQueryTest.

Class

ConfigEntityQueryTest
Tests Config Entity Query functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testLookupKeys() {
  \Drupal::service('state')
    ->set('config_test.lookup_keys', TRUE);
  \Drupal::entityManager()
    ->clearCachedDefinitions();
  $key_value = $this->container
    ->get('keyvalue')
    ->get(QueryFactory::CONFIG_LOOKUP_PREFIX . 'config_test');
  $test_entities = [];
  $entity = entity_create('config_test', array(
    'label' => $this
      ->randomMachineName(),
    'id' => '1',
    'style' => 'test',
  ));
  $test_entities[$entity
    ->getConfigDependencyName()] = $entity;
  $entity
    ->enforceIsNew();
  $entity
    ->save();
  $expected[] = $entity
    ->getConfigDependencyName();
  $this
    ->assertEqual($expected, $key_value
    ->get('style:test'));
  $entity = entity_create('config_test', array(
    'label' => $this
      ->randomMachineName(),
    'id' => '2',
    'style' => 'test',
  ));
  $test_entities[$entity
    ->getConfigDependencyName()] = $entity;
  $entity
    ->enforceIsNew();
  $entity
    ->save();
  $expected[] = $entity
    ->getConfigDependencyName();
  $this
    ->assertEqual($expected, $key_value
    ->get('style:test'));
  $entity = entity_create('config_test', array(
    'label' => $this
      ->randomMachineName(),
    '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
    ->assertEqual($expected, $key_value
    ->get('style:test'));
  $this
    ->assertEqual([
    $entity
      ->getConfigDependencyName(),
  ], $key_value
    ->get('style:blah'));

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

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