You are here

public function MediaEntityContext::randomDocument in Lightning Media 8.3

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

Generates random text files, saved as media items.

@Given a random document @Given :count random documents

Parameters

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

File

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

Class

MediaEntityContext
Contains step definitions for interacting with media entities.

Namespace

Acquia\LightningExtension\Context

Code

public function randomDocument($n = 1) {
  $random = new Random();

  /** @var EntityContext $context */
  $context = $this
    ->getContext(EntityContext::class);
  for ($i = 0; $i < $n; $i++) {
    $uri = uniqid('public://random_') . '.txt';
    file_put_contents($uri, $random
      ->paragraphs(2));
    $file = File::create([
      'uri' => $uri,
    ]);
    $file
      ->setMimeType('text/plain');
    $file
      ->setTemporary();
    $file
      ->save();
    $media = Media::create([
      'bundle' => 'document',
      'name' => $random
        ->name(32),
      'image' => $file
        ->id(),
      'field_media_in_library' => TRUE,
    ]);
    $context
      ->save($media
      ->setPublished());
  }
}