From 417a3e58511c2244604151e3ed9a8b6e9fb1459f Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Thu, 17 Nov 2022 16:19:28 +0000 Subject: [PATCH] Issue #3260175 by jsricardo, andregp, joachim, nod_, alexpott: Saving media entity without an owner crashes (cherry picked from commit fbac9e721a69c5e790aad628353b15570b474c05) --- core/modules/media/src/Entity/Media.php | 4 ++++ .../src/Functional/MediaUiFunctionalTest.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php index 2170f91979bd..415b19cc88c8 100644 --- a/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -297,6 +297,10 @@ protected function shouldUpdateThumbnail($is_new = FALSE) { public function preSave(EntityStorageInterface $storage) { parent::preSave($storage); + if (!$this->getOwner()) { + $this->setOwnerId(0); + } + // If no thumbnail has been explicitly set, use the default thumbnail. if ($this->get('thumbnail')->isEmpty()) { $this->thumbnail->target_id = $this->loadThumbnail()->id(); diff --git a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php index 5446c3a3ca7e..362467b3807c 100644 --- a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php +++ b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php @@ -92,6 +92,21 @@ public function testMediaWithOnlyOneMediaType() { ->loadUnchanged($media_id); $this->assertSame($media->getName(), $media_name2); + // Change the authored by field to an empty string, which should assign + // authorship to the anonymous user (uid 0). + $this->drupalGet('media/' . $media_id . '/edit'); + $edit['uid[0][target_id]'] = ''; + $this->submitForm($edit, 'Save'); + /** @var \Drupal\media\MediaInterface $media */ + $media = $this->container->get('entity_type.manager') + ->getStorage('media') + ->loadUnchanged($media_id); + $uid = $media->getOwnerId(); + // Most SQL database drivers stringify fetches but entities are not + // necessarily stored in a SQL database. At the same time, NULL/FALSE/"" + // won't do. + $this->assertTrue($uid === 0 || $uid === '0', 'Media authored by anonymous user.'); + // Test that there is no empty vertical tabs element, if the container is // empty (see #2750697). // Make the "Publisher ID" and "Created" fields hidden. -- GitLab