3 namespace Drupal\Tests\field\Functional\Boolean;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests the Boolean field formatter settings.
14 class BooleanFormatterSettingsTest extends BrowserTestBase {
21 public static $modules = ['field', 'field_ui', 'text', 'node', 'user'];
24 * The name of the entity bundle that is created in the test.
31 * The name of the Boolean field to use for testing.
40 protected function setUp() {
43 // Create a content type. Use Node because it has Field UI pages that work.
44 $type_name = mb_strtolower($this->randomMachineName(8)) . '_test';
45 $type = $this->drupalCreateContentType(['name' => $type_name, 'type' => $type_name]);
46 $this->bundle = $type->id();
48 $admin_user = $this->drupalCreateUser(['access content', 'administer content types', 'administer node fields', 'administer node display', 'bypass node access', 'administer nodes']);
49 $this->drupalLogin($admin_user);
51 $this->fieldName = mb_strtolower($this->randomMachineName(8));
53 $field_storage = FieldStorageConfig::create([
54 'field_name' => $this->fieldName,
55 'entity_type' => 'node',
58 $field_storage->save();
60 $instance = FieldConfig::create([
61 'field_storage' => $field_storage,
62 'bundle' => $this->bundle,
63 'label' => $this->randomMachineName(),
67 $display = entity_get_display('node', $this->bundle, 'default')
68 ->setComponent($this->fieldName, [
76 * Tests the formatter settings page for the Boolean formatter.
78 public function testBooleanFormatterSettings() {
79 // List the options we expect to see on the settings form. Omit the one
80 // with the Unicode check/x characters, which does not appear to work
81 // well in WebTestBase.
91 // For several different values of the field settings, test that the
92 // options, including default, are shown correctly.
99 $assert_session = $this->assertSession();
100 foreach ($settings as $values) {
101 // Set up the field settings.
102 $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/fields/node.' . $this->bundle . '.' . $this->fieldName);
103 $this->drupalPostForm(NULL, [
104 'settings[on_label]' => $values[0],
105 'settings[off_label]' => $values[1],
108 // Open the Manage Display page and trigger the field settings form.
109 $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/display');
110 $this->drupalPostForm(NULL, [], $this->fieldName . '_settings_edit');
112 // Test that the settings options are present in the correct format.
113 foreach ($options as $string) {
114 $assert_session->pageTextContains($string);
116 $assert_session->pageTextContains(t('Field settings (@on_label / @off_label)', ['@on_label' => $values[0], '@off_label' => $values[1]]));
118 // Test that the settings summary are present in the correct format.
119 $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/display');
120 $result = $this->xpath('//div[contains(@class, :class) and contains(text(), :text)]', [
121 ':class' => 'field-plugin-summary',
122 ':text' => (string) t('Display: @true_label / @false_label', ['@true_label' => $values[0], '@false_label' => $values[1]]),
124 $this->assertEqual(count($result), 1, "Boolean formatter settings summary exist.");