function JsonApiFunctionalTest::testMetaEvent

Tests adding metadata to a resource.

File

core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalTest.php, line 563

Class

JsonApiFunctionalTest
General functional test class.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testMetaEvent() : void {
  $this->createDefaultContent(3, 5, FALSE, FALSE, static::IS_NOT_MULTILINGUAL);
  // Tests resource meta is added.
  $this->container
    ->get('module_installer')
    ->install([
    'jsonapi_test_meta_events',
  ]);
  $node = $this->nodes[0];
  \Drupal::state()->set('jsonapi_test_meta_events.object_meta', [
    'enabled_type' => 'node--article',
    'enabled_id' => $node->uuid(),
    'fields' => [
      'title',
    ],
    'user_is_admin_context' => TRUE,
  ]);
  $this->drupalLogin($this->user);
  // Tests if the relationship has correct metadata when loading a single
  // resource.
  $result = Json::decode($this->drupalGet('jsonapi/node/article/' . $node->uuid()));
  $expectedMeta = [
    'resource_meta_user_has_admin_role' => 'no',
    'resource_meta_user_id' => $this->user
      ->id(),
    'resource_meta_title' => $node->getTitle(),
  ];
  $this->assertEquals($expectedMeta, $result['data']['meta']);
  // Test if the cache tags bubbled up
  $this->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'jsonapi_test_meta_events.object_meta');
  $this->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.roles');
  // 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()) {
      $this->assertEquals($expectedMeta, $resource['meta']);
    }
    else {
      $this->assertArrayNotHasKey('meta', $resource);
    }
  }
  // Test if the cache tags bubbled up
  $this->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'jsonapi_test_meta_events.object_meta');
  $this->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.roles');
  // Now try the same requests with a superuser, see if we get other caches
  $this->mink
    ->resetSessions();
  $this->drupalResetSession();
  $this->drupalLogin($this->adminUser);
  // Tests if the relationship has correct metadata when loading a single
  // resource.
  $result = Json::decode($this->drupalGet('jsonapi/node/article/' . $node->uuid()));
  $expectedMeta = [
    'resource_meta_user_has_admin_role' => 'yes',
    'resource_meta_user_id' => $this->adminUser
      ->id(),
    'resource_meta_title' => $node->getTitle(),
  ];
  $this->assertEquals($expectedMeta, $result['data']['meta']);
  // Test if the cache tags bubbled up.
  $this->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'jsonapi_test_meta_events.object_meta');
  $this->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.roles');
  $this->assertSession()
    ->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
  // Tests if the relationship has correct metadata when loading a single
  // resource.
  $result = Json::decode($this->drupalGet('jsonapi/node/article/' . $node->uuid()));
  $this->assertEquals($expectedMeta, $result['data']['meta']);
  // Test if the cache tags bubbled up.
  $this->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'jsonapi_test_meta_events.object_meta');
  $this->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.roles');
  $this->assertSession()
    ->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.