external_entity-1.0.x-dev/tests/src/Kernel/ExternalEntityConnectionTest.php
tests/src/Kernel/ExternalEntityConnectionTest.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\external_entity\Kernel;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\Attributes\Group;
use Drupal\Core\Form\FormStateInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use Drupal\external_entity\Entity\Query\SearchQuery;
use Drupal\Tests\external_entity\Concerns\HttpClient;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\external_entity\Entity\ExternalEntityConnection;
use Drupal\Tests\external_entity\Concerns\StubbedHttpResponses;
use Drupal\Tests\external_entity\Concerns\ExternalEntityInstances;
use Drupal\external_entity\Definition\ExternalEntitySearchDefinition;
use Drupal\external_entity\Definition\ExternalEntityDefaultDefinition;
use Drupal\external_entity\Contracts\ExternalEntityConnectionInterface;
use Drupal\external_entity\Definition\ExternalEntityResourceDefinition;
use Drupal\external_entity\Plugin\ExternalEntity\ConnectionType\ExternalEntityServer;
use Drupal\external_entity\Plugin\ExternalEntity\AuthenticationType\BasicAuthenticationType;
/**
* Define test for the external entity connection form.
*/
#[Group('external_entity')]
#[CoversClass(ExternalEntityServer::class)]
#[CoversClass(BasicAuthenticationType::class)]
#[CoversClass(ExternalEntityConnection::class)]
class ExternalEntityConnectionTest extends EntityKernelTestBase {
use HttpClient;
use StubbedHttpResponses;
use ExternalEntityInstances;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'views',
'external_entity'
];
/**
* @return void
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function testExternalConnectionFoundation(): void {
$connection = $this->createExternalEntityConnectionInstance();
$connection_type = $connection->connectionTypeInstance();
$this->assertSame([
'server_domain' => 'https://example.com',
'resources' => ['node' => 'node'],
], $connection_type->getConfiguration());
$this->assertInstanceOf(ExternalEntityServer::class, $connection_type);
$authentication_type = $connection->createAuthenticationTypeInstance();
$this->assertSame([
'username' => 'admin',
'password' => 'password',
], $authentication_type->getConfiguration());
$this->assertInstanceOf(BasicAuthenticationType::class, $authentication_type);
}
/**
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \JsonException
*/
public function testBasicAuthentication(): void {
$requestHistory = [];
$client = $this->mockHttpClientFactory(new Response(
headers: ['Content-Type' => 'application/json'],
body: json_encode($this->fetchStubbedData(), JSON_THROW_ON_ERROR)
), $requestHistory);
$connection = $this->createMock(ExternalEntityConnectionInterface::class);
$authentication_type = new BasicAuthenticationType([
'username' => 'admin',
'password' => 'password',
], 'basic_authentication', []);
$authentication_type->setConnection($connection);
$connection->method('createAuthenticationTypeInstance')->willReturn($authentication_type);
$connection_type = new ExternalEntityServer([
'server_domain' => 'https://example.com',
'resources' => ['node' => 'node'],
], 'external_entity_server', [], $client);
$connection_type->setConnection($connection);
$connection_type->fetchResourceDefinitions();
$auth = $requestHistory[0]['options']['auth'];
$this->assertEqualsCanonicalizing(['admin', 'password'], $auth);
}
/**
* @return void
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \JsonException
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testExternalEntityServerValidateConfiguration(): void {
$this->mockHttpClientFactory(new Response(
headers: ['Content-Type' => 'application/json'],
body: json_encode([
'status' => 'error',
'version' => '1.0.0'
], JSON_THROW_ON_ERROR),
));
$instance = $this->createConnectionTypeInstance('external_entity_server', [
'server_domain' => 'https://example.com',
'resources' => ['node' => 'node']
]);
$form = ['server_domain' => []];
$form_state = $this->createMock(FormStateInterface::class);
$form_state
->method('hasValue')
->willReturn(TRUE);
$form_state
->expects($this->once())
->method('setError')
->willReturn(TRUE);
$instance->validateConfigurationForm(
$form,
$form_state
);
}
/**
* @return void
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \JsonException
* @throws \PHPUnit\Framework\MockObject\Exception
*/
public function testExternalEntityServerLookupDefinitions(): void {
$this->mockHttpClientFactory(new Response(
headers: ['Content-Type' => 'application/json'],
body: json_encode($this->lookupStubbedData(
'0ac544a2-6bf9-4886-9da9-b07efc681d3f'),
JSON_THROW_ON_ERROR
),
));
$instance = $this->createConnectionTypeInstance('external_entity_server', [
'server_domain' => 'https://example.com',
'resources' => ['node' => 'node']
]);
$resources = $instance->lookupDefinitions(['0ac544a2-6bf9-4886-9da9-b07efc681d3f']);
$this->assertInstanceOf(
ExternalEntityDefaultDefinition::class,
$resources['0ac544a2-6bf9-4886-9da9-b07efc681d3f']
);
}
/**
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \JsonException
*/
public function testFetchResourceDefinitions(): void {
$this->mockHttpClientFactory(new Response(
headers: ['Content-Type' => 'application/json'],
body: json_encode($this->fetchStubbedData(), JSON_THROW_ON_ERROR),
));
$instance = $this->createConnectionTypeInstance('external_entity_server', [
'server_domain' => 'https://example.com',
'resources' => ['node' => 'node', 'paragraphs' => 'paragraphs']
]);
$resources = $instance->fetchResourceDefinitions();
$this->assertArrayHasKey('node', $resources);
$definition = $resources['node'];
$this->assertInstanceOf(ExternalEntityResourceDefinition::class, $definition);
$this->assertNotEmpty($definition->getProperties());
$this->assertNotEmpty($definition->getVariations());
}
/**
* @throws \PHPUnit\Framework\MockObject\Exception
* @throws \JsonException
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
public function testSearchDefinitions(): 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);
$instance = $this->createConnectionTypeInstance('external_entity_server', [
'server_domain' => 'https://example.com'
]);
$definitions = $instance->searchDefinitions('node', $search_query);
$this->assertInstanceOf(ExternalEntitySearchDefinition::class, $definitions);
}
}
