ContentEntityFormFieldValidationFilteringTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Drupal\FunctionalTests\Entity;
  3. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  4. use Drupal\field\Entity\FieldConfig;
  5. use Drupal\field\Entity\FieldStorageConfig;
  6. use Drupal\Tests\BrowserTestBase;
  7. use Drupal\Tests\TestFileCreationTrait;
  8. /**
  9. * Tests field validation filtering on content entity forms.
  10. *
  11. * @group Entity
  12. */
  13. class ContentEntityFormFieldValidationFilteringTest extends BrowserTestBase {
  14. use TestFileCreationTrait;
  15. /**
  16. * The ID of the type of the entity under test.
  17. *
  18. * @var string
  19. */
  20. protected $entityTypeId;
  21. /**
  22. * The single-valued field name being tested with the entity type.
  23. *
  24. * @var string
  25. */
  26. protected $fieldNameSingle;
  27. /**
  28. * The multi-valued field name being tested with the entity type.
  29. *
  30. * @var string
  31. */
  32. protected $fieldNameMultiple;
  33. /**
  34. * The name of the file field being tested with the entity type.
  35. *
  36. * @var string
  37. */
  38. protected $fieldNameFile;
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public static $modules = ['entity_test', 'field_test', 'file', 'image'];
  43. /**
  44. * {@inheritdoc}
  45. */
  46. protected function setUp() {
  47. parent::setUp();
  48. $web_user = $this->drupalCreateUser(['administer entity_test content']);
  49. $this->drupalLogin($web_user);
  50. // Create two fields of field type "test_field", one with single cardinality
  51. // and one with unlimited cardinality on the entity type "entity_test". It
  52. // is important to use this field type because its default widget has a
  53. // custom \Drupal\Core\Field\WidgetInterface::errorElement() implementation.
  54. $this->entityTypeId = 'entity_test';
  55. $this->fieldNameSingle = 'test_single';
  56. $this->fieldNameMultiple = 'test_multiple';
  57. $this->fieldNameFile = 'test_file';
  58. FieldStorageConfig::create([
  59. 'field_name' => $this->fieldNameSingle,
  60. 'entity_type' => $this->entityTypeId,
  61. 'type' => 'test_field',
  62. 'cardinality' => 1,
  63. ])->save();
  64. FieldConfig::create([
  65. 'entity_type' => $this->entityTypeId,
  66. 'field_name' => $this->fieldNameSingle,
  67. 'bundle' => $this->entityTypeId,
  68. 'label' => 'Test single',
  69. 'required' => TRUE,
  70. 'translatable' => FALSE,
  71. ])->save();
  72. FieldStorageConfig::create([
  73. 'field_name' => $this->fieldNameMultiple,
  74. 'entity_type' => $this->entityTypeId,
  75. 'type' => 'test_field',
  76. 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  77. ])->save();
  78. FieldConfig::create([
  79. 'entity_type' => $this->entityTypeId,
  80. 'field_name' => $this->fieldNameMultiple,
  81. 'bundle' => $this->entityTypeId,
  82. 'label' => 'Test multiple',
  83. 'translatable' => FALSE,
  84. ])->save();
  85. // Also create a file field to test its '#limit_validation_errors'
  86. // implementation.
  87. FieldStorageConfig::create([
  88. 'field_name' => $this->fieldNameFile,
  89. 'entity_type' => $this->entityTypeId,
  90. 'type' => 'file',
  91. 'cardinality' => 1,
  92. ])->save();
  93. FieldConfig::create([
  94. 'entity_type' => $this->entityTypeId,
  95. 'field_name' => $this->fieldNameFile,
  96. 'bundle' => $this->entityTypeId,
  97. 'label' => 'Test file',
  98. 'translatable' => FALSE,
  99. ])->save();
  100. entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default')
  101. ->setComponent($this->fieldNameSingle, ['type' => 'test_field_widget'])
  102. ->setComponent($this->fieldNameMultiple, ['type' => 'test_field_widget'])
  103. ->setComponent($this->fieldNameFile, ['type' => 'file_generic'])
  104. ->save();
  105. }
  106. /**
  107. * Tests field widgets with #limit_validation_errors.
  108. */
  109. public function testFieldWidgetsWithLimitedValidationErrors() {
  110. $assert_session = $this->assertSession();
  111. $this->drupalGet($this->entityTypeId . '/add');
  112. // The 'Test multiple' field is the only multi-valued field in the form, so
  113. // try to add a new item for it. This tests the '#limit_validation_errors'
  114. // property set by \Drupal\Core\Field\WidgetBase::formMultipleElements().
  115. $assert_session->elementsCount('css', 'div#edit-test-multiple-wrapper div.form-type-textfield input', 1);
  116. $this->drupalPostForm(NULL, [], 'Add another item');
  117. $assert_session->elementsCount('css', 'div#edit-test-multiple-wrapper div.form-type-textfield input', 2);
  118. // Now try to upload a file. This tests the '#limit_validation_errors'
  119. // property set by
  120. // \Drupal\file\Plugin\Field\FieldWidget\FileWidget::process().
  121. $text_file = current($this->getTestFiles('text'));
  122. $edit = [
  123. 'files[test_file_0]' => \Drupal::service('file_system')->realpath($text_file->uri),
  124. ];
  125. $assert_session->elementNotExists('css', 'input#edit-test-file-0-remove-button');
  126. $this->drupalPostForm(NULL, $edit, 'Upload');
  127. $assert_session->elementExists('css', 'input#edit-test-file-0-remove-button');
  128. // Make the 'Test multiple' field required and check that adding another
  129. // item throws a validation error.
  130. $field_config = FieldConfig::loadByName($this->entityTypeId, $this->entityTypeId, $this->fieldNameMultiple);
  131. $field_config->setRequired(TRUE);
  132. $field_config->save();
  133. $this->drupalPostForm($this->entityTypeId . '/add', [], 'Add another item');
  134. $assert_session->pageTextContains('Test multiple (value 1) field is required.');
  135. // Check that saving the form without entering any value for the required
  136. // field still throws the proper validation errors.
  137. $this->drupalPostForm(NULL, [], 'Save');
  138. $assert_session->pageTextContains('Test single field is required.');
  139. $assert_session->pageTextContains('Test multiple (value 1) field is required.');
  140. }
  141. }