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