Verified Commit 00ef8df8 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

task: #3588332 file_tokens() fails when [file:owner] resolves on a file whose...

task: #3588332 file_tokens() fails when [file:owner] resolves on a file whose owner has been deleted

By: benstallings
By: cilefen
By: smustgrave
By: mohit_aghera
By: godotislate
By: quietone
(cherry picked from commit 2e0db3e8)
parent a1b67d8d
Loading
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -88,10 +88,13 @@ public function tokens($type, $tokens, array $data, array $options, BubbleableMe
            break;

          case 'owner':
            $owner = $file->getOwner();
            // Only replace the token when the file has a valid owner. When the
            // owner is missing the token is left unreplaced, consistent with
            // the chained [file:owner:*] tokens handled below.
            if ($owner = $file->getOwner()) {
              $bubbleable_metadata->addCacheableDependency($owner);
            $name = $owner->label();
            $replacements[$original] = $name;
              $replacements[$original] = $owner->label();
            }
            break;
        }
      }
+11 −0
Original line number Diff line number Diff line
@@ -109,6 +109,17 @@ public function testFileTokenReplacement(): void {
      ]);
      $this->assertEquals($expected, $output, "Unsanitized file token $input replaced.");
    }

    // Assign the file to a non-existent user so that getOwner() returns NULL,
    // then verify that the owner tokens are left unreplaced rather than causing
    // an error. Both [file:owner] and the chained [file:owner:*] tokens are
    // skipped when there is no user entity to derive their values from.
    $file->setOwnerId(999999)->save();
    $this->assertNull($file->getOwner());
    foreach (['[file:owner]', '[file:owner:uid]', '[file:owner:name]'] as $input) {
      $output = $token_service->replace($input, ['file' => $file], ['langcode' => $language_interface->getId()]);
      $this->assertSame($input, $output, "File token $input is left unreplaced when the owner is missing.");
    }
  }

}