You are here

function FileFieldValidateTestCase::testNodeMaxSize in FileField 6.3

Test the max file size per node validator.

File

tests/filefield.test, line 451

Class

FileFieldValidateTestCase
Test class to check for various validations.

Code

function testNodeMaxSize() {
  $type = $this->node_type;
  $field = $this->field;
  $small_file = $this
    ->getTestFile('text', 131072);

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

  // 1.2MB
  // Set the max file upload size.
  $max_node_limit = '256K';
  $file_limit = 262144;
  $this
    ->updateFileField($field['field_name'], $type->name, array(
    'multiple' => '1',
  ), array(
    'max_filesize_per_node' => $max_node_limit,
  ));

  // 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 node limit (%maxsize).', array(
    '%filesize' => format_size($small_file->filesize),
    '%maxsize' => $max_node_limit,
  )));
  $this
    ->assertFileEntryExists($node_file, t('File entry exists after uploading a file (%filesize) under the max node limit (%maxsize).', array(
    '%filesize' => format_size($small_file->filesize),
    '%maxsize' => $max_node_limit,
  )));

  // Add a second file to the same node which should pass.
  $nid = $this
    ->uploadNodeFile($small_file, $field, $nid);
  $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 node limit (%maxsize).', array(
    '%filesize' => format_size($small_file->filesize),
    '%maxsize' => $max_node_limit,
  )));
  $this
    ->assertFileEntryExists($node_file, t('File entry exists after uploading a file (%filesize) under the max node limit (%maxsize).', array(
    '%filesize' => format_size($small_file->filesize),
    '%maxsize' => $max_node_limit,
  )));

  // Add a third file to the same node which should fail.
  $nid = $this
    ->uploadNodeFile($small_file, $field, $nid);
  $error_message = t('exceeds field settings of %msize.', array(
    '%msize' => format_size($file_limit),
  ));
  $this
    ->assertRaw($error_message, t('File not uploaded as the file (%filesize) exceeds the max node limit (%maxsize).', array(
    '%filesize' => format_size($small_file->filesize),
    '%maxsize' => $max_node_limit,
  )));

  // Check that uploading the large file fails (1M limit).
  $nid = $this
    ->uploadNodeFile($large_file, $field, $type->name);
  $error_message = t('exceeds field settings of %msize.', array(
    '%msize' => format_size($file_limit),
  ));
  $this
    ->assertRaw($error_message, t('File not uploaded as the file (%filesize) exceeds the max node limit (%maxsize).', array(
    '%filesize' => format_size($large_file->filesize),
    '%maxsize' => $max_node_limit,
  )));

  // Turn off the max filesize per node.
  $this
    ->updateFileField($field['field_name'], $type->name, array(
    'multiple' => '0',
  ), array(
    'max_filesize_per_node' => '',
  ));
}