public function MediaBridgeFileTest::testImageField in Open Social 10.0.x
Same name and namespace in other branches
- 10.3.x modules/custom/social_graphql/tests/src/Kernel/MediaBridgeFileTest.php \Drupal\Tests\social_graphql\Kernel\MediaBridgeFileTest::testImageField()
- 10.1.x modules/custom/social_graphql/tests/src/Kernel/MediaBridgeFileTest.php \Drupal\Tests\social_graphql\Kernel\MediaBridgeFileTest::testImageField()
- 10.2.x modules/custom/social_graphql/tests/src/Kernel/MediaBridgeFileTest.php \Drupal\Tests\social_graphql\Kernel\MediaBridgeFileTest::testImageField()
Tests that the MediaBridge class works for Image fields.
File
- modules/
custom/ social_graphql/ tests/ src/ Kernel/ MediaBridgeFileTest.php, line 96
Class
- MediaBridgeFileTest
- Tests that the MediaBridge works with FileItems.
Namespace
Drupal\Tests\social_graphql\KernelCode
public function testImageField() {
// Create an instance of our data producer.
/** @var \Drupal\social_graphql\Plugin\GraphQL\DataProducer\MediaBridge $data_producer */
$data_producer = $this->dataProducerPluginManager
->createInstance('media_bridge');
// Create a test entity from core that contains an image field.
$entity = EntityTest::create();
$entity->image_test->target_id = $this->image
->id();
$entity->image_test->alt = $alt = $this
->randomMachineName();
$entity->image_test->title = $title = $this
->randomMachineName();
$entity->name->value = $this
->randomMachineName();
$entity
->save();
// Reload the entity since loading behaviour for fields is slightly
// different from newly created entities.
$entity = EntityTest::load($entity
->id());
$file_field = $entity
->get('image_test')
->first();
// The uuid of the image.
$resolved_uuid = $this
->await($data_producer
->resolve($file_field, 'id'));
self::assertEquals($this->image
->uuid(), $resolved_uuid);
// The URL should be produced.
$resolved_url = $this
->await($data_producer
->resolve($file_field, 'url'));
self::assertEquals($GLOBALS['base_url'] . '/' . $this->siteDirectory . '/files/social_graphql-example.jpg', $resolved_url);
// The title should be produced.
$resolved_title = $this
->await($data_producer
->resolve($file_field, 'title'));
self::assertEquals($title, $resolved_title);
// The alt text should be produced.
$resolved_alt = $this
->await($data_producer
->resolve($file_field, 'alt'));
self::assertEquals($alt, $resolved_alt);
}