FileDownloadLinkMediaTestTrait.php in File Download Link 8
File
tests/src/Kernel/FileDownloadLinkMediaTestTrait.php
View source
<?php
namespace Drupal\Tests\file_download_link\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
trait FileDownloadLinkMediaTestTrait {
use MediaTypeCreationTrait;
public $node;
public $media;
protected function createTestMedia() {
$this
->createMediaType('file', [
'id' => 'test_media',
'label' => 'Test Media',
]);
file_put_contents('public://file.txt', str_repeat('t', 10));
$file_file = File::create([
'uri' => 'public://file.txt',
'filename' => 'file.txt',
]);
$file_file
->save();
$media = Media::create([
'bundle' => 'test_media',
'name' => 'Test Media',
]);
$media
->set('field_media_file', $file_file
->id());
$media
->set('status', 1);
$media
->save();
return $media;
}
protected function createTestNode() {
$node_type = NodeType::create([
'type' => 'test_node',
'name' => 'Test Node',
]);
$node_type
->save();
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_media',
'entity_type' => 'node',
'type' => 'entity_reference',
'settings' => [
'target_type' => 'media',
],
]);
$field_storage
->save();
$instance = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'test_node',
'label' => 'Media',
]);
$instance
->save();
$node = Node::create([
'type' => 'test_node',
'title' => 'Test Entity',
]);
$node
->set('field_media', $this->media
->id());
$node
->set('status', 1);
$node
->save();
return $node;
}
}