You are here

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

@covers ::scan

File

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

Class

PlaceholderResolverTest
Tests the placeholder resolver.

Namespace

Drupal\Tests\typed_data\Kernel

Code

public function testScanningForPlaceholders() {
  $text = 'token {{example.foo}} and {{example.foo.bar}} just as {{example.foo|default(bar)}} and {{ example.whitespace }}';
  $placeholders = $this->placeholderResolver
    ->scan($text);
  $this
    ->assertEquals([
    'example' => [
      'foo' => '{{example.foo}}',
      'foo.bar' => '{{example.foo.bar}}',
      'foo|default(bar)' => '{{example.foo|default(bar)}}',
      'whitespace' => '{{ example.whitespace }}',
    ],
  ], $placeholders);

  // Test a simple placeholder with filters only.
  $text = "text {{ date | filter }} text";
  $placeholders = $this->placeholderResolver
    ->scan($text);
  $this
    ->assertEquals([
    'date' => [
      '| filter' => '{{ date | filter }}',
    ],
  ], $placeholders);

  // Test a simple placeholder with and without a filter.
  $text = "text {{ date | filter }} text {{ date }}";
  $placeholders = $this->placeholderResolver
    ->scan($text);
  $this
    ->assertEquals([
    'date' => [
      '| filter' => '{{ date | filter }}',
      '' => '{{ date }}',
    ],
  ], $placeholders);

  // Test a compound placeholder with and without a filter.
  $text = "text {{ node.title.value | lower }} text {{ node.title.value }}";
  $placeholders = $this->placeholderResolver
    ->scan($text);
  $this
    ->assertEquals([
    'node' => [
      'title.value | lower' => '{{ node.title.value | lower }}',
      'title.value' => '{{ node.title.value }}',
    ],
  ], $placeholders);

  // Test a global context variable placeholder.
  $text = "global context variable token {{ @service_id:context.property }}";
  $placeholders = $this->placeholderResolver
    ->scan($text);
  $this
    ->assertEquals([
    '@service_id:context' => [
      'property' => '{{ @service_id:context.property }}',
    ],
  ], $placeholders);

  // Test a global context variable placeholder with a period in the
  // service id.
  $text = "global context variable token {{ @service.id:context.property }}";
  $placeholders = $this->placeholderResolver
    ->scan($text);
  $this
    ->assertEquals([
    '@service.id:context' => [
      'property' => '{{ @service.id:context.property }}',
    ],
  ], $placeholders);
}