MediaSourceFileTest.php in Drupal 9
File
core/modules/media/tests/src/Kernel/MediaSourceFileTest.php
View source
<?php
namespace Drupal\Tests\media\Kernel;
class MediaSourceFileTest extends MediaKernelTestBase {
public function testFileExtensionConstraint() {
$mediaType = $this
->createMediaType('file');
$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());
$media = $this
->generateMedia('test.txt', $mediaType);
$result = $media
->validate();
$this
->assertCount(0, $result);
}
public function testFileDeletion() {
$mediaType = $this
->createMediaType('file');
$media = $this
->generateMedia('test.txt', $mediaType);
$media
->save();
$source_field_name = $mediaType
->getSource()
->getSourceFieldDefinition($mediaType)
->getName();
$file = $media
->get($source_field_name)->entity;
$file
->delete();
$this
->assertEmpty($this->container
->get('entity_type.manager')
->getStorage('file')
->loadByProperties([
'filename' => 'test.txt',
]));
}
}