You are here

public function DataFetcherTest::testBubbleableMetadata in Typed Data API enhancements 8

@covers ::fetchDataByPropertyPath

File

tests/src/Kernel/DataFetcherTest.php, line 257

Class

DataFetcherTest
Class DataFetcherTest.

Namespace

Drupal\Tests\typed_data\Kernel

Code

public function testBubbleableMetadata() {
  $this->node->field_integer
    ->setValue([]);

  // Save the node, so that it gets an ID and it has a cache tag.
  $this->node
    ->save();

  // Also add a user for testing cache tags of references.
  $user = $this->entityTypeManager
    ->getStorage('user')
    ->create([
    'name' => 'test',
    'type' => 'user',
  ]);
  $user
    ->save();
  $this->node->uid->entity = $user;
  $bubbleable_metadata = new BubbleableMetadata();
  $this->dataFetcher
    ->fetchDataByPropertyPath($this->node
    ->getTypedData(), 'title.value', $bubbleable_metadata)
    ->getValue();
  $expected = [
    'node:' . $this->node
      ->id(),
  ];
  $this
    ->assertEquals($expected, $bubbleable_metadata
    ->getCacheTags());

  // Test cache tags of references are added correctly.
  $this->dataFetcher
    ->fetchDataByPropertyPath($this->node
    ->getTypedData(), 'uid.entity.name', $bubbleable_metadata)
    ->getValue();
  $expected = [
    'node:' . $this->node
      ->id(),
    'user:' . $user
      ->id(),
  ];
  $this
    ->assertEquals($expected, $bubbleable_metadata
    ->getCacheTags());
}