function FileFieldWidgetTest::testSingleValuedWidget in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/file/src/Tests/FileFieldWidgetTest.php \Drupal\file\Tests\FileFieldWidgetTest::testSingleValuedWidget()
Tests upload and remove buttons for a single-valued File field.
File
- core/modules/ file/ src/ Tests/ FileFieldWidgetTest.php, line 48 
- 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\TestsCode
function testSingleValuedWidget() {
  $node_storage = $this->container
    ->get('entity.manager')
    ->getStorage('node');
  $type_name = 'article';
  $field_name = strtolower($this
    ->randomMachineName());
  $this
    ->createFileField($field_name, 'node', $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 drupalPostAjaxForm()
    //   does not yet support file uploads.
    $nid = $this
      ->uploadNodeFile($test_file, $field_name, $type_name);
    $node_storage
      ->resetCache(array(
      $nid,
    ));
    $node = $node_storage
      ->load($nid);
    $node_file = File::load($node->{$field_name}->target_id);
    $this
      ->assertFileExists($node_file, 'New file saved to disk on node creation.');
    // Ensure the file can be downloaded.
    $this
      ->drupalGet(file_create_url($node_file
      ->getFileUri()));
    $this
      ->assertResponse(200, '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'), 'Node with file does not display the "Upload" button.');
    $this
      ->assertFieldByXpath('//input[@type="submit"]', t('Remove'), 'Node with file displays the "Remove" button.');
    // "Click" the remove button (emulating either a nojs or js submission).
    switch ($type) {
      case 'nojs':
        $this
          ->drupalPostForm(NULL, array(), t('Remove'));
        break;
      case 'js':
        $button = $this
          ->xpath('//input[@type="submit" and @value="' . t('Remove') . '"]');
        $this
          ->drupalPostAjaxForm(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'), 'After clicking the "Remove" button, it is no longer displayed.');
    $this
      ->assertFieldByXpath('//input[@type="submit"]', t('Upload'), 'After clicking the "Remove" button, the "Upload" button is displayed.');
    // Test label has correct 'for' attribute.
    $input = $this
      ->xpath('//input[@name="files[' . $field_name . '_0]"]');
    $label = $this
      ->xpath('//label[@for="' . (string) $input[0]['id'] . '"]');
    $this
      ->assertTrue(isset($label[0]), 'Label for upload found.');
    // Save the node and ensure it does not have the file.
    $this
      ->drupalPostForm(NULL, array(), t('Save and keep published'));
    $node_storage
      ->resetCache(array(
      $nid,
    ));
    $node = $node_storage
      ->load($nid);
    $this
      ->assertTrue(empty($node->{$field_name}->target_id), 'File was successfully removed from the node.');
  }
}