function FileFieldWidgetTest::testSingleValuedWidget

Tests upload and remove buttons for a single-valued File field.

File

drupal/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php, line 25
Definition of Drupal\file\Tests\FileFieldWidgetTest.

Class

FileFieldWidgetTest
Tests file field widget.

Namespace

Drupal\file\Tests

Code

function testSingleValuedWidget() {

  // Use 'page' instead of 'article', so that the 'article' image field does
  // not conflict with this test. If in the future the 'page' type gets its
  // own default file or image field, this test can be made more robust by
  // using a custom node type.
  $type_name = 'page';
  $field_name = strtolower($this
    ->randomName());
  $this
    ->createFileField($field_name, $type_name);
  $field = field_info_field($field_name);
  $instance = field_info_instance('node', $field_name, $type_name);
  $test_file = $this
    ->getTestFile('text');
  foreach (array(
    'nojs',
    'js',
  ) as $type) {

    // Create a new node with the uploaded file and ensure it got uploaded
    // successfully.
    // @todo This only tests a 'nojs' submission, because drupalPostAJAX()
    //   does not yet support file uploads.
    $nid = $this
      ->uploadNodeFile($test_file, $field_name, $type_name);
    $node = node_load($nid, TRUE);
    $node_file = file_load($node->{$field_name}[LANGUAGE_NOT_SPECIFIED][0]['fid']);
    $this
      ->assertFileExists($node_file, t('New file saved to disk on node creation.'));

    // Ensure the file can be downloaded.
    $this
      ->drupalGet(file_create_url($node_file->uri));
    $this
      ->assertResponse(200, t('Confirmed that the generated URL is correct by downloading the shipped file.'));

    // Ensure the edit page has a remove button instead of an upload button.
    $this
      ->drupalGet("node/{$nid}/edit");
    $this
      ->assertNoFieldByXPath('//input[@type="submit"]', t('Upload'), t('Node with file does not display the "Upload" button.'));
    $this
      ->assertFieldByXpath('//input[@type="submit"]', t('Remove'), t('Node with file displays the "Remove" button.'));

    // "Click" the remove button (emulating either a nojs or js submission).
    switch ($type) {
      case 'nojs':
        $this
          ->drupalPost(NULL, array(), t('Remove'));
        break;
      case 'js':
        $button = $this
          ->xpath('//input[@type="submit" and @value="' . t('Remove') . '"]');
        $this
          ->drupalPostAJAX(NULL, array(), array(
          (string) $button[0]['name'] => (string) $button[0]['value'],
        ));
        break;
    }

    // Ensure the page now has an upload button instead of a remove button.
    $this
      ->assertNoFieldByXPath('//input[@type="submit"]', t('Remove'), t('After clicking the "Remove" button, it is no longer displayed.'));
    $this
      ->assertFieldByXpath('//input[@type="submit"]', t('Upload'), t('After clicking the "Remove" button, the "Upload" button is displayed.'));

    // Test label has correct 'for' attribute.
    $label = $this
      ->xpath("//label[@for='edit-" . drupal_clean_css_identifier($field_name) . "-" . LANGUAGE_NOT_SPECIFIED . "-0-upload']");
    $this
      ->assertTrue(isset($label[0]), 'Label for upload found.');

    // Save the node and ensure it does not have the file.
    $this
      ->drupalPost(NULL, array(), t('Save'));
    $node = node_load($nid, TRUE);
    $this
      ->assertTrue(empty($node->{$field_name}[LANGUAGE_NOT_SPECIFIED][0]['fid']), t('File was successfully removed from the node.'));
  }
}