comparison core/modules/media/tests/src/Kernel/MediaSourceTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
14 * @group media 14 * @group media
15 */ 15 */
16 class MediaSourceTest extends MediaKernelTestBase { 16 class MediaSourceTest extends MediaKernelTestBase {
17 17
18 /** 18 /**
19 * Tests that metadata is correctly mapped irrespective of how media is saved.
20 */
21 public function testSave() {
22 $field_storage = FieldStorageConfig::create([
23 'entity_type' => 'media',
24 'field_name' => 'field_to_map_to',
25 'type' => 'string',
26 ]);
27 $field_storage->save();
28
29 FieldConfig::create([
30 'field_storage' => $field_storage,
31 'bundle' => $this->testMediaType->id(),
32 'label' => 'Field to map to',
33 ])->save();
34
35 // Set an arbitrary metadata value to be mapped.
36 $this->container->get('state')
37 ->set('media_source_test_attributes', [
38 'attribute_to_map' => [
39 'title' => 'Attribute to map',
40 'value' => 'Snowball',
41 ],
42 'thumbnail_uri' => [
43 'value' => 'public://TheSisko.png',
44 ],
45 ]);
46 $this->testMediaType->setFieldMap([
47 'attribute_to_map' => 'field_to_map_to',
48 ])->save();
49
50 /** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
51 $storage = $this->container->get('entity_type.manager')
52 ->getStorage('media');
53
54 /** @var \Drupal\media\MediaInterface $a */
55 $a = $storage->create([
56 'bundle' => $this->testMediaType->id(),
57 ]);
58 /** @var \Drupal\media\MediaInterface $b */
59 $b = $storage->create([
60 'bundle' => $this->testMediaType->id(),
61 ]);
62
63 // Set a random source value on both items.
64 $a->set($a->getSource()->getSourceFieldDefinition($a->bundle->entity)->getName(), $this->randomString());
65 $b->set($b->getSource()->getSourceFieldDefinition($b->bundle->entity)->getName(), $this->randomString());
66
67 $a->save();
68 $storage->save($b);
69
70 // Assert that the default name was mapped into the name field for both
71 // media items.
72 $this->assertFalse($a->get('name')->isEmpty());
73 $this->assertFalse($b->get('name')->isEmpty());
74
75 // Assert that arbitrary metadata was mapped correctly.
76 $this->assertFalse($a->get('field_to_map_to')->isEmpty());
77 $this->assertFalse($b->get('field_to_map_to')->isEmpty());
78
79 // Assert that the thumbnail was mapped correctly from the source.
80 $this->assertSame('public://TheSisko.png', $a->thumbnail->entity->getFileUri());
81 $this->assertSame('public://TheSisko.png', $b->thumbnail->entity->getFileUri());
82 }
83
84 /**
19 * Tests default media name functionality. 85 * Tests default media name functionality.
20 */ 86 */
21 public function testDefaultName() { 87 public function testDefaultName() {
22 // Make sure that the default name is set if not provided by the user. 88 // Make sure that the default name is set if not provided by the user.
23 /** @var \Drupal\media\MediaInterface $media */ 89 /** @var \Drupal\media\MediaInterface $media */
24 $media = Media::create(['bundle' => $this->testMediaType->id()]); 90 $media = Media::create(['bundle' => $this->testMediaType->id()]);
25 $media_source = $media->getSource(); 91 $media_source = $media->getSource();
26 $this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.'); 92 $this->assertSame('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
27 $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.'); 93 $this->assertSame('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
28 $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->getName(), 'Default name was not used correctly by getName().'); 94 $this->assertSame('media:' . $media->bundle() . ':' . $media->uuid(), $media->getName(), 'Default name was not used correctly by getName().');
29 $this->assertEquals($media->getName(), $media->label(), 'Default name and label are not the same.'); 95 $this->assertSame($media->getName(), $media->label(), 'Default name and label are not the same.');
30 $media->save(); 96 $media->save();
31 $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->getName(), 'Default name was not saved correctly.'); 97 $this->assertSame('media:' . $media->bundle() . ':' . $media->uuid(), $media->getName(), 'Default name was not saved correctly.');
32 $this->assertEquals($media->getName(), $media->label(), 'The label changed during save.'); 98 $this->assertSame($media->getName(), $media->label(), 'The label changed during save.');
33 99
34 // Make sure that the user-supplied name is used. 100 // Make sure that the user-supplied name is used.
35 /** @var \Drupal\media\MediaInterface $media */ 101 /** @var \Drupal\media\MediaInterface $media */
36 $name = 'User-supplied name'; 102 $name = 'User-supplied name';
37 $media = Media::create([ 103 $media = Media::create([
38 'bundle' => $this->testMediaType->id(), 104 'bundle' => $this->testMediaType->id(),
39 'name' => $name, 105 'name' => $name,
40 ]); 106 ]);
41 $media_source = $media->getSource(); 107 $media_source = $media->getSource();
42 $this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.'); 108 $this->assertSame('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
43 $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.'); 109 $this->assertSame('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
44 $media->save(); 110 $media->save();
45 $this->assertEquals($name, $media->getName(), 'User-supplied name was not set correctly.'); 111 $this->assertSame($name, $media->getName(), 'User-supplied name was not set correctly.');
46 $this->assertEquals($media->getName(), $media->label(), 'The user-supplied name does not match the label.'); 112 $this->assertSame($media->getName(), $media->label(), 'The user-supplied name does not match the label.');
47 113
48 // Change the default name attribute and see if it is used to set the name. 114 // Change the default name attribute and see if it is used to set the name.
49 $name = 'Old Major'; 115 $name = 'Old Major';
50 \Drupal::state()->set('media_source_test_attributes', ['alternative_name' => ['title' => 'Alternative name', 'value' => $name]]); 116 \Drupal::state()->set('media_source_test_attributes', ['alternative_name' => ['title' => 'Alternative name', 'value' => $name]]);
51 \Drupal::state()->set('media_source_test_definition', ['default_name_metadata_attribute' => 'alternative_name']); 117 \Drupal::state()->set('media_source_test_definition', ['default_name_metadata_attribute' => 'alternative_name']);
52 /** @var \Drupal\media\MediaInterface $media */ 118 /** @var \Drupal\media\MediaInterface $media */
53 $media = Media::create(['bundle' => $this->testMediaType->id()]); 119 $media = Media::create(['bundle' => $this->testMediaType->id()]);
54 $media_source = $media->getSource(); 120 $media_source = $media->getSource();
55 $this->assertEquals('alternative_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Correct metadata attribute is not used for the default name.'); 121 $this->assertSame('alternative_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Correct metadata attribute is not used for the default name.');
56 $this->assertEquals($name, $media_source->getMetadata($media, 'alternative_name'), 'Value of the default name metadata attribute does not look correct.'); 122 $this->assertSame($name, $media_source->getMetadata($media, 'alternative_name'), 'Value of the default name metadata attribute does not look correct.');
57 $media->save(); 123 $media->save();
58 $this->assertEquals($name, $media->getName(), 'Default name was not set correctly.'); 124 $this->assertSame($name, $media->getName(), 'Default name was not set correctly.');
59 $this->assertEquals($media->getName(), $media->label(), 'The default name does not match the label.'); 125 $this->assertSame($media->getName(), $media->label(), 'The default name does not match the label.');
60 } 126 }
61 127
62 /** 128 /**
63 * Tests metadata mapping functionality. 129 * Tests metadata mapping functionality.
64 */ 130 */
107 $media = Media::create([ 173 $media = Media::create([
108 'bundle' => $this->testMediaType->id(), 174 'bundle' => $this->testMediaType->id(),
109 'field_media_test' => 'some_value', 175 'field_media_test' => 'some_value',
110 ]); 176 ]);
111 $media_source = $media->getSource(); 177 $media_source = $media->getSource();
112 $this->assertEquals('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.'); 178 $this->assertSame('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
113 $media->save(); 179 $media->save();
114 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.'); 180 $this->assertSame('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
115 181
116 // Change the metadata attribute value and re-save the entity. Field value 182 // Change the metadata attribute value and re-save the entity. Field value
117 // should stay the same. 183 // should stay the same.
118 \Drupal::state()->set('media_source_test_attributes', [ 184 \Drupal::state()->set('media_source_test_attributes', [
119 $attribute_name => ['title' => 'Attribute to map', 'value' => 'Pinkeye'], 185 $attribute_name => ['title' => 'Attribute to map', 'value' => 'Pinkeye'],
120 ]); 186 ]);
121 $this->assertEquals('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.'); 187 $this->assertSame('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
122 $media->save(); 188 $media->save();
123 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.'); 189 $this->assertSame('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
124 190
125 // Now change the value of the source field and make sure that the mapped 191 // Now change the value of the source field and make sure that the mapped
126 // values update too. 192 // values update too.
127 $this->assertEquals('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.'); 193 $this->assertSame('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
128 $media->set('field_media_test', 'some_new_value'); 194 $media->set('field_media_test', 'some_new_value');
129 $media->save(); 195 $media->save();
130 $this->assertEquals('Pinkeye', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.'); 196 $this->assertSame('Pinkeye', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
131 197
132 // Remove the value of the mapped field and make sure that it is re-mapped 198 // Remove the value of the mapped field and make sure that it is re-mapped
133 // on save. 199 // on save.
134 \Drupal::state()->set('media_source_test_attributes', [ 200 \Drupal::state()->set('media_source_test_attributes', [
135 $attribute_name => ['title' => 'Attribute to map', 'value' => 'Snowball'], 201 $attribute_name => ['title' => 'Attribute to map', 'value' => 'Snowball'],
136 ]); 202 ]);
137 $media->{$field_name}->value = NULL; 203 $media->{$field_name}->value = NULL;
138 $this->assertEquals('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.'); 204 $this->assertSame('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
139 $media->save(); 205 $media->save();
140 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.'); 206 $this->assertSame('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
141 } 207 }
142 208
143 /** 209 /**
144 * Tests the getSourceFieldValue() method. 210 * Tests the getSourceFieldValue() method.
145 */ 211 */
161 file_put_contents('public://thumbnail1.jpg', ''); 227 file_put_contents('public://thumbnail1.jpg', '');
162 file_put_contents('public://thumbnail2.jpg', ''); 228 file_put_contents('public://thumbnail2.jpg', '');
163 229
164 // Save a media item and make sure thumbnail was added. 230 // Save a media item and make sure thumbnail was added.
165 \Drupal::state()->set('media_source_test_attributes', [ 231 \Drupal::state()->set('media_source_test_attributes', [
166 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'], 232 'thumbnail_uri' => ['value' => 'public://thumbnail1.jpg'],
167 ]); 233 ]);
168 /** @var \Drupal\media\MediaInterface $media */ 234 /** @var \Drupal\media\MediaInterface $media */
169 $media = Media::create([ 235 $media = Media::create([
170 'bundle' => $this->testMediaType->id(), 236 'bundle' => $this->testMediaType->id(),
171 'name' => 'Mr. Jones', 237 'name' => 'Mr. Jones',
172 'field_media_test' => 'some_value', 238 'field_media_test' => 'some_value',
173 ]); 239 ]);
174 $media_source = $media->getSource(); 240 $media_source = $media->getSource();
175 $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.'); 241 $this->assertSame('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
176 $media->save(); 242 $media->save();
177 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not added to the media item.'); 243 $this->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not added to the media item.');
178 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); 244 // We expect the title not to be present on the Thumbnail.
179 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); 245 $this->assertEmpty($media->thumbnail->title);
246 $this->assertSame('', $media->thumbnail->alt);
180 247
181 // Now change the metadata attribute and make sure that the thumbnail stays 248 // Now change the metadata attribute and make sure that the thumbnail stays
182 // the same. 249 // the same.
183 \Drupal::state()->set('media_source_test_attributes', [ 250 \Drupal::state()->set('media_source_test_attributes', [
184 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail2.jpg'], 251 'thumbnail_uri' => ['value' => 'public://thumbnail2.jpg'],
185 ]); 252 ]);
186 $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.'); 253 $this->assertSame('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
187 $media->save(); 254 $media->save();
188 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not preserved.'); 255 $this->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not preserved.');
189 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); 256 $this->assertEmpty($media->thumbnail->title);
190 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); 257 $this->assertSame('', $media->thumbnail->alt);
191 258
192 // Remove the thumbnail and make sure that it is auto-updated on save. 259 // Remove the thumbnail and make sure that it is auto-updated on save.
193 $media->thumbnail->target_id = NULL; 260 $media->thumbnail->target_id = NULL;
194 $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.'); 261 $this->assertSame('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
195 $media->save(); 262 $media->save();
196 $this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media item.'); 263 $this->assertSame('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media item.');
197 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); 264 $this->assertEmpty($media->thumbnail->title);
198 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); 265 $this->assertSame('', $media->thumbnail->alt);
199 266
200 // Change the metadata attribute again, change the source field value too 267 // Change the metadata attribute again, change the source field value too
201 // and make sure that the thumbnail updates. 268 // and make sure that the thumbnail updates.
202 \Drupal::state()->set('media_source_test_attributes', [ 269 \Drupal::state()->set('media_source_test_attributes', [
203 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'], 270 'thumbnail_uri' => ['value' => 'public://thumbnail1.jpg'],
204 ]); 271 ]);
205 $media->field_media_test->value = 'some_new_value'; 272 $media->field_media_test->value = 'some_new_value';
206 $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.'); 273 $this->assertSame('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
207 $media->save(); 274 $media->save();
208 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media item.'); 275 $this->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media item.');
209 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); 276 $this->assertEmpty($media->thumbnail->title);
210 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); 277 $this->assertSame('', $media->thumbnail->alt);
211 278
212 // Change the thumbnail metadata attribute and make sure that the thumbnail 279 // Change the thumbnail metadata attribute and make sure that the thumbnail
213 // is set correctly. 280 // is set correctly.
214 \Drupal::state()->set('media_source_test_attributes', [ 281 \Drupal::state()->set('media_source_test_attributes', [
215 'thumbnail_uri' => ['title' => 'Should not be used', 'value' => 'public://thumbnail1.jpg'], 282 'thumbnail_uri' => ['value' => 'public://thumbnail1.jpg'],
216 'alternative_thumbnail_uri' => ['title' => 'Should be used', 'value' => 'public://thumbnail2.jpg'], 283 'alternative_thumbnail_uri' => ['value' => 'public://thumbnail2.jpg'],
217 ]); 284 ]);
218 \Drupal::state()->set('media_source_test_definition', ['thumbnail_uri_metadata_attribute' => 'alternative_thumbnail_uri']); 285 \Drupal::state()->set('media_source_test_definition', ['thumbnail_uri_metadata_attribute' => 'alternative_thumbnail_uri']);
219 $media = Media::create([ 286 $media = Media::create([
220 'bundle' => $this->testMediaType->id(), 287 'bundle' => $this->testMediaType->id(),
221 'name' => 'Mr. Jones', 288 'name' => 'Mr. Jones',
222 'field_media_test' => 'some_value', 289 'field_media_test' => 'some_value',
223 ]); 290 ]);
224 $media_source = $media->getSource(); 291 $media_source = $media->getSource();
225 $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.'); 292 $this->assertSame('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
226 $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'alternative_thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.'); 293 $this->assertSame('public://thumbnail2.jpg', $media_source->getMetadata($media, 'alternative_thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
227 $media->save(); 294 $media->save();
228 $this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'Correct metadata attribute was not used for the thumbnail.'); 295 $this->assertSame('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'Correct metadata attribute was not used for the thumbnail.');
229 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); 296 $this->assertEmpty($media->thumbnail->title);
230 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); 297 $this->assertSame('', $media->thumbnail->alt);
231 298
232 // Enable queued thumbnails and make sure that the entity gets the default 299 // Enable queued thumbnails and make sure that the entity gets the default
233 // thumbnail initially. 300 // thumbnail initially.
234 \Drupal::state()->set('media_source_test_definition', []); 301 \Drupal::state()->set('media_source_test_definition', []);
235 \Drupal::state()->set('media_source_test_attributes', [ 302 \Drupal::state()->set('media_source_test_attributes', [
236 'thumbnail_uri' => ['title' => 'Should not be used', 'value' => 'public://thumbnail1.jpg'], 303 'thumbnail_uri' => ['value' => 'public://thumbnail1.jpg'],
237 ]); 304 ]);
238 $this->testMediaType->setQueueThumbnailDownloadsStatus(TRUE)->save(); 305 $this->testMediaType->setQueueThumbnailDownloadsStatus(TRUE)->save();
239 $media = Media::create([ 306 $media = Media::create([
240 'bundle' => $this->testMediaType->id(), 307 'bundle' => $this->testMediaType->id(),
241 'name' => 'Mr. Jones', 308 'name' => 'Mr. Jones',
242 'field_media_test' => 'some_value', 309 'field_media_test' => 'some_value',
243 ]); 310 ]);
244 $this->assertEquals('public://thumbnail1.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.'); 311 $this->assertSame('public://thumbnail1.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
245 $media->save(); 312 $media->save();
246 $this->assertEquals('public://media-icons/generic/generic.png', $media->thumbnail->entity->getFileUri(), 'Default thumbnail was not set initially.'); 313 $this->assertSame('public://media-icons/generic/generic.png', $media->thumbnail->entity->getFileUri(), 'Default thumbnail was not set initially.');
247 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); 314 $this->assertEmpty($media->thumbnail->title);
248 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); 315 $this->assertSame('', $media->thumbnail->alt);
249 316
250 // Process the queue item and make sure that the thumbnail was updated too. 317 // Process the queue item and make sure that the thumbnail was updated too.
251 $queue_name = 'media_entity_thumbnail'; 318 $queue_name = 'media_entity_thumbnail';
252 /** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */ 319 /** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */
253 $queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance($queue_name); 320 $queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance($queue_name);
254 $queue = \Drupal::queue($queue_name); 321 $queue = \Drupal::queue($queue_name);
255 $this->assertEquals(1, $queue->numberOfItems(), 'Item was not added to the queue.'); 322 $this->assertSame(1, $queue->numberOfItems(), 'Item was not added to the queue.');
256 323
257 $item = $queue->claimItem(); 324 $item = $queue->claimItem();
258 $this->assertEquals($media->id(), $item->data['id'], 'Queue item that was created does not belong to the correct entity.'); 325 $this->assertSame($media->id(), $item->data['id'], 'Queue item that was created does not belong to the correct entity.');
259 326
260 $queue_worker->processItem($item->data); 327 $queue_worker->processItem($item->data);
261 $queue->deleteItem($item); 328 $queue->deleteItem($item);
262 $this->assertEquals(0, $queue->numberOfItems(), 'Item was not removed from the queue.'); 329 $this->assertSame(0, $queue->numberOfItems(), 'Item was not removed from the queue.');
263 330
264 $media = Media::load($media->id()); 331 $media = Media::load($media->id());
265 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not updated by the queue.'); 332 $this->assertSame('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not updated by the queue.');
266 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); 333 $this->assertEmpty($media->thumbnail->title);
267 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); 334 $this->assertSame('', $media->thumbnail->alt);
268 335
269 // Set alt and title metadata attributes and make sure they are used for the 336 // Set the alt metadata attribute and make sure it's used for the thumbnail.
270 // thumbnail.
271 \Drupal::state()->set('media_source_test_definition', [ 337 \Drupal::state()->set('media_source_test_definition', [
272 'thumbnail_alt_metadata_attribute' => 'alt', 338 'thumbnail_alt_metadata_attribute' => 'alt',
273 'thumbnail_title_metadata_attribute' => 'title', 339 ]);
274 ]); 340 \Drupal::state()->set('media_source_test_attributes', [
275 \Drupal::state()->set('media_source_test_attributes', [ 341 'alt' => ['value' => 'This will be alt.'],
276 'alt' => ['title' => 'Alt text', 'value' => 'This will be alt.'],
277 'title' => ['title' => 'Title text', 'value' => 'This will be title.'],
278 ]); 342 ]);
279 $media = Media::create([ 343 $media = Media::create([
280 'bundle' => $this->testMediaType->id(), 344 'bundle' => $this->testMediaType->id(),
281 'name' => 'Boxer', 345 'name' => 'Boxer',
282 'field_media_test' => 'some_value', 346 'field_media_test' => 'some_value',
283 ]); 347 ]);
284 $media->save(); 348 $media->save();
285 $this->assertEquals('Boxer', $media->getName(), 'Correct name was not set on the media item.'); 349 $this->assertSame('Boxer', $media->getName(), 'Correct name was not set on the media item.');
286 $this->assertEquals('This will be title.', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); 350 $this->assertEmpty($media->thumbnail->title);
287 $this->assertEquals('This will be alt.', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); 351 $this->assertSame('This will be alt.', $media->thumbnail->alt);
288 } 352 }
289 353
290 /** 354 /**
291 * Tests the media item constraints functionality. 355 * Tests the media item constraints functionality.
292 */ 356 */
371 $field_storage = $field->getFieldStorageDefinition(); 435 $field_storage = $field->getFieldStorageDefinition();
372 436
373 // Test field storage. 437 // Test field storage.
374 $this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.'); 438 $this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.');
375 $this->assertFalse($field_storage->isLocked(), 'Field storage is not locked.'); 439 $this->assertFalse($field_storage->isLocked(), 'Field storage is not locked.');
376 $this->assertEquals('string', $field_storage->getType(), 'Field is not of correct type.'); 440 $this->assertSame('string', $field_storage->getType(), 'Field is not of correct type.');
377 $this->assertEquals('field_media_test_1', $field_storage->getName(), 'Incorrect field name is used.'); 441 $this->assertSame('field_media_test_1', $field_storage->getName(), 'Incorrect field name is used.');
378 $this->assertEquals('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.'); 442 $this->assertSame('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.');
379 443
380 // Test field. 444 // Test field.
381 $this->assertTrue($field->isNew(), 'Field is saved automatically.'); 445 $this->assertTrue($field->isNew(), 'Field is saved automatically.');
382 $this->assertEquals('field_media_test_1', $field->getName(), 'Incorrect field name is used.'); 446 $this->assertSame('field_media_test_1', $field->getName(), 'Incorrect field name is used.');
383 $this->assertEquals('string', $field->getType(), 'Field is of incorrect type.'); 447 $this->assertSame('string', $field->getType(), 'Field is of incorrect type.');
384 $this->assertTrue($field->isRequired(), 'Field is not required.'); 448 $this->assertTrue($field->isRequired(), 'Field is not required.');
385 $this->assertEquals('Test source', $field->label(), 'Incorrect label is used.'); 449 $this->assertEquals('Test source', $field->label(), 'Incorrect label is used.');
386 $this->assertEquals('test_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.'); 450 $this->assertSame('test_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.');
387 451
388 // Fields should be automatically saved only when creating the media type 452 // Fields should be automatically saved only when creating the media type
389 // using the media type creation form. Make sure that they are not saved 453 // using the media type creation form. Make sure that they are not saved
390 // when creating a media type programmatically. 454 // when creating a media type programmatically.
391 // Drupal\Tests\media\FunctionalJavascript\MediaTypeCreationTest is testing 455 // Drupal\Tests\media\FunctionalJavascript\MediaTypeCreationTest is testing
406 $field_storage = $field->getFieldStorageDefinition(); 470 $field_storage = $field->getFieldStorageDefinition();
407 471
408 // Test field storage. 472 // Test field storage.
409 $this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.'); 473 $this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.');
410 $this->assertFalse($field_storage->isLocked(), 'Field storage is not locked.'); 474 $this->assertFalse($field_storage->isLocked(), 'Field storage is not locked.');
411 $this->assertEquals('string_long', $field_storage->getType(), 'Field is of incorrect type.'); 475 $this->assertSame('string_long', $field_storage->getType(), 'Field is of incorrect type.');
412 $this->assertEquals('field_media_test_constraints_1', $field_storage->getName(), 'Incorrect field name is used.'); 476 $this->assertSame('field_media_test_constraints_1', $field_storage->getName(), 'Incorrect field name is used.');
413 $this->assertEquals('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.'); 477 $this->assertSame('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.');
414 478
415 // Test field. 479 // Test field.
416 $this->assertTrue($field->isNew(), 'Field is saved automatically.'); 480 $this->assertTrue($field->isNew(), 'Field is saved automatically.');
417 $this->assertEquals('field_media_test_constraints_1', $field->getName(), 'Incorrect field name is used.'); 481 $this->assertSame('field_media_test_constraints_1', $field->getName(), 'Incorrect field name is used.');
418 $this->assertEquals('string_long', $field->getType(), 'Field is of incorrect type.'); 482 $this->assertSame('string_long', $field->getType(), 'Field is of incorrect type.');
419 $this->assertTrue($field->isRequired(), 'Field is not required.'); 483 $this->assertTrue($field->isRequired(), 'Field is not required.');
420 $this->assertEquals('Test source with constraints', $field->label(), 'Incorrect label is used.'); 484 $this->assertEquals('Test source with constraints', $field->label(), 'Incorrect label is used.');
421 $this->assertEquals('test_constraints_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.'); 485 $this->assertSame('test_constraints_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.');
422 } 486 }
423 487
424 /** 488 /**
425 * Tests configuration form submit handler on the base media source plugin. 489 * Tests configuration form submit handler on the base media source plugin.
426 */ 490 */
432 $form_state->setValues(['test_config_value' => 'Somewhere over the rainbow.']); 496 $form_state->setValues(['test_config_value' => 'Somewhere over the rainbow.']);
433 497
434 /** @var \Drupal\media\MediaSourceInterface $source */ 498 /** @var \Drupal\media\MediaSourceInterface $source */
435 $source = $manager->createInstance('test', []); 499 $source = $manager->createInstance('test', []);
436 $source->submitConfigurationForm($form, $form_state); 500 $source->submitConfigurationForm($form, $form_state);
437 $expected = ['test_config_value' => 'Somewhere over the rainbow.', 'source_field' => 'field_media_test_1']; 501 $expected = ['source_field' => 'field_media_test_1', 'test_config_value' => 'Somewhere over the rainbow.'];
438 $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.'); 502 $this->assertSame($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
439 503
440 // Try to save a NULL value. 504 // Try to save a NULL value.
441 $form_state->setValue('test_config_value', NULL); 505 $form_state->setValue('test_config_value', NULL);
442 $source->submitConfigurationForm($form, $form_state); 506 $source->submitConfigurationForm($form, $form_state);
443 $expected['test_config_value'] = NULL; 507 $expected['test_config_value'] = NULL;
444 $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.'); 508 $this->assertSame($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
445 509
446 // Make sure that the config keys are determined correctly even if the 510 // Make sure that the config keys are determined correctly even if the
447 // existing value is NULL. 511 // existing value is NULL.
448 $form_state->setValue('test_config_value', 'Somewhere over the rainbow.'); 512 $form_state->setValue('test_config_value', 'Somewhere over the rainbow.');
449 $source->submitConfigurationForm($form, $form_state); 513 $source->submitConfigurationForm($form, $form_state);
450 $expected['test_config_value'] = 'Somewhere over the rainbow.'; 514 $expected['test_config_value'] = 'Somewhere over the rainbow.';
451 $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.'); 515 $this->assertSame($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
452 516
453 // Make sure that a non-relevant value will be skipped. 517 // Make sure that a non-relevant value will be skipped.
454 $form_state->setValue('not_relevant', 'Should not be saved in the plugin.'); 518 $form_state->setValue('not_relevant', 'Should not be saved in the plugin.');
455 $source->submitConfigurationForm($form, $form_state); 519 $source->submitConfigurationForm($form, $form_state);
456 $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.'); 520 $this->assertSame($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
457 } 521 }
458 522
459 /** 523 /**
460 * Tests different display options for the source field. 524 * Tests different display options for the source field.
461 */ 525 */