1 field.test FieldDisplayAPITestCase::testFieldViewField()

Test the field_view_field() function.

File

core/modules/field/tests/field.test, line 2026
Tests for field.module.

Class

FieldDisplayAPITestCase

Code

function testFieldViewField() {
  // No display settings: check that default display settings are used.
  $output = field_view_field('test_entity', $this->entity, $this->field_name);
  $this->backdropSetContent(backdrop_render($output));
  $settings = field_info_formatter_settings('field_test_default');
  $setting = $settings['test_formatter_setting'];
  $this->assertNoText($this->label, 'Label was not displayed.');
  foreach ($this->values as $delta => $value) {
    $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(
    'label' => 'above',
    'type' => 'field_test_multiple',
    'settings' => array(
      'test_formatter_setting_multiple' => $this->randomName(),
      'alter' => TRUE,
    ),
  );
  $output = field_view_field('test_entity', $this->entity, $this->field_name, $display);
  $this->backdropSetContent(backdrop_render($output));
  $setting = $display['settings']['test_formatter_setting_multiple'];
  $this->assertText($this->label, 'Label was displayed.');
  $this->assertText('field_test_field_attach_view_alter', 'Alter fired, display passed.');
  $array = array();
  foreach ($this->values as $delta => $value) {
    $array[] = $delta . ':' . $value['value'];
  }
  $this->assertText($setting . '|' . implode('|', $array), 'Values were displayed with expected setting.');

  // Check the prepare_view steps are invoked.
  $display = array(
    'label' => 'above',
    'type' => 'field_test_with_prepare_view',
    'settings' => array(
      'test_formatter_setting_additional' => $this->randomName(),
    ),
  );
  $output = field_view_field('test_entity', $this->entity, $this->field_name, $display);
  $view = backdrop_render($output);
  $this->backdropSetContent($view);
  $setting = $display['settings']['test_formatter_setting_additional'];
  $this->assertText($this->label, 'Label was displayed.');
  $this->assertNoText('field_test_field_attach_view_alter', 'Alter not fired.');
  foreach ($this->values as $delta => $value) {
    $this->assertText($setting . '|' . $value['value'] . '|' . ($value['value'] + 1), format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
  }

  // Display mode: check that display settings specified in the instance are
  // used.
  $output = field_view_field('test_entity', $this->entity, $this->field_name, 'teaser');
  $this->backdropSetContent(backdrop_render($output));
  $setting = $this->instance['display']['teaser']['settings']['test_formatter_setting'];
  $this->assertNoText($this->label, 'Label was not displayed.');
  foreach ($this->values as $delta => $value) {
    $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
  }

  // Unknown display mode: check that display settings for 'default' display
  // mode are used.
  $output = field_view_field('test_entity', $this->entity, $this->field_name, 'unknown_view_mode');
  $this->backdropSetContent(backdrop_render($output));
  $setting = $this->instance['display']['default']['settings']['test_formatter_setting'];
  $this->assertNoText($this->label, 'Label was not displayed.');
  foreach ($this->values as $delta => $value) {
    $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
  }
}