You are here

public function NodeJsonOutput::testNode in Metatag 8

Create an entity, view its JSON output, confirm Metatag data exists.

File

tests/src/Functional/NodeJsonOutput.php, line 49

Class

NodeJsonOutput
Verify that the JSON output from core works as intended.

Namespace

Drupal\Tests\metatag\Functional

Code

public function testNode() {
  $this
    ->provisionResource();

  /* @var\Drupal\node\NodeInterface $node */
  $node = $this
    ->createContentTypeNode('Test JSON output', 'Testing JSON output for a content type');
  $url = $node
    ->toUrl();

  // Load the node's page.
  $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Load the JSON output.
  $url
    ->setOption('query', [
    '_format' => 'json',
  ]);
  $response = $this
    ->drupalGet($url);
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Decode the JSON output.
  $response = $this
    ->getSession()
    ->getPage()
    ->getContent();
  $this
    ->assertNotEmpty($response);
  $json = json_decode($response);
  $this
    ->verbose($json, 'JSON output');
  $this
    ->assertNotEmpty($json);

  // Confirm the JSON object's values.
  $this
    ->assertTrue(isset($json->nid));
  if (isset($json->nid)) {
    $this
      ->assertTrue($json->nid[0]->value == $node
      ->id());
  }
  $this
    ->assertTrue(isset($json->metatag));
  if (isset($json->metatag)) {
    $this
      ->assertTrue($json->metatag->value->title == $node
      ->label() . ' | Drupal');

    // @todo Test other meta tags.
  }
}