public function MediaHelperTest::testUseFile in Lightning Media 8.3
@covers ::getSourceField @covers ::prepareFileDestination @covers ::useFile
File
- tests/
src/ Kernel/ MediaHelperTest.php, line 57
Class
- MediaHelperTest
- Contains unit-level test coverage of MediaHelper.
Namespace
Drupal\Tests\lightning_media\KernelCode
public function testUseFile() {
$media_type = $this
->createMediaType('file');
/** @var \Drupal\media\MediaInterface $media */
$media = Media::create([
'bundle' => $media_type
->id(),
]);
/** @var \Drupal\field\Entity\FieldConfig $source_field */
$source_field = $media
->getSource()
->getSourceFieldDefinition($media_type);
$source_field
->setSetting('file_directory', 'wambooli')
->save();
$field_name = $source_field
->getName();
/** @var \Drupal\file\FileInterface $file */
$file = File::create([
'uri' => $this
->generateFile('foo', 80, 10),
]);
$file
->save();
$this
->assertDirectoryNotExists('public://wambooli');
$this
->assertTrue($media
->get($field_name)
->isEmpty());
// Move the file, so we can ensure that useFile() uses the most up-to-date
// file URI.
$moved_file = file_move($file, uniqid('public://'));
$this
->assertSame($file
->id(), $moved_file
->id());
$this
->assertNotSame($file
->getFileUri(), $moved_file
->getFileUri());
$file = MediaHelper::useFile($media, $file);
$this
->assertSame($file
->id(), $media->{$field_name}->target_id);
$this
->assertFileExists($file
->getFileUri());
}