MediaHelperTest.php in Lightning Media 8.3
File
tests/src/Kernel/MediaHelperTest.php
View source
<?php
namespace Drupal\Tests\lightning_media\Kernel;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\KernelTests\KernelTestBase;
use Drupal\lightning_media\MediaHelper;
use Drupal\media\Entity\Media;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
class MediaHelperTest extends KernelTestBase {
use MediaTypeCreationTrait;
use TestFileCreationTrait;
protected static $modules = [
'field',
'file',
'image',
'lightning_media',
'media',
'system',
'user',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('file');
FieldStorageConfig::create([
'entity_type' => 'media',
'type' => 'boolean',
'field_name' => 'field_media_in_library',
])
->save();
}
public function testUseFile() {
$media_type = $this
->createMediaType('file');
$media = Media::create([
'bundle' => $media_type
->id(),
]);
$source_field = $media
->getSource()
->getSourceFieldDefinition($media_type);
$source_field
->setSetting('file_directory', 'wambooli')
->save();
$field_name = $source_field
->getName();
$file = File::create([
'uri' => $this
->generateFile('foo', 80, 10),
]);
$file
->save();
$this
->assertDirectoryNotExists('public://wambooli');
$this
->assertTrue($media
->get($field_name)
->isEmpty());
$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());
}
}