public function MediaEntityContext::randomDocument in Lightning Media 8
Same name and namespace in other branches
- 8.2 tests/contexts/MediaEntityContext.behat.inc \Acquia\LightningExtension\Context\MediaEntityContext::randomDocument()
- 8.3 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 120
Class
Namespace
Acquia\LightningExtension\ContextCode
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());
}
}