EntityEmbedHooksTest.php in Entity Embed 8
File
tests/src/Functional/EntityEmbedHooksTest.php
View source
<?php
namespace Drupal\Tests\entity_embed\Functional;
class EntityEmbedHooksTest extends EntityEmbedTestBase {
protected $state;
protected function setUp() {
parent::setUp();
$this->state = $this->container
->get('state');
}
public function testDisplayPluginAlterHooks() {
$this->state
->set('entity_embed_test_entity_embed_display_plugins_alter', TRUE);
$plugins = $this->container
->get('plugin.manager.entity_embed.display')
->getDefinitionOptionsForEntity($this->node);
foreach ($plugins as $plugin => $plugin_info) {
$this
->assertTrue(strpos($plugin, 'testing_hook:') === 0, 'Name of the plugin is prefixed by hook_entity_embed_display_plugins_alter()');
}
}
public function testEntityEmbedHooks() {
$this->state
->set('entity_embed_test_entity_embed_alter', TRUE);
$content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node
->uuid() . '" data-entity-embed-display="default" data-entity-embed-display-settings=\'{"view_mode":"teaser"}\'>This placeholder should not be rendered.</drupal-entity>';
$settings = [];
$settings['type'] = 'page';
$settings['title'] = 'Test hook_entity_embed_alter()';
$settings['body'] = [
[
'value' => $content,
'format' => 'custom_format',
],
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseContains($this->node->body->value);
$this
->assertSession()
->responseNotContains('This placeholder should not be rendered.');
$this
->assertSession()
->responseContains('Title set by hook_entity_embed_alter');
$this
->assertSession()
->responseContains('test-class-added-in-alter-hook');
$this
->assertSession()
->responseNotContains($this->node->title->value);
$this->state
->set('entity_embed_test_entity_embed_alter', FALSE);
$this->state
->set('entity_embed_test_entity_embed_context_alter', TRUE);
$content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node
->uuid() . '" data-entity-embed-display="default" data-entity-embed-display-settings=\'{"view_mode":"teaser"}\'>This placeholder should not be rendered.</drupal-entity>';
$settings = [];
$settings['type'] = 'page';
$settings['title'] = 'Test hook_entity_embed_context_alter()';
$settings['body'] = [
[
'value' => $content,
'format' => 'custom_format',
],
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseNotContains('This placeholder should not be rendered.');
$this
->assertSession()
->responseNotContains($this->node->body->value);
$this
->assertSession()
->responseContains('Title set by hook_entity_embed_context_alter');
$this
->assertSession()
->linkByHrefExists('node/' . $this->node
->id(), 0, 'Link to the embedded node exists.');
$this->state
->set('entity_embed_test_entity_embed_context_alter', FALSE);
}
}