You are here

public function ThunderTestTrait::getMediaByName in Thunder 6.1.x

Same name and namespace in other branches
  1. 6.2.x tests/src/Traits/ThunderTestTrait.php \Drupal\Tests\thunder\Traits\ThunderTestTrait::getMediaByName()

Get a media item from the database based on its name.

Parameters

string|\Drupal\Component\Render\MarkupInterface $name: A media name, usually generated by $this->randomMachineName().

bool $reset: (optional) Whether to reset the entity cache.

Return value

\Drupal\media\MediaInterface|bool A media entity matching $name.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

2 calls to ThunderTestTrait::getMediaByName()
ArticleCreationTest::testCreateArticle in tests/src/FunctionalJavascript/ArticleCreationTest.php
Test Creation of Article.
MediaGalleryModifyTest::testAddRemove in tests/src/FunctionalJavascript/MediaGalleryModifyTest.php
Test add/remove Images in Gallery.

File

tests/src/Traits/ThunderTestTrait.php, line 231

Class

ThunderTestTrait
Use this trait to reuse an existing database.

Namespace

Drupal\Tests\thunder\Traits

Code

public function getMediaByName($name, $reset = FALSE) {
  if ($reset) {
    \Drupal::entityTypeManager()
      ->getStorage('media')
      ->resetCache();
  }

  // Cast MarkupInterface objects to string.
  $name = (string) $name;
  $medias = \Drupal::entityTypeManager()
    ->getStorage('media')
    ->loadByProperties([
    'name' => $name,
  ]);
  return reset($medias);
}