FileFieldValidateTest.php

Definition of Drupal\file\Tests\FileFieldValidateTest.

Namespace

Drupal\file\Tests

File

drupal/core/modules/file/lib/Drupal/file/Tests/FileFieldValidateTest.php
View source
<?php

/**
 * @file
 * Definition of Drupal\file\Tests\FileFieldValidateTest.
 */
namespace Drupal\file\Tests;


/**
 * Tests various validations.
 */
class FileFieldValidateTest extends FileFieldTestBase {
  protected $field;
  protected $node_type;
  public static function getInfo() {
    return array(
      'name' => 'File field validation tests',
      'description' => 'Tests validation functions such as file type, max file size, max size per node, and required.',
      'group' => 'File',
    );
  }

  /**
   * Tests the required property on file fields.
   */
  function testRequired() {
    $type_name = 'article';
    $field_name = strtolower($this
      ->randomName());
    $this
      ->createFileField($field_name, $type_name, array(), array(
      'required' => '1',
    ));
    $field = field_info_field($field_name);
    $instance = field_info_instance('node', $field_name, $type_name);
    $test_file = $this
      ->getTestFile('text');

    // Try to post a new node without uploading a file.
    $langcode = LANGUAGE_NOT_SPECIFIED;
    $edit = array(
      "title" => $this
        ->randomName(),
    );
    $this
      ->drupalPost('node/add/' . $type_name, $edit, t('Save'));
    $this
      ->assertRaw(t('!title field is required.', array(
      '!title' => $instance['label'],
    )), t('Node save failed when required file field was empty.'));

    // Create a new node with the uploaded file.
    $nid = $this
      ->uploadNodeFile($test_file, $field_name, $type_name);
    $this
      ->assertTrue($nid !== FALSE, t('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', array(
      '@test_file' => $test_file->uri,
      '@field_name' => $field_name,
      '@type_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('File exists after uploading to the required field.'));
    $this
      ->assertFileEntryExists($node_file, t('File entry exists after uploading to the required field.'));

    // Try again with a multiple value field.
    field_delete_field($field_name);
    $this
      ->createFileField($field_name, $type_name, array(
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    ), array(
      'required' => '1',
    ));

    // Try to post a new node without uploading a file in the multivalue field.
    $edit = array(
      'title' => $this
        ->randomName(),
    );
    $this
      ->drupalPost('node/add/' . $type_name, $edit, t('Save'));
    $this
      ->assertRaw(t('!title field is required.', array(
      '!title' => $instance['label'],
    )), t('Node save failed when required multiple value file field was empty.'));

    // Create a new node with the uploaded file into the multivalue field.
    $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('File exists after uploading to the required multiple value field.'));
    $this
      ->assertFileEntryExists($node_file, t('File entry exists after uploading to the required multipel value field.'));

    // Remove our file field.
    field_delete_field($field_name);
  }

  /**
   * Tests the max file size validator.
   */
  function testFileMaxSize() {
    $type_name = 'article';
    $field_name = strtolower($this
      ->randomName());
    $this
      ->createFileField($field_name, $type_name, array(), array(
      'required' => '1',
    ));
    $field = field_info_field($field_name);
    $instance = field_info_instance('node', $field_name, $type_name);
    $small_file = $this
      ->getTestFile('text', 131072);

    // 128KB.
    $large_file = $this
      ->getTestFile('text', 1310720);

    // 1.2MB
    // Test uploading both a large and small file with different increments.
    $sizes = array(
      '1M' => 1048576,
      '1024K' => 1048576,
      '1048576' => 1048576,
    );
    foreach ($sizes as $max_filesize => $file_limit) {

      // Set the max file upload size.
      $this
        ->updateFileField($field_name, $type_name, array(
        'max_filesize' => $max_filesize,
      ));
      $instance = field_info_instance('node', $field_name, $type_name);

      // Create a new node with the small file, which should pass.
      $nid = $this
        ->uploadNodeFile($small_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('File exists after uploading a file (%filesize) under the max limit (%maxsize).', array(
        '%filesize' => format_size($small_file->filesize),
        '%maxsize' => $max_filesize,
      )));
      $this
        ->assertFileEntryExists($node_file, t('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', array(
        '%filesize' => format_size($small_file->filesize),
        '%maxsize' => $max_filesize,
      )));

      // Check that uploading the large file fails (1M limit).
      $nid = $this
        ->uploadNodeFile($large_file, $field_name, $type_name);
      $error_message = t('The file is %filesize exceeding the maximum file size of %maxsize.', array(
        '%filesize' => format_size($large_file->filesize),
        '%maxsize' => format_size($file_limit),
      ));
      $this
        ->assertRaw($error_message, t('Node save failed when file (%filesize) exceeded the max upload size (%maxsize).', array(
        '%filesize' => format_size($large_file->filesize),
        '%maxsize' => $max_filesize,
      )));
    }

    // Turn off the max filesize.
    $this
      ->updateFileField($field_name, $type_name, array(
      'max_filesize' => '',
    ));

    // Upload the big file successfully.
    $nid = $this
      ->uploadNodeFile($large_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('File exists after uploading a file (%filesize) with no max limit.', array(
      '%filesize' => format_size($large_file->filesize),
    )));
    $this
      ->assertFileEntryExists($node_file, t('File entry exists after uploading a file (%filesize) with no max limit.', array(
      '%filesize' => format_size($large_file->filesize),
    )));

    // Remove our file field.
    field_delete_field($field_name);
  }

  /**
   * Tests file extension checking.
   */
  function testFileExtension() {
    $type_name = 'article';
    $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('image');
    list(, $test_file_extension) = explode('.', $test_file->filename);

    // Disable extension checking.
    $this
      ->updateFileField($field_name, $type_name, array(
      'file_extensions' => '',
    ));

    // Check that the file can be uploaded with no extension checking.
    $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('File exists after uploading a file with no extension checking.'));
    $this
      ->assertFileEntryExists($node_file, t('File entry exists after uploading a file with no extension checking.'));

    // Enable extension checking for text files.
    $this
      ->updateFileField($field_name, $type_name, array(
      'file_extensions' => 'txt',
    ));

    // Check that the file with the wrong extension cannot be uploaded.
    $nid = $this
      ->uploadNodeFile($test_file, $field_name, $type_name);
    $error_message = t('Only files with the following extensions are allowed: %files-allowed.', array(
      '%files-allowed' => 'txt',
    ));
    $this
      ->assertRaw($error_message, t('Node save failed when file uploaded with the wrong extension.'));

    // Enable extension checking for text and image files.
    $this
      ->updateFileField($field_name, $type_name, array(
      'file_extensions' => "txt {$test_file_extension}",
    ));

    // Check that the file can be uploaded with extension checking.
    $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('File exists after uploading a file with extension checking.'));
    $this
      ->assertFileEntryExists($node_file, t('File entry exists after uploading a file with extension checking.'));

    // Remove our file field.
    field_delete_field($field_name);
  }

}

Classes

Namesort descending Description
FileFieldValidateTest Tests various validations.