Pull merge.
[yaffs-website] / web / core / modules / field / tests / src / FunctionalJavascript / Boolean / BooleanFormatterSettingsTest.php
1 <?php
2
3 namespace Drupal\Tests\field\FunctionalJavascript\Boolean;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8
9 /**
10  * Tests the Boolean field formatter settings.
11  *
12  * @group field
13  */
14 class BooleanFormatterSettingsTest extends WebDriverTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['field', 'field_ui', 'text', 'node', 'user'];
22
23   /**
24    * The name of the entity bundle that is created in the test.
25    *
26    * @var string
27    */
28   protected $bundle;
29
30   /**
31    * The name of the Boolean field to use for testing.
32    *
33    * @var string
34    */
35   protected $fieldName;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
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();
47
48     $admin_user = $this->drupalCreateUser([
49       'access content',
50       'administer content types',
51       'administer node fields',
52       'administer node display',
53       'bypass node access',
54       'administer nodes',
55     ]);
56     $this->drupalLogin($admin_user);
57
58     $this->fieldName = mb_strtolower($this->randomMachineName(8));
59
60     $field_storage = FieldStorageConfig::create([
61       'field_name' => $this->fieldName,
62       'entity_type' => 'node',
63       'type' => 'boolean',
64     ]);
65     $field_storage->save();
66
67     $instance = FieldConfig::create([
68       'field_storage' => $field_storage,
69       'bundle' => $this->bundle,
70       'label' => $this->randomMachineName(),
71     ]);
72     $instance->save();
73
74     $display = entity_get_display('node', $this->bundle, 'default')
75       ->setComponent($this->fieldName, [
76         'type' => 'boolean',
77         'settings' => [],
78       ]);
79     $display->save();
80   }
81
82   /**
83    * Tests the formatter settings page for the Boolean formatter.
84    */
85   public function testBooleanFormatterSettings() {
86     // List the options we expect to see on the settings form. Omit the one
87     // with the Unicode check/x characters, which does not appear to work
88     // well in WebTestBase.
89     $options = [
90       'Yes / No',
91       'True / False',
92       'On / Off',
93       'Enabled / Disabled',
94       '1 / 0',
95       'Custom',
96     ];
97
98     // For several different values of the field settings, test that the
99     // options, including default, are shown correctly.
100     $settings = [
101       ['Yes', 'No'],
102       ['On', 'Off'],
103       ['TRUE', 'FALSE'],
104     ];
105
106     $assert_session = $this->assertSession();
107     foreach ($settings as $values) {
108       // Set up the field settings.
109       $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/fields/node.' . $this->bundle . '.' . $this->fieldName);
110       $this->drupalPostForm(NULL, [
111         'settings[on_label]' => $values[0],
112         'settings[off_label]' => $values[1],
113       ], t('Save settings'));
114
115       // Open the Manage Display page and trigger the field settings form.
116       $this->drupalGet('admin/structure/types/manage/' . $this->bundle . '/display');
117       $this->getSession()->getPage()->pressButton($this->fieldName . '_settings_edit');
118       $assert_session->waitForElement('css', '.ajax-new-content');
119
120       // Test that the settings options are present in the correct format.
121       foreach ($options as $string) {
122         $assert_session->pageTextContains($string);
123       }
124       $assert_session->pageTextContains(t('Field settings (@on_label / @off_label)', ['@on_label' => $values[0], '@off_label' => $values[1]]));
125     }
126   }
127
128 }