You are here

public function ImageFieldFormatterTest::testImageFieldFormatter in Entity Embed 7.3

Tests image field formatter Entity Embed Display plugin.

File

./entity_embed.test, line 704
Test integration for the entity_embed module.

Class

ImageFieldFormatterTest
Tests the image field formatter provided by entity_embed.

Code

public function testImageFieldFormatter() {

  // Ensure that image field formatters are available as plugins.
  $this
    ->assertAvailableDisplayPlugins('file', array(
    'entityreference_label',
    'entityreference_entity_id',
    'entityreference_entity_view',
    'file_default',
    'file_table',
    'file_url_plain',
    'image',
  ));

  // Ensure that correct form attributes are returned for the image plugin.
  $form = array();
  $form_state = form_state_defaults();
  $conf_form = entity_embed_field_info_formatter_settings('file', $this->image, 'image', array(), $form, $form_state);

  // Ensure that correct form attributes are returned for the image plugin.
  $this
    ->assertIdentical(array_keys($conf_form), array(
    'image_style',
    'image_link',
  ));
  $this
    ->assertIdentical($conf_form['image_style']['#type'], 'select');
  $this
    ->assertIdentical((string) $conf_form['image_style']['#title'], 'Image style');
  $this
    ->assertIdentical($conf_form['image_link']['#type'], 'select');
  $this
    ->assertIdentical((string) $conf_form['image_link']['#title'], 'Link image to');

  // Test entity embed using 'Image' Entity Embed Display plugin.
  $embed_settings = array(
    'image_style' => 'thumbnail',
    'image_link' => 'file',
  );
  $content = '<drupal-entity data-entity-type="file" data-entity-id="' . $this->image->fid . '" data-entity-embed-display="image:image" data-entity-embed-settings=\'' . drupal_json_encode($embed_settings) . '\'>This placeholder should not be rendered.</drupal-entity>';
  $settings = array();
  $settings['type'] = 'page';
  $settings['title'] = 'Test entity embed with image:image';
  $settings['body'] = array(
    LANGUAGE_NONE => array(
      array(
        'value' => $content,
        'format' => 'custom_format',
      ),
    ),
  );
  $node = $this
    ->drupalCreateNode($settings);
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.');
  $this
    ->assertLinkByHref(file_create_url($this->image->uri), 0, 'Link to the embedded image exists.');

  // Embed all three field types in one, to ensure they all render correctly.
  $content = '<drupal-entity data-entity-type="node" data-entity-id="' . $this->node->nid . '" data-entity-embed-display="entity_reference:entity_reference_label"></drupal-entity>';
  $content .= '<drupal-entity data-entity-type="file" data-entity-id="' . $this->file->fid . '" data-entity-embed-display="file:file_default"></drupal-entity>';
  $content .= '<drupal-entity data-entity-type="file" data-entity-id="' . $this->image->fid . '" data-entity-embed-display="image:image"></drupal-entity>';
  $settings = array();
  $settings['type'] = 'page';
  $settings['title'] = 'Test node entity embedded first then a file entity';
  $settings['body'] = array(
    LANGUAGE_NONE => array(
      array(
        'value' => $content,
        'format' => 'custom_format',
      ),
    ),
  );
  $node = $this
    ->drupalCreateNode($settings);
  $this
    ->drupalGet('node/' . $node->nid);
}