ImageFieldFormatterTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 97
rs 10
wmc 2
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testImageFieldFormatter() 0 60 1
1
<?php
2
3
namespace Drupal\entity_embed\Tests;
4
5
use Drupal\Component\Serialization\Json;
6
use Drupal\Core\Form\FormState;
7
8
/**
9
 * Tests the image field formatter provided by entity_embed.
10
 *
11
 * @group entity_embed
12
 */
13
class ImageFieldFormatterTest extends EntityEmbedTestBase {
14
15
  /**
16
   * Modules to enable.
17
   *
18
   * @var array
19
   */
20
  public static $modules = ['file', 'image', 'responsive_image'];
21
22
  /**
23
   * Created file entity.
24
   *
25
   * @var \Drupal\file\FileInterface
26
   */
27
  protected $image;
28
29
  /**
30
   * Created file entity.
31
   *
32
   * @var \Drupal\file\FileInterface
33
   */
34
  protected $file;
35
36
  /**
37
   *
38
   */
39
  protected function setUp() {
40
    parent::setUp();
41
    $this->image = $this->getTestFile('image');
42
    $this->file = $this->getTestFile('text');
43
  }
44
45
  /**
46
   * Tests image field formatter Entity Embed Display plugin.
47
   */
48
  public function testImageFieldFormatter() {
49
    // Ensure that image field formatters are available as plugins.
50
    $this->assertAvailableDisplayPlugins($this->image, [
51
      'entity_reference:entity_reference_label',
52
      'entity_reference:entity_reference_entity_id',
53
      'file:file_default',
54
      'file:file_table',
55
      'file:file_url_plain',
56
      'image:responsive_image',
57
      'image:image',
58
    ]);
59
60
    // Ensure that correct form attributes are returned for the image plugin.
61
    $form = array();
62
    $form_state = new FormState();
63
    $display = $this->container->get('plugin.manager.entity_embed.display')
64
      ->createInstance('image:image', []);
65
    $display->setContextValue('entity', $this->image);
66
    $conf_form = $display->buildConfigurationForm($form, $form_state);
67
    $this->assertIdentical(array_keys($conf_form), array(
68
      'image_style',
69
      'image_link',
70
      'alt',
71
      'title',
72
    ));
73
    $this->assertIdentical($conf_form['image_style']['#type'], 'select');
74
    $this->assertIdentical((string) $conf_form['image_style']['#title'], 'Image style');
75
    $this->assertIdentical($conf_form['image_link']['#type'], 'select');
76
    $this->assertIdentical((string) $conf_form['image_link']['#title'], 'Link image to');
77
    $this->assertIdentical($conf_form['alt']['#type'], 'textfield');
78
    $this->assertIdentical((string) $conf_form['alt']['#title'], 'Alternate text');
79
    $this->assertIdentical($conf_form['title']['#type'], 'textfield');
80
    $this->assertIdentical((string) $conf_form['title']['#title'], 'Title');
81
82
    // Test entity embed using 'Image' Entity Embed Display plugin.
83
    $alt_text = "This is sample description";
84
    $title = "This is sample title";
85
    $embed_settings = array('image_link' => 'file');
86
    $content = '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->image->uuid() . '" data-entity-embed-display="image:image" data-entity-embed-display-settings=\'' . Json::encode($embed_settings) . '\' alt="' . $alt_text . '" title="' . $title . '">This placeholder should not be rendered.</drupal-entity>';
87
    $settings = array();
88
    $settings['type'] = 'page';
89
    $settings['title'] = 'Test entity embed with image:image';
90
    $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
91
    $node = $this->drupalCreateNode($settings);
92
    $this->drupalGet('node/' . $node->id());
93
    $this->assertRaw($alt_text, 'Alternate text for the embedded image is visible when embed is successful.');
94
    $this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.');
95
    $this->assertLinkByHref(file_create_url($this->image->getFileUri()), 0, 'Link to the embedded image exists.');
96
97
    // Embed all three field types in one, to ensure they all render correctly.
98
    $content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-entity-embed-display="entity_reference:entity_reference_label"></drupal-entity>';
99
    $content .= '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->file->uuid() . '" data-entity-embed-display="file:file_default"></drupal-entity>';
100
    $content .= '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->image->uuid() . '" data-entity-embed-display="image:image"></drupal-entity>';
101
    $settings = array();
102
    $settings['type'] = 'page';
103
    $settings['title'] = 'Test node entity embedded first then a file entity';
104
    $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
105
    $node = $this->drupalCreateNode($settings);
106
    $this->drupalGet('node/' . $node->id());
107
  }
108
109
}
110