class MediaSourceFileTest in Drupal 10
Same name in this branch
- 10 core/modules/media/tests/src/Functional/MediaSourceFileTest.php \Drupal\Tests\media\Functional\MediaSourceFileTest
- 10 core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php \Drupal\Tests\media\FunctionalJavascript\MediaSourceFileTest
- 10 core/modules/media/tests/src/Kernel/MediaSourceFileTest.php \Drupal\Tests\media\Kernel\MediaSourceFileTest
Same name and namespace in other branches
- 8 core/modules/media/tests/src/Kernel/MediaSourceFileTest.php \Drupal\Tests\media\Kernel\MediaSourceFileTest
- 9 core/modules/media/tests/src/Kernel/MediaSourceFileTest.php \Drupal\Tests\media\Kernel\MediaSourceFileTest
Tests the file media source.
@group media
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \PHPUnit\Framework\TestCase implements ServiceProviderInterface uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, AssertContentTrait, ConfigTestTrait, ExtensionListTestTrait, RandomGeneratorTrait, TestRequirementsTrait, PhpUnitWarnings
- class \Drupal\Tests\media\Kernel\MediaKernelTestBase uses MediaTypeCreationTrait
- class \Drupal\Tests\media\Kernel\MediaSourceFileTest
- class \Drupal\Tests\media\Kernel\MediaKernelTestBase uses MediaTypeCreationTrait
Expanded class hierarchy of MediaSourceFileTest
File
- core/
modules/ media/ tests/ src/ Kernel/ MediaSourceFileTest.php, line 10
Namespace
Drupal\Tests\media\KernelView source
class MediaSourceFileTest extends MediaKernelTestBase {
/**
* Tests the file extension constraint.
*/
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);
}
/**
* Tests a media file can be deleted.
*/
public function testFileDeletion() {
$mediaType = $this
->createMediaType('file');
$media = $this
->generateMedia('test.txt', $mediaType);
$media
->save();
$source_field_name = $mediaType
->getSource()
->getSourceFieldDefinition($mediaType)
->getName();
/** @var \Drupal\file\FileInterface $file */
$file = $media
->get($source_field_name)->entity;
$file
->delete();
$this
->assertEmpty($this->container
->get('entity_type.manager')
->getStorage('file')
->loadByProperties([
'filename' => 'test.txt',
]));
}
}