You are here

public function ValidatorTest::testFileValidateNameLength in Drupal 9

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

This will ensure the filename length is valid.

File

core/modules/file/tests/src/Kernel/ValidatorTest.php, line 205

Class

ValidatorTest
Tests the functions used to validate uploaded files.

Namespace

Drupal\Tests\file\Kernel

Code

public function testFileValidateNameLength() {

  // Create a new file entity.
  $file = File::create();

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

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

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