Commit 99524ce1 authored by catch's avatar catch
Browse files

Issue #3260175 by jsricardo, andregp, joachim, nod_, alexpott: Saving media...

Issue #3260175 by jsricardo, andregp, joachim, nod_, alexpott: Saving media entity without an owner crashes

(cherry picked from commit fbac9e72)
parent 49f5e5de
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -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();
+15 −0
Original line number Diff line number Diff line
@@ -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.