MediaEntityContext.behat.inc in Lightning Media 8
File
tests/contexts/MediaEntityContext.behat.inc
View source
<?php
namespace Acquia\LightningExtension\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Drupal\Component\Utility\Random;
use Drupal\DrupalExtension\Context\DrupalSubContextBase;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
class MediaEntityContext extends DrupalSubContextBase {
public function createMultiple(TableNode $table) {
$this
->getContext(EntityContext::class)
->createMultiple('media', $table);
}
public function assertMediaItemPage() {
$this
->assertSession()
->addressMatches('/\\/media\\/[0-9]+$/');
try {
$this
->assertSession()
->statusCodeEquals(200);
} catch (UnsupportedDriverActionException $e) {
}
}
public function fromEmbedCode($bundle, PyStringNode $embed_code) {
$media = Media::create([
'bundle' => $bundle,
'name' => $this
->getRandom()
->string(),
'embed_code' => (string) $embed_code,
'field_media_in_library' => TRUE,
]);
$this
->getContext(EntityContext::class)
->save($media
->setPublished());
}
public function randomImage($n = 1, $name = NULL, $alt = NULL) {
$random = new Random();
$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());
}
}
public function randomDocument($n = 1) {
$random = new Random();
$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());
}
}
}