You are here

public function MediaBridgeMediaTest::testMediaSource in Open Social 10.3.x

Same name and namespace in other branches
  1. 10.0.x modules/custom/social_graphql/tests/src/Kernel/MediaBridgeMediaTest.php \Drupal\Tests\social_graphql\Kernel\MediaBridgeMediaTest::testMediaSource()
  2. 10.1.x modules/custom/social_graphql/tests/src/Kernel/MediaBridgeMediaTest.php \Drupal\Tests\social_graphql\Kernel\MediaBridgeMediaTest::testMediaSource()
  3. 10.2.x modules/custom/social_graphql/tests/src/Kernel/MediaBridgeMediaTest.php \Drupal\Tests\social_graphql\Kernel\MediaBridgeMediaTest::testMediaSource()

Tests that the MediaBridge class works for Media entities.

File

modules/custom/social_graphql/tests/src/Kernel/MediaBridgeMediaTest.php, line 56

Class

MediaBridgeMediaTest
Tests that the MediaBridge works with Media entities.

Namespace

Drupal\Tests\social_graphql\Kernel

Code

public function testMediaSource() {

  // Create an image media type.
  $image_media_type = $this
    ->createMediaType('image');
  $title = $this
    ->randomString();
  $alt = $this
    ->randomString();

  /** @var \Drupal\media\Entity\Media $entity */
  $entity = Media::create([
    'bundle' => $image_media_type
      ->id(),
    'title' => $title,
    'alt' => $alt,
  ]);
  $source_field_name = $entity
    ->getSource()
    ->getConfiguration()['source_field'];
  $entity->{$source_field_name}->target_id = $this->image
    ->id();
  $entity->{$source_field_name}->alt = $alt;
  $entity->{$source_field_name}->title = $title;
  $entity
    ->save();

  // Reload the entity since loading behaviour for fields is slightly
  // different from newly created entities.
  $entity = Media::load($entity
    ->id());

  // Create an instance of our data producer.

  /** @var \Drupal\social_graphql\Plugin\GraphQL\DataProducer\MediaBridge $data_producer */
  $data_producer = $this->dataProducerPluginManager
    ->createInstance('media_bridge');

  // The uuid of the Media entity.
  $resolved_uuid = $this
    ->await($data_producer
    ->resolve($entity, 'id'));
  self::assertEquals($entity
    ->uuid(), $resolved_uuid);

  // The URL should be produced.
  $resolved_url = $this
    ->await($data_producer
    ->resolve($entity, 'url'));
  self::assertEquals($GLOBALS['base_url'] . '/' . $this->siteDirectory . '/files/social_graphql-example-2.jpg', $resolved_url);

  // The title should be produced.
  $resolved_title = $this
    ->await($data_producer
    ->resolve($entity, 'title'));
  self::assertEquals($title, $resolved_title);

  // The alt text should be produced.
  $resolved_alt = $this
    ->await($data_producer
    ->resolve($entity, 'alt'));
  self::assertEquals($alt, $resolved_alt);
}