You are here

public function FileFieldWidgetTest::testWidgetElement in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Tests/FileFieldWidgetTest.php \Drupal\file\Tests\FileFieldWidgetTest::testWidgetElement()

Tests file widget element.

File

core/modules/file/src/Tests/FileFieldWidgetTest.php, line 428
Contains \Drupal\file\Tests\FileFieldWidgetTest.

Class

FileFieldWidgetTest
Tests the file field widget, single and multi-valued, with and without AJAX, with public and private files.

Namespace

Drupal\file\Tests

Code

public function testWidgetElement() {
  $field_name = Unicode::strtolower($this
    ->randomMachineName());
  $html_name = str_replace('_', '-', $field_name);
  $this
    ->createFileField($field_name, 'node', 'article', [
    'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
  ]);
  $file = $this
    ->getTestFile('text');
  $xpath = "//details[@data-drupal-selector='edit-{$html_name}']/div[@class='details-wrapper']/table";
  $this
    ->drupalGet('node/add/article');
  $elements = $this
    ->xpath($xpath);

  // If the field has no item, the table should not be visible.
  $this
    ->assertIdentical(count($elements), 0);

  // Upload a file.
  $edit['files[' . $field_name . '_0][]'] = $this->container
    ->get('file_system')
    ->realpath($file
    ->getFileUri());
  $this
    ->drupalPostAjaxForm(NULL, $edit, "{$field_name}_0_upload_button");
  $elements = $this
    ->xpath($xpath);

  // If the field has at least a item, the table should be visible.
  $this
    ->assertIdentical(count($elements), 1);
}