ContentEntityFormFieldValidationFilteringTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 $defaultTheme = 'classy';
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function setUp() {
  51. parent::setUp();
  52. $web_user = $this->drupalCreateUser(['administer entity_test content']);
  53. $this->drupalLogin($web_user);
  54. // Create two fields of field type "test_field", one with single cardinality
  55. // and one with unlimited cardinality on the entity type "entity_test". It
  56. // is important to use this field type because its default widget has a
  57. // custom \Drupal\Core\Field\WidgetInterface::errorElement() implementation.
  58. $this->entityTypeId = 'entity_test';
  59. $this->fieldNameSingle = 'test_single';
  60. $this->fieldNameMultiple = 'test_multiple';
  61. $this->fieldNameFile = 'test_file';
  62. FieldStorageConfig::create([
  63. 'field_name' => $this->fieldNameSingle,
  64. 'entity_type' => $this->entityTypeId,
  65. 'type' => 'test_field',
  66. 'cardinality' => 1,
  67. ])->save();
  68. FieldConfig::create([
  69. 'entity_type' => $this->entityTypeId,
  70. 'field_name' => $this->fieldNameSingle,
  71. 'bundle' => $this->entityTypeId,
  72. 'label' => 'Test single',
  73. 'required' => TRUE,
  74. 'translatable' => FALSE,
  75. ])->save();
  76. FieldStorageConfig::create([
  77. 'field_name' => $this->fieldNameMultiple,
  78. 'entity_type' => $this->entityTypeId,
  79. 'type' => 'test_field',
  80. 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  81. ])->save();
  82. FieldConfig::create([
  83. 'entity_type' => $this->entityTypeId,
  84. 'field_name' => $this->fieldNameMultiple,
  85. 'bundle' => $this->entityTypeId,
  86. 'label' => 'Test multiple',
  87. 'translatable' => FALSE,
  88. ])->save();
  89. // Also create a file field to test its '#limit_validation_errors'
  90. // implementation.
  91. FieldStorageConfig::create([
  92. 'field_name' => $this->fieldNameFile,
  93. 'entity_type' => $this->entityTypeId,
  94. 'type' => 'file',
  95. 'cardinality' => 1,
  96. ])->save();
  97. FieldConfig::create([
  98. 'entity_type' => $this->entityTypeId,
  99. 'field_name' => $this->fieldNameFile,
  100. 'bundle' => $this->entityTypeId,
  101. 'label' => 'Test file',
  102. 'translatable' => FALSE,
  103. ])->save();
  104. $this->container->get('entity_display.repository')
  105. ->getFormDisplay($this->entityTypeId, $this->entityTypeId, 'default')
  106. ->setComponent($this->fieldNameSingle, ['type' => 'test_field_widget'])
  107. ->setComponent($this->fieldNameMultiple, ['type' => 'test_field_widget'])
  108. ->setComponent($this->fieldNameFile, ['type' => 'file_generic'])
  109. ->save();
  110. }
  111. /**
  112. * Tests field widgets with #limit_validation_errors.
  113. */
  114. public function testFieldWidgetsWithLimitedValidationErrors() {
  115. $assert_session = $this->assertSession();
  116. $this->drupalGet($this->entityTypeId . '/add');
  117. // The 'Test multiple' field is the only multi-valued field in the form, so
  118. // try to add a new item for it. This tests the '#limit_validation_errors'
  119. // property set by \Drupal\Core\Field\WidgetBase::formMultipleElements().
  120. $assert_session->elementsCount('css', 'div#edit-test-multiple-wrapper div.form-type-textfield input', 1);
  121. $this->drupalPostForm(NULL, [], 'Add another item');
  122. $assert_session->elementsCount('css', 'div#edit-test-multiple-wrapper div.form-type-textfield input', 2);
  123. // Now try to upload a file. This tests the '#limit_validation_errors'
  124. // property set by
  125. // \Drupal\file\Plugin\Field\FieldWidget\FileWidget::process().
  126. $text_file = current($this->getTestFiles('text'));
  127. $edit = [
  128. 'files[test_file_0]' => \Drupal::service('file_system')->realpath($text_file->uri),
  129. ];
  130. $assert_session->elementNotExists('css', 'input#edit-test-file-0-remove-button');
  131. $this->drupalPostForm(NULL, $edit, 'Upload');
  132. $assert_session->elementExists('css', 'input#edit-test-file-0-remove-button');
  133. // Make the 'Test multiple' field required and check that adding another
  134. // item throws a validation error.
  135. $field_config = FieldConfig::loadByName($this->entityTypeId, $this->entityTypeId, $this->fieldNameMultiple);
  136. $field_config->setRequired(TRUE);
  137. $field_config->save();
  138. $this->drupalPostForm($this->entityTypeId . '/add', [], 'Add another item');
  139. $assert_session->pageTextContains('Test multiple (value 1) field is required.');
  140. // Check that saving the form without entering any value for the required
  141. // field still throws the proper validation errors.
  142. $this->drupalPostForm(NULL, [], 'Save');
  143. $assert_session->pageTextContains('Test single field is required.');
  144. $assert_session->pageTextContains('Test multiple (value 1) field is required.');
  145. }
  146. }