View source
<?php
namespace Drupal\Tests\field\Functional\Boolean;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase;
class BooleanFieldTest extends BrowserTestBase {
protected static $modules = [
'entity_test',
'field_ui',
'options',
'field_test_boolean_access_denied',
];
protected $defaultTheme = 'classy';
protected $fieldStorage;
protected $field;
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'view test entity',
'administer entity_test content',
'administer entity_test form display',
'administer entity_test fields',
]));
}
public function testBooleanField() {
$on = $this
->randomMachineName();
$off = $this
->randomMachineName();
$label = $this
->randomMachineName();
$field_name = mb_strtolower($this
->randomMachineName());
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'boolean',
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'label' => $label,
'required' => TRUE,
'settings' => [
'on_label' => $on,
'off_label' => $off,
],
]);
$this->field
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'boolean_checkbox',
])
->save();
$display_repository
->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent($field_name, [
'type' => 'boolean',
])
->save();
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->fieldValueEquals("{$field_name}[value]", '');
$this
->assertSession()
->pageTextContains($this->field
->label());
$this
->assertSession()
->responseNotContains($on);
$edit = [
"{$field_name}[value]" => 1,
];
$this
->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\\d+)|', $this
->getUrl(), $match);
$id = $match[1];
$this
->assertSession()
->pageTextContains('entity_test ' . $id . ' has been created.');
$entity = EntityTest::load($id);
$this
->drupalGet($entity
->toUrl());
$this
->assertSession()
->responseContains('<div class="field__item">' . $on . '</div>');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => FALSE,
],
])
->save();
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->fieldValueEquals("{$field_name}[value]", '');
$this
->assertSession()
->pageTextContains($on);
$this
->assertSession()
->pageTextNotContains($this->field
->label());
$on = $this
->randomMachineName();
$edit = [
'settings[on_label]' => $on,
];
$this
->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name);
$this
->submitForm($edit, 'Save settings');
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->pageTextContains($on);
$fieldEditUrl = 'entity_test/structure/entity_test/form-display';
$this
->drupalGet($fieldEditUrl);
$this
->submitForm([], $field_name . "_settings_edit");
$this
->assertSession()
->pageTextContains('Use field label instead of the "On" label as the label.');
$edit = [
'fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1,
];
$this
->submitForm($edit, $field_name . "_plugin_settings_update");
$this
->submitForm([], 'Save');
$this
->drupalGet($fieldEditUrl);
$this
->assertSession()
->pageTextContains('Use field label: Yes');
$this
->submitForm([], $field_name . "_settings_edit");
$this
->assertSession()
->pageTextContains('Use field label instead of the "On" label as the label.');
$this
->getSession()
->getPage()
->hasCheckedField('fields[' . $field_name . '][settings_edit_form][settings][display_label]');
$this
->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name);
$this
->assertSession()
->fieldValueEquals('edit-settings-on-label', $on);
$this
->assertSession()
->fieldValueEquals('edit-settings-off-label', $off);
}
public function testFormAccess() {
$on = 'boolean_on';
$off = 'boolean_off';
$label = 'boolean_label';
$field_name = 'boolean_name';
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'boolean',
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'label' => $label,
'settings' => [
'on_label' => $on,
'off_label' => $off,
],
]);
$this->field
->save();
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('entity_test', 'entity_test')
->setComponent($field_name, [
'type' => 'boolean_checkbox',
])
->save();
$display_repository
->getViewDisplay('entity_test', 'entity_test', 'full')
->setComponent($field_name, [
'type' => 'boolean',
])
->save();
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->fieldExists("{$field_name}[value]");
$this
->submitForm([], 'Save');
preg_match('|entity_test/manage/(\\d+)|', $this
->getUrl(), $match);
$id = $match[1];
$this
->assertSession()
->pageTextContains('entity_test ' . $id . ' has been created.');
\Drupal::state()
->set('field.test_boolean_field_access_field', $field_name);
$this
->drupalGet('entity_test/add');
$this
->assertSession()
->fieldNotExists("{$field_name}[value]");
$this
->submitForm([], 'Save');
preg_match('|entity_test/manage/(\\d+)|', $this
->getUrl(), $match);
$id = $match[1];
$this
->assertSession()
->pageTextContains('entity_test ' . $id . ' has been created.');
}
}