You are here

public function EntityEmbedDisplayManagerTest::testGetDefinitionsForContexts in Entity Embed 8

@covers ::getDefinitionsForContexts

File

tests/src/Functional/EntityEmbedDisplayManagerTest.php, line 119

Class

EntityEmbedDisplayManagerTest
@coversDefaultClass \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedDisplayManager @group entity_embed

Namespace

Drupal\Tests\entity_embed\Functional

Code

public function testGetDefinitionsForContexts() {
  $options = $this->entityEmbedDisplayManager
    ->getDefinitionOptionsForContext([
    'entity' => $this->image,
    'entity_type' => $this->image
      ->getEntityTypeId(),
    'embed_button' => $this->imageButton,
  ]);
  $expected = [
    'image:image' => 'Image',
  ];
  $this
    ->assertEquals($expected, $options);
  $options = $this->entityEmbedDisplayManager
    ->getDefinitionOptionsForContext([
    'entity' => $this->image,
    'entity_type' => $this->image
      ->getEntityTypeId(),
  ]);

  // All available plugins for the entity type.
  $expected = [
    'image:image' => 'Image',
    'entity_reference:entity_reference_entity_id' => 'Entity ID',
    'file:file_default' => 'Generic file',
    'entity_reference:entity_reference_label' => 'Label',
    'file:file_table' => 'Table of files',
    'file:file_url_plain' => 'URL to file',
    'image:image_url' => 'URL to image',
  ];
  $this
    ->assertEquals($expected, $options);

  // Test that output is the same as ::getDefinitionOptionsForEntity().
  $options = $this->entityEmbedDisplayManager
    ->getDefinitionOptionsForEntity($this->image);
  $this
    ->assertEquals($expected, $options);
  $options = $this->entityEmbedDisplayManager
    ->getDefinitionOptionsForContext([
    'entity' => $this->invalidImage,
    'entity_type' => $this->invalidImage
      ->getEntityTypeId(),
    'embed_button' => $this->imageButton,
  ]);

  // Since the image is invalid, the `image:image` display isn't returned.
  $this
    ->assertEmpty($options);
  $options = $this->entityEmbedDisplayManager
    ->getDefinitionOptionsForContext([
    'entity' => $this->invalidImage,
    'entity_type' => $this->invalidImage
      ->getEntityTypeId(),
  ]);

  // Since the image is invalid, the image display plugins aren't returned.
  $expected = [
    'entity_reference:entity_reference_entity_id' => 'Entity ID',
    'file:file_default' => 'Generic file',
    'entity_reference:entity_reference_label' => 'Label',
    'file:file_table' => 'Table of files',
    'file:file_url_plain' => 'URL to file',
  ];
  $this
    ->assertEquals($expected, $options);

  // Test that output is the same as ::getDefinitionOptionsForEntity().
  $options = $this->entityEmbedDisplayManager
    ->getDefinitionOptionsForEntity($this->invalidImage);
  $this
    ->assertEquals($expected, $options);
}