You are here

function FileFieldValidateTestCase::testFileMaxSize in FileField 6.3

Test the max file size validator.

File

tests/filefield.test, line 406

Class

FileFieldValidateTestCase
Test class to check for various validations.

Code

function testFileMaxSize() {
  $type = $this->node_type;
  $field = $this->field;
  $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_per_file => $file_limit) {

    // Set the max file upload size.
    $this
      ->updateFileField($field['field_name'], $type->name, array(), array(
      'max_filesize_per_file' => $max_filesize_per_file,
    ));

    // Create a new node with the small file, which should pass.
    $nid = $this
      ->uploadNodeFile($small_file, $field, $type->name);
    $node = node_load($nid, NULL, TRUE);
    $node_file = $node->{$field['field_name']}[0];
    $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_per_file,
    )));
    $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_per_file,
    )));

    // Check that uploading the large file fails (1M limit).
    $nid = $this
      ->uploadNodeFile($large_file, $field, $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_per_file,
    )));
  }

  // Turn off the max filesize.
  $this
    ->updateFileField($field['field_name'], $type->name, array(), array(
    'max_filesize_per_file' => '',
  ));

  // Upload the big file successfully.
  $nid = $this
    ->uploadNodeFile($large_file, $field, $type->name);
  $node = node_load($nid, NULL, TRUE);
  $node_file = $node->{$field['field_name']}[0];
  $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),
  )));
}