function ContentExportTest::testExportWithDependencies

Tests exporting a piece of content with its dependencies.

File

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

Class

ContentExportTest
Tests exporting content in YAML format.

Namespace

Drupal\FunctionalTests\DefaultContent

Code

public function testExportWithDependencies() : void {
  $image_uri = $this->getRandomGenerator()
    ->image(uniqid('public://') . '.png', '200x200', '300x300');
  $file = File::create([
    'uri' => $image_uri,
  ]);
  $file->save();
  $media = Media::create([
    'bundle' => 'image',
    'field_media_image' => [
      $file,
    ],
  ]);
  $media->save();
  $this->createEntityReferenceField('node', 'article', 'field_media', 'Media', 'media', selection_handler_settings: [
    'target_bundles' => [
      'image' => 'image',
    ],
  ]);
  $node = $this->drupalCreateNode([
    'type' => 'article',
    'field_tags' => Term::load(1),
    'field_media' => $media,
    'uid' => User::load(2),
  ]);
  $command = [
    'content:export',
    'node',
    $node->id(),
    '--with-dependencies',
  ];
  // With no `--dir` option, we should get an error.
  $process = $this->runDrupalCommand($command);
  $this->assertGreaterThan(0, $process->wait());
  $this->assertStringContainsString('The --dir option is required when exporting with dependencies.', $process->getErrorOutput());
  $command[] = "--dir=public://content";
  $process = $this->runDrupalCommand($command);
  $this->assertSame(0, $process->wait());
  $expected_output_dir = $this->container
    ->get(FileSystemInterface::class)
    ->realpath('public://content');
  $this->assertStringContainsString('5 items were exported to ', $process->getOutput());
  $this->assertFileExists($expected_output_dir . '/node/' . $node->uuid() . '.yml');
  $this->assertFileExists($expected_output_dir . '/taxonomy_term/' . $node->field_tags[0]->entity
    ->uuid() . '.yml');
  $this->assertFileExists($expected_output_dir . '/media/' . $media->uuid() . '.yml');
  $this->assertFileExists($expected_output_dir . '/file/' . $file->uuid() . '.yml');
  $this->assertFileExists($expected_output_dir . '/user/' . $node->getOwner()
    ->uuid() . '.yml');
  // The physical file should have been copied too.
  $original_file_hash = hash_file('sha256', $file->getFileUri());
  $this->assertIsString($original_file_hash);
  $exported_file_hash = hash_file('sha256', $expected_output_dir . '/file/' . $file->getFilename());
  $this->assertIsString($exported_file_hash);
  $this->assertTrue(hash_equals($original_file_hash, $exported_file_hash));
}

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