class FileIsImageConstraintValidatorTest

Same name and namespace in other branches
  1. 10 core/modules/file/tests/src/Kernel/Plugin/Validation/Constraint/FileIsImageConstraintValidatorTest.php \Drupal\Tests\file\Kernel\Plugin\Validation\Constraint\FileIsImageConstraintValidatorTest

Tests the FileIsImageConstraintValidator.

@group file @coversDefaultClass \Drupal\file\Plugin\Validation\Constraint\FileIsImageConstraintValidator

Hierarchy

Expanded class hierarchy of FileIsImageConstraintValidatorTest

File

core/modules/file/tests/src/Kernel/Plugin/Validation/Constraint/FileIsImageConstraintValidatorTest.php, line 17

Namespace

Drupal\Tests\file\Kernel\Plugin\Validation\Constraint
View source
class FileIsImageConstraintValidatorTest extends FileValidatorTestBase {
  
  /**
   * An image file.
   *
   * @var \Drupal\file\FileInterface
   */
  protected FileInterface $image;
  
  /**
   * A file which is not an image.
   *
   * @var \Drupal\file\FileInterface
   */
  protected FileInterface $nonImage;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->image = File::create();
    $this->image
      ->setFileUri('core/misc/druplicon.png');
    /** @var \Drupal\Core\File\FileSystemInterface $file_system */
    $file_system = \Drupal::service('file_system');
    $this->image
      ->setFilename($file_system->basename($this->image
      ->getFileUri()));
    $this->nonImage = File::create();
    $this->nonImage
      ->setFileUri('core/assets/vendor/jquery/jquery.min.js');
    $this->nonImage
      ->setFilename($file_system->basename($this->nonImage
      ->getFileUri()));
  }
  
  /**
   * This ensures a specific file is actually an image.
   *
   * @covers ::validate
   */
  public function testFileIsImage() : void {
    $this->assertFileExists($this->image
      ->getFileUri());
    $validators = [
      'FileIsImage' => [],
    ];
    $violations = $this->validator
      ->validate($this->image, $validators);
    $this->assertCount(0, $violations, 'No error reported for our image file.');
    $this->assertFileExists($this->nonImage
      ->getFileUri());
    $violations = $this->validator
      ->validate($this->nonImage, $validators);
    $this->assertCount(1, $violations, 'An error reported for our non-image file.');
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
ExtensionListTestTrait::getModulePath protected function Gets the path for the specified module.
ExtensionListTestTrait::getThemePath protected function Gets the path for the specified theme.
FileIsImageConstraintValidatorTest::$image protected property An image file.
FileIsImageConstraintValidatorTest::$nonImage protected property A file which is not an image.
FileIsImageConstraintValidatorTest::setUp protected function Overrides FileValidatorTestBase::setUp
FileIsImageConstraintValidatorTest::testFileIsImage public function This ensures a specific file is actually an image.
FileValidatorTestBase::$file protected property The file.
FileValidatorTestBase::$modules protected static property Modules to install. 1
FileValidatorTestBase::$validator protected property The file validator.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.

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