Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\image\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
Chris@0
|
6 use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
Chris@0
|
7 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
8 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
9 use Drupal\image\Entity\ImageStyle;
|
Chris@0
|
10 use Drupal\KernelTests\KernelTestBase;
|
Chris@0
|
11 use Drupal\node\Entity\NodeType;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Tests the integration of ImageStyle with the core.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @group image
|
Chris@0
|
17 */
|
Chris@0
|
18 class ImageStyleIntegrationTest extends KernelTestBase {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * {@inheritdoc}
|
Chris@0
|
22 */
|
Chris@0
|
23 public static $modules = ['image', 'file', 'field', 'system', 'user', 'node'];
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * Tests the dependency between ImageStyle and entity display components.
|
Chris@0
|
27 */
|
Chris@0
|
28 public function testEntityDisplayDependency() {
|
Chris@0
|
29 // Create two image styles.
|
Chris@0
|
30 /** @var \Drupal\image\ImageStyleInterface $style */
|
Chris@0
|
31 $style = ImageStyle::create(['name' => 'main_style']);
|
Chris@0
|
32 $style->save();
|
Chris@0
|
33 /** @var \Drupal\image\ImageStyleInterface $replacement */
|
Chris@0
|
34 $replacement = ImageStyle::create(['name' => 'replacement_style']);
|
Chris@0
|
35 $replacement->save();
|
Chris@0
|
36
|
Chris@0
|
37 // Create a node-type, named 'note'.
|
Chris@0
|
38 $node_type = NodeType::create(['type' => 'note']);
|
Chris@0
|
39 $node_type->save();
|
Chris@0
|
40
|
Chris@0
|
41 // Create an image field and attach it to the 'note' node-type.
|
Chris@0
|
42 FieldStorageConfig::create([
|
Chris@0
|
43 'entity_type' => 'node',
|
Chris@0
|
44 'field_name' => 'sticker',
|
Chris@0
|
45 'type' => 'image',
|
Chris@0
|
46 ])->save();
|
Chris@0
|
47 FieldConfig::create([
|
Chris@0
|
48 'entity_type' => 'node',
|
Chris@0
|
49 'field_name' => 'sticker',
|
Chris@0
|
50 'bundle' => 'note',
|
Chris@0
|
51 ])->save();
|
Chris@0
|
52
|
Chris@0
|
53 // Create the default entity view display and set the 'sticker' field to use
|
Chris@0
|
54 // the 'main_style' images style in formatter.
|
Chris@0
|
55 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
|
Chris@0
|
56 $view_display = EntityViewDisplay::create([
|
Chris@0
|
57 'targetEntityType' => 'node',
|
Chris@0
|
58 'bundle' => 'note',
|
Chris@0
|
59 'mode' => 'default',
|
Chris@0
|
60 'status' => TRUE,
|
Chris@0
|
61 ])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]);
|
Chris@0
|
62 $view_display->save();
|
Chris@0
|
63
|
Chris@0
|
64 // Create the default entity form display and set the 'sticker' field to use
|
Chris@0
|
65 // the 'main_style' images style in the widget.
|
Chris@0
|
66 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
|
Chris@0
|
67 $form_display = EntityFormDisplay::create([
|
Chris@0
|
68 'targetEntityType' => 'node',
|
Chris@0
|
69 'bundle' => 'note',
|
Chris@0
|
70 'mode' => 'default',
|
Chris@0
|
71 'status' => TRUE,
|
Chris@0
|
72 ])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]);
|
Chris@0
|
73 $form_display->save();
|
Chris@0
|
74
|
Chris@0
|
75 // Check that the entity displays exists before dependency removal.
|
Chris@0
|
76 $this->assertNotNull(EntityViewDisplay::load($view_display->id()));
|
Chris@0
|
77 $this->assertNotNull(EntityFormDisplay::load($form_display->id()));
|
Chris@0
|
78
|
Chris@0
|
79 // Delete the 'main_style' image style. Before that, emulate the UI process
|
Chris@0
|
80 // of selecting a replacement style by setting the replacement image style
|
Chris@0
|
81 // ID in the image style storage.
|
Chris@0
|
82 /** @var \Drupal\image\ImageStyleStorageInterface $storage */
|
Chris@0
|
83 $storage = $this->container->get('entity.manager')->getStorage($style->getEntityTypeId());
|
Chris@0
|
84 $storage->setReplacementId('main_style', 'replacement_style');
|
Chris@0
|
85 $style->delete();
|
Chris@0
|
86
|
Chris@0
|
87 // Check that the entity displays exists after dependency removal.
|
Chris@0
|
88 $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
|
Chris@0
|
89 $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
|
Chris@0
|
90 // Check that the 'sticker' formatter component exists in both displays.
|
Chris@0
|
91 $this->assertNotNull($formatter = $view_display->getComponent('sticker'));
|
Chris@0
|
92 $this->assertNotNull($widget = $form_display->getComponent('sticker'));
|
Chris@0
|
93 // Check that both displays are using now 'replacement_style' for images.
|
Chris@0
|
94 $this->assertSame('replacement_style', $formatter['settings']['image_style']);
|
Chris@0
|
95 $this->assertSame('replacement_style', $widget['settings']['preview_image_style']);
|
Chris@0
|
96
|
Chris@0
|
97 // Delete the 'replacement_style' without setting a replacement image style.
|
Chris@0
|
98 $replacement->delete();
|
Chris@0
|
99
|
Chris@0
|
100 // The entity view and form displays exists after dependency removal.
|
Chris@0
|
101 $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
|
Chris@0
|
102 $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
|
Chris@0
|
103 // The 'sticker' formatter component should be hidden in view display.
|
Chris@0
|
104 $this->assertNull($view_display->getComponent('sticker'));
|
Chris@0
|
105 $this->assertTrue($view_display->get('hidden')['sticker']);
|
Chris@0
|
106 // The 'sticker' widget component should be active in form displays, but the
|
Chris@0
|
107 // image preview should be disabled.
|
Chris@0
|
108 $this->assertNotNull($widget = $form_display->getComponent('sticker'));
|
Chris@0
|
109 $this->assertSame('', $widget['settings']['preview_image_style']);
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@17
|
112 /**
|
Chris@17
|
113 * Tests renaming the ImageStyle.
|
Chris@17
|
114 */
|
Chris@17
|
115 public function testEntityDisplayDependencyRename() {
|
Chris@17
|
116 // Create an image style.
|
Chris@17
|
117 /** @var \Drupal\image\ImageStyleInterface $style */
|
Chris@17
|
118 $style = ImageStyle::create(['name' => 'main_style']);
|
Chris@17
|
119 $style->save();
|
Chris@17
|
120
|
Chris@17
|
121 // Create a node-type, named 'note'.
|
Chris@17
|
122 $node_type = NodeType::create(['type' => 'note']);
|
Chris@17
|
123 $node_type->save();
|
Chris@17
|
124
|
Chris@17
|
125 // Create an image field and attach it to the 'note' node-type.
|
Chris@17
|
126 FieldStorageConfig::create([
|
Chris@17
|
127 'entity_type' => 'node',
|
Chris@17
|
128 'field_name' => 'sticker',
|
Chris@17
|
129 'type' => 'image',
|
Chris@17
|
130 ])->save();
|
Chris@17
|
131 FieldConfig::create([
|
Chris@17
|
132 'entity_type' => 'node',
|
Chris@17
|
133 'field_name' => 'sticker',
|
Chris@17
|
134 'bundle' => 'note',
|
Chris@17
|
135 ])->save();
|
Chris@17
|
136
|
Chris@17
|
137 // Create the default entity view display and set the 'sticker' field to use
|
Chris@17
|
138 // the 'main_style' images style in formatter.
|
Chris@17
|
139 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
|
Chris@17
|
140 $view_display = EntityViewDisplay::create([
|
Chris@17
|
141 'targetEntityType' => 'node',
|
Chris@17
|
142 'bundle' => 'note',
|
Chris@17
|
143 'mode' => 'default',
|
Chris@17
|
144 'status' => TRUE,
|
Chris@17
|
145 ])->setComponent('sticker', ['settings' => ['image_style' => 'main_style']]);
|
Chris@17
|
146 $view_display->save();
|
Chris@17
|
147
|
Chris@17
|
148 // Create the default entity form display and set the 'sticker' field to use
|
Chris@17
|
149 // the 'main_style' images style in the widget.
|
Chris@17
|
150 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
|
Chris@17
|
151 $form_display = EntityFormDisplay::create([
|
Chris@17
|
152 'targetEntityType' => 'node',
|
Chris@17
|
153 'bundle' => 'note',
|
Chris@17
|
154 'mode' => 'default',
|
Chris@17
|
155 'status' => TRUE,
|
Chris@17
|
156 ])->setComponent('sticker', ['settings' => ['preview_image_style' => 'main_style']]);
|
Chris@17
|
157 $form_display->save();
|
Chris@17
|
158
|
Chris@17
|
159 // Check that the entity displays exists before dependency renaming.
|
Chris@17
|
160 $this->assertNotNull(EntityViewDisplay::load($view_display->id()));
|
Chris@17
|
161 $this->assertNotNull(EntityFormDisplay::load($form_display->id()));
|
Chris@17
|
162
|
Chris@17
|
163 // Rename the 'main_style' image style.
|
Chris@17
|
164 $style->setName('main_style_renamed');
|
Chris@17
|
165 $style->save();
|
Chris@17
|
166
|
Chris@17
|
167 // Check that the entity displays exists after dependency renaming.
|
Chris@17
|
168 $this->assertNotNull($view_display = EntityViewDisplay::load($view_display->id()));
|
Chris@17
|
169 $this->assertNotNull($form_display = EntityFormDisplay::load($form_display->id()));
|
Chris@17
|
170 // Check that the 'sticker' formatter component exists in both displays.
|
Chris@17
|
171 $this->assertNotNull($formatter = $view_display->getComponent('sticker'));
|
Chris@17
|
172 $this->assertNotNull($widget = $form_display->getComponent('sticker'));
|
Chris@17
|
173 // Check that both displays are using now 'main_style_renamed' for images.
|
Chris@17
|
174 $this->assertSame('main_style_renamed', $formatter['settings']['image_style']);
|
Chris@17
|
175 $this->assertSame('main_style_renamed', $widget['settings']['preview_image_style']);
|
Chris@17
|
176 }
|
Chris@17
|
177
|
Chris@0
|
178 }
|