external_entity-1.0.x-dev/tests/src/Kernel/ExternalEntityTest.php
tests/src/Kernel/ExternalEntityTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\external_entity\Kernel;
use GuzzleHttp\Psr7\Response;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\Group;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\external_entity\Entity\ExternalEntity;
use Drupal\external_entity\Entity\Query\SearchQuery;
use Drupal\Tests\external_entity\Concerns\HttpClient;
use Drupal\Tests\external_entity\Concerns\StubbedHttpResponses;
use Drupal\Tests\external_entity\Concerns\ExternalEntityInstances;
/**
* Test description.
*/
#[Group('external_entity')]
final class ExternalEntityTest extends KernelTestBase {
use HttpClient;
use StubbedHttpResponses;
use ExternalEntityInstances;
protected $strictConfigSchema = FALSE;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'views',
'external_entity',
'external_entity_test',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig('external_entity_test');
$this->installEntitySchema('external_entity');
}
/**
* @return void
*/
public function testIdStructure(): void {
/** @var \Drupal\external_entity\Annotation\ExternalEntityConnectionType $entity */
$entity = new ExternalEntity([
'uuid' => '0ac544a2-6bf9-4886-9da9-b07efc741d3e',
'type' => 'test_bundle',
], 'external_entity');
$this->assertEquals(
implode(ExternalEntity::ID_DELIMITER, [
$entity->bundle(),
$entity->uuid()
]),
$entity->id(),
);
}
/**
* @return void
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\external_entity\Exception\NotFoundResourceDisplayException
* @throws \JsonException
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testExternalEntityProperties(): void {
$this->mockHttpClientFactory([
new Response(
headers: ['Content-Type' => 'application/json'],
body: json_encode($this->searchStubbedData(), JSON_THROW_ON_ERROR),
),
]);
$search_query = $this->createMock(SearchQuery::class);
/** @var \Drupal\external_entity\Contracts\ExternalEntityConnectionInterface $instance */
$instance = $this->createConnectionTypeInstance('external_entity_server', [
'server_domain' => 'https://example.com'
]);
$search_definition = $instance->searchDefinitions('node', $search_query);
$uuid = '0ac544a2-6bf9-4886-9da9-b07abc1234ef';
$external_entity_definition = $search_definition->getResults()[$uuid];
/** @var \Drupal\external_entity\Contracts\ExternalEntityStorageInterface $external_entity_storage */
$external_entity_storage = \Drupal::service('entity_type.manager')->getStorage('external_entity');
$entity = $external_entity_storage->createEntityFromDefinition(
'demo',
$external_entity_definition
);
$properties = $entity->getProperties();
$this->assertNotEmpty($properties);
$this->assertEmpty(array_diff_key(
$properties,
array_flip(['id', 'uuid', 'label', 'body']))
);
}
/**
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \JsonException
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\external_entity\Exception\NotFoundResourceDisplayException
*/
public function testExternalEntityFields(): void {
$this->mockHttpClientFactory([
new Response(
headers: ['Content-Type' => 'application/json'],
body: json_encode($this->searchStubbedData(), JSON_THROW_ON_ERROR),
),
new Response(
headers: ['Content-Type' => 'application/json'],
body: json_encode($this->fetchStubbedData(), JSON_THROW_ON_ERROR),
)
]);
$search_query = $this->createMock(SearchQuery::class);
/** @var \Drupal\external_entity\Contracts\ExternalEntityConnectionInterface $instance */
$instance = $this->createConnectionTypeInstance('external_entity_server', [
'server_domain' => 'https://example.com'
]);
$search_definition = $instance->searchDefinitions('node', $search_query);
$uuid = '0ac544a2-6bf9-4886-9da9-b07abc1234ef';
$external_entity_definition = $search_definition->getResults()[$uuid];
/** @var \Drupal\external_entity\Contracts\ExternalEntityStorageInterface $external_entity_storage */
$external_entity_storage = \Drupal::service('entity_type.manager')->getStorage('external_entity');
$entity = $external_entity_storage->createEntityFromDefinition(
'demo',
$external_entity_definition
);
$fields = $entity->getFields();
$this->assertNotEmpty($fields);
$this->assertContainsOnlyInstancesOf(FieldItemListInterface::class, $fields);
}
/**
* @return void
* @throws \JsonException
* @throws \PHPUnit\Framework\MockObject\Exception
* @coversClass \Drupal\external_entity\Entity\ExternalEntity::getFieldDefinitions
* @coversClass \Drupal\external_entity\Entity\ExternalEntity::getFieldDefinition
*/
public function testFieldDefinitions(): void {
$this->mockHttpClientFactory(new Response(
headers: ['Content-Type' => 'application/json'],
body: json_encode($this->fetchStubbedData(), JSON_THROW_ON_ERROR),
));
$entity = $this->createExternalEntityInstance();
$definitions = $entity->getFieldDefinitions();
$this->assertNotEmpty($entity->getFieldDefinition('uuid'));
$this->assertEmpty(array_diff_key($definitions, array_flip(['id', 'uuid', 'label'])));
$this->assertContainsOnlyInstancesOf(
BaseFieldDefinition::class,
$definitions
);
}
/**
* @return \Drupal\external_entity\Entity\ExternalEntity
*/
protected function createExternalEntityInstance(): ExternalEntity {
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$storage = \Drupal::service('entity_type.manager')->getStorage('external_entity');
/** @var \Drupal\external_entity\Entity\ExternalEntity $entity */
return $storage->create([
'type' => 'demo',
'resource' => 'node',
'variation' => 'page',
'uuid' => '0ac544a2-6bf9-4886-9da9-b07efc681d3e',
]);
}
}
