You are here

public function MediaEntityContext::randomImage in Lightning Media 8.3

Same name and namespace in other branches
  1. 8 tests/contexts/MediaEntityContext.behat.inc \Acquia\LightningExtension\Context\MediaEntityContext::randomImage()
  2. 8.2 tests/contexts/MediaEntityContext.behat.inc \Acquia\LightningExtension\Context\MediaEntityContext::randomImage()

Generates random images, saved as media items.

@Given a random image @Given a random image named :name @Given a random image with alt text :alt @Given a random image named :name with alt text :alt @Given :count random images

Parameters

int $n: (optional) How many to generate. Defaults to 1.

string $name: (optional) The label of the media item wrapping the image. Defaults to a random string.

string $alt: (optional) The alt text for the image.

File

tests/contexts/MediaEntityContext.behat.inc, line 110

Class

MediaEntityContext
Contains step definitions for interacting with media entities.

Namespace

Acquia\LightningExtension\Context

Code

public function randomImage($n = 1, $name = NULL, $alt = NULL) {
  $random = new Random();

  /** @var EntityContext $context */
  $context = $this
    ->getContext(EntityContext::class);
  for ($i = 0; $i < $n; $i++) {
    $uri = $random
      ->image(uniqid('public://random_') . '.png', '240x240', '640x480');
    $file = File::create([
      'uri' => $uri,
    ]);
    $file
      ->setMimeType('image/png');
    $file
      ->setTemporary();
    $file
      ->save();
    $media = Media::create([
      'bundle' => 'image',
      'name' => $name ?: $random
        ->name(32),
      'image' => $file
        ->id(),
      'field_media_in_library' => TRUE,
    ]);
    if ($alt) {
      $media->image->alt = $alt;
    }
    $context
      ->save($media
      ->setPublished());
  }
}