trait MediaTrait in Entity Reference Hierarchy 8.2
Same name and namespace in other branches
- 3.x modules/entity_hierarchy_microsite/tests/src/Traits/MediaTrait.php \Drupal\Tests\entity_hierarchy_microsite\Traits\MediaTrait
Defines a class for media utilties in tests.
Hierarchy
- trait \Drupal\Tests\entity_hierarchy_microsite\Traits\MediaTrait uses MediaTypeCreationTrait, TestFileCreationTrait
2 files declare their use of MediaTrait
- EntityHierarchyMicrositeKernelTestBase.php in modules/
entity_hierarchy_microsite/ tests/ src/ Kernel/ EntityHierarchyMicrositeKernelTestBase.php - MicrositeFunctionalTestBase.php in modules/
entity_hierarchy_microsite/ tests/ src/ Functional/ MicrositeFunctionalTestBase.php
File
- modules/
entity_hierarchy_microsite/ tests/ src/ Traits/ MediaTrait.php, line 13
Namespace
Drupal\Tests\entity_hierarchy_microsite\TraitsView source
trait MediaTrait {
use MediaTypeCreationTrait;
use TestFileCreationTrait;
/**
* Create an image media entity.
*
* @param array $values
* Optional key => values to assign to the media entity.
* @param \Drupal\file\Entity\File $file
* Optional file entity to use.
*
* @return \Drupal\media\Entity\Media
* A media entity.
*/
protected function createImageMedia(array $values = [], File $file = NULL) {
if (!$file) {
$image = $this
->getTestFiles('image')[0];
$file = $this
->createFile($image->uri);
}
$values = $values + [
'bundle' => 'image',
'field_media_image' => [
'target_id' => $file
->id(),
],
];
$media = Media::create($values);
$media
->save();
return $media;
}
/**
* Creates a file entity.
*
* @param string $uri
* The file uri.
* @param int $status
* The file status.
*
* @return \Drupal\file\FileInterface
* The file entity.
*/
protected function createFile($uri, $status = 1) {
$file = File::create([
'uri' => $uri,
'status' => $status,
]);
$file
->save();
return $file;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MediaTrait:: |
protected | function | Creates a file entity. | |
MediaTrait:: |
protected | function | Create an image media entity. | |
MediaTypeCreationTrait:: |
protected | function | Create a media type for a source plugin. | |
TestFileCreationTrait:: |
protected | property | Whether the files were copied to the test files directory. | |
TestFileCreationTrait:: |
protected | function | Compares two files based on size and file name. | |
TestFileCreationTrait:: |
public static | function | Generates a test file. | |
TestFileCreationTrait:: |
protected | function | Gets a list of files that can be used in tests. |