View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
class LinkFieldTest extends EntityShareClientFunctionalTestBase {
public static $modules = [
'jsonapi_extras',
];
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected function setUp() : void {
parent::setUp();
$this->entityTypeManager
->getStorage('jsonapi_resource_config')
->create([
'id' => 'node--es_test',
'disabled' => FALSE,
'path' => 'node/es_test',
'resourceType' => 'node--es_test',
'resourceFields' => [
'field_es_test_link' => [
'fieldName' => 'field_es_test_link',
'publicName' => 'field_es_test_link',
'enhancer' => [
'id' => 'entity_share_uuid_link',
],
'disabled' => FALSE,
],
],
])
->save();
$this
->postSetupFixture();
}
protected function getEntitiesDataArray() {
return [
'node' => [
'en' => [
'es_test' => $this
->getCompleteNodeInfos([
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_link_external' => $this
->getCompleteNodeInfos([
'field_es_test_link' => [
'value' => [
[
'uri' => $this->faker->url,
'title' => $this->faker
->text(255),
],
],
'checker_callback' => 'getFilteredStructureValues',
],
]),
'es_test_link_external_without_text' => $this
->getCompleteNodeInfos([
'field_es_test_link' => [
'value' => [
[
'uri' => $this->faker->url,
],
],
'checker_callback' => 'getFilteredStructureValues',
],
]),
'es_test_link_external_with_options' => $this
->getCompleteNodeInfos([
'field_es_test_link' => [
'value' => [
[
'uri' => $this->faker->url,
'title' => $this->faker
->text(255),
'options' => [
'attributes' => [
'class' => [
$this->faker
->text(20),
$this->faker
->text(20),
$this->faker
->text(20),
],
],
],
],
],
'checker_callback' => 'getFilteredStructureValues',
],
]),
'es_test_link_internal' => $this
->getCompleteNodeInfos([
'field_es_test_link' => [
'value_callback' => function () {
return [
[
'uri' => 'entity:node/' . $this
->getEntityId('node', 'es_test'),
],
];
},
'checker_callback' => 'getExpectedInternalLinkValue',
],
]),
],
],
];
}
public function testBasicPull() {
$this
->pullEveryChannels();
$this
->checkCreatedEntities();
$this
->resetImportedContent();
$selected_entities = [
'es_test_link_internal',
];
$this
->importSelectedEntities($selected_entities);
$linking_entity = $this
->loadEntity('node', 'es_test_link_internal');
$this
->assertNotNull($linking_entity, 'The linking node has been created.');
$linked_entity = $this
->loadEntity('node', 'es_test');
$this
->assertNull($linked_entity, 'The linked node has not been created.');
$this
->mergePluginsToImportConfig([
'link_internal_content_importer' => [
'max_recursion_depth' => -1,
'weights' => [
'prepare_importable_entity_data' => 20,
],
],
]);
$this
->resetImportedContent();
$selected_entities = [
'es_test_link_internal',
];
$this
->importSelectedEntities($selected_entities);
$linking_entity = $this
->loadEntity('node', 'es_test_link_internal');
$this
->assertNotNull($linking_entity, 'The linking node has been created.');
$linked_entity = $this
->loadEntity('node', 'es_test');
$this
->assertNotNull($linked_entity, 'The linked node has been created.');
}
protected function populateRequestService() {
parent::populateRequestService();
$selected_entities = [
'es_test_link_internal',
];
$prepared_url = $this
->prepareUrlFilteredOnUuids($selected_entities, 'node_es_test_en');
$this
->discoverJsonApiEndpoints($prepared_url);
$route_name = sprintf('jsonapi.%s--%s.individual', 'node', 'es_test');
$linked_content_url = Url::fromRoute($route_name, [
'entity' => 'es_test',
])
->setOption('language', $this->container
->get('language_manager')
->getLanguage('en'))
->setOption('absolute', TRUE);
$this
->discoverJsonApiEndpoints($linked_content_url
->toString());
}
protected function getExpectedInternalLinkValue(ContentEntityInterface $entity, string $field_name) {
return [
[
'uri' => 'entity:node/6',
],
];
}
}