|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\graphql_core\Kernel\Images; |
| 4 | + |
| 5 | +use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
| 6 | +use Drupal\Core\Entity\Entity\EntityViewDisplay; |
| 7 | +use Drupal\Core\Entity\Entity\EntityViewMode; |
| 8 | +use Drupal\field\Entity\FieldConfig; |
| 9 | +use Drupal\field\Entity\FieldStorageConfig; |
| 10 | +use Drupal\image\Entity\ImageStyle; |
| 11 | +use Drupal\responsive_image\Entity\ResponsiveImageStyle; |
| 12 | +use Drupal\simpletest\ContentTypeCreationTrait; |
| 13 | +use Drupal\simpletest\NodeCreationTrait; |
| 14 | +use Drupal\Tests\graphql\Kernel\GraphQLFileTestBase; |
| 15 | +use Drupal\user\Entity\Role; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test file attachments. |
| 19 | + * |
| 20 | + * @group graphql_image |
| 21 | + */ |
| 22 | +class ImageFieldTest extends GraphQLFileTestBase { |
| 23 | + use NodeCreationTrait; |
| 24 | + use ContentTypeCreationTrait; |
| 25 | + |
| 26 | + /** |
| 27 | + * {@inheritdoc} |
| 28 | + */ |
| 29 | + public static $modules = [ |
| 30 | + 'node', |
| 31 | + 'field', |
| 32 | + 'text', |
| 33 | + 'filter', |
| 34 | + 'file', |
| 35 | + 'image', |
| 36 | + 'graphql_core', |
| 37 | + ]; |
| 38 | + |
| 39 | + /** |
| 40 | + * {@inheritdoc} |
| 41 | + */ |
| 42 | + protected function setUp() { |
| 43 | + parent::setUp(); |
| 44 | + $this->installConfig('node'); |
| 45 | + $this->installConfig('filter'); |
| 46 | + $this->installConfig('image'); |
| 47 | + $this->installEntitySchema('node'); |
| 48 | + $this->installSchema('node', 'node_access'); |
| 49 | + $this->installSchema('file', 'file_usage'); |
| 50 | + $this->installEntitySchema('file'); |
| 51 | + $this->createContentType(['type' => 'test']); |
| 52 | + |
| 53 | + Role::load('anonymous') |
| 54 | + ->grantPermission('access content') |
| 55 | + ->save(); |
| 56 | + |
| 57 | + EntityViewMode::create([ |
| 58 | + 'targetEntityType' => 'node', |
| 59 | + 'id' => "node.graphql", |
| 60 | + ])->save(); |
| 61 | + |
| 62 | + |
| 63 | + FieldStorageConfig::create([ |
| 64 | + 'field_name' => 'image', |
| 65 | + 'type' => 'image', |
| 66 | + 'entity_type' => 'node', |
| 67 | + ])->save(); |
| 68 | + |
| 69 | + FieldConfig::create([ |
| 70 | + 'field_name' => 'image', |
| 71 | + 'entity_type' => 'node', |
| 72 | + 'bundle' => 'test', |
| 73 | + 'label' => 'Image', |
| 74 | + ])->save(); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Test a simple file field. |
| 80 | + */ |
| 81 | + public function testImageField() { |
| 82 | + $a = $this->createNode([ |
| 83 | + 'title' => 'Node A', |
| 84 | + 'type' => 'test', |
| 85 | + ]); |
| 86 | + |
| 87 | + $a->image->generateSampleItems(1); |
| 88 | + |
| 89 | + $a->save(); |
| 90 | + |
| 91 | + $result = $this->executeQueryFile('image.gql', ['path' => '/node/' . $a->id()]); |
| 92 | + $image = $result['data']['route']['node']['image']; |
| 93 | + |
| 94 | + $this->assertEquals($a->image->alt, $image['alt'], 'Alt text correct.'); |
| 95 | + $this->assertEquals($a->image->title, $image['title'], 'Title text correct.'); |
| 96 | + $this->assertEquals($a->image->entity->url(), $image['entity']['url'], 'Retrieve correct image url.'); |
| 97 | + $imageStyle = ImageStyle::load('thumbnail'); |
| 98 | + $styleUrl = $imageStyle->buildUrl($a->image->entity->uri->value); |
| 99 | + $this->assertEquals($styleUrl, $image['thumbnailImage']['url']); |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments