View source
<?php
namespace Drupal\Tests\empty_fields\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\user\Entity\User;
class EmptyFieldsTest extends BrowserTestBase {
use FieldUiTestTrait;
protected static $modules = [
'block',
'empty_fields',
'field_ui',
'user',
];
protected $defaultTheme = 'stark';
protected $webUser;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this->webUser = $this
->createUser([
'administer user fields',
'administer user display',
]);
$this
->drupalLogin($this->webUser);
}
public function testEmptyFieldsOutput() {
$url = $this->webUser
->toUrl('canonical');
$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');
$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');
$this
->drupalGet($url);
$this
->assertSession()
->responseNotContains($field_label);
$this
->assertSession()
->responseNotContains($field_value);
$repository = \Drupal::service('entity_display.repository');
$display = $repository
->getViewDisplay('user', 'user', 'default');
$component = $display
->getComponent($field_name);
$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', '<')) {
$this
->assertSame(' ', $elements[1]
->getHtml());
}
else {
$this
->assertSame(' ', $elements[1]
->getHtml());
}
$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());
$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);
\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'));
$component['third_party_settings']['empty_fields'] = [
'handler' => 'nbsp',
];
$display
->setComponent($field_name, $component)
->save();
$this
->drupalGet($url);
$this
->assertSession()
->responseNotContains($field_label);
}
}