View source
<?php
namespace Drupal\Tests\entity_embed\Functional;
use Drupal\Core\Form\FormState;
class EntityReferenceFieldFormatterTest extends EntityEmbedTestBase {
protected $menu;
protected function setUp() {
parent::setUp();
$this->menu = \Drupal::entityTypeManager()
->getStorage('menu')
->create([
'id' => 'menu_name',
'label' => 'Label',
'description' => 'Description text',
]);
$this->menu
->save();
}
public function testEntityReferenceFieldFormatter() {
$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',
]);
$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));
$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']);
$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']);
$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.");
}
public function testFilterEntityReferencePlugins() {
$content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node
->uuid() . '" data-entity-embed-display="entity_reference:entity_reference_label" data-entity-embed-display-settings=\'{"link":1}\'>This placeholder should not be rendered.</drupal-entity>';
$settings = [];
$settings['type'] = 'page';
$settings['title'] = 'Test entity_reference:entity_reference_label Entity Embed Display plugin';
$settings['body'] = [
[
'value' => $content,
'format' => 'custom_format',
],
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseContains($this->node->title->value);
$this
->assertSession()
->responseNotContains($this->node->body->value);
$this
->assertSession()
->responseNotContains('This placeholder should not be rendered.');
$this
->assertSession()
->linkByHrefExists('node/' . $this->node
->id(), 0, 'Link to the embedded node exists.');
$content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node
->uuid() . '" data-entity-embed-display="entity_reference:entity_reference_entity_id">This placeholder should not be rendered.</drupal-entity>';
$settings = [];
$settings['type'] = 'page';
$settings['title'] = 'Test entity_reference:entity_reference_entity_id Entity Embed Display plugin';
$settings['body'] = [
[
'value' => $content,
'format' => 'custom_format',
],
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseContains($this->node
->id());
$this
->assertSession()
->responseNotContains($this->node->title->value);
$this
->assertSession()
->responseNotContains($this->node->body->value);
$this
->assertSession()
->responseNotContains('This placeholder should not be rendered.');
$this
->assertSession()
->linkByHrefNotExists('node/' . $this->node
->id(), 'Link to the embedded node does not exists.');
$content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node
->uuid() . '" data-entity-embed-display="entity_reference:entity_reference_entity_view" data-entity-embed-display-settings=\'{"view_mode":"teaser"}\'>This placeholder should not be rendered.</drupal-entity>';
$settings = [];
$settings['type'] = 'page';
$settings['title'] = 'Test entity_reference:entity_reference_label Entity Embed Display plugin';
$settings['body'] = [
[
'value' => $content,
'format' => 'custom_format',
],
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseContains($this->node->body->value, 'Body of embedded node does not exists in page.');
$this
->assertSession()
->responseNotContains('This placeholder should not be rendered.');
}
}