You are here

public function PlaceholderResolverTest::testResolvingPlaceholders in Typed Data API enhancements 8

@covers ::resolvePlaceholders

File

tests/src/Kernel/PlaceholderResolverTest.php, line 239

Class

PlaceholderResolverTest
Tests the placeholder resolver.

Namespace

Drupal\Tests\typed_data\Kernel

Code

public function testResolvingPlaceholders() {

  // Test resolving multiple tokens.
  $text = 'test {{node.title}} and {{node.title.value}}';
  $result = $this->placeholderResolver
    ->resolvePlaceholders($text, [
    'node' => $this->node
      ->getTypedData(),
  ]);
  $expected = [
    '{{node.title}}' => 'test',
    '{{node.title.value}}' => 'test',
  ];
  $this
    ->assertEquals($expected, $result);

  // Test resolving multiple tokens, one with a filter.
  $this->node->title->value = 'tEsT';
  $result = $this->placeholderResolver
    ->resolvePlaceHolders("test {{ node.title.value | lower }} and {{ node.title.value }}", [
    'node' => $this->node
      ->getTypedData(),
  ]);
  $expected = [
    '{{ node.title.value | lower }}' => 'test',
    '{{ node.title.value }}' => 'tEsT',
  ];
  $this
    ->assertEquals($expected, $result);

  // Test a placeholder without accessing a property.
  $text = 'test {{string}}';
  $result = $this->placeholderResolver
    ->resolvePlaceholders($text, [
    'string' => $this->typedDataManager
      ->create(DataDefinition::create('string'), 'replacement'),
  ]);
  $expected = [
    '{{string}}' => 'replacement',
  ];
  $this
    ->assertEquals($expected, $result);

  // Test a global context variable placeholder.
  $text = 'test {{ @typed_data_global_context_test.simple_test_context:dragons }}';
  $context = $this->simpleTestContext
    ->getRuntimeContexts([
    'dragons',
  ]);
  $result = $this->placeholderResolver
    ->resolvePlaceholders($text, [
    '@typed_data_global_context_test.simple_test_context:dragons' => $context['dragons']
      ->getContextData(),
  ]);
  $expected = [
    '{{ @typed_data_global_context_test.simple_test_context:dragons }}' => 'Dragons are better than unicorns!',
  ];
  $this
    ->assertEquals($expected, $result);
}