Commit 5817db2a authored by dawehner's avatar dawehner

fixed some minor points and added basic test coverage

parent d5ead529
......@@ -18,7 +18,7 @@ function default_content_drush_command() {
'options' => [
'file' => dt('Write out the exported content to a file instead of stdout'),
],
'aliases' => array('dce'),
'aliases' => ['dce'],
'required-arguments' => 2,
];
$items['default-content-export-references'] = [
......@@ -30,7 +30,7 @@ function default_content_drush_command() {
'options' => [
'folder' => dt('Folder to export to, entities are grouped by entity type into directories.'),
],
'aliases' => array('dcer'),
'aliases' => ['dcer'],
'required-arguments' => 2,
];
......@@ -63,7 +63,7 @@ function drush_default_content_export($entity_type_id, $entity_id) {
*
* @param string $entity_type_id
* The entity type ID.
* @param $entity_id
* @param mixed $entity_id
* The entity ID to export.
*/
function drush_default_content_export_references($entity_type_id, $entity_id) {
......
......@@ -11,8 +11,6 @@ use Drupal\Component\Utility\String;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Session\AccountInterface;
use Drupal\rest\Plugin\Type\ResourcePluginManager;
use Gliph\Graph\DirectedAdjacencyList;
......@@ -216,7 +214,9 @@ class DefaultContentManager implements DefaultContentManagerInterface {
if ($dependent_entity instanceof ConfigEntityInterface) {
unset($entity_dependencies[$id]);
}
$entity_dependencies = array_merge($entity_dependencies, $this->getEntityReferencesRecursive($dependent_entity, $depth + 1));
else {
$entity_dependencies = array_merge($entity_dependencies, $this->getEntityReferencesRecursive($dependent_entity, $depth + 1));
}
}
// Build in some support against infinite recursion.
......
......@@ -49,7 +49,7 @@ interface DefaultContentManagerInterface {
*
* @param string $entity_type_id
* The entity type ID.
* @param $entity_id
* @param mixed $entity_id
* The entity ID to export.
*
* @return string[][]
......
<?php
/**
* @file
* Contains \Drupal\default_content\Tests\DefaultContentManagerIntegrationTest.
*/
namespace Drupal\default_content\Tests;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\simpletest\KernelTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\user\Entity\User;
/**
* @coversDefaultClass \Drupal\default_content\DefaultContentManager
* @group default_content
*/
class DefaultContentManagerIntegrationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system'];
/**
* The tested default content manager.
*
* @var \Drupal\default_content\DefaultContentManager
*/
protected $defaultContentManager;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', ['router', 'sequences']);
}
/**
* Tests exportContent().
*/
public function testExportContent() {
\Drupal::moduleHandler()->install(['taxonomy', 'default_content']);
\Drupal::service('router.builder')->rebuild();
$this->defaultContentManager = \Drupal::service('default_content.manager');
$vocabulary = Vocabulary::create(['vid' => 'test']);
$vocabulary->save();
$term = Term::create(['vid' => $vocabulary->id(), 'name' => 'test_name']);
$term->save();
$term = Term::load($term->id());
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
$expected = $serializer->serialize($term, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
$exported = $this->defaultContentManager->exportContent('taxonomy_term', $term->id());
$exported_decoded = json_decode($exported);
// Ensure the proper UUID is part of it.
$this->assertEqual($exported_decoded->uuid[0]->value, $term->uuid());
$this->assertEqual($exported, $expected);
}
/**
* Tests exportContentWithReferences().
*/
public function testExportWithReferences() {
\Drupal::moduleHandler()->install(['node', 'default_content']);
\Drupal::service('router.builder')->rebuild();
$this->defaultContentManager = \Drupal::service('default_content.manager');
$user = User::create(['name' => 'my username']);
$user->save();
$node_type = NodeType::create(['type' => 'test']);
$node_type->save();
$node = Node::create(['type' => $node_type->id(), 'title' => 'test node', 'uid' => $user->id()]);
$node->save();
/** @var \Symfony\Component\Serializer\Serializer $serializer */
$serializer = \Drupal::service('serializer');
$expected_node = $serializer->serialize($node, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
$expected_user = $serializer->serialize($user, 'hal_json', ['json_encode_options' => JSON_PRETTY_PRINT]);
$exported_by_entity_type = $this->defaultContentManager->exportContentWithReferences('node', $node->id());
// Ensure that the node type is not tryed to be exported.
$this->assertEqual(array_keys($exported_by_entity_type), ['node', 'user']);
// Ensure the right UUIDs are exported.
$this->assertEqual([$node->uuid()], array_keys($exported_by_entity_type['node']));
$this->assertEqual([$user->uuid()], array_keys($exported_by_entity_type['user']));
// Compare the actual serialized data.
$this->assertEqual(reset($exported_by_entity_type['node']), $expected_node);
$this->assertEqual(reset($exported_by_entity_type['user']), $expected_user);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment