function ContentExportTest::testExportContent
Ensures that all imported content can be exported properly.
File
-
core/
tests/ Drupal/ FunctionalTests/ DefaultContent/ ContentExportTest.php, line 75
Class
- ContentExportTest
- Tests exporting content in YAML format.
Namespace
Drupal\FunctionalTests\DefaultContentCode
public function testExportContent() : void {
// We should get an error if we try to export a non-existent entity type.
$process = $this->runDrupalCommand([
'content:export',
'camels',
42,
'--no-ansi',
]);
$this->assertSame(1, $process->wait());
$this->assertStringContainsString('The entity type "camels" does not exist.', $process->getOutput());
// We should get an error if we try to export a non-existent entity.
$process = $this->runDrupalCommand([
'content:export',
'taxonomy_term',
42,
'--no-ansi',
]);
$this->assertSame(1, $process->wait());
$this->assertStringContainsString('taxonomy_term 42 does not exist.', $process->getOutput());
// We should get an error if we try to export a config entity.
$process = $this->runDrupalCommand([
'content:export',
'taxonomy_vocabulary',
'tags',
'--no-ansi',
]);
$this->assertSame(1, $process->wait());
$this->assertStringContainsString('taxonomy_vocabulary is not a content entity type.', $process->getOutput());
$entity_repository = $this->container
->get(EntityRepositoryInterface::class);
foreach ($this->finder->data as $uuid => $imported_data) {
$entity_type_id = $imported_data['_meta']['entity_type'];
$entity = $entity_repository->loadEntityByUuid($entity_type_id, $uuid);
$this->assertInstanceOf(ContentEntityInterface::class, $entity);
$process = $this->runDrupalCommand([
'content:export',
$entity->getEntityTypeId(),
$entity->id(),
]);
// The export should succeed without error.
$this->assertSame(0, $process->wait(), $process->getErrorOutput());
// The path is added by the importer and is never exported.
unset($imported_data['_meta']['path']);
// The output should be identical to the imported data. Sort recursively
// by key to prevent false negatives.
$exported_data = Yaml::decode($process->getOutput());
// If the entity is a file, the file URI might vary slightly -- i.e., if
// the file already existed, the imported one would have been renamed. We
// need to account for that.
if ($entity->getEntityTypeId() === 'file') {
$imported_uri = $entity->getFileUri();
$extension = strlen('.' . pathinfo($imported_uri, PATHINFO_EXTENSION));
$imported_uri = substr($imported_uri, 0, -$extension);
$exported_uri = substr($exported_data['default']['uri'][0]['value'], 0, -$extension);
$this->assertStringStartsWith($imported_uri, $exported_uri);
// We know they match; no need to consider them further.
unset($exported_data['default']['uri'][0]['value'], $imported_data['default']['uri'][0]['value']);
}
// This specific node is special -- it is always reassigned to the current
// user during import, because its owner does not exist. Therefore, the
// current user is who it should be referring to when exported.
if ($uuid === '7f1dd75a-0be2-4d3b-be5d-9d1a868b9267') {
$new_owner = $this->adminUser
->uuid();
$exported_data['_meta']['depends'] = $imported_data['_meta']['depends'] = [
$new_owner => 'user',
];
$exported_data['default']['uid'][0]['entity'] = $imported_data['default']['uid'][0]['entity'] = $new_owner;
}
self::recursiveSortByKey($exported_data);
self::recursiveSortByKey($imported_data);
$this->assertSame($imported_data, $exported_data);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.