Completed
Push — 8.x-1.x ( c248cd...375743 )
by Janez
02:14
created

testEntityReferenceFieldFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 1
eloc 39
c 5
b 1
f 0
nc 1
nop 0
dl 0
loc 57
rs 9.6818

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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