function LegacyValidatorTest::testFileValidateNameLength

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

This will ensure the filename length is valid.

File

core/modules/file/tests/src/Kernel/LegacyValidatorTest.php, line 216

Class

LegacyValidatorTest
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()));
    $this->expectDeprecation('file_validate_name_length() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'file.validator\' service instead. See https://www.drupal.org/node/3363700');
    $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.