MediaBridgeMediaTest.php in Open Social 10.0.x
File
modules/custom/social_graphql/tests/src/Kernel/MediaBridgeMediaTest.php
View source
<?php
namespace Drupal\Tests\social_graphql\Kernel;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\Tests\media\Kernel\MediaKernelTestBase;
use GraphQL\Executor\Promise\Adapter\SyncPromise;
class MediaBridgeMediaTest extends MediaKernelTestBase {
public static $modules = [
'graphql',
'social_graphql',
];
protected $dataProducerPluginManager;
protected function setUp() {
parent::setUp();
\Drupal::service('file_system')
->copy($this->root . '/core/misc/druplicon.png', 'public://social_graphql-example-2.jpg');
$this->image = File::create([
'uri' => 'public://social_graphql-example-2.jpg',
]);
$this->image
->save();
$this->dataProducerPluginManager = $this->container
->get('plugin.manager.graphql.data_producer');
}
public function testMediaSource() {
$image_media_type = $this
->createMediaType('image');
$title = $this
->randomString();
$alt = $this
->randomString();
$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();
$entity = Media::load($entity
->id());
$data_producer = $this->dataProducerPluginManager
->createInstance('media_bridge');
$resolved_uuid = $this
->await($data_producer
->resolve($entity, 'id'));
self::assertEquals($entity
->uuid(), $resolved_uuid);
$resolved_url = $this
->await($data_producer
->resolve($entity, 'url'));
self::assertEquals($GLOBALS['base_url'] . '/' . $this->siteDirectory . '/files/social_graphql-example-2.jpg', $resolved_url);
$resolved_title = $this
->await($data_producer
->resolve($entity, 'title'));
self::assertEquals($title, $resolved_title);
$resolved_alt = $this
->await($data_producer
->resolve($entity, 'alt'));
self::assertEquals($alt, $resolved_alt);
}
protected function await($value) {
if ($value instanceof SyncPromise) {
$value::runQueue();
return $value->result;
}
return $value;
}
}