function ContentExportTest::testMissingDependenciesAreLogged

Tests that the exporter handles missing dependencies gracefully.

File

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

Class

ContentExportTest
Tests exporting content in YAML format.

Namespace

Drupal\FunctionalTests\DefaultContent

Code

public function testMissingDependenciesAreLogged() : void {
  $this->createEntityReferenceField('node', 'article', 'field_related', 'Related Content', 'node', selection_handler_settings: [
    'target_bundles' => [
      'page' => 'page',
    ],
  ]);
  $page = $this->drupalCreateNode([
    'type' => 'page',
  ]);
  $page_id = $page->id();
  $article = $this->drupalCreateNode([
    'type' => 'article',
    'field_related' => $page,
  ]);
  $page->delete();
  // We need to clear the caches or the related content is included because
  // the article is cached.
  $entity_storage = $this->container
    ->get(EntityTypeManagerInterface::class)
    ->getStorage('node');
  $entity_storage->resetCache([
    $page->id(),
    $article->id(),
  ]);
  $article = $entity_storage->load($article->id());
  /** @var \Drupal\Core\DefaultContent\Exporter $exporter */
  $exporter = $this->container
    ->get(Exporter::class);
  $logger = new TestLogger();
  $exporter->setLogger($logger);
  $dependencies = $exporter->export($article)->metadata
    ->getDependencies();
  // The export succeeded without throwing an exception, and depends only on
  // the author. The page should not be among the dependencies.
  $author_uuid = $this->adminUser
    ->uuid();
  $this->assertCount(1, $dependencies);
  $this->assertSame([
    'user',
    $author_uuid,
  ], $dependencies[0]);
  // The invalid reference should have been logged.
  $predicate = function (array $record) use ($page_id, $article) : bool {
    return $record['message'] === 'Failed to export reference to @target_type %missing_id referenced by %field on @entity_type %label because the referenced @target_type does not exist.' && $record['context']['@target_type'] === 'content item' && $record['context']['%missing_id'] === $page_id && $record['context']['%field'] === 'Related Content' && $record['context']['@entity_type'] === 'content item' && $record['context']['%label'] === $article->label();
  };
  $this->assertTrue($logger->hasRecordThatPasses($predicate, LogLevel::WARNING));
}

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