function FileValidatorTest::testValidate

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

Tests the validator.

File

core/modules/file/tests/src/Kernel/Validation/FileValidatorTest.php, line 30

Class

FileValidatorTest
Tests the file validator.

Namespace

Drupal\Tests\file\Kernel\Validation

Code

public function testValidate() : void {
  // Use plugin IDs to test they work.
  // Each Constraint has its own tests under
  // core/modules/file/tests/src/Kernel/Plugin/Validation/Constraint.
  $validators = [
    'FileNameLength' => [],
  ];
  FileTestHelper::reset();
  $violations = $this->validator
    ->validate($this->file, $validators);
  $this->assertCount(0, $violations);
  $this->assertCount(1, FileTestHelper::getCalls('validate'));
  FileTestHelper::reset();
  $this->file
    ->set('filename', '');
  $violations = $this->validator
    ->validate($this->file, $validators);
  $this->assertCount(1, $violations);
  $this->assertEquals($violations[0]->getMessage(), $violations[0]->getMessage(), 'Message names are equal');
  $this->assertCount(1, FileTestHelper::getCalls('validate'));
  FileTestHelper::reset();
  $this->file
    ->set('filename', $this->randomMachineName(241));
  $violations = $this->validator
    ->validate($this->file, $validators);
  $this->assertCount(1, $violations);
  $this->assertEquals("The file's name exceeds the 240 characters limit. Rename the file and try again.", $violations[0]->getMessage());
  $this->assertCount(1, FileTestHelper::getCalls('validate'));
}

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