3 namespace Drupal\field\Tests\Boolean;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\simpletest\WebTestBase;
12 * Tests the Boolean field formatter settings.
16 class BooleanFormatterSettingsTest extends WebTestBase {
23 public static $modules = ['field', 'field_ui', 'text', 'node', 'user'];
26 * The name of the entity bundle that is created in the test.
33 * The name of the Boolean field to use for testing.
42 protected function setUp() {
45 // Create a content type. Use Node because it has Field UI pages that work.
46 $type_name = Unicode::strtolower($this->randomMachineName(8)) . '_test';
47 $type = $this->drupalCreateContentType(['name' => $type_name, 'type' => $type_name]);
48 $this->bundle = $type->id();
50 $admin_user = $this->drupalCreateUser(['access content', 'administer content types', 'administer node fields', 'administer node display', 'bypass node access', 'administer nodes']);
51 $this->drupalLogin($admin_user);
53 $this->fieldName = Unicode::strtolower($this->randomMachineName(8));
55 $field_storage = FieldStorageConfig::create([
56 'field_name' => $this->fieldName,
57 'entity_type' => 'node',
60 $field_storage->save();
62 $instance = FieldConfig::create([
63 'field_storage' => $field_storage,
64 'bundle' => $this->bundle,
65 'label' => $this->randomMachineName(),
69 $display = entity_get_display('node', $this->bundle, 'default')
70 ->setComponent($this->fieldName, [
78 * Tests the formatter settings page for the Boolean formatter.
80 public function testBooleanFormatterSettings() {
81 // List the options we expect to see on the settings form. Omit the one
82 // with the Unicode check/x characters, which does not appear to work
83 // well in WebTestBase.
93 // Define what the "default" option should look like, depending on the
95 $default = 'Field settings (@on / @off)';
97 // For several different values of the field settings, test that the
98 // options, including default, are shown correctly.
105 foreach ($settings as $values) {
106 // Set up the field settings.
107 $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/fields/node.' . $this->bundle . '.' . $this->fieldName);
108 $this->drupalPostForm(NULL, [
109 'settings[on_label]' => $values[0],
110 'settings[off_label]' => $values[1],
113 // Open the Manage Display page and trigger the field settings form.
114 $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/display');
115 $this->drupalPostAjaxForm(NULL, [], $this->fieldName . '_settings_edit');
117 // Test that the settings options are present in the correct format.
118 foreach ($options as $string) {
119 $this->assertText($string);
121 $this->assertText(SafeMarkup::format($default, ['@on' => $values[0], '@off' => $values[1]]));
124 foreach ($settings as $values) {
125 $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/display');
126 $result = $this->xpath('//div[contains(@class, :class) and contains(text(), :text)]', [':class' => 'field-plugin-summary', ':text' => 'Display: TRUE / FALSE']);
127 $this->assertEqual(count($result), 1, "Boolean formatter settings summary exist.");