1 field.test FieldDisplayAPITestCase::testFieldViewValue()

Test the field_view_value() function.

File

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

Class

FieldDisplayAPITestCase

Code

function testFieldViewValue() {
  // No display settings: check that default display settings are used.
  $settings = field_info_formatter_settings('field_test_default');
  $setting = $settings['test_formatter_setting'];
  foreach ($this->values as $delta => $value) {
    $item = $this->entity->{$this->field_name}[LANGUAGE_NONE][$delta];
    $output = field_view_value('test_entity', $this->entity, $this->field_name, $item);
    $this->backdropSetContent(backdrop_render($output));
    $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->randomName(),
    ),
  );
  $setting = $display['settings']['test_formatter_setting_multiple'];
  foreach ($this->values as $delta => $value) {
    $item = $this->entity->{$this->field_name}[LANGUAGE_NONE][$delta];
    $output = field_view_value('test_entity', $this->entity, $this->field_name, $item, $display);
    $this->backdropSetContent(backdrop_render($output));
    $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->randomName(),
    ),
  );
  $setting = $display['settings']['test_formatter_setting_additional'];
  foreach ($this->values as $delta => $value) {
    $item = $this->entity->{$this->field_name}[LANGUAGE_NONE][$delta];
    $output = field_view_value('test_entity', $this->entity, $this->field_name, $item, $display);
    $this->backdropSetContent(backdrop_render($output));
    $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.
  $setting = $this->instance['display']['teaser']['settings']['test_formatter_setting'];
  foreach ($this->values as $delta => $value) {
    $item = $this->entity->{$this->field_name}[LANGUAGE_NONE][$delta];
    $output = field_view_value('test_entity', $this->entity, $this->field_name, $item, 'teaser');
    $this->backdropSetContent(backdrop_render($output));
    $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' are used.
  $setting = $this->instance['display']['default']['settings']['test_formatter_setting'];
  foreach ($this->values as $delta => $value) {
    $item = $this->entity->{$this->field_name}[LANGUAGE_NONE][$delta];
    $output = field_view_value('test_entity', $this->entity, $this->field_name, $item, 'unknown_view_mode');
    $this->backdropSetContent(backdrop_render($output));
    $this->assertText($setting . '|' . $value['value'], format_string('Value @delta was displayed with expected setting.', array('@delta' => $delta)));
  }
}