You are here

function ValidatorTest::testFileValidateNameLength in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Tests/ValidatorTest.php \Drupal\file\Tests\ValidatorTest::testFileValidateNameLength()

This will ensure the filename length is valid.

File

core/modules/file/src/Tests/ValidatorTest.php, line 121
Contains \Drupal\file\Tests\ValidatorTest.

Class

ValidatorTest
Tests the functions used to validate uploaded files.

Namespace

Drupal\file\Tests

Code

function testFileValidateNameLength() {

  // Create a new file entity.
  $file = entity_create('file');

  // Add a filename with an allowed length and test it.
  $file
    ->setFilename(str_repeat('x', 240));
  $this
    ->assertEqual(strlen($file
    ->getFilename()), 240);
  $errors = file_validate_name_length($file);
  $this
    ->assertEqual(count($errors), 0, 'No errors reported for 240 length filename.', 'File');

  // Add a filename with a length too long and test it.
  $file
    ->setFilename(str_repeat('x', 241));
  $errors = file_validate_name_length($file);
  $this
    ->assertEqual(count($errors), 1, 'An error reported for 241 length filename.', 'File');

  // Add a filename with an empty string and test it.
  $file
    ->setFilename('');
  $errors = file_validate_name_length($file);
  $this
    ->assertEqual(count($errors), 1, 'An error reported for 0 length filename.', 'File');
}