You are here

public function EmptyFieldsTest::testEmptyFieldsOutput in Empty fields 8

Tests that the module actually works.

File

tests/src/Functional/EmptyFieldsTest.php, line 50

Class

EmptyFieldsTest
Tests the empty fields functionality.

Namespace

Drupal\Tests\empty_fields\Functional

Code

public function testEmptyFieldsOutput() {
  $url = $this->webUser
    ->toUrl('canonical');

  // Create a field.
  $field_name = mb_strtolower($this
    ->randomMachineName());
  $field_label = $this
    ->randomMachineName() . '_label';
  $this
    ->fieldUIAddNewField('admin/config/people/accounts', $field_name, $field_label, 'string');
  $field_name = 'field_' . $field_name;
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();
  $field_value = $this
    ->randomMachineName();
  $edit = [
    $field_name . '[0][value]' => $field_value,
  ];
  $this
    ->drupalPostForm($this->webUser
    ->toUrl('edit-form'), $edit, 'Save');

  // Verify that field is displayed when has value.
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->responseContains($field_label);
  $this
    ->assertSession()
    ->responseContains($field_value);
  $edit = [
    $field_name . '[0][value]' => '',
  ];
  $this
    ->drupalPostForm($this->webUser
    ->toUrl('edit-form'), $edit, 'Save');

  // Make sure empty field is hidden.
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->responseNotContains($field_label);
  $this
    ->assertSession()
    ->responseNotContains($field_value);

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $repository */
  $repository = \Drupal::service('entity_display.repository');
  $display = $repository
    ->getViewDisplay('user', 'user', 'default');
  $component = $display
    ->getComponent($field_name);

  // Tests 'nbsp' plugin.
  $component['third_party_settings']['empty_fields'] = [
    'handler' => 'nbsp',
  ];
  $display
    ->setComponent($field_name, $component)
    ->save();
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->responseContains($field_label);
  $elements = $this
    ->cssSelect('.empty-fields__nbsp div');
  $this
    ->assertCount(2, $elements);
  if (version_compare(\Drupal::VERSION, '9.0', '<')) {

    // See http://www.fileformat.info/info/unicode/char/00a0/index.htm
    $this
      ->assertSame(' ', $elements[1]
      ->getHtml());
  }
  else {
    $this
      ->assertSame('&nbsp;', $elements[1]
      ->getHtml());
  }

  // Tests 'text' plugin.
  $text = 'This field is empty';
  $component['third_party_settings']['empty_fields'] = [
    'handler' => 'text',
    'settings' => [
      'empty_text' => $text,
    ],
  ];
  $display
    ->setComponent($field_name, $component)
    ->save();
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->responseContains($field_label);
  $elements = $this
    ->cssSelect('.empty-fields__text div');
  $this
    ->assertCount(2, $elements);
  $this
    ->assertSame($text, $elements[1]
    ->getHtml());

  // Tests 'broken' plugin as fallback for unknown.
  $text = 'This plugin broken or missing.';
  $component['third_party_settings']['empty_fields'] = [
    'handler' => 'unknown',
  ];
  $display
    ->setComponent($field_name, $component)
    ->save();
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->responseContains($field_label);
  $elements = $this
    ->cssSelect('.empty-fields__unknown div');
  $this
    ->assertCount(2, $elements);
  $this
    ->assertSame($text, $elements[1]
    ->getHtml());
  $this
    ->drupalGet('admin/config/people/accounts/display');
  $this
    ->assertSession()
    ->responseContains($text);

  // Tests field access using core test module.
  \Drupal::service('module_installer')
    ->install([
    'field_test_boolean_access_denied',
  ]);
  $user = User::load($this->webUser
    ->id());
  $this
    ->assertTrue($user->{$field_name}
    ->access('view'));
  \Drupal::state()
    ->set('field.test_boolean_field_access_field', $field_name);
  $this
    ->assertFalse($user->{$field_name}
    ->access('view'));

  // Update setting to invalidate cache.
  $component['third_party_settings']['empty_fields'] = [
    'handler' => 'nbsp',
  ];
  $display
    ->setComponent($field_name, $component)
    ->save();
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->responseNotContains($field_label);
}