You are here

public function EntityReferenceFieldFormatterTest::testEntityReferenceFieldFormatter in Entity Embed 8

Tests entity reference field formatters.

File

tests/src/Functional/EntityReferenceFieldFormatterTest.php, line 41

Class

EntityReferenceFieldFormatterTest
Tests the entity reference field formatters provided by entity_embed.

Namespace

Drupal\Tests\entity_embed\Functional

Code

public function testEntityReferenceFieldFormatter() {

  // Ensure that entity reference field formatters are available as plugins.
  $this
    ->assertAvailableDisplayPlugins($this->node, [
    'entity_reference:entity_reference_label',
    'entity_reference:entity_reference_entity_id',
    'view_mode:node.full',
    'view_mode:node.rss',
    'view_mode:node.search_index',
    'view_mode:node.search_result',
    'view_mode:node.teaser',
  ]);
  $this->container
    ->get('config.factory')
    ->getEditable('entity_embed.settings')
    ->set('rendered_entity_mode', TRUE)
    ->save();
  $this->container
    ->get('plugin.manager.entity_embed.display')
    ->clearCachedDefinitions();
  $this
    ->assertAvailableDisplayPlugins($this->node, [
    'entity_reference:entity_reference_label',
    'entity_reference:entity_reference_entity_id',
    'entity_reference:entity_reference_entity_view',
  ]);

  // Ensure that correct form attributes are returned for
  // 'entity_reference:entity_reference_entity_id' plugin.
  $form = [];
  $form_state = new FormState();
  $display = $this->container
    ->get('plugin.manager.entity_embed.display')
    ->createInstance('entity_reference:entity_reference_entity_id', []);
  $display
    ->setContextValue('entity', $this->node);
  $conf_form = $display
    ->buildConfigurationForm($form, $form_state);
  $this
    ->assertSame([], array_keys($conf_form));

  // Ensure that correct form attributes are returned for
  // 'entity_reference:entity_reference_entity_view' plugin.
  $form = [];
  $form_state = new FormState();
  $display = $this->container
    ->get('plugin.manager.entity_embed.display')
    ->createInstance('entity_reference:entity_reference_entity_view', []);
  $display
    ->setContextValue('entity', $this->node);
  $conf_form = $display
    ->buildConfigurationForm($form, $form_state);
  $this
    ->assertSame('select', $conf_form['view_mode']['#type']);
  $this
    ->assertSame('View mode', (string) $conf_form['view_mode']['#title']);

  // Ensure that correct form attributes are returned for
  // 'entity_reference:entity_reference_label' plugin.
  $form = [];
  $form_state = new FormState();
  $display = $this->container
    ->get('plugin.manager.entity_embed.display')
    ->createInstance('entity_reference:entity_reference_label', []);
  $display
    ->setContextValue('entity', $this->node);
  $conf_form = $display
    ->buildConfigurationForm($form, $form_state);
  $this
    ->assertSame([
    'link',
  ], array_keys($conf_form));
  $this
    ->assertSame('checkbox', $conf_form['link']['#type']);
  $this
    ->assertSame('Link label to the referenced entity', (string) $conf_form['link']['#title']);

  // Ensure that 'Rendered Entity' plugin is not available for an entity not
  // having a view controller.
  $plugin_options = $this->container
    ->get('plugin.manager.entity_embed.display')
    ->getDefinitionOptionsForEntity($this->menu);
  $this
    ->assertArrayNotHasKey('entity_reference:entity_reference_entity_view', $plugin_options, "The 'Rendered entity' plugin is not available.");
}