function FileFieldWidgetTest::testWidgetElement

Same name in other branches
  1. 8.9.x core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testWidgetElement()
  2. 10 core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testWidgetElement()
  3. 11.x core/modules/file/tests/src/Functional/FileFieldWidgetTest.php \Drupal\Tests\file\Functional\FileFieldWidgetTest::testWidgetElement()

Tests file widget element.

File

core/modules/file/tests/src/Functional/FileFieldWidgetTest.php, line 393

Class

FileFieldWidgetTest
Tests the file field widget with public and private files.

Namespace

Drupal\Tests\file\Functional

Code

public function testWidgetElement() {
    $field_name = mb_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}']/table";
    $this->drupalGet('node/add/article');
    // If the field has no item, the table should not be visible.
    $this->assertSession()
        ->elementNotExists('xpath', $xpath);
    // Upload a file.
    $edit['files[' . $field_name . '_0][]'] = $this->container
        ->get('file_system')
        ->realpath($file->getFileUri());
    $this->submitForm($edit, "{$field_name}_0_upload_button");
    // If the field has at least one item, the table should be visible.
    $this->assertSession()
        ->elementsCount('xpath', $xpath, 1);
    // Test for AJAX error when using progress bar on file field widget.
    $http_client = $this->getHttpClient();
    $key = $this->randomMachineName();
    $post_request = $http_client->request('POST', $this->buildUrl('file/progress/' . $key), [
        'headers' => [
            'Accept' => 'application/json',
            'Content-Type' => 'application/x-www-form-urlencoded',
        ],
        'http_errors' => FALSE,
    ]);
    $this->assertNotEquals(500, $post_request->getStatusCode());
    $body = Json::decode($post_request->getBody());
    $this->assertStringContainsString('Starting upload...', $body['message']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.