class MediaEntityContext in Lightning Media 8.2
Same name and namespace in other branches
- 8 tests/contexts/MediaEntityContext.behat.inc \Acquia\LightningExtension\Context\MediaEntityContext
- 8.3 tests/contexts/MediaEntityContext.behat.inc \Acquia\LightningExtension\Context\MediaEntityContext
Contains step definitions for interacting with media entities.
@internal This class is part of Lightning's internal testing code. It is not an API and should not be extended. This class will be marked final, and all protected members will be made private, in Lightning Media 3.x.
Hierarchy
- class \Acquia\LightningExtension\Context\MediaEntityContext extends \Drupal\DrupalExtension\Context\DrupalSubContextBase
Expanded class hierarchy of MediaEntityContext
File
- tests/
contexts/ MediaEntityContext.behat.inc, line 21
Namespace
Acquia\LightningExtension\ContextView source
class MediaEntityContext extends DrupalSubContextBase {
/**
* Creates a set of media items from tabular data.
*
* @param \Behat\Gherkin\Node\TableNode $table
* The table of entity data.
*
* @Given media items:
*/
public function createMultiple(TableNode $table) {
$this
->getContext(EntityContext::class)
->createMultiple('media', $table);
}
/**
* Asserts that we are visiting a media entity.
*
* @Then I should be visiting a media item
*/
public function assertMediaItemPage() {
$this
->assertSession()
->addressMatches('#^/media/([\\w-]+/)?\\d+$#');
try {
$this
->assertSession()
->statusCodeEquals(200);
} catch (UnsupportedDriverActionException $e) {
// This isn't a critically important assertion, so don't worry about it.
}
}
/**
* Creates a media item from an embed code.
*
* @param string $bundle
* The media bundle ID.
* @param \Behat\Gherkin\Node\PyStringNode $embed_code
* The embed code.
*
* @Given :bundle media from embed code:
*
* @When I create :bundle media from embed code:
*/
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());
}
/**
* Generates random images, saved as media items.
*
* @param int $n
* (optional) How many to generate. Defaults to 1.
* @param string $name
* (optional) The label of the media item wrapping the image. Defaults to
* a random string.
* @param string $alt
* (optional) The alt text for the image.
*
* @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
*/
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());
}
}
/**
* Generates random text files, saved as media items.
*
* @param int $n
* (optional) How many to generate. Defaults to 1.
*
* @Given a random document
* @Given :count random documents
*/
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());
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MediaEntityContext:: |
public | function | Asserts that we are visiting a media entity. | |
MediaEntityContext:: |
public | function | Creates a set of media items from tabular data. | |
MediaEntityContext:: |
public | function | Creates a media item from an embed code. | |
MediaEntityContext:: |
public | function | Generates random text files, saved as media items. | |
MediaEntityContext:: |
public | function | Generates random images, saved as media items. |