0dadce90469fd8ab7b27f7da474f55b4cd983599
[yaffs-website] / web / core / modules / field / tests / src / Functional / Boolean / BooleanFormatterSettingsTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Functional\Boolean;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests the Boolean field formatter settings.
11  *
12  * @group field
13  */
14 class BooleanFormatterSettingsTest extends BrowserTestBase {
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(['access content', 'administer content types', 'administer node fields', 'administer node display', 'bypass node access', 'administer nodes']);
49     $this->drupalLogin($admin_user);
50
51     $this->fieldName = mb_strtolower($this->randomMachineName(8));
52
53     $field_storage = FieldStorageConfig::create([
54       'field_name' => $this->fieldName,
55       'entity_type' => 'node',
56       'type' => 'boolean',
57     ]);
58     $field_storage->save();
59
60     $instance = FieldConfig::create([
61       'field_storage' => $field_storage,
62       'bundle' => $this->bundle,
63       'label' => $this->randomMachineName(),
64     ]);
65     $instance->save();
66
67     $display = entity_get_display('node', $this->bundle, 'default')
68       ->setComponent($this->fieldName, [
69         'type' => 'boolean',
70         'settings' => [],
71       ]);
72     $display->save();
73   }
74
75   /**
76    * Tests the formatter settings page for the Boolean formatter.
77    */
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.
82     $options = [
83       'Yes / No',
84       'True / False',
85       'On / Off',
86       'Enabled / Disabled',
87       '1 / 0',
88       'Custom',
89     ];
90
91     // For several different values of the field settings, test that the
92     // options, including default, are shown correctly.
93     $settings = [
94       ['Yes', 'No'],
95       ['On', 'Off'],
96       ['TRUE', 'FALSE'],
97     ];
98
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],
106       ], 'Save settings');
107
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');
111
112       // Test that the settings options are present in the correct format.
113       foreach ($options as $string) {
114         $assert_session->pageTextContains($string);
115       }
116       $assert_session->pageTextContains(t('Field settings (@on_label / @off_label)', ['@on_label' => $values[0], '@off_label' => $values[1]]));
117
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]]),
123       ]);
124       $this->assertEqual(count($result), 1, "Boolean formatter settings summary exist.");
125     }
126   }
127
128 }