BooleanFieldTest.php 7.74 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\field\Tests\Boolean;

5
use Drupal\Component\Utility\Unicode;
6
use Drupal\entity_test\Entity\EntityTest;
7
use Drupal\field\Entity\FieldStorageConfig;
8
use Drupal\field\Entity\FieldConfig;
9 10 11 12 13 14 15 16 17 18 19 20 21 22
use Drupal\simpletest\WebTestBase;

/**
 * Tests boolean field functionality.
 *
 * @group field
 */
class BooleanFieldTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
23 24 25 26 27 28
  public static $modules = [
    'entity_test',
    'field_ui',
    'options',
    'field_test_boolean_access_denied',
  ];
29 30 31 32

  /**
   * A field to use in this test class.
   *
33
   * @var \Drupal\field\Entity\FieldStorageConfig
34
   */
35
  protected $fieldStorage;
36 37

  /**
38
   * The field used in this test class.
39
   *
40
   * @var \Drupal\field\Entity\FieldConfig
41
   */
42
  protected $field;
43 44 45 46

  /**
   * {@inheritdoc}
   */
47
  protected function setUp() {
48 49
    parent::setUp();

50
    $this->drupalLogin($this->drupalCreateUser([
51 52 53 54
      'view test entity',
      'administer entity_test content',
      'administer entity_test form display',
      'administer entity_test fields',
55
    ]));
56 57 58 59 60
  }

  /**
   * Tests boolean field.
   */
61
  public function testBooleanField() {
62 63 64
    $on = $this->randomMachineName();
    $off = $this->randomMachineName();
    $label = $this->randomMachineName();
65 66

    // Create a field with settings to validate.
67
    $field_name = Unicode::strtolower($this->randomMachineName());
68
    $this->fieldStorage = FieldStorageConfig::create([
69
      'field_name' => $field_name,
70 71
      'entity_type' => 'entity_test',
      'type' => 'boolean',
72
    ]);
73
    $this->fieldStorage->save();
74
    $this->field = FieldConfig::create([
75 76 77 78 79
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
      'label' => $label,
      'required' => TRUE,
80
      'settings' => [
81 82
        'on_label' => $on,
        'off_label' => $off,
83 84
      ],
    ]);
85
    $this->field->save();
86 87 88

    // Create a form display for the default form mode.
    entity_get_form_display('entity_test', 'entity_test', 'default')
89
      ->setComponent($field_name, [
90
        'type' => 'boolean_checkbox',
91
      ])
92 93 94
      ->save();
    // Create a display for the full view mode.
    entity_get_display('entity_test', 'entity_test', 'full')
95
      ->setComponent($field_name, [
96
        'type' => 'boolean',
97
      ])
98 99 100 101 102
      ->save();

    // Display creation form.
    $this->drupalGet('entity_test/add');
    $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
103 104
    $this->assertText($this->field->label(), 'Uses field label by default.');
    $this->assertNoRaw($on, 'Does not use the "On" label.');
105 106

    // Submit and ensure it is accepted.
107
    $edit = [
108
      "{$field_name}[value]" => 1,
109
    ];
110 111 112
    $this->drupalPostForm(NULL, $edit, t('Save'));
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
    $id = $match[1];
113
    $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
114 115

    // Verify that boolean value is displayed.
116
    $entity = EntityTest::load($id);
117 118
    $display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), 'full');
    $content = $display->build($entity);
119
    $this->setRawContent(\Drupal::service('renderer')->renderRoot($content));
120
    $this->assertRaw('<div class="field__item">' . $on . '</div>');
121

122
    // Test with "On" label option.
123
    entity_get_form_display('entity_test', 'entity_test', 'default')
124
      ->setComponent($field_name, [
125
        'type' => 'boolean_checkbox',
126
        'settings' => [
127
          'display_label' => FALSE,
128 129
        ]
      ])
130 131 132 133
      ->save();

    $this->drupalGet('entity_test/add');
    $this->assertFieldByName("{$field_name}[value]", '', 'Widget found.');
134 135 136 137 138
    $this->assertRaw($on);
    $this->assertNoText($this->field->label());

    // Test if we can change the on label.
    $on = $this->randomMachineName();
139
    $edit = [
140
      'settings[on_label]' => $on,
141
    ];
142 143 144 145
    $this->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name, $edit, t('Save settings'));
    // Check if we see the updated labels in the creation form.
    $this->drupalGet('entity_test/add');
    $this->assertRaw($on);
146 147 148 149 150 151 152

    // Go to the form display page and check if the default settings works as
    // expected.
    $fieldEditUrl = 'entity_test/structure/entity_test/form-display';
    $this->drupalGet($fieldEditUrl);

    // Click on the widget settings button to open the widget settings form.
153
    $this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit");
154 155

    $this->assertText(
156
      'Use field label instead of the "On" label as the label.',
157 158 159 160
      t('Display setting checkbox available.')
    );

    // Enable setting.
161
    $edit = ['fields[' . $field_name . '][settings_edit_form][settings][display_label]' => 1];
162 163 164 165 166 167 168 169
    $this->drupalPostAjaxForm(NULL, $edit, $field_name . "_plugin_settings_update");
    $this->drupalPostForm(NULL, NULL, 'Save');

    // Go again to the form display page and check if the setting
    // is stored and has the expected effect.
    $this->drupalGet($fieldEditUrl);
    $this->assertText('Use field label: Yes', 'Checking the display settings checkbox updated the value.');

170
    $this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit");
171
    $this->assertText(
172
      'Use field label instead of the "On" label as the label.',
173 174 175
      t('Display setting checkbox is available')
    );
    $this->assertFieldByXPath(
176
      '*//input[starts-with(@id, "edit-fields-' . $field_name . '-settings-edit-form-settings-display-label") and @value="1"]',
177 178 179 180 181
      TRUE,
      t('Display label changes label of the checkbox')
    );

    // Test the boolean field settings.
182
    $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.' . $field_name);
183 184
    $this->assertFieldById('edit-settings-on-label', $on);
    $this->assertFieldById('edit-settings-off-label', $off);
185 186
  }

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  /**
   * Test field access.
   */
  public function testFormAccess() {
    $on = 'boolean_on';
    $off = 'boolean_off';
    $label = 'boolean_label';
    $field_name = 'boolean_name';
    $this->fieldStorage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'type' => 'boolean',
    ]);
    $this->fieldStorage->save();
    $this->field = FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
      'label' => $label,
      'settings' => [
        'on_label' => $on,
        'off_label' => $off,
      ],
    ]);
    $this->field->save();

    // Create a form display for the default form mode.
    entity_get_form_display('entity_test', 'entity_test', 'default')
      ->setComponent($field_name, [
        'type' => 'boolean_checkbox',
      ])
      ->save();

    // Create a display for the full view mode.
    entity_get_display('entity_test', 'entity_test', 'full')
      ->setComponent($field_name, [
        'type' => 'boolean',
      ])
      ->save();

    // Display creation form.
    $this->drupalGet('entity_test/add');
    $this->assertFieldByName("{$field_name}[value]");

    // Should be posted OK.
    $this->drupalPostForm(NULL, [], t('Save'));
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
    $id = $match[1];
    $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));

    // Tell the test module to disable access to the field.
    \Drupal::state()->set('field.test_boolean_field_access_field', $field_name);
    $this->drupalGet('entity_test/add');
    // Field should not be there anymore.
    $this->assertNoFieldByName("{$field_name}[value]");
    // Should still be able to post the form.
    $this->drupalPostForm(NULL, [], t('Save'));
    preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
    $id = $match[1];
    $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
  }

249
}