public function EntityShareServerFunctionalTest::testBasicChannel in Entity Share 8.3
Same name and namespace in other branches
- 8.2 modules/entity_share_server/tests/src/Functional/EntityShareServerFunctionalTest.php \Drupal\Tests\entity_share_server\Functional\EntityShareServerFunctionalTest::testBasicChannel()
Test that a channel provides correct URLs.
File
- modules/
entity_share_server/ tests/ src/ Functional/ EntityShareServerFunctionalTest.php, line 24
Class
- EntityShareServerFunctionalTest
- General functional test class.
Namespace
Drupal\Tests\entity_share_server\FunctionalCode
public function testBasicChannel() {
// Prepare a node and its translation.
$node = $this
->createNode([
'type' => 'es_test',
'uuid' => 'es_test',
'title' => 'Entity share test 1 en',
'status' => NodeInterface::PUBLISHED,
]);
$node
->addTranslation('fr', [
'title' => 'Entity share test 1 fr',
]);
$node
->save();
// Create channels.
$channel_storage = $this->entityTypeManager
->getStorage('channel');
$es_test_en_channel = $channel_storage
->create([
'id' => 'es_test_en',
'label' => 'Entity share test en',
'channel_entity_type' => 'node',
'channel_bundle' => 'es_test',
'channel_langcode' => 'en',
'authorized_users' => [
$this->channelUser
->uuid(),
],
]);
$es_test_en_channel
->save();
$es_test_fr_channel = $channel_storage
->create([
'id' => 'es_test_fr',
'label' => 'Entity share test fr',
'channel_entity_type' => 'node',
'channel_bundle' => 'es_test',
'channel_langcode' => 'fr',
'authorized_users' => [
$this->channelUser
->uuid(),
],
]);
$es_test_fr_channel
->save();
$entity_share_entrypoint_url = Url::fromRoute('entity_share_server.resource_list');
$response = $this
->request('GET', $entity_share_entrypoint_url, $this
->getAuthenticationRequestOptions($this->channelUser));
$entity_share_endpoint_response = Json::decode((string) $response
->getBody());
$expected_search_configuration = [
'label' => [
'path' => 'title',
'label' => 'Label',
],
];
// Test that the field_mapping entry exists.
$this
->assertTrue(isset($entity_share_endpoint_response['data']['field_mappings']), 'The field mappings has been found');
// Test the english channel info.
$this
->assertTrue(isset($entity_share_endpoint_response['data']['channels']['es_test_en']), 'The english channel has been found');
$this
->assertEquals($entity_share_endpoint_response['data']['channels']['es_test_en']['label'], $es_test_en_channel
->label(), 'The expected channel label has been found.');
$this
->assertEquals($entity_share_endpoint_response['data']['channels']['es_test_en']['channel_entity_type'], $es_test_en_channel
->get('channel_entity_type'), 'The expected channel entity type has been found.');
$this
->assertEquals($entity_share_endpoint_response['data']['channels']['es_test_en']['search_configuration'], $expected_search_configuration, 'The expected search configuration had been found.');
// Test that the node can be found on the channel URL.
$response = $this
->request('GET', Url::fromUri($entity_share_endpoint_response['data']['channels']['es_test_en']['url']), $this
->getAuthenticationRequestOptions($this->channelUser));
$es_test_en_channel_url_response = Json::decode((string) $response
->getBody());
$this
->assertEquals($es_test_en_channel_url_response['data'][0]['attributes']['title'], $node
->label(), 'The channel url is correct. The created node has been found.');
// Test that the channel URL uuid contains only changed timestamp.
$response = $this
->request('GET', Url::fromUri($entity_share_endpoint_response['data']['channels']['es_test_en']['url_uuid']), $this
->getAuthenticationRequestOptions($this->channelUser));
$es_test_en_channel_url_uuid_response = Json::decode((string) $response
->getBody());
$this
->assertEquals(count($es_test_en_channel_url_uuid_response['data'][0]['attributes']), 1, 'There is only one attribute.');
$this
->assertTrue(isset($es_test_en_channel_url_uuid_response['data'][0]['attributes']['changed']), 'The only attribute is changed.');
// Test the French channel info.
$this
->assertTrue(isset($entity_share_endpoint_response['data']['channels']['es_test_fr']), 'The French channel has been found');
$this
->assertEquals($entity_share_endpoint_response['data']['channels']['es_test_fr']['label'], $es_test_fr_channel
->label(), 'The expected channel label has been found.');
$this
->assertEquals($entity_share_endpoint_response['data']['channels']['es_test_fr']['channel_entity_type'], $es_test_fr_channel
->get('channel_entity_type'), 'The expected channel entity type has been found.');
$this
->assertEquals($entity_share_endpoint_response['data']['channels']['es_test_fr']['search_configuration'], $expected_search_configuration, 'The expected search configuration had been found.');
// Test that the node translation can be found on the channel URL.
$response = $this
->request('GET', Url::fromUri($entity_share_endpoint_response['data']['channels']['es_test_fr']['url']), $this
->getAuthenticationRequestOptions($this->channelUser));
$es_test_fr_channel_url_response = Json::decode((string) $response
->getBody());
$this
->assertEquals($es_test_fr_channel_url_response['data'][0]['attributes']['title'], $node
->getTranslation('fr')
->label(), 'The channel url is correct. The created node has been found.');
}