function MediaSourceFileTest::testFileExtensionConstraint

Same name in other branches
  1. 9 core/modules/media/tests/src/Kernel/MediaSourceFileTest.php \Drupal\Tests\media\Kernel\MediaSourceFileTest::testFileExtensionConstraint()
  2. 10 core/modules/media/tests/src/Kernel/MediaSourceFileTest.php \Drupal\Tests\media\Kernel\MediaSourceFileTest::testFileExtensionConstraint()
  3. 11.x core/modules/media/tests/src/Kernel/MediaSourceFileTest.php \Drupal\Tests\media\Kernel\MediaSourceFileTest::testFileExtensionConstraint()

Tests the file extension constraint.

File

core/modules/media/tests/src/Kernel/MediaSourceFileTest.php, line 15

Class

MediaSourceFileTest
Tests the file media source.

Namespace

Drupal\Tests\media\Kernel

Code

public function testFileExtensionConstraint() {
    $mediaType = $this->createMediaType('file');
    // Create a random file that should fail.
    $media = $this->generateMedia('test.patch', $mediaType);
    $result = $media->validate();
    $this->assertCount(1, $result);
    $this->assertSame('field_media_file.0', $result->get(0)
        ->getPropertyPath());
    $this->assertStringContainsString('Only files with the following extensions are allowed:', (string) $result->get(0)
        ->getMessage());
    // Create a random file that should pass.
    $media = $this->generateMedia('test.txt', $mediaType);
    $result = $media->validate();
    $this->assertCount(0, $result);
}

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