3 namespace Drupal\Tests\media_entity\FunctionalJavascript;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\media_entity\Entity\Media;
11 * Tests the integration between Inline Entity Form and Media Entity.
15 class MediaIefIntegrationTest extends MediaEntityJavascriptTestBase {
22 public static $modules = ['inline_entity_form'];
27 protected function setUp() {
30 /** @var \Drupal\media_entity\MediaBundleInterface $media_bundle */
31 $media_bundle = $this->drupalCreateMediaBundle();
33 // Create a new content type.
35 'name' => 'Media entity CT',
36 'title_label' => 'An example Custom Content type.',
37 'type' => 'media_entity_ct',
39 $content_type = $this->createContentType($values);
40 // Create an entity_reference field.
41 FieldStorageConfig::create([
42 'field_name' => 'ref_media_entities',
43 'type' => 'entity_reference',
44 'entity_type' => 'node',
45 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
47 'target_type' => 'media',
51 'field_name' => 'ref_media_entities',
52 'field_type' => 'entity_reference',
53 'entity_type' => 'node',
54 'bundle' => $content_type->id(),
55 'label' => 'Media referenced',
57 'handler' => 'default:media',
58 'handler_settings' => [
60 $media_bundle->id() => $media_bundle->id(),
65 'auto_create' => FALSE,
66 'auto_create_bundle' => $media_bundle->id(),
71 // Set widget to inline_entity_form.
72 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
73 $form_display = $this->container->get('entity_type.manager')
74 ->getStorage('entity_form_display')
75 ->load('node.media_entity_ct.default');
76 $form_display->setComponent('ref_media_entities', [
77 'type' => 'inline_entity_form_complex',
84 * Tests inline_entity_form integration with media entities.
86 public function testMediaIefIntegration() {
88 $session = $this->getSession();
89 $page = $session->getPage();
90 $assert_session = $this->assertSession();
92 // Open up a node form and check the IEF widget.
93 $this->drupalGet('/node/add/media_entity_ct');
94 $assert_session->buttonExists('edit-ref-media-entities-actions-ief-add');
95 $page->pressButton('edit-ref-media-entities-actions-ief-add');
96 $assert_session->assertWaitOnAjaxRequest();
98 // Check the presence of the entity's label field.
99 $page->findField('ref_media_entities[form][inline_entity_form][name][0][value]')->isVisible();
101 // Submit the form to create a media entity and verify that it is correctly
103 $media_name = $this->randomMachineName();
104 $page->fillField('ref_media_entities[form][inline_entity_form][name][0][value]', $media_name);
105 $page->fillField('ref_media_entities[form][inline_entity_form][uid][0][target_id]', $this->adminUser->getDisplayName() . ' (' . $this->adminUser->id() . ')');
106 $page->pressButton('Create media');
107 $assert_session->assertWaitOnAjaxRequest();
109 // We need to save the node in order for IEF to do its thing.
110 $page->fillField('title[0][value]', $this->randomString());
111 $page->pressButton('Save');
113 $media_id = $this->container->get('entity.query')->get('media')->execute();
114 $media_id = reset($media_id);
115 /** @var \Drupal\media_entity\MediaInterface $media */
116 $media = Media::load($media_id);
117 $this->assertEquals($media_name, $media->label(), 'A media inside IEF was correctly created.');