More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / field / src / Tests / Boolean / BooleanFormatterSettingsTest.php
1 <?php
2
3 namespace Drupal\field\Tests\Boolean;
4
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;
10
11 /**
12  * Tests the Boolean field formatter settings.
13  *
14  * @group field
15  */
16 class BooleanFormatterSettingsTest extends WebTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['field', 'field_ui', 'text', 'node', 'user'];
24
25   /**
26    * The name of the entity bundle that is created in the test.
27    *
28    * @var string
29    */
30   protected $bundle;
31
32   /**
33    * The name of the Boolean field to use for testing.
34    *
35    * @var string
36    */
37   protected $fieldName;
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44
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();
49
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);
52
53     $this->fieldName = Unicode::strtolower($this->randomMachineName(8));
54
55     $field_storage = FieldStorageConfig::create([
56       'field_name' => $this->fieldName,
57       'entity_type' => 'node',
58       'type' => 'boolean',
59     ]);
60     $field_storage->save();
61
62     $instance = FieldConfig::create([
63       'field_storage' => $field_storage,
64       'bundle' => $this->bundle,
65       'label' => $this->randomMachineName(),
66     ]);
67     $instance->save();
68
69     $display = entity_get_display('node', $this->bundle, 'default')
70       ->setComponent($this->fieldName, [
71         'type' => 'boolean',
72         'settings' => [],
73       ]);
74     $display->save();
75   }
76
77   /**
78    * Tests the formatter settings page for the Boolean formatter.
79    */
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.
84     $options = [
85       'Yes / No',
86       'True / False',
87       'On / Off',
88       'Enabled / Disabled',
89       '1 / 0',
90       'Custom',
91     ];
92
93     // Define what the "default" option should look like, depending on the
94     // field settings.
95     $default = 'Field settings (@on / @off)';
96
97     // For several different values of the field settings, test that the
98     // options, including default, are shown correctly.
99     $settings = [
100       ['Yes', 'No'],
101       ['On', 'Off'],
102       ['TRUE', 'FALSE'],
103     ];
104
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],
111       ], 'Save settings');
112
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');
116
117       // Test that the settings options are present in the correct format.
118       foreach ($options as $string) {
119         $this->assertText($string);
120       }
121       $this->assertText(SafeMarkup::format($default, ['@on' => $values[0], '@off' => $values[1]]));
122     }
123
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.");
128     }
129   }
130
131 }