From 2f209a4eb04502039d978db88c83978a60ba402d Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 15 Jan 2020 10:22:40 +0000
Subject: [PATCH] Issue #3100470 by phenaproxima, oknate, Meenakshi.g, Wim
 Leers, bnjmnm: EditorMediaDialog triggers an "undefined index" notice for
 data-view-mode

(cherry picked from commit 61aa003c83d989d9cb48dbe9fe7fc5b8ff2472f6)
---
 .../media/src/Form/EditorMediaDialog.php      |  2 +-
 .../src/Kernel/EditorMediaDialogTest.php      | 92 +++++++++++++++++++
 2 files changed, 93 insertions(+), 1 deletion(-)
 create mode 100644 core/modules/media/tests/src/Kernel/EditorMediaDialogTest.php

diff --git a/core/modules/media/src/Form/EditorMediaDialog.php b/core/modules/media/src/Form/EditorMediaDialog.php
index 450c7157a4da..efe9cccbbb44 100644
--- a/core/modules/media/src/Form/EditorMediaDialog.php
+++ b/core/modules/media/src/Form/EditorMediaDialog.php
@@ -173,7 +173,7 @@ public function buildForm(array $form, FormStateInterface $form_state, EditorInt
     ];
 
     $view_mode_options = array_intersect_key($this->entityDisplayRepository->getViewModeOptions('media'), $media_embed_filter->settings['allowed_view_modes']);
-    $default_view_mode = static::getViewModeDefaultValue($view_mode_options, $media_embed_filter, $media_embed_element['data-view-mode']);
+    $default_view_mode = static::getViewModeDefaultValue($view_mode_options, $media_embed_filter, $media_embed_element['data-view-mode'] ?? NULL);
 
     $form['view_mode'] = [
       '#title' => $this->t("Display"),
diff --git a/core/modules/media/tests/src/Kernel/EditorMediaDialogTest.php b/core/modules/media/tests/src/Kernel/EditorMediaDialogTest.php
new file mode 100644
index 000000000000..ee3e4df6abe3
--- /dev/null
+++ b/core/modules/media/tests/src/Kernel/EditorMediaDialogTest.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace Drupal\Tests\media\Kernel;
+
+use Drupal\Core\Form\FormState;
+use Drupal\editor\EditorInterface;
+use Drupal\filter\Entity\FilterFormat;
+use Drupal\KernelTests\KernelTestBase;
+use Drupal\media\Entity\Media;
+use Drupal\media\Form\EditorMediaDialog;
+use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
+
+/**
+ * @coversDefaultClass \Drupal\media\Form\EditorMediaDialog
+ * @group media
+ */
+class EditorMediaDialogTest extends KernelTestBase {
+
+  use MediaTypeCreationTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $modules = [
+    'field',
+    'file',
+    'filter',
+    'image',
+    'media',
+    'media_test_source',
+    'system',
+    'user',
+  ];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+
+    $this->installSchema('file', ['file_usage']);
+    $this->installEntitySchema('file');
+    $this->installEntitySchema('media');
+    $this->installEntitySchema('user');
+  }
+
+  /**
+   * Tests that the form builds successfully.
+   *
+   * @covers ::buildForm
+   */
+  public function testBuildForm() {
+    $format = FilterFormat::create([
+      'filters' => [
+        'media_embed' => ['status' => TRUE],
+      ],
+    ]);
+
+    $editor = $this->prophesize(EditorInterface::class);
+    $editor->getFilterFormat()->willReturn($format);
+
+    // Create a sample media entity to be embedded.
+    $media = Media::create([
+      'bundle' => $this->createMediaType('test')->id(),
+      'name' => 'Screaming hairy armadillo',
+      'field_media_test' => $this->randomString(),
+    ]);
+    $media->save();
+
+    $form_state = new FormState();
+    $form_state->setUserInput([
+      'editor_object' => [
+        'attributes' => [
+          'data-entity-type' => 'media',
+          'data-entity-uuid' => $media->uuid(),
+          'data-align' => 'center',
+        ],
+        'hasCaption' => 'false',
+        'label' => $media->label(),
+        'link' => '',
+        'hostEntityLangcode' => $media->language()->getId(),
+        'classes' => '',
+      ],
+    ]);
+    $form_state->setRequestMethod('POST');
+
+    EditorMediaDialog::create($this->container)
+      ->buildForm([], $form_state, $editor->reveal());
+    $this->pass('Form was built without errors.');
+  }
+
+}
-- 
GitLab