trait FileDownloadLinkMediaTestTrait in File Download Link 8
Trait for testing file_download_link_media formatter.
Takes care of creating a test node and media.
Hierarchy
- trait \Drupal\Tests\file_download_link\Kernel\FileDownloadLinkMediaTestTrait uses MediaTypeCreationTrait
File
- tests/
src/ Kernel/ FileDownloadLinkMediaTestTrait.php, line 18
Namespace
Drupal\Tests\file_download_link\KernelView source
trait FileDownloadLinkMediaTestTrait {
use MediaTypeCreationTrait;
/**
* A test node.
*
* @var Drupal\node\Entity\Node
*/
public $node;
/**
* A test media.
*
* @var Drupal\media\Entity\Media
*/
public $media;
/**
* Helper function to create media that can be used for testing.
*
* @return Drupal\media\Entity\Media
* A media to be used for testing.
*/
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;
}
/**
* Helper function to create node that can be used for testing.
*
* @return Drupal\node\Entity\Node
* An node to be used for testing.
*/
protected function createTestNode() {
$node_type = NodeType::create([
'type' => 'test_node',
'name' => 'Test Node',
]);
$node_type
->save();
// Our entity will have an image field and a file field.
$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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FileDownloadLinkMediaTestTrait:: |
public | property | A test media. | |
FileDownloadLinkMediaTestTrait:: |
public | property | A test node. | |
FileDownloadLinkMediaTestTrait:: |
protected | function | Helper function to create media that can be used for testing. | |
FileDownloadLinkMediaTestTrait:: |
protected | function | Helper function to create node that can be used for testing. | |
MediaTypeCreationTrait:: |
protected | function | Create a media type for a source plugin. |