View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\node\NodeInterface;
class ContentEntityReferenceTest extends EntityShareClientFunctionalTestBase {
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected function setUp() : void {
parent::setUp();
$this
->postSetupFixture();
}
protected function getEntitiesDataArray() {
return [
'node' => [
'en' => [
'es_test_level_3' => $this
->getCompleteNodeInfos([
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_level_2' => $this
->getCompleteNodeInfos([
'field_es_test_content_reference' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('node', 'es_test_level_3'),
],
];
},
'checker_callback' => 'getExpectedContentReferenceValue',
],
]),
'es_test_level_1' => $this
->getCompleteNodeInfos([
'field_es_test_content_reference' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('node', 'es_test_level_2'),
],
];
},
'checker_callback' => 'getExpectedContentReferenceValue',
],
]),
'es_test_level_0' => $this
->getCompleteNodeInfos([
'field_es_test_content_reference' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('node', 'es_test_level_1'),
],
];
},
'checker_callback' => 'getExpectedContentReferenceValue',
],
]),
],
],
];
}
public function testEntityReference() {
$this
->pullEveryChannels();
$this
->checkCreatedEntities();
$this
->resetImportedContent();
$selected_entities = [
'es_test_level_0',
];
$this
->importSelectedEntities($selected_entities);
$this
->checkCreatedEntities();
$new_plugin_configurations = [
'entity_reference' => [
'max_recursion_depth' => 2,
'weights' => [
'process_entity' => 10,
],
],
];
$this
->mergePluginsToImportConfig($new_plugin_configurations);
$this
->resetImportedContent();
$selected_entities = [
'es_test_level_0',
];
$this
->importSelectedEntities($selected_entities);
$recreated_entities = $this
->loadEntity('node', 'es_test_level_1');
$this
->assertTrue(!empty($recreated_entities), 'The node with UUID es_test_level_1 has been recreated.');
$recreated_entities = $this
->loadEntity('node', 'es_test_level_2');
$this
->assertTrue(!empty($recreated_entities), 'The node with UUID es_test_level_2 has been recreated.');
$recreated_entities = $this
->loadEntity('node', 'es_test_level_3');
$this
->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_3 has not been recreated.');
$new_plugin_configurations = [
'entity_reference' => [
'max_recursion_depth' => 1,
'weights' => [
'process_entity' => 10,
],
],
];
$this
->mergePluginsToImportConfig($new_plugin_configurations);
$this
->resetImportedContent();
$selected_entities = [
'es_test_level_0',
];
$this
->importSelectedEntities($selected_entities);
$recreated_entities = $this
->loadEntity('node', 'es_test_level_1');
$this
->assertTrue(!empty($recreated_entities), 'The node with UUID es_test_level_1 has been recreated.');
$recreated_entities = $this
->loadEntity('node', 'es_test_level_2');
$this
->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_2 has not been recreated.');
$recreated_entities = $this
->loadEntity('node', 'es_test_level_3');
$this
->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_3 has not been recreated.');
$new_plugin_configurations = [
'entity_reference' => [
'max_recursion_depth' => 0,
'weights' => [
'process_entity' => 10,
],
],
];
$this
->mergePluginsToImportConfig($new_plugin_configurations);
$this
->resetImportedContent();
$selected_entities = [
'es_test_level_0',
];
$this
->importSelectedEntities($selected_entities);
$recreated_entities = $this
->loadEntity('node', 'es_test_level_1');
$this
->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_1 has not been recreated.');
$recreated_entities = $this
->loadEntity('node', 'es_test_level_2');
$this
->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_2 has not been recreated.');
$recreated_entities = $this
->loadEntity('node', 'es_test_level_3');
$this
->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_3 has not been recreated.');
$new_plugin_configurations = [
'skip_imported' => [
'weights' => [
'is_entity_importable' => -5,
],
],
'entity_reference' => [
'max_recursion_depth' => 2,
'weights' => [
'process_entity' => 10,
],
],
];
$this
->mergePluginsToImportConfig($new_plugin_configurations);
$this
->resetImportedContent();
$selected_entities = [
'es_test_level_2',
];
$this
->importSelectedEntities($selected_entities);
$target_entity = $this
->loadEntity('node', 'es_test_level_3');
$this
->assertNotNull($target_entity, 'The target node has been created in the first import.');
$referencing_entity = $this
->loadEntity('node', 'es_test_level_2');
$this
->assertNotNull($referencing_entity, 'The referencing node has been created in the first import.');
$this->importService
->getRuntimeImportContext()
->clearImportedEntities();
$selected_entities = [
'es_test_level_3',
];
$this
->importSelectedEntities($selected_entities);
$target_entity = $this
->loadEntity('node', 'es_test_level_3');
$this
->assertNotNull($target_entity, 'The target node still exists after the second import.');
$expected_value = $target_entity
->id();
$actual_value = $referencing_entity->field_es_test_content_reference->target_id;
$this
->assertEquals($actual_value, $expected_value, 'The referencing node references the target node after second import.');
$this
->resetImportedContent();
$selected_entities = [
'es_test_level_3',
];
$this
->importSelectedEntities($selected_entities);
$target_entity = $this
->loadEntity('node', 'es_test_level_3');
$this
->assertNotNull($target_entity, 'The target node has been created in the first import.');
$this->importService
->getRuntimeImportContext()
->clearImportedEntities();
$selected_entities = [
'es_test_level_2',
];
$this
->importSelectedEntities($selected_entities);
$target_entity = $this
->loadEntity('node', 'es_test_level_3');
$this
->assertNotNull($target_entity, 'The target node still exists after the second import.');
$referencing_entity = $this
->loadEntity('node', 'es_test_level_2');
$this
->assertNotNull($referencing_entity, 'The referencing node has been created in the second import.');
$expected_value = $target_entity
->id();
$actual_value = $referencing_entity->field_es_test_content_reference->target_id;
$this
->assertEquals($actual_value, $expected_value, 'The referencing node references the target node after second import.');
}
protected function getExpectedContentReferenceValue(ContentEntityInterface $entity, string $field_name) {
$level = (int) str_replace('es_test_level_', '', $entity
->uuid());
$target_uuid = 'es_test_level_' . ($level + 1);
return [
[
'target_id' => $this
->getEntityId('node', $target_uuid),
],
];
}
protected function populateRequestService() {
parent::populateRequestService();
$selected_entities = [
'es_test_level_0',
];
$prepared_url = $this
->prepareUrlFilteredOnUuids($selected_entities, 'node_es_test_en');
$this
->discoverJsonApiEndpoints($prepared_url);
$selected_entities = [
'es_test_level_2',
];
$prepared_url = $this
->prepareUrlFilteredOnUuids($selected_entities, 'node_es_test_en');
$this
->discoverJsonApiEndpoints($prepared_url);
$selected_entities = [
'es_test_level_3',
];
$prepared_url = $this
->prepareUrlFilteredOnUuids($selected_entities, 'node_es_test_en');
$this
->discoverJsonApiEndpoints($prepared_url);
}
}