1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\Tests\graphql_core\Kernel\Images; |
4
|
|
|
|
5
|
|
|
use Drupal\image\Entity\ImageStyle; |
6
|
|
|
use Drupal\Tests\graphql_core\Kernel\GraphQLContentTestBase; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Test file attachments. |
10
|
|
|
* |
11
|
|
|
* @group graphql_image |
12
|
|
|
*/ |
13
|
|
|
class ImageFieldTest extends GraphQLContentTestBase { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
public static $modules = [ |
19
|
|
|
'file', |
20
|
|
|
'image', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
protected function setUp() { |
27
|
|
|
parent::setUp(); |
28
|
|
|
$this->installConfig('image'); |
29
|
|
|
$this->installSchema('file', 'file_usage'); |
30
|
|
|
$this->installEntitySchema('file'); |
31
|
|
|
$this->addField('image', 'image'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Test a simple file field. |
36
|
|
|
*/ |
37
|
|
|
public function testImageField() { |
38
|
|
|
$a = $this->createNode([ |
39
|
|
|
'title' => 'Node A', |
40
|
|
|
'type' => 'test', |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
$a->image->generateSampleItems(1); |
44
|
|
|
|
45
|
|
|
$a->save(); |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
$style = ImageStyle::load('thumbnail'); |
49
|
|
|
|
50
|
|
|
$dimensions = [ |
51
|
|
|
'width' => $a->image[0]->width, |
52
|
|
|
'height' => $a->image[0]->height, |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
$style->transformDimensions($dimensions, $a->image[0]->entity->getFileUri()); |
56
|
|
|
|
57
|
|
|
// TODO: Check cache metadata. |
58
|
|
|
$metadata = $this->defaultCacheMetaData(); |
59
|
|
|
$metadata->addCacheTags([ |
60
|
|
|
'config:field.storage.node.image', |
61
|
|
|
'entity_bundles', |
62
|
|
|
'entity_field_info', |
63
|
|
|
'entity_types', |
64
|
|
|
'file:1', |
65
|
|
|
'node:1', |
66
|
|
|
// TODO: missing image style config cache tags? |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
$this->assertResults($this->getQueryFromFile('image.gql'), [ |
70
|
|
|
'path' => '/node/' . $a->id(), |
71
|
|
|
], [ |
72
|
|
|
'route' => [ |
73
|
|
|
'node' => [ |
74
|
|
|
'image' => [[ |
75
|
|
|
'alt' => $a->image->alt, |
76
|
|
|
'title' => $a->image->title, |
77
|
|
|
'entity' => ['url' => $a->image->entity->url()], |
78
|
|
|
'width' => $a->image[0]->width, |
79
|
|
|
'height' => $a->image[0]->height, |
80
|
|
|
'thumbnailImage' => [ |
81
|
|
|
'url' => $style->buildUrl($a->image->entity->uri->value), |
82
|
|
|
'width' => $dimensions['width'], |
83
|
|
|
'height' => $dimensions['height'], |
84
|
|
|
], |
85
|
|
|
]], |
86
|
|
|
], |
87
|
|
|
], |
88
|
|
|
], $metadata); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|