View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\entity_share\EntityShareUtility;
use Drupal\node\NodeInterface;
class InfiniteLoopTest extends EntityShareClientFunctionalTestBase {
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected function setUp() {
parent::setUp();
$this
->postSetupFixture();
}
protected function prepareContent() {
$node_storage = $this->entityTypeManager
->getStorage('node');
$node_1 = $node_storage
->create([
'uuid' => 'es_test_content_reference_one',
'type' => static::$entityBundleId,
'title' => $this
->randomString(),
'status' => NodeInterface::PUBLISHED,
]);
$node_1
->save();
$node_2 = $node_storage
->create([
'uuid' => 'es_test_content_reference_two',
'type' => static::$entityBundleId,
'title' => $this
->randomString(),
'status' => NodeInterface::PUBLISHED,
'field_es_test_content_reference' => $node_1
->id(),
]);
$node_2
->save();
$node_1
->set('field_es_test_content_reference', $node_2
->id());
$node_1
->save();
$this->entities = [
'node' => [
'es_test_content_reference_one' => $node_1,
'es_test_content_reference_two' => $node_2,
],
];
}
protected function getEntitiesDataArray() {
return [];
}
public function testInfiniteLoopFromNodeOne() {
$selected_entities = [
'es_test_content_reference_one',
];
$this
->infiniteLoopTestHelper($selected_entities);
}
public function testInfiniteLoopFromNodeTwo() {
$selected_entities = [
'es_test_content_reference_two',
];
$this
->infiniteLoopTestHelper($selected_entities);
}
protected function populateRequestService() {
parent::populateRequestService();
$selected_entities = [
'es_test_content_reference_one',
];
$prepared_url = $this
->prepareUrlFilteredOnUuids($selected_entities, 'node_es_test_en');
$this->jsonapiHelper
->setRemote($this->remote);
$http_client = $this->remoteManager
->prepareJsonApiClient($this->remote);
$this
->discoverJsonApiEndpoints($http_client, $prepared_url);
$selected_entities = [
'es_test_content_reference_two',
];
$prepared_url = $this
->prepareUrlFilteredOnUuids($selected_entities, 'node_es_test_en');
$this->jsonapiHelper
->setRemote($this->remote);
$http_client = $this->remoteManager
->prepareJsonApiClient($this->remote);
$this
->discoverJsonApiEndpoints($http_client, $prepared_url);
}
protected function infiniteLoopTestHelper(array $selected_entities) {
$prepared_url = $this
->prepareUrlFilteredOnUuids($selected_entities, 'node_es_test_en');
$this->jsonapiHelper
->setRemote($this->remote);
$http_client = $this->remoteManager
->prepareJsonApiClient($this->remote);
$response = $this->requestService
->request($http_client, 'GET', $prepared_url);
$json = Json::decode((string) $response
->getBody());
$this->jsonapiHelper
->importEntityListData(EntityShareUtility::prepareData($json['data']));
$uuids = [
'es_test_content_reference_one',
'es_test_content_reference_two',
];
foreach ($uuids as $uuid) {
$node = $this
->loadEntity('node', $uuid);
$this
->assertTrue($node instanceof NodeInterface, 'The node with the uuid ' . $uuid . ' has been recreated.');
}
}
}