function ContentExportTest::testExportFileEntityWithMissingPhysicalFile

Tests exporting file entities without an accompanying physical file.

File

core/tests/Drupal/FunctionalTests/DefaultContent/ContentExportTest.php, line 342

Class

ContentExportTest
Tests exporting content in YAML format.

Namespace

Drupal\FunctionalTests\DefaultContent

Code

public function testExportFileEntityWithMissingPhysicalFile() : void {
  $file = $this->container
    ->get(EntityRepositoryInterface::class)
    ->loadEntityByUuid('file', '2b8e0616-3ef0-4a91-8cfb-b31d9128f9f8');
  $this->assertInstanceOf(File::class, $file);
  $this->assertFileDoesNotExist($file->getFileUri());
  $logger = new TestLogger();
  $this->container
    ->get('logger.factory')
    ->addLogger($logger);
  /** @var \Drupal\Core\DefaultContent\Exporter $exporter */
  $exporter = $this->container
    ->get(Exporter::class);
  $attachments = $exporter->export($file)->metadata
    ->getAttachments();
  // The export succeeded without throwing an exception, but the physical file
  // does not exist, so it should not have been attached.
  $this->assertEmpty($attachments);
  // The problem should have been logged.
  $predicate = function (array $record) use ($file) : bool {
    return $record['level'] === RfcLogLevel::WARNING && $record['message'] === 'The file (%uri) associated with file entity %name does not exist.' && $record['context']['%uri'] === $file->getFileUri() && $record['context']['%name'] === $file->label();
  };
  $this->assertTrue($logger->hasRecordThatPasses($predicate));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.