annotate core/modules/field/src/Tests/FormTest.php @ 6:875880e46745

Styling
author Chris Cannam
date Fri, 08 Dec 2017 13:21:27 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\field\Tests;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Html;
Chris@0 6 use Drupal\Core\Entity\Entity\EntityFormDisplay;
Chris@0 7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
Chris@0 8 use Drupal\Core\Form\FormState;
Chris@0 9 use Drupal\entity_test\Entity\EntityTest;
Chris@0 10 use Drupal\entity_test\Entity\EntityTestBaseFieldDisplay;
Chris@0 11 use Drupal\field\Entity\FieldConfig;
Chris@0 12 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Tests field form handling.
Chris@0 16 *
Chris@0 17 * @group field
Chris@0 18 */
Chris@0 19 class FormTest extends FieldTestBase {
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Modules to enable.
Chris@0 23 *
Chris@0 24 * Locale is installed so that TranslatableMarkup actually does something.
Chris@0 25 *
Chris@0 26 * @var array
Chris@0 27 */
Chris@0 28 public static $modules = ['node', 'field_test', 'options', 'entity_test', 'locale'];
Chris@0 29
Chris@0 30 /**
Chris@0 31 * An array of values defining a field single.
Chris@0 32 *
Chris@0 33 * @var array
Chris@0 34 */
Chris@0 35 protected $fieldStorageSingle;
Chris@0 36
Chris@0 37 /**
Chris@0 38 * An array of values defining a field multiple.
Chris@0 39 *
Chris@0 40 * @var array
Chris@0 41 */
Chris@0 42 protected $fieldStorageMultiple;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * An array of values defining a field with unlimited cardinality.
Chris@0 46 *
Chris@0 47 * @var array
Chris@0 48 */
Chris@0 49 protected $fieldStorageUnlimited;
Chris@0 50
Chris@0 51 /**
Chris@0 52 * An array of values defining a field.
Chris@0 53 *
Chris@0 54 * @var array
Chris@0 55 */
Chris@0 56 protected $field;
Chris@0 57
Chris@0 58 protected function setUp() {
Chris@0 59 parent::setUp();
Chris@0 60
Chris@0 61 $web_user = $this->drupalCreateUser(['view test entity', 'administer entity_test content']);
Chris@0 62 $this->drupalLogin($web_user);
Chris@0 63
Chris@0 64 $this->fieldStorageSingle = [
Chris@0 65 'field_name' => 'field_single',
Chris@0 66 'entity_type' => 'entity_test',
Chris@0 67 'type' => 'test_field',
Chris@0 68 ];
Chris@0 69 $this->fieldStorageMultiple = [
Chris@0 70 'field_name' => 'field_multiple',
Chris@0 71 'entity_type' => 'entity_test',
Chris@0 72 'type' => 'test_field',
Chris@0 73 'cardinality' => 4,
Chris@0 74 ];
Chris@0 75 $this->fieldStorageUnlimited = [
Chris@0 76 'field_name' => 'field_unlimited',
Chris@0 77 'entity_type' => 'entity_test',
Chris@0 78 'type' => 'test_field',
Chris@0 79 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
Chris@0 80 ];
Chris@0 81
Chris@0 82 $this->field = [
Chris@0 83 'entity_type' => 'entity_test',
Chris@0 84 'bundle' => 'entity_test',
Chris@0 85 'label' => $this->randomMachineName() . '_label',
Chris@0 86 'description' => '[site:name]_description',
Chris@0 87 'weight' => mt_rand(0, 127),
Chris@0 88 'settings' => [
Chris@0 89 'test_field_setting' => $this->randomMachineName(),
Chris@0 90 ],
Chris@0 91 ];
Chris@0 92 }
Chris@0 93
Chris@0 94 public function testFieldFormSingle() {
Chris@0 95 $field_storage = $this->fieldStorageSingle;
Chris@0 96 $field_name = $field_storage['field_name'];
Chris@0 97 $this->field['field_name'] = $field_name;
Chris@0 98 FieldStorageConfig::create($field_storage)->save();
Chris@0 99 FieldConfig::create($this->field)->save();
Chris@0 100 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
Chris@0 101 ->setComponent($field_name)
Chris@0 102 ->save();
Chris@0 103
Chris@0 104 // Display creation form.
Chris@0 105 $this->drupalGet('entity_test/add');
Chris@0 106
Chris@0 107 // Create token value expected for description.
Chris@0 108 $token_description = Html::escape($this->config('system.site')->get('name')) . '_description';
Chris@0 109 $this->assertText($token_description, 'Token replacement for description is displayed');
Chris@0 110 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed');
Chris@0 111 $this->assertNoField("{$field_name}[1][value]", 'No extraneous widget is displayed');
Chris@0 112
Chris@0 113 // Check that hook_field_widget_form_alter() does not believe this is the
Chris@0 114 // default value form.
Chris@0 115 $this->assertNoText('From hook_field_widget_form_alter(): Default form is true.', 'Not default value form in hook_field_widget_form_alter().');
Chris@0 116
Chris@0 117 // Submit with invalid value (field-level validation).
Chris@0 118 $edit = [
Chris@0 119 "{$field_name}[0][value]" => -1
Chris@0 120 ];
Chris@0 121 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 122 $this->assertRaw(t('%name does not accept the value -1.', ['%name' => $this->field['label']]), 'Field validation fails with invalid input.');
Chris@0 123 // TODO : check that the correct field is flagged for error.
Chris@0 124
Chris@0 125 // Create an entity
Chris@0 126 $value = mt_rand(1, 127);
Chris@0 127 $edit = [
Chris@0 128 "{$field_name}[0][value]" => $value,
Chris@0 129 ];
Chris@0 130 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 131 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
Chris@0 132 $id = $match[1];
Chris@0 133 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
Chris@0 134 $entity = EntityTest::load($id);
Chris@0 135 $this->assertEqual($entity->{$field_name}->value, $value, 'Field value was saved');
Chris@0 136
Chris@0 137 // Display edit form.
Chris@0 138 $this->drupalGet('entity_test/manage/' . $id . '/edit');
Chris@0 139 $this->assertFieldByName("{$field_name}[0][value]", $value, 'Widget is displayed with the correct default value');
Chris@0 140 $this->assertNoField("{$field_name}[1][value]", 'No extraneous widget is displayed');
Chris@0 141
Chris@0 142 // Update the entity.
Chris@0 143 $value = mt_rand(1, 127);
Chris@0 144 $edit = [
Chris@0 145 "{$field_name}[0][value]" => $value,
Chris@0 146 ];
Chris@0 147 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 148 $this->assertText(t('entity_test @id has been updated.', ['@id' => $id]), 'Entity was updated');
Chris@0 149 $this->container->get('entity.manager')->getStorage('entity_test')->resetCache([$id]);
Chris@0 150 $entity = EntityTest::load($id);
Chris@0 151 $this->assertEqual($entity->{$field_name}->value, $value, 'Field value was updated');
Chris@0 152
Chris@0 153 // Empty the field.
Chris@0 154 $value = '';
Chris@0 155 $edit = [
Chris@0 156 "{$field_name}[0][value]" => $value
Chris@0 157 ];
Chris@0 158 $this->drupalPostForm('entity_test/manage/' . $id . '/edit', $edit, t('Save'));
Chris@0 159 $this->assertText(t('entity_test @id has been updated.', ['@id' => $id]), 'Entity was updated');
Chris@0 160 $this->container->get('entity.manager')->getStorage('entity_test')->resetCache([$id]);
Chris@0 161 $entity = EntityTest::load($id);
Chris@0 162 $this->assertTrue($entity->{$field_name}->isEmpty(), 'Field was emptied');
Chris@0 163 }
Chris@0 164
Chris@0 165 /**
Chris@0 166 * Tests field widget default values on entity forms.
Chris@0 167 */
Chris@0 168 public function testFieldFormDefaultValue() {
Chris@0 169 $field_storage = $this->fieldStorageSingle;
Chris@0 170 $field_name = $field_storage['field_name'];
Chris@0 171 $this->field['field_name'] = $field_name;
Chris@0 172 $default = rand(1, 127);
Chris@0 173 $this->field['default_value'] = [['value' => $default]];
Chris@0 174 FieldStorageConfig::create($field_storage)->save();
Chris@0 175 FieldConfig::create($this->field)->save();
Chris@0 176 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
Chris@0 177 ->setComponent($field_name)
Chris@0 178 ->save();
Chris@0 179
Chris@0 180 // Display creation form.
Chris@0 181 $this->drupalGet('entity_test/add');
Chris@0 182 // Test that the default value is displayed correctly.
Chris@0 183 $this->assertFieldByXpath("//input[@name='{$field_name}[0][value]' and @value='$default']");
Chris@0 184
Chris@0 185 // Try to submit an empty value.
Chris@0 186 $edit = [
Chris@0 187 "{$field_name}[0][value]" => '',
Chris@0 188 ];
Chris@0 189 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 190 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
Chris@0 191 $id = $match[1];
Chris@0 192 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created.');
Chris@0 193 $entity = EntityTest::load($id);
Chris@0 194 $this->assertTrue($entity->{$field_name}->isEmpty(), 'Field is now empty.');
Chris@0 195 }
Chris@0 196
Chris@0 197 public function testFieldFormSingleRequired() {
Chris@0 198 $field_storage = $this->fieldStorageSingle;
Chris@0 199 $field_name = $field_storage['field_name'];
Chris@0 200 $this->field['field_name'] = $field_name;
Chris@0 201 $this->field['required'] = TRUE;
Chris@0 202 FieldStorageConfig::create($field_storage)->save();
Chris@0 203 FieldConfig::create($this->field)->save();
Chris@0 204 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
Chris@0 205 ->setComponent($field_name)
Chris@0 206 ->save();
Chris@0 207
Chris@0 208 // Submit with missing required value.
Chris@0 209 $edit = [];
Chris@0 210 $this->drupalPostForm('entity_test/add', $edit, t('Save'));
Chris@0 211 $this->assertRaw(t('@name field is required.', ['@name' => $this->field['label']]), 'Required field with no value fails validation');
Chris@0 212
Chris@0 213 // Create an entity
Chris@0 214 $value = mt_rand(1, 127);
Chris@0 215 $edit = [
Chris@0 216 "{$field_name}[0][value]" => $value,
Chris@0 217 ];
Chris@0 218 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 219 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
Chris@0 220 $id = $match[1];
Chris@0 221 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
Chris@0 222 $entity = EntityTest::load($id);
Chris@0 223 $this->assertEqual($entity->{$field_name}->value, $value, 'Field value was saved');
Chris@0 224
Chris@0 225 // Edit with missing required value.
Chris@0 226 $value = '';
Chris@0 227 $edit = [
Chris@0 228 "{$field_name}[0][value]" => $value,
Chris@0 229 ];
Chris@0 230 $this->drupalPostForm('entity_test/manage/' . $id . '/edit', $edit, t('Save'));
Chris@0 231 $this->assertRaw(t('@name field is required.', ['@name' => $this->field['label']]), 'Required field with no value fails validation');
Chris@0 232 }
Chris@0 233
Chris@0 234 public function testFieldFormUnlimited() {
Chris@0 235 $field_storage = $this->fieldStorageUnlimited;
Chris@0 236 $field_name = $field_storage['field_name'];
Chris@0 237 $this->field['field_name'] = $field_name;
Chris@0 238 FieldStorageConfig::create($field_storage)->save();
Chris@0 239 FieldConfig::create($this->field)->save();
Chris@0 240 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
Chris@0 241 ->setComponent($field_name)
Chris@0 242 ->save();
Chris@0 243
Chris@0 244 // Display creation form -> 1 widget.
Chris@0 245 $this->drupalGet('entity_test/add');
Chris@0 246 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget 1 is displayed');
Chris@0 247 $this->assertNoField("{$field_name}[1][value]", 'No extraneous widget is displayed');
Chris@0 248
Chris@0 249 // Check if aria-describedby attribute is placed on multiple value widgets.
Chris@0 250 $elements = $this->xpath('//table[@id="field-unlimited-values" and @aria-describedby="edit-field-unlimited--description"]');
Chris@0 251 $this->assertTrue(isset($elements[0]), t('aria-describedby attribute is properly placed on multiple value widgets.'));
Chris@0 252
Chris@0 253 // Press 'add more' button -> 2 widgets.
Chris@0 254 $this->drupalPostForm(NULL, [], t('Add another item'));
Chris@0 255 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget 1 is displayed');
Chris@0 256 $this->assertFieldByName("{$field_name}[1][value]", '', 'New widget is displayed');
Chris@0 257 $this->assertNoField("{$field_name}[2][value]", 'No extraneous widget is displayed');
Chris@0 258 // TODO : check that non-field inputs are preserved ('title'), etc.
Chris@0 259
Chris@0 260 // Yet another time so that we can play with more values -> 3 widgets.
Chris@0 261 $this->drupalPostForm(NULL, [], t('Add another item'));
Chris@0 262
Chris@0 263 // Prepare values and weights.
Chris@0 264 $count = 3;
Chris@0 265 $delta_range = $count - 1;
Chris@0 266 $values = $weights = $pattern = $expected_values = [];
Chris@0 267 $edit = [];
Chris@0 268 for ($delta = 0; $delta <= $delta_range; $delta++) {
Chris@0 269 // Assign unique random values and weights.
Chris@0 270 do {
Chris@0 271 $value = mt_rand(1, 127);
Chris@0 272 } while (in_array($value, $values));
Chris@0 273 do {
Chris@0 274 $weight = mt_rand(-$delta_range, $delta_range);
Chris@0 275 } while (in_array($weight, $weights));
Chris@0 276 $edit["{$field_name}[$delta][value]"] = $value;
Chris@0 277 $edit["{$field_name}[$delta][_weight]"] = $weight;
Chris@0 278 // We'll need three slightly different formats to check the values.
Chris@0 279 $values[$delta] = $value;
Chris@0 280 $weights[$delta] = $weight;
Chris@0 281 $field_values[$weight]['value'] = (string) $value;
Chris@0 282 $pattern[$weight] = "<input [^>]*value=\"$value\" [^>]*";
Chris@0 283 }
Chris@0 284
Chris@0 285 // Press 'add more' button -> 4 widgets
Chris@0 286 $this->drupalPostForm(NULL, $edit, t('Add another item'));
Chris@0 287 for ($delta = 0; $delta <= $delta_range; $delta++) {
Chris@0 288 $this->assertFieldByName("{$field_name}[$delta][value]", $values[$delta], "Widget $delta is displayed and has the right value");
Chris@0 289 $this->assertFieldByName("{$field_name}[$delta][_weight]", $weights[$delta], "Widget $delta has the right weight");
Chris@0 290 }
Chris@0 291 ksort($pattern);
Chris@0 292 $pattern = implode('.*', array_values($pattern));
Chris@0 293 $this->assertPattern("|$pattern|s", 'Widgets are displayed in the correct order');
Chris@0 294 $this->assertFieldByName("{$field_name}[$delta][value]", '', "New widget is displayed");
Chris@0 295 $this->assertFieldByName("{$field_name}[$delta][_weight]", $delta, "New widget has the right weight");
Chris@0 296 $this->assertNoField("{$field_name}[" . ($delta + 1) . '][value]', 'No extraneous widget is displayed');
Chris@0 297
Chris@0 298 // Submit the form and create the entity.
Chris@0 299 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 300 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
Chris@0 301 $id = $match[1];
Chris@0 302 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]), 'Entity was created');
Chris@0 303 $entity = EntityTest::load($id);
Chris@0 304 ksort($field_values);
Chris@0 305 $field_values = array_values($field_values);
Chris@0 306 $this->assertIdentical($entity->{$field_name}->getValue(), $field_values, 'Field values were saved in the correct order');
Chris@0 307
Chris@0 308 // Display edit form: check that the expected number of widgets is
Chris@0 309 // displayed, with correct values change values, reorder, leave an empty
Chris@0 310 // value in the middle.
Chris@0 311 // Submit: check that the entity is updated with correct values
Chris@0 312 // Re-submit: check that the field can be emptied.
Chris@0 313
Chris@0 314 // Test with several multiple fields in a form
Chris@0 315 }
Chris@0 316
Chris@0 317 /**
Chris@0 318 * Tests the position of the required label.
Chris@0 319 */
Chris@0 320 public function testFieldFormUnlimitedRequired() {
Chris@0 321 $field_name = $this->fieldStorageUnlimited['field_name'];
Chris@0 322 $this->field['field_name'] = $field_name;
Chris@0 323 $this->field['required'] = TRUE;
Chris@0 324 FieldStorageConfig::create($this->fieldStorageUnlimited)->save();
Chris@0 325 FieldConfig::create($this->field)->save();
Chris@0 326 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
Chris@0 327 ->setComponent($field_name)
Chris@0 328 ->save();
Chris@0 329
Chris@0 330 // Display creation form -> 1 widget.
Chris@0 331 $this->drupalGet('entity_test/add');
Chris@0 332 // Check that the Required symbol is present for the multifield label.
Chris@0 333 $element = $this->xpath('//h4[contains(@class, "label") and contains(@class, "js-form-required") and contains(text(), :value)]', [':value' => $this->field['label']]);
Chris@0 334 $this->assertTrue(isset($element[0]), 'Required symbol added field label.');
Chris@0 335 // Check that the label of the field input is visually hidden and contains
Chris@0 336 // the field title and an indication of the delta for a11y.
Chris@0 337 $element = $this->xpath('//label[@for=:for and contains(@class, "visually-hidden") and contains(text(), :value)]', [':for' => 'edit-field-unlimited-0-value', ':value' => $this->field['label'] . ' (value 1)']);
Chris@0 338 $this->assertTrue(isset($element[0]), 'Required symbol not added for field input.');
Chris@0 339 }
Chris@0 340
Chris@0 341 /**
Chris@0 342 * Tests widget handling of multiple required radios.
Chris@0 343 */
Chris@0 344 public function testFieldFormMultivalueWithRequiredRadio() {
Chris@0 345 // Create a multivalue test field.
Chris@0 346 $field_storage = $this->fieldStorageUnlimited;
Chris@0 347 $field_name = $field_storage['field_name'];
Chris@0 348 $this->field['field_name'] = $field_name;
Chris@0 349 FieldStorageConfig::create($field_storage)->save();
Chris@0 350 FieldConfig::create($this->field)->save();
Chris@0 351 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
Chris@0 352 ->setComponent($field_name)
Chris@0 353 ->save();
Chris@0 354
Chris@0 355 // Add a required radio field.
Chris@0 356 FieldStorageConfig::create([
Chris@0 357 'field_name' => 'required_radio_test',
Chris@0 358 'entity_type' => 'entity_test',
Chris@0 359 'type' => 'list_string',
Chris@0 360 'settings' => [
Chris@0 361 'allowed_values' => ['yes' => 'yes', 'no' => 'no'],
Chris@0 362 ],
Chris@0 363 ])->save();
Chris@0 364 $field = [
Chris@0 365 'field_name' => 'required_radio_test',
Chris@0 366 'entity_type' => 'entity_test',
Chris@0 367 'bundle' => 'entity_test',
Chris@0 368 'required' => TRUE,
Chris@0 369 ];
Chris@0 370 FieldConfig::create($field)->save();
Chris@0 371 entity_get_form_display($field['entity_type'], $field['bundle'], 'default')
Chris@0 372 ->setComponent($field['field_name'], [
Chris@0 373 'type' => 'options_buttons',
Chris@0 374 ])
Chris@0 375 ->save();
Chris@0 376
Chris@0 377 // Display creation form.
Chris@0 378 $this->drupalGet('entity_test/add');
Chris@0 379
Chris@0 380 // Press the 'Add more' button.
Chris@0 381 $this->drupalPostForm(NULL, [], t('Add another item'));
Chris@0 382
Chris@0 383 // Verify that no error is thrown by the radio element.
Chris@0 384 $this->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.');
Chris@0 385
Chris@0 386 // Verify that the widget is added.
Chris@0 387 $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget 1 is displayed');
Chris@0 388 $this->assertFieldByName("{$field_name}[1][value]", '', 'New widget is displayed');
Chris@0 389 $this->assertNoField("{$field_name}[2][value]", 'No extraneous widget is displayed');
Chris@0 390 }
Chris@0 391
Chris@0 392 /**
Chris@0 393 * Tests widgets handling multiple values.
Chris@0 394 */
Chris@0 395 public function testFieldFormMultipleWidget() {
Chris@0 396 // Create a field with fixed cardinality, configure the form to use a
Chris@0 397 // "multiple" widget.
Chris@0 398 $field_storage = $this->fieldStorageMultiple;
Chris@0 399 $field_name = $field_storage['field_name'];
Chris@0 400 $this->field['field_name'] = $field_name;
Chris@0 401 FieldStorageConfig::create($field_storage)->save();
Chris@0 402 FieldConfig::create($this->field)->save();
Chris@0 403 entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
Chris@0 404 ->setComponent($field_name, [
Chris@0 405 'type' => 'test_field_widget_multiple',
Chris@0 406 ])
Chris@0 407 ->save();
Chris@0 408
Chris@0 409 // Display creation form.
Chris@0 410 $this->drupalGet('entity_test/add');
Chris@0 411 $this->assertFieldByName($field_name, '', 'Widget is displayed.');
Chris@0 412
Chris@0 413 // Create entity with three values.
Chris@0 414 $edit = [
Chris@0 415 $field_name => '1, 2, 3',
Chris@0 416 ];
Chris@0 417 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 418 preg_match('|entity_test/manage/(\d+)|', $this->url, $match);
Chris@0 419 $id = $match[1];
Chris@0 420
Chris@0 421 // Check that the values were saved.
Chris@0 422 $entity_init = EntityTest::load($id);
Chris@0 423 $this->assertFieldValues($entity_init, $field_name, [1, 2, 3]);
Chris@0 424
Chris@0 425 // Display the form, check that the values are correctly filled in.
Chris@0 426 $this->drupalGet('entity_test/manage/' . $id . '/edit');
Chris@0 427 $this->assertFieldByName($field_name, '1, 2, 3', 'Widget is displayed.');
Chris@0 428
Chris@0 429 // Submit the form with more values than the field accepts.
Chris@0 430 $edit = [$field_name => '1, 2, 3, 4, 5'];
Chris@0 431 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 432 $this->assertRaw('this field cannot hold more than 4 values', 'Form validation failed.');
Chris@0 433 // Check that the field values were not submitted.
Chris@0 434 $this->assertFieldValues($entity_init, $field_name, [1, 2, 3]);
Chris@0 435 }
Chris@0 436
Chris@0 437 /**
Chris@0 438 * Tests fields with no 'edit' access.
Chris@0 439 */
Chris@0 440 public function testFieldFormAccess() {
Chris@0 441 $entity_type = 'entity_test_rev';
Chris@0 442 // Create a "regular" field.
Chris@0 443 $field_storage = $this->fieldStorageSingle;
Chris@0 444 $field_storage['entity_type'] = $entity_type;
Chris@0 445 $field_name = $field_storage['field_name'];
Chris@0 446 $field = $this->field;
Chris@0 447 $field['field_name'] = $field_name;
Chris@0 448 $field['entity_type'] = $entity_type;
Chris@0 449 $field['bundle'] = $entity_type;
Chris@0 450 FieldStorageConfig::create($field_storage)->save();
Chris@0 451 FieldConfig::create($field)->save();
Chris@0 452 entity_get_form_display($entity_type, $entity_type, 'default')
Chris@0 453 ->setComponent($field_name)
Chris@0 454 ->save();
Chris@0 455
Chris@0 456 // Create a field with no edit access. See
Chris@0 457 // field_test_entity_field_access().
Chris@0 458 $field_storage_no_access = [
Chris@0 459 'field_name' => 'field_no_edit_access',
Chris@0 460 'entity_type' => $entity_type,
Chris@0 461 'type' => 'test_field',
Chris@0 462 ];
Chris@0 463 $field_name_no_access = $field_storage_no_access['field_name'];
Chris@0 464 $field_no_access = [
Chris@0 465 'field_name' => $field_name_no_access,
Chris@0 466 'entity_type' => $entity_type,
Chris@0 467 'bundle' => $entity_type,
Chris@0 468 'default_value' => [0 => ['value' => 99]],
Chris@0 469 ];
Chris@0 470 FieldStorageConfig::create($field_storage_no_access)->save();
Chris@0 471 FieldConfig::create($field_no_access)->save();
Chris@0 472 entity_get_form_display($field_no_access['entity_type'], $field_no_access['bundle'], 'default')
Chris@0 473 ->setComponent($field_name_no_access)
Chris@0 474 ->save();
Chris@0 475
Chris@0 476 // Test that the form structure includes full information for each delta
Chris@0 477 // apart from #access.
Chris@0 478 $entity = $this->container->get('entity_type.manager')
Chris@0 479 ->getStorage($entity_type)
Chris@0 480 ->create(['id' => 0, 'revision_id' => 0]);
Chris@0 481
Chris@0 482 $display = entity_get_form_display($entity_type, $entity_type, 'default');
Chris@0 483 $form = [];
Chris@0 484 $form_state = new FormState();
Chris@0 485 $display->buildForm($entity, $form, $form_state);
Chris@0 486
Chris@0 487 $this->assertFalse($form[$field_name_no_access]['#access'], 'Field #access is FALSE for the field without edit access.');
Chris@0 488
Chris@0 489 // Display creation form.
Chris@0 490 $this->drupalGet($entity_type . '/add');
Chris@0 491 $this->assertNoFieldByName("{$field_name_no_access}[0][value]", '', 'Widget is not displayed if field access is denied.');
Chris@0 492
Chris@0 493 // Create entity.
Chris@0 494 $edit = [
Chris@0 495 "{$field_name}[0][value]" => 1,
Chris@0 496 ];
Chris@0 497 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 498 preg_match("|$entity_type/manage/(\d+)|", $this->url, $match);
Chris@0 499 $id = $match[1];
Chris@0 500
Chris@0 501 // Check that the default value was saved.
Chris@0 502 $storage = $this->container->get('entity_type.manager')
Chris@0 503 ->getStorage($entity_type);
Chris@0 504 $entity = $storage->load($id);
Chris@0 505 $this->assertEqual($entity->$field_name_no_access->value, 99, 'Default value was saved for the field with no edit access.');
Chris@0 506 $this->assertEqual($entity->$field_name->value, 1, 'Entered value vas saved for the field with edit access.');
Chris@0 507
Chris@0 508 // Create a new revision.
Chris@0 509 $edit = [
Chris@0 510 "{$field_name}[0][value]" => 2,
Chris@0 511 'revision' => TRUE,
Chris@0 512 ];
Chris@0 513 $this->drupalPostForm($entity_type . '/manage/' . $id . '/edit', $edit, t('Save'));
Chris@0 514
Chris@0 515 // Check that the new revision has the expected values.
Chris@0 516 $storage->resetCache([$id]);
Chris@0 517 $entity = $storage->load($id);
Chris@0 518 $this->assertEqual($entity->$field_name_no_access->value, 99, 'New revision has the expected value for the field with no edit access.');
Chris@0 519 $this->assertEqual($entity->$field_name->value, 2, 'New revision has the expected value for the field with edit access.');
Chris@0 520
Chris@0 521 // Check that the revision is also saved in the revisions table.
Chris@0 522 $entity = $this->container->get('entity_type.manager')
Chris@0 523 ->getStorage($entity_type)
Chris@0 524 ->loadRevision($entity->getRevisionId());
Chris@0 525 $this->assertEqual($entity->$field_name_no_access->value, 99, 'New revision has the expected value for the field with no edit access.');
Chris@0 526 $this->assertEqual($entity->$field_name->value, 2, 'New revision has the expected value for the field with edit access.');
Chris@0 527 }
Chris@0 528
Chris@0 529 /**
Chris@0 530 * Tests hiding a field in a form.
Chris@0 531 */
Chris@0 532 public function testHiddenField() {
Chris@0 533 $entity_type = 'entity_test_rev';
Chris@0 534 $field_storage = $this->fieldStorageSingle;
Chris@0 535 $field_storage['entity_type'] = $entity_type;
Chris@0 536 $field_name = $field_storage['field_name'];
Chris@0 537 $this->field['field_name'] = $field_name;
Chris@0 538 $this->field['default_value'] = [0 => ['value' => 99]];
Chris@0 539 $this->field['entity_type'] = $entity_type;
Chris@0 540 $this->field['bundle'] = $entity_type;
Chris@0 541 FieldStorageConfig::create($field_storage)->save();
Chris@0 542 $this->field = FieldConfig::create($this->field);
Chris@0 543 $this->field->save();
Chris@0 544 // We explicitly do not assign a widget in a form display, so the field
Chris@0 545 // stays hidden in forms.
Chris@0 546
Chris@0 547 // Display the entity creation form.
Chris@0 548 $this->drupalGet($entity_type . '/add');
Chris@0 549
Chris@0 550 // Create an entity and test that the default value is assigned correctly to
Chris@0 551 // the field that uses the hidden widget.
Chris@0 552 $this->assertNoField("{$field_name}[0][value]", 'The field does not appear in the form');
Chris@0 553 $this->drupalPostForm(NULL, [], t('Save'));
Chris@0 554 preg_match('|' . $entity_type . '/manage/(\d+)|', $this->url, $match);
Chris@0 555 $id = $match[1];
Chris@0 556 $this->assertText(t('entity_test_rev @id has been created.', ['@id' => $id]), 'Entity was created');
Chris@0 557 $storage = $this->container->get('entity_type.manager')
Chris@0 558 ->getStorage($entity_type);
Chris@0 559
Chris@0 560 $entity = $storage->load($id);
Chris@0 561 $this->assertEqual($entity->{$field_name}->value, 99, 'Default value was saved');
Chris@0 562
Chris@0 563 // Update the field to remove the default value, and switch to the default
Chris@0 564 // widget.
Chris@0 565 $this->field->setDefaultValue([]);
Chris@0 566 $this->field->save();
Chris@0 567 entity_get_form_display($entity_type, $this->field->getTargetBundle(), 'default')
Chris@0 568 ->setComponent($this->field->getName(), [
Chris@0 569 'type' => 'test_field_widget',
Chris@0 570 ])
Chris@0 571 ->save();
Chris@0 572
Chris@0 573 // Display edit form.
Chris@0 574 $this->drupalGet($entity_type . '/manage/' . $id . '/edit');
Chris@0 575 $this->assertFieldByName("{$field_name}[0][value]", 99, 'Widget is displayed with the correct default value');
Chris@0 576
Chris@0 577 // Update the entity.
Chris@0 578 $value = mt_rand(1, 127);
Chris@0 579 $edit = ["{$field_name}[0][value]" => $value];
Chris@0 580 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 581 $this->assertText(t('entity_test_rev @id has been updated.', ['@id' => $id]), 'Entity was updated');
Chris@0 582 $storage->resetCache([$id]);
Chris@0 583 $entity = $storage->load($id);
Chris@0 584 $this->assertEqual($entity->{$field_name}->value, $value, 'Field value was updated');
Chris@0 585
Chris@0 586 // Set the field back to hidden.
Chris@0 587 entity_get_form_display($entity_type, $this->field->getTargetBundle(), 'default')
Chris@0 588 ->removeComponent($this->field->getName())
Chris@0 589 ->save();
Chris@0 590
Chris@0 591 // Create a new revision.
Chris@0 592 $edit = ['revision' => TRUE];
Chris@0 593 $this->drupalPostForm($entity_type . '/manage/' . $id . '/edit', $edit, t('Save'));
Chris@0 594
Chris@0 595 // Check that the expected value has been carried over to the new revision.
Chris@0 596 $storage->resetCache([$id]);
Chris@0 597 $entity = $storage->load($id);
Chris@0 598 $this->assertEqual($entity->{$field_name}->value, $value, 'New revision has the expected value for the field with the Hidden widget');
Chris@0 599 }
Chris@0 600
Chris@0 601 /**
Chris@0 602 * Tests the form display of the label for multi-value fields.
Chris@0 603 */
Chris@0 604 public function testLabelOnMultiValueFields() {
Chris@0 605 $user = $this->drupalCreateUser(['administer entity_test content']);
Chris@0 606 $this->drupalLogin($user);
Chris@0 607
Chris@0 608 FieldStorageConfig::create([
Chris@0 609 'entity_type' => 'entity_test_base_field_display',
Chris@0 610 'field_name' => 'foo',
Chris@0 611 'type' => 'text',
Chris@0 612 'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
Chris@0 613 ])->save();
Chris@0 614 FieldConfig::create([
Chris@0 615 'entity_type' => 'entity_test_base_field_display',
Chris@0 616 'bundle' => 'bar',
Chris@0 617 'field_name' => 'foo',
Chris@0 618 // Set a dangerous label to test XSS filtering.
Chris@0 619 'label' => "<script>alert('a configurable field');</script>",
Chris@0 620 ])->save();
Chris@0 621 EntityFormDisplay::create([
Chris@0 622 'targetEntityType' => 'entity_test_base_field_display',
Chris@0 623 'bundle' => 'bar',
Chris@0 624 'mode' => 'default',
Chris@0 625 ])->setComponent('foo', ['type' => 'text_textfield'])->enable()->save();
Chris@0 626
Chris@0 627 $entity = EntityTestBaseFieldDisplay::create(['type' => 'bar']);
Chris@0 628 $entity->save();
Chris@0 629
Chris@0 630 $this->drupalGet('entity_test_base_field_display/manage/' . $entity->id());
Chris@0 631 $this->assertResponse(200);
Chris@0 632 $this->assertText('A field with multiple values');
Chris@0 633 // Test if labels were XSS filtered.
Chris@0 634 $this->assertEscaped("<script>alert('a configurable field');</script>");
Chris@0 635 }
Chris@0 636
Chris@0 637 }