You are here

public function TextFieldTest::testRequiredLongTextWithFileUpload in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/text/tests/src/Functional/TextFieldTest.php \Drupal\Tests\text\Functional\TextFieldTest::testRequiredLongTextWithFileUpload()
  2. 9 core/modules/text/tests/src/Functional/TextFieldTest.php \Drupal\Tests\text\Functional\TextFieldTest::testRequiredLongTextWithFileUpload()

Tests required long text with file upload.

File

core/modules/text/tests/src/Functional/TextFieldTest.php, line 82

Class

TextFieldTest
Tests the creation of text fields.

Namespace

Drupal\Tests\text\Functional

Code

public function testRequiredLongTextWithFileUpload() {

  // Create a text field.
  $text_field_name = 'text_long';
  $field_storage = FieldStorageConfig::create([
    'field_name' => $text_field_name,
    'entity_type' => 'entity_test',
    'type' => 'text_with_summary',
  ]);
  $field_storage
    ->save();
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
    'label' => $this
      ->randomMachineName() . '_label',
    'required' => TRUE,
  ])
    ->save();

  // Create a file field.
  $file_field_name = 'file_field';
  $field_storage = FieldStorageConfig::create([
    'field_name' => $file_field_name,
    'entity_type' => 'entity_test',
    'type' => 'file',
  ]);
  $field_storage
    ->save();
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'entity_test',
    'label' => $this
      ->randomMachineName() . '_label',
  ])
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository
    ->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($text_field_name, [
    'type' => 'text_textarea_with_summary',
  ])
    ->setComponent($file_field_name, [
    'type' => 'file_generic',
  ])
    ->save();
  $display_repository
    ->getViewDisplay('entity_test', 'entity_test', 'full')
    ->setComponent($text_field_name)
    ->setComponent($file_field_name)
    ->save();
  $test_file = current($this
    ->drupalGetTestFiles('text'));
  $edit['files[file_field_0]'] = \Drupal::service('file_system')
    ->realpath($test_file->uri);
  $this
    ->drupalGet('entity_test/add');
  $this
    ->submitForm($edit, 'Upload');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $edit = [
    'text_long[0][value]' => 'Long text',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalGet('entity_test/1');
  $this
    ->assertSession()
    ->pageTextContains('Long text');
}