You are here

public function SettingsContextHandlerTest::testContextsFromThirdPartySettings in Core Context 8

Tests extracting contexts from a config entity's third-party settings.

File

tests/src/Kernel/SettingsContextHandlerTest.php, line 31

Class

SettingsContextHandlerTest
Tests handling of contexts stored in config entity third-party settings.

Namespace

Drupal\Tests\core_context\Kernel

Code

public function testContextsFromThirdPartySettings() {

  /** @var \Drupal\core_context\EntityContextHandlerInterface $handler */
  $handler = $this->container
    ->get('entity_type.manager')
    ->getHandler('entity_test_bundle', 'context');
  $entity = EntityTestBundle::create([
    'id' => 'test',
    'label' => 'Test',
  ]);
  $entity
    ->save();
  $contexts = $handler
    ->getContexts($entity);
  $this
    ->assertIsArray($contexts);
  $this
    ->assertEmpty($contexts);
  $entity
    ->setThirdPartySetting('core_context', 'contexts', [
    'password' => [
      'type' => 'string',
      'label' => 'Password',
      'description' => 'The password to enter Moria.',
      'value' => 'Mellon',
    ],
  ])
    ->save();
  $contexts = $handler
    ->getContexts($entity);
  $this
    ->assertIsArray($contexts);
  $this
    ->assertCount(1, $contexts);
  $this
    ->assertInstanceOf(Context::class, $contexts['password']);

  /** @var \Drupal\Core\Plugin\Context\Context $context */
  $context = $contexts['password'];
  $context_definition = $context
    ->getContextDefinition();
  $this
    ->assertSame('string', $context_definition
    ->getDataType());
  $this
    ->assertSame('Password', $context_definition
    ->getLabel());
  $this
    ->assertSame('The password to enter Moria.', $context_definition
    ->getDescription());
  $this
    ->assertSame('Mellon', $context
    ->getContextValue());

  // Ensure that the entity's cache metadata has been applied to the context.
  $this
    ->assertSame($context
    ->getCacheMaxAge(), $entity
    ->getCacheMaxAge());
  $this
    ->assertSame($context
    ->getCacheTags(), $entity
    ->getCacheTags());
  $this
    ->assertSame($context
    ->getCacheContexts(), $entity
    ->getCacheContexts());
}