function ContentExportTest::testExportEntitiesFilteredByBundle
Tests exporting entities filtered by bundle.
File
-
core/
tests/ Drupal/ FunctionalTests/ DefaultContent/ ContentExportTest.php, line 429
Class
- ContentExportTest
- Tests exporting content in YAML format.
Namespace
Drupal\FunctionalTests\DefaultContentCode
public function testExportEntitiesFilteredByBundle() : void {
$command = [
'content:export',
'node',
'--bundle=article',
];
// 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 to export multiple entities', $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('2 entities were exported to ', $process->getOutput());
$this->assertFileExists($expected_output_dir . '/node/2d3581c3-92c7-4600-8991-a0d4b3741198.yml');
$this->assertFileExists($expected_output_dir . '/node/e1714f23-70c0-4493-8e92-af1901771921.yml');
// Create two additional taxonomy vocabularies, with two terms each, to
// test multiple `--bundle` options.
$vocabulary1 = $this->createVocabulary();
$vocabulary2 = $this->createVocabulary();
$term1 = $this->createTerm($vocabulary1)
->uuid();
$term2 = $this->createTerm($vocabulary1)
->uuid();
$term3 = $this->createTerm($vocabulary2)
->uuid();
$term4 = $this->createTerm($vocabulary2)
->uuid();
$process = $this->runDrupalCommand([
'content:export',
'taxonomy_term',
'--bundle=tags',
'--bundle=' . $vocabulary2->id(),
'--dir=public://content',
]);
$this->assertSame(0, $process->wait());
$this->assertStringContainsString('4 entities were exported to ', $process->getOutput());
$tags = $this->container
->get(EntityTypeManagerInterface::class)
->getStorage('taxonomy_term')
->loadByProperties([
'vid' => 'tags',
]);
$this->assertCount(2, $tags);
foreach ($tags as $tag) {
$this->assertFileExists($expected_output_dir . '/taxonomy_term/' . $tag->uuid() . '.yml');
}
$this->assertFileDoesNotExist($expected_output_dir . '/taxonomy_term/' . $term1 . '.yml');
$this->assertFileDoesNotExist($expected_output_dir . '/taxonomy_term/' . $term2 . '.yml');
$this->assertFileExists($expected_output_dir . '/taxonomy_term/' . $term3 . '.yml');
$this->assertFileExists($expected_output_dir . '/taxonomy_term/' . $term4 . '.yml');
// Export a single entity, with the bundle filter matching the entity's
// bundle.
$process = $this->runDrupalCommand([
'content:export',
'taxonomy_term',
1,
'--bundle=tags',
]);
$this->assertSame(0, $process->wait());
// If we try that with a mismatched bundle filter, it should result in no
// entity being exported, which is an error, but a hint should be given.
$process = $this->runDrupalCommand([
'content:export',
'taxonomy_term',
1,
'--bundle=' . $vocabulary1->id(),
]);
$this->assertSame(1, $process->wait());
$output = $process->getOutput();
$this->assertStringContainsString('taxonomy_term 1 does not exist.', $output);
$this->assertStringContainsString('Maybe this entity is not one of the specified bundles: ' . $vocabulary1->id(), $output);
// We should get an error if we try to export bundles that don't exist.
$process = $this->runDrupalCommand([
'content:export',
'taxonomy_term',
'--bundle=junk',
'--bundle=tags',
]);
$this->assertSame(1, $process->wait());
$this->assertStringContainsString('These bundles do not exist on the taxonomy_term entity type: junk', $process->getOutput());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.