Commit 206190fe authored by alexpott's avatar alexpott Committed by andypost

Issue #2872278 by alexpott, larowlan: Content created during an installation is owned by user 0

parent 9d068fac
......@@ -5,7 +5,7 @@ services:
class: Drupal\default_content\Scanner
default_content.importer:
class: Drupal\default_content\Importer
arguments: ['@serializer', '@plugin.manager.rest', '@entity_type.manager', '@rest.link_manager', '@event_dispatcher', '@default_content.scanner', '%default_content.link_domain%']
arguments: ['@serializer', '@plugin.manager.rest', '@entity_type.manager', '@rest.link_manager', '@event_dispatcher', '@default_content.scanner', '%default_content.link_domain%', '@account_switcher']
default_content.exporter:
class: Drupal\default_content\Exporter
arguments: ['@serializer', '@entity_type.manager', '@entity.repository', '@rest.link_manager', '@event_dispatcher', '@module_handler', '@info_parser', '%default_content.link_domain%']
......
......@@ -6,10 +6,12 @@ use Drupal\Component\Graph\Graph;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\default_content\Event\DefaultContentEvents;
use Drupal\default_content\Event\ImportEvent;
use Drupal\rest\LinkManager\LinkManagerInterface;
use Drupal\rest\Plugin\Type\ResourcePluginManager;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Serializer;
......@@ -83,6 +85,13 @@ class Importer implements ImporterInterface {
*/
protected $scanner;
/**
* The account switcher.
*
* @var \Drupal\Core\Session\AccountSwitcherInterface
*/
protected $accountSwitcher;
/**
* Constructs the default content manager.
*
......@@ -100,8 +109,10 @@ class Importer implements ImporterInterface {
* The file scanner.
* @param string $link_domain
* Defines relation domain URI for entity links.
* @param \Drupal\Core\Session\AccountSwitcherInterface $account_switcher
* The account switcher.
*/
public function __construct(Serializer $serializer, ResourcePluginManager $resource_plugin_manager, EntityTypeManagerInterface $entity_type_manager, LinkManagerInterface $link_manager, EventDispatcherInterface $event_dispatcher, ScannerInterface $scanner, $link_domain) {
public function __construct(Serializer $serializer, ResourcePluginManager $resource_plugin_manager, EntityTypeManagerInterface $entity_type_manager, LinkManagerInterface $link_manager, EventDispatcherInterface $event_dispatcher, ScannerInterface $scanner, $link_domain, AccountSwitcherInterface $account_switcher) {
$this->serializer = $serializer;
$this->resourcePluginManager = $resource_plugin_manager;
$this->entityTypeManager = $entity_type_manager;
......@@ -109,6 +120,7 @@ class Importer implements ImporterInterface {
$this->eventDispatcher = $event_dispatcher;
$this->scanner = $scanner;
$this->linkDomain = $link_domain;
$this->accountSwitcher = $account_switcher;
}
/**
......@@ -119,6 +131,8 @@ class Importer implements ImporterInterface {
$folder = drupal_get_path('module', $module) . "/content";
if (file_exists($folder)) {
$root_user = $this->entityTypeManager->getStorage('user')->load(1);
$this->accountSwitcher->switchTo($root_user);
$file_map = [];
foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
$reflection = new \ReflectionClass($entity_type->getClass());
......@@ -182,11 +196,16 @@ class Importer implements ImporterInterface {
$class = $definition['serialization_class'];
$entity = $this->serializer->deserialize($contents, $class, 'hal_json', ['request_method' => 'POST']);
$entity->enforceIsNew(TRUE);
// Ensure that the entity is not owned by the anonymous user.
if ($entity instanceof EntityOwnerInterface && empty($entity->getOwnerId())) {
$entity->setOwner($root_user);
}
$entity->save();
$created[$entity->uuid()] = $entity;
}
}
$this->eventDispatcher->dispatch(DefaultContentEvents::IMPORT, new ImportEvent($created, $module));
$this->accountSwitcher->switchBack();
}
// Reset the tree.
$this->resetTree();
......
......@@ -52,7 +52,7 @@
{
"_links": {
"self": {
"href": "http://drupal.org/user/1"
"href": "http://drupal.org/user/2"
},
"type": {
"href": "http://drupal.org/rest/type/user/user"
......@@ -69,7 +69,7 @@
{
"_links": {
"self": {
"href": "http://drupal.org/user/1"
"href": "http://drupal.org/user/2"
},
"type": {
"href": "http://drupal.org/rest/type/user/user"
......
{
"_links": {
"self": {
"href": "http://drupal.org/node/2"
},
"type": {
"href": "http://drupal.org/rest/type/node/page"
},
"http://drupal.org/rest/relation/node/page/uid": [
{
"href": "http://drupal.org/user/1"
}
],
"http://drupal.org/rest/relation/node/page/revision_uid": [
{
"href": "http://drupal.org/user/1"
}
],
"http://drupal.org/rest/relation/node/page/field_tags":[
{
"href":"http://drupal.org/taxonomy/term/1"
}
]
},
"uuid": [
{
"value": "78c412a3-b83f-4efb-8a05-5a6ecea10ad4"
}
],
"type": [
{
"target_id": "page"
}
],
"langcode": [
{
"value": "en"
}
],
"title": [
{
"value": "Imported node with owned by user 1"
}
],
"body": [
{
"value": "Crikey it works!"
}
],
"_embedded": {
"http://drupal.org/rest/relation/node/page/uid": [
{
"_links": {
"self": {
"href": "http://drupal.org/user/1"
},
"type": {
"href": "http://drupal.org/rest/type/user/user"
}
},
"uuid":[
{
"value":"fe301be5-7017-4ff8-b2d3-09dc0a30bd43"
}
]
}
],
"http://drupal.org/rest/relation/node/page/revision_uid": [
{
"_links": {
"self": {
"href": "http://drupal.org/user/1"
},
"type": {
"href": "http://drupal.org/rest/type/user/user"
}
},
"uuid":[
{
"value":"fe301be5-7017-4ff8-b2d3-09dc0a30bd43"
}
]
}
],
"http://drupal.org/rest/relation/node/page/field_tags": [
{
"_links":{
"self":{
"href":"http://drupal.org/taxonomy/term/1"
},
"type":{
"href":"http://drupal.org/rest/type/taxonomy_term/tags"
}
},
"uuid":[
{
"value":"550f86ad-aa11-4047-953f-636d42889f85"
}
]
}
]
},
"status": [
{
"value": "1"
}
],
"created": [
{
"value": "1381645976"
}
],
"changed": [
{
"value": "1381646540"
}
],
"promote": [
{
"value": "1"
}
],
"sticky": [
{
"value": "1"
}
],
"revision_timestamp": [
{
"value": "1381646540"
}
],
"revision_log": [
{
"value": ""
}
]
}
{
"_links": {
"self": {
"href": "http://drupal.org/node/3"
},
"type": {
"href": "http://drupal.org/rest/type/node/page"
},
"http://drupal.org/rest/relation/node/page/uid": [
{
"href": "http://drupal.org/user/99"
}
],
"http://drupal.org/rest/relation/node/page/revision_uid": [
{
"href": "http://drupal.org/user/99"
}
],
"http://drupal.org/rest/relation/node/page/field_tags":[
{
"href":"http://drupal.org/taxonomy/term/1"
}
]
},
"uuid": [
{
"value": "78c412a3-b83f-4efb-8a05-5a6ecea10aee"
}
],
"type": [
{
"target_id": "page"
}
],
"langcode": [
{
"value": "en"
}
],
"title": [
{
"value": "Imported node with owned by user that does not exist"
}
],
"body": [
{
"value": "Crikey it works!"
}
],
"_embedded": {
"http://drupal.org/rest/relation/node/page/uid": [
{
"_links": {
"self": {
"href": "http://drupal.org/user/99"
},
"type": {
"href": "http://drupal.org/rest/type/user/user"
}
},
"uuid":[
{
"value":"99301be5-7017-4ff8-b2d3-09dc0a30bd43"
}
]
}
],
"http://drupal.org/rest/relation/node/page/revision_uid": [
{
"_links": {
"self": {
"href": "http://drupal.org/user/99"
},
"type": {
"href": "http://drupal.org/rest/type/user/user"
}
},
"uuid":[
{
"value":"99301be5-7017-4ff8-b2d3-09dc0a30bd43"
}
]
}
],
"http://drupal.org/rest/relation/node/page/field_tags": [
{
"_links":{
"self":{
"href":"http://drupal.org/taxonomy/term/1"
},
"type":{
"href":"http://drupal.org/rest/type/taxonomy_term/tags"
}
},
"uuid":[
{
"value":"550f86ad-aa11-4047-953f-636d42889f85"
}
]
}
]
},
"status": [
{
"value": "1"
}
],
"created": [
{
"value": "1381645976"
}
],
"changed": [
{
"value": "1381646540"
}
],
"promote": [
{
"value": "1"
}
],
"sticky": [
{
"value": "1"
}
],
"revision_timestamp": [
{
"value": "1381646540"
}
],
"revision_log": [
{
"value": ""
}
]
}
......@@ -6,6 +6,7 @@ use Drupal\Core\Config\FileStorage;
use Drupal\simpletest\BrowserTestBase;
use Drupal\simpletest\ContentTypeCreationTrait;
use Drupal\simpletest\NodeCreationTrait;
use Drupal\user\Entity\User;
/**
* Test import of default content.
......@@ -29,6 +30,14 @@ class DefaultContentTest extends BrowserTestBase {
*/
protected function setUp() {
parent::setUp();
// Create user 2 with the correct UUID.
User::create([
'uid' => 2,
'uuid' => 'ab301be5-7017-4ff8-b2d3-09dc0a30bd43',
'name' => 'User 2',
'mail' => 'user2@example.com',
'status' => TRUE,
])->save();
$this->createContentType(array('type' => 'page'));
}
......@@ -77,6 +86,14 @@ class DefaultContentTest extends BrowserTestBase {
$node = $this->getNodeByTitle('Imported node');
$this->assertEquals($node->body->value, 'Crikey it works!');
$this->assertEquals($node->getType(), 'page');
$this->assertSame('2', $node->getOwnerId(), 'The node created is owned by user 2');
$node = $this->getNodeByTitle('Imported node with owned by user 1');
$this->assertSame('1', $node->getOwnerId(), 'The node created is owned by user 1');
$node = $this->getNodeByTitle('Imported node with owned by user that does not exist');
$this->assertSame('1', $node->getOwnerId(), 'The node created is owned by user 1');
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadMultiple();
$term = reset($terms);
$this->assertTrue(!empty($term));
......
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