View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\user\UserInterface;
class MissingEntityReferenceTest extends EntityShareClientFunctionalTestBase {
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected function setUp() {
parent::setUp();
$node_storage = $this->entityTypeManager
->getStorage('node');
$term_storage = $this->entityTypeManager
->getStorage('taxonomy_term');
$node_to_be_referenced = $node_storage
->create([
'uuid' => 'es_test_to_be_deleted',
'langcode' => 'en',
'type' => 'es_test',
'title' => $this
->randomString(),
]);
$node_to_be_referenced
->save();
$term_to_be_referenced = $term_storage
->create([
'uuid' => 'es_test_tag_deleted',
'langcode' => 'en',
'vid' => 'es_test',
'name' => $this
->randomString(),
]);
$term_to_be_referenced
->save();
$this
->prepareContent();
$node_to_be_referenced
->delete();
$term_to_be_referenced
->delete();
$this
->populateRequestService();
$this
->deleteContent();
}
protected function getEntitiesDataArray() {
return [
'taxonomy_term' => [
'en' => [
'es_test_tag' => $this
->getCompleteTaxonomyTermInfos([
'vid' => [
'value' => 'es_test',
],
]),
'es_test_tag_2' => $this
->getCompleteTaxonomyTermInfos([
'vid' => [
'value' => 'es_test',
],
]),
],
],
'node' => [
'en' => [
'es_test_content_reference' => $this
->getCompleteNodeInfos([
'field_es_test_content_reference' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('node', 'es_test_to_be_deleted'),
],
];
},
],
'field_es_test_taxonomy' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('taxonomy_term', 'es_test_tag'),
],
[
'target_id' => $this
->getEntityId('taxonomy_term', 'es_test_tag_deleted'),
],
[
'target_id' => $this
->getEntityId('taxonomy_term', 'es_test_tag_2'),
],
];
},
],
]),
],
],
];
}
public function testMissingReferenceEntityValue() {
$this
->pullEveryChannels();
$existing_entities = $this->entityTypeManager
->getStorage('node')
->loadByProperties([
'uuid' => 'es_test_content_reference',
]);
$this
->assertNotEmpty($existing_entities, 'The content has been imported');
if (!empty($existing_entities)) {
$node = array_shift($existing_entities);
$content_reference_value = $node
->get('field_es_test_content_reference')
->getValue();
$expected_content_reference_value = [];
$this
->assertEquals($expected_content_reference_value, $content_reference_value, 'The content reference field is empty.');
$term_reference_value = $node
->get('field_es_test_taxonomy')
->getValue();
$expected_term_reference_value = [
[
'target_id' => $this
->getEntityId('taxonomy_term', 'es_test_tag'),
],
[
'target_id' => $this
->getEntityId('taxonomy_term', 'es_test_tag_2'),
],
];
$this
->assertEquals($expected_term_reference_value, $term_reference_value, 'The term reference field does not have the missing entity value and only reference 2 entities.');
}
$deleted_taxonomy_term_id = $this
->getEntityId('taxonomy_term', 'es_test_tag_deleted');
$this
->assertEmpty($deleted_taxonomy_term_id, 'The deleted taxonomy term has not been recreated.');
$deleted_node_id = $this
->getEntityId('node', 'es_test_to_be_deleted');
$this
->assertEmpty($deleted_node_id, 'The deleted node has not been recreated.');
}
protected function createChannel(UserInterface $user) {
parent::createChannel($user);
$channel_storage = $this->entityTypeManager
->getStorage('channel');
$channel = $channel_storage
->create([
'id' => 'taxonomy_term_es_test_en',
'label' => $this
->randomString(),
'channel_entity_type' => 'taxonomy_term',
'channel_bundle' => 'es_test',
'channel_langcode' => static::$entityLangcode,
'authorized_users' => [
$user
->uuid(),
],
]);
$channel
->save();
$this->channels[$channel
->id()] = $channel;
}
}