1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\entity_embed\Tests; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Form\FormState; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Tests the entity reference field formatters provided by entity_embed. |
9
|
|
|
* |
10
|
|
|
* @group entity_embed |
11
|
|
|
*/ |
12
|
|
|
class EntityReferenceFieldFormatterTest extends EntityEmbedTestBase { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The test 'menu' entity. |
16
|
|
|
* |
17
|
|
|
* @var \Drupal\Core\Menu\MenuInterface |
18
|
|
|
*/ |
19
|
|
|
protected $menu; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
protected function setUp() { |
25
|
|
|
parent::setUp(); |
26
|
|
|
|
27
|
|
|
// Add a new menu entity which does not has a view controller. |
28
|
|
|
$this->menu = entity_create('menu', array( |
29
|
|
|
'id' => 'menu_name', |
30
|
|
|
'label' => 'Label', |
31
|
|
|
'description' => 'Description text', |
32
|
|
|
)); |
33
|
|
|
$this->menu->save(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Tests entity reference field formatters. |
38
|
|
|
*/ |
39
|
|
|
public function testEntityReferenceFieldFormatter() { |
40
|
|
|
// Ensure that entity reference field formatters are available as plugins. |
41
|
|
|
$this->assertAvailableDisplayPlugins($this->node, [ |
42
|
|
|
'entity_reference:entity_reference_label', |
43
|
|
|
'entity_reference:entity_reference_entity_id', |
44
|
|
|
'view_mode:node.full', |
45
|
|
|
'view_mode:node.rss', |
46
|
|
|
'view_mode:node.search_index', |
47
|
|
|
'view_mode:node.search_result', |
48
|
|
|
'view_mode:node.teaser', |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
$this->container->get('config.factory')->getEditable('entity_embed.settings') |
52
|
|
|
->set('rendered_entity_mode', TRUE)->save(); |
53
|
|
|
$this->container->get('plugin.manager.entity_embed.display')->clearCachedDefinitions(); |
54
|
|
|
|
55
|
|
|
$this->assertAvailableDisplayPlugins($this->node, [ |
56
|
|
|
'entity_reference:entity_reference_label', |
57
|
|
|
'entity_reference:entity_reference_entity_id', |
58
|
|
|
'entity_reference:entity_reference_entity_view', |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
// Ensure that correct form attributes are returned for |
62
|
|
|
// 'entity_reference:entity_reference_entity_id' plugin. |
63
|
|
|
$form = array(); |
64
|
|
|
$form_state = new FormState(); |
65
|
|
|
$display = $this->container->get('plugin.manager.entity_embed.display')->createInstance('entity_reference:entity_reference_entity_id', array()); |
66
|
|
|
$display->setContextValue('entity', $this->node); |
67
|
|
|
$conf_form = $display->buildConfigurationForm($form, $form_state); |
68
|
|
|
$this->assertIdentical(array_keys($conf_form), array()); |
69
|
|
|
|
70
|
|
|
// Ensure that correct form attributes are returned for |
71
|
|
|
// 'entity_reference:entity_reference_entity_view' plugin. |
72
|
|
|
$form = array(); |
73
|
|
|
$form_state = new FormState(); |
74
|
|
|
$display = $this->container->get('plugin.manager.entity_embed.display')->createInstance('entity_reference:entity_reference_entity_view', array()); |
75
|
|
|
$display->setContextValue('entity', $this->node); |
76
|
|
|
$conf_form = $display->buildConfigurationForm($form, $form_state); |
77
|
|
|
$this->assertIdentical($conf_form['view_mode']['#type'], 'select'); |
78
|
|
|
$this->assertIdentical((string) $conf_form['view_mode']['#title'], 'View mode'); |
79
|
|
|
|
80
|
|
|
// Ensure that correct form attributes are returned for |
81
|
|
|
// 'entity_reference:entity_reference_label' plugin. |
82
|
|
|
$form = array(); |
83
|
|
|
$form_state = new FormState(); |
84
|
|
|
$display = $this->container->get('plugin.manager.entity_embed.display')->createInstance('entity_reference:entity_reference_label', array()); |
85
|
|
|
$display->setContextValue('entity', $this->node); |
86
|
|
|
$conf_form = $display->buildConfigurationForm($form, $form_state); |
87
|
|
|
$this->assertIdentical(array_keys($conf_form), array('link')); |
88
|
|
|
$this->assertIdentical($conf_form['link']['#type'], 'checkbox'); |
89
|
|
|
$this->assertIdentical((string) $conf_form['link']['#title'], 'Link label to the referenced entity'); |
90
|
|
|
|
91
|
|
|
// Ensure that 'Rendered Entity' plugin is not available for an entity not |
92
|
|
|
// having a view controller. |
93
|
|
|
$plugin_options = $this->container->get('plugin.manager.entity_embed.display')->getDefinitionOptionsForEntity($this->menu); |
94
|
|
|
$this->assertFalse(array_key_exists('entity_reference:entity_reference_entity_view', $plugin_options), "The 'Rendered entity' plugin is not available."); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Tests filter using entity reference Entity Embed Display plugins. |
99
|
|
|
*/ |
100
|
|
|
public function testFilterEntityReferencePlugins() { |
101
|
|
|
// Test 'Label' Entity Embed Display plugin. |
102
|
|
|
$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>'; |
103
|
|
|
$settings = array(); |
104
|
|
|
$settings['type'] = 'page'; |
105
|
|
|
$settings['title'] = 'Test entity_reference:entity_reference_label Entity Embed Display plugin'; |
106
|
|
|
$settings['body'] = array(array('value' => $content, 'format' => 'custom_format')); |
107
|
|
|
$node = $this->drupalCreateNode($settings); |
108
|
|
|
$this->drupalGet('node/' . $node->id()); |
109
|
|
|
$this->assertText($this->node->title->value, 'Title of the embedded node exists in page.'); |
110
|
|
|
$this->assertNoText($this->node->body->value, 'Body of embedded node does not exists in page.'); |
111
|
|
|
$this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.'); |
112
|
|
|
$this->assertLinkByHref('node/' . $this->node->id(), 0, 'Link to the embedded node exists.'); |
113
|
|
|
|
114
|
|
|
// Test 'Entity ID' Entity Embed Display plugin. |
115
|
|
|
$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>'; |
116
|
|
|
$settings = array(); |
117
|
|
|
$settings['type'] = 'page'; |
118
|
|
|
$settings['title'] = 'Test entity_reference:entity_reference_entity_id Entity Embed Display plugin'; |
119
|
|
|
$settings['body'] = array(array('value' => $content, 'format' => 'custom_format')); |
120
|
|
|
$node = $this->drupalCreateNode($settings); |
121
|
|
|
$this->drupalGet('node/' . $node->id()); |
122
|
|
|
$this->assertText($this->node->id(), 'ID of the embedded node exists in page.'); |
123
|
|
|
$this->assertNoText($this->node->title->value, 'Title of the embedded node does not exists in page.'); |
124
|
|
|
$this->assertNoText($this->node->body->value, 'Body of embedded node does not exists in page.'); |
125
|
|
|
$this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.'); |
126
|
|
|
$this->assertNoLinkByHref('node/' . $this->node->id(), 'Link to the embedded node does not exists.'); |
127
|
|
|
|
128
|
|
|
// Test 'Rendered entity' Entity Embed Display plugin. |
129
|
|
|
$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>'; |
130
|
|
|
$settings = array(); |
131
|
|
|
$settings['type'] = 'page'; |
132
|
|
|
$settings['title'] = 'Test entity_reference:entity_reference_label Entity Embed Display plugin'; |
133
|
|
|
$settings['body'] = array(array('value' => $content, 'format' => 'custom_format')); |
134
|
|
|
$node = $this->drupalCreateNode($settings); |
135
|
|
|
$this->drupalGet('node/' . $node->id()); |
136
|
|
|
$this->assertText($this->node->body->value, 'Body of embedded node does not exists in page.'); |
137
|
|
|
$this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|