Interim commit.
[yaffs-website] / web / modules / contrib / entity_embed / src / Tests / EntityEmbedHooksTest.php
1 <?php
2
3 namespace Drupal\entity_embed\Tests;
4
5 /**
6  * Tests the hooks provided by entity_embed module.
7  *
8  * @group entity_embed
9  */
10 class EntityEmbedHooksTest extends EntityEmbedTestBase {
11
12   /**
13    * The state service.
14    *
15    * @var \Drupal\Core\State\StateInterface
16    */
17   protected $state;
18
19   /**
20    *
21    */
22   protected function setUp() {
23     parent::setUp();
24     $this->state = $this->container->get('state');
25   }
26
27   /**
28    * Tests hook_entity_embed_display_plugins_alter().
29    */
30   public function testDisplayPluginAlterHooks() {
31     // Enable entity_embed_test.module's
32     // hook_entity_embed_display_plugins_alter() implementation and ensure it is
33     // working as designed.
34     $this->state->set('entity_embed_test_entity_embed_display_plugins_alter', TRUE);
35     $plugins = $this->container->get('plugin.manager.entity_embed.display')
36       ->getDefinitionOptionsForEntity($this->node);
37     // Ensure that name of each plugin is prefixed with 'testing_hook:'.
38     foreach ($plugins as $plugin => $plugin_info) {
39       $this->assertTrue(strpos($plugin, 'testing_hook:') === 0, 'Name of the plugin is prefixed by hook_entity_embed_display_plugins_alter()');
40     }
41   }
42
43   /**
44    * Tests the hooks provided by entity_embed module.
45    *
46    * This method tests all the hooks provided by entity_embed except
47    * hook_entity_embed_display_plugins_alter, which is tested by a separate
48    * method.
49    */
50   public function testEntityEmbedHooks() {
51     // Enable entity_embed_test.module's hook_entity_embed_alter()
52     // implementation and ensure it is working as designed.
53     $this->state->set('entity_embed_test_entity_embed_alter', TRUE);
54     $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>';
55     $settings = array();
56     $settings['type'] = 'page';
57     $settings['title'] = 'Test hook_entity_embed_alter()';
58     $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
59     $node = $this->drupalCreateNode($settings);
60     $this->drupalGet('node/' . $node->id());
61     $this->assertText($this->node->body->value, 'Embedded node exists in page.');
62     $this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.');
63     // Ensure that embedded node's title has been replaced.
64     $this->assertText('Title set by hook_entity_embed_alter', 'Title of the embedded node is replaced by hook_entity_embed_alter()');
65     $this->assertNoText($this->node->title->value, 'Original title of the embedded node is not visible.');
66     $this->state->set('entity_embed_test_entity_embed_alter', FALSE);
67
68     // Enable entity_embed_test.module's hook_entity_embed_context_alter()
69     // implementation and ensure it is working as designed.
70     $this->state->set('entity_embed_test_entity_embed_context_alter', TRUE);
71     $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>';
72     $settings = array();
73     $settings['type'] = 'page';
74     $settings['title'] = 'Test hook_entity_embed_context_alter()';
75     $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
76     $node = $this->drupalCreateNode($settings);
77     $this->drupalGet('node/' . $node->id());
78     $this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.');
79     // To ensure that 'label' plugin is used, verify that the body of the
80     // embedded node is not visible and the title links to the embedded node.
81     $this->assertNoText($this->node->body->value, 'Body of the embedded node does not exists in page.');
82     $this->assertText('Title set by hook_entity_embed_context_alter', 'Title of the embedded node is replaced by hook_entity_embed_context_alter()');
83     $this->assertNoText($this->node->title->value, 'Original title of the embedded node is not visible.');
84     $this->assertLinkByHref('node/' . $this->node->id(), 0, 'Link to the embedded node exists.');
85     $this->state->set('entity_embed_test_entity_embed_context_alter', FALSE);
86   }
87
88 }