You are here

public function MultigraphServicesTest::testMultigraph in Monitoring 8

Test multigraph API calls.

File

modules/multigraph/tests/src/Functional/MultigraphServicesTest.php, line 54
Contains \Drupal\monitoring_multigraph\Tests\MultigraphServicesTest.

Class

MultigraphServicesTest
Tests for REST services provided by Monitoring Multigraph.

Namespace

Drupal\Tests\monitoring_multigraph\Functional

Code

public function testMultigraph() {
  $this
    ->drupalLogin($this->servicesAccount);
  $response_data = $this
    ->doJsonRequest('monitoring-multigraph');
  $this
    ->assertResponse(200);

  /** @var \Drupal\monitoring_multigraph\MultigraphInterface[] $multigraphs */
  $multigraphs = \Drupal::entityTypeManager()
    ->getStorage('monitoring_multigraph')
    ->loadMultiple();

  // Test the list of multigraphs.
  foreach ($multigraphs as $name => $multigraph) {
    $this
      ->assertEqual($response_data[$name]['id'], $multigraph
      ->id());
    $this
      ->assertEqual($response_data[$name]['label'], $multigraph
      ->label());
    $this
      ->assertEqual($response_data[$name]['description'], $multigraph
      ->getDescription());
    $this
      ->assertEqual($response_data[$name]['sensors'], $multigraph
      ->getSensorsRaw());
    $this
      ->assertEqual($response_data[$name]['uri'], Url::fromRoute('rest.monitoring-multigraph.GET', [
      'id' => $multigraph
        ->id(),
      '_format' => 'json',
    ])
      ->setAbsolute()
      ->toString());
  }

  // Test response for non-existing multigraph.
  $name = 'multigraph_that_does_not_exist';
  $this
    ->doJsonRequest('monitoring-multigraph/' . $name);
  $this
    ->assertResponse(404);

  // Test the predefined multigraph.
  $name = 'watchdog_severe_entries';
  $response_data = $this
    ->doJsonRequest('monitoring-multigraph/' . $name);
  $this
    ->assertResponse(200);
  $multigraph = $multigraphs[$name];
  $this
    ->assertEqual($response_data['id'], $multigraph
    ->id());
  $this
    ->assertEqual($response_data['label'], $multigraph
    ->label());
  $this
    ->assertEqual($response_data['description'], $multigraph
    ->getDescription());
  $this
    ->assertEqual($response_data['sensors'], $multigraph
    ->getSensorsRaw());
  $this
    ->assertEqual($response_data['uri'], Url::fromRoute('rest.monitoring-multigraph.GET', [
    'id' => $multigraph
      ->id(),
    '_format' => 'json',
  ])
    ->setAbsolute()
    ->toString());
}