function DisplayApiTest::testFieldEmpty

Tests that the prepareView() formatter method still fires for empty values.

File

drupal/core/modules/field/lib/Drupal/field/Tests/DisplayApiTest.php, line 220
Definition of Drupal\field\Tests\DisplayApiTest.

Class

DisplayApiTest

Namespace

Drupal\field\Tests

Code

function testFieldEmpty() {

  // Uses \Drupal\field_test\Plugin\field\formatter\TestFieldEmptyFormatter.
  $display = array(
    'label' => 'hidden',
    'type' => 'field_empty_test',
    'settings' => array(
      'test_empty_string' => '**EMPTY FIELD**' . $this
        ->randomName(),
    ),
  );

  // $this->entity is set by the setUp() method and by default contains 4
  // numeric values.  We only want to test the display of this one field.
  $output = field_view_field($this->entity, $this->field_name, $display);
  $view = drupal_render($output);
  $this->content = $view;

  // The test field by default contains values, so should not display the
  // default "empty" text.
  $this
    ->assertNoText($display['settings']['test_empty_string']);

  // Now remove the values from the test field and retest.
  $this->entity->{$this->field_name}[Language::LANGCODE_NOT_SPECIFIED] = array();
  field_test_entity_save($this->entity);
  $output = field_view_field($this->entity, $this->field_name, $display);
  $view = drupal_render($output);
  $this->content = $view;

  // This time, as the field values have been removed, we *should* show the
  // default "empty" text.
  $this
    ->assertText($display['settings']['test_empty_string']);
}