You are here

trait MediaTrait in Entity Reference Hierarchy 8.2

Same name and namespace in other branches
  1. 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

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\Traits
View 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

Namesort descending Modifiers Type Description Overrides
MediaTrait::createFile protected function Creates a file entity.
MediaTrait::createImageMedia protected function Create an image media entity.
MediaTypeCreationTrait::createMediaType protected function Create a media type for a source plugin.
TestFileCreationTrait::$generatedTestFiles protected property Whether the files were copied to the test files directory.
TestFileCreationTrait::compareFiles protected function Compares two files based on size and file name.
TestFileCreationTrait::generateFile public static function Generates a test file.
TestFileCreationTrait::getTestFiles protected function Gets a list of files that can be used in tests.