function ValidatorTest::testFileValidateNameLength

Same name in other branches
  1. 8.9.x 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 208

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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.