You are here

function DisplayApiTest::testFieldItemView in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/DisplayApiTest.php \Drupal\field\Tests\DisplayApiTest::testFieldItemView()

Tests the FieldItemInterface::view() method.

File

core/modules/field/src/Tests/DisplayApiTest.php, line 218
Contains \Drupal\field\Tests\DisplayApiTest.

Class

DisplayApiTest
Tests the field display API.

Namespace

Drupal\field\Tests

Code

function testFieldItemView() {

  // No display settings: check that default display settings are used.
  $settings = \Drupal::service('plugin.manager.field.formatter')
    ->getDefaultSettings('field_test_default');
  $setting = $settings['test_formatter_setting'];
  foreach ($this->values as $delta => $value) {
    $item = $this->entity->{$this->fieldName}[$delta];
    $build = $item
      ->view();
    $this
      ->render($build);
    $this
      ->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array(
      '@delta' => $delta,
    )));
  }

  // Check that explicit display settings are used.
  $display = array(
    'type' => 'field_test_multiple',
    'settings' => array(
      'test_formatter_setting_multiple' => $this
        ->randomMachineName(),
    ),
  );
  $setting = $display['settings']['test_formatter_setting_multiple'];
  foreach ($this->values as $delta => $value) {
    $item = $this->entity->{$this->fieldName}[$delta];
    $build = $item
      ->view($display);
    $this
      ->render($build);
    $this
      ->assertText($setting . '|0:' . $value['value'], format_string('Value @delta was displayed with expected setting.', array(
      '@delta' => $delta,
    )));
  }

  // Check that prepare_view steps are invoked.
  $display = array(
    'type' => 'field_test_with_prepare_view',
    'settings' => array(
      'test_formatter_setting_additional' => $this
        ->randomMachineName(),
    ),
  );
  $setting = $display['settings']['test_formatter_setting_additional'];
  foreach ($this->values as $delta => $value) {
    $item = $this->entity->{$this->fieldName}[$delta];
    $build = $item
      ->view($display);
    $this
      ->render($build);
    $this
      ->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), format_string('Value @delta was displayed with expected setting.', array(
      '@delta' => $delta,
    )));
  }

  // View mode: check that display settings specified in the field are used.
  $setting = $this->displayOptions['teaser']['settings']['test_formatter_setting'];
  foreach ($this->values as $delta => $value) {
    $item = $this->entity->{$this->fieldName}[$delta];
    $build = $item
      ->view('teaser');
    $this
      ->render($build);
    $this
      ->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array(
      '@delta' => $delta,
    )));
  }

  // Unknown view mode: check that display settings for 'default' view mode
  // are used.
  $setting = $this->displayOptions['default']['settings']['test_formatter_setting'];
  foreach ($this->values as $delta => $value) {
    $item = $this->entity->{$this->fieldName}[$delta];
    $build = $item
      ->view('unknown_view_mode');
    $this
      ->render($build);
    $this
      ->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array(
      '@delta' => $delta,
    )));
  }
}