function JsonApiFunctionalTest::testMetaRelationEvent
Tests adding metadata to the relationship.
File
-
core/
modules/ jsonapi/ tests/ src/ Functional/ JsonApiFunctionalTest.php, line 637
Class
- JsonApiFunctionalTest
- General functional test class.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
public function testMetaRelationEvent() : void {
$this->createDefaultContent(3, 5, FALSE, FALSE, static::IS_NOT_MULTILINGUAL);
$this->container
->get('module_installer')
->install([
'jsonapi_test_meta_events',
]);
$node = $this->nodes[0];
\Drupal::state()->set('jsonapi_test_meta_events.relationship_meta', [
'enabled_type' => 'node--article',
'enabled_relation' => 'field_tags',
'enabled_id' => $node->uuid(),
'fields' => [
'name',
],
]);
// Test if the relationship has the correct metadata when loading a
// resource collection.
$result = Json::decode($this->drupalGet('jsonapi/node/article'));
foreach ($result['data'] as $resource) {
if ($resource['id'] === $node->uuid()) {
$tagNames = $resource['relationships']['field_tags']['meta']['relationship_meta_name'];
$tags = $node->field_tags
->referencedEntities();
$this->assertCount(count($tags), $tagNames);
foreach ($tags as $tag) {
$this->assertContains($tag->label(), $tagNames);
}
}
else {
$this->assertArrayNotHasKey('meta', $resource['relationships']['field_tags']);
}
}
// Test if the cache tags bubbled up.
$this->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'jsonapi_test_meta_events.relationship_meta');
// Test if relationship has correct metadata when loading a single resource
$resource = Json::decode($this->drupalGet('jsonapi/node/article/' . $node->uuid()));
if ($resource['data']['id'] === $node->uuid()) {
$tagNames = $resource['data']['relationships']['field_tags']['meta']['relationship_meta_name'];
$tags = $node->field_tags
->referencedEntities();
$this->assertCount(count($tags), $tagNames);
foreach ($tags as $tag) {
$this->assertContains($tag->label(), $tagNames);
}
}
// Test if the cache tags bubbled up
$this->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'jsonapi_test_meta_events.relationship_meta');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.