public function QueryTopicTest::testSupportsFieldsIncludedInModule in Open Social 10.3.x
Test that the fields provided by the module can be queried.
File
- modules/
social_features/ social_topic/ tests/ src/ Kernel/ GraphQL/ QueryTopicTest.php, line 96
Class
- QueryTopicTest
- Tests the topic field on the Query type.
Namespace
Drupal\Tests\social_topic\Kernel\GraphQLCode
public function testSupportsFieldsIncludedInModule() : void {
$topic_types = Vocabulary::load('topic_types');
$topic_type_term = $this
->createTerm($topic_types);
$raw_topic_body = "This is a link test: https://social.localhost";
$html_topic_body = "<div><p>This is a link test: <a href=\"https://social.localhost\">https://social.localhost</a></p>\n</div>";
$topic_image = File::create();
$topic_image
->setFileUri('core/misc/druplicon.png');
$topic_image
->setFilename(\Drupal::service('file_system')
->basename($topic_image
->getFileUri()));
$topic_image
->save();
$topic = $this
->createNode([
'type' => 'topic',
'field_topic_type' => $topic_type_term
->id(),
'field_content_visibility' => 'public',
'field_topic_image' => $topic_image
->id(),
'status' => NodeInterface::PUBLISHED,
'body' => $raw_topic_body,
]);
// Set up a user that can view public topics and create a published
// comment and view it.
$this
->setUpCurrentUser([], array_merge([
'skip comment approval',
'access comments',
'view node.topic.field_content_visibility:public content',
], $this
->userPermissions()));
$comment = Comment::create([
'entity_id' => $topic
->id(),
'entity_type' => $topic
->getEntityTypeId(),
'comment_type' => 'comment',
'field_name' => 'comments',
]);
$comment
->save();
$query = '
query ($id: ID!) {
topic(id: $id) {
id
title
author {
id
displayName
}
type {
id
label
}
bodyHtml
comments(first: 1) {
nodes {
id
}
}
url
created {
timestamp
}
heroImage {
url
}
}
}
';
$this
->assertResults($query, [
'id' => $topic
->uuid(),
], [
'topic' => [
'id' => $topic
->uuid(),
'title' => $topic
->label(),
'author' => [
'id' => $topic
->getOwner()
->uuid(),
'displayName' => $topic
->getOwner()
->getDisplayName(),
],
'type' => [
'id' => $topic_type_term
->uuid(),
'label' => $topic_type_term
->label(),
],
'bodyHtml' => $html_topic_body,
'comments' => [
'nodes' => [
[
'id' => $comment
->uuid(),
],
],
],
'url' => $topic
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString(),
'created' => [
'timestamp' => $topic
->getCreatedTime(),
],
'heroImage' => [
'url' => file_create_url($topic_image
->getFileUri()),
],
],
], $this
->defaultCacheMetaData()
->setCacheMaxAge(0)
->addCacheableDependency($topic)
->addCacheableDependency($topic
->getOwner())
->addCacheTags([
'taxonomy_term:1',
'config:filter.format.plain_text',
'config:filter.settings',
'comment:1',
])
->addCacheContexts([
'languages:language_interface',
'user.node_grants:view',
'url.site',
]));
}