You are here

public function RouteEntityTest::testRouteEntity in GraphQL 8.4

@covers \Drupal\graphql\Plugin\GraphQL\DataProducer\Routing\RouteEntity::resolve

File

tests/src/Kernel/DataProducer/Routing/RouteEntityTest.php, line 100

Class

RouteEntityTest
Test class for the RouteEntity data producer.

Namespace

Drupal\Tests\graphql\Kernel\DataProducer\Routing

Code

public function testRouteEntity() : void {

  // Published node to published translations.
  $url = Url::fromRoute('entity.node.canonical', [
    'node' => $this->published_node
      ->id(),
  ]);
  $result = $this
    ->executeDataProducer('route_entity', [
    'url' => $url,
  ]);
  $this
    ->assertEquals($this->published_node
    ->id(), $result
    ->id());
  $this
    ->assertEquals($this->published_node
    ->label(), $result
    ->label());
  $result = $this
    ->executeDataProducer('route_entity', [
    'url' => $url,
    'language' => 'fr',
  ]);
  $this
    ->assertEquals($this->translation_fr_published
    ->id(), $result
    ->id());
  $this
    ->assertEquals($this->translation_fr_published
    ->label(), $result
    ->label());
  $result = $this
    ->executeDataProducer('route_entity', [
    'url' => $url,
    'language' => 'de',
  ]);
  $this
    ->assertEquals($this->translation_de_published
    ->id(), $result
    ->id());
  $this
    ->assertEquals($this->translation_de_published
    ->label(), $result
    ->label());

  // Unpublished node to unpublished translations. Make sure we are not
  // allowed to get the unpublished nodes or translations.
  $url = Url::fromRoute('entity.node.canonical', [
    'node' => $this->unpublished_node
      ->id(),
  ]);
  foreach ([
    NULL,
    'fr',
    'de',
  ] as $lang) {
    $result = $this
      ->executeDataProducer('route_entity', [
      'url' => $url,
      'language' => $lang,
    ]);
    $this
      ->assertNull($result);
  }

  // Unpublished node to published translations. Make sure we are not able to
  // get unpublished source, but we are able to get published translations.
  $url = Url::fromRoute('entity.node.canonical', [
    'node' => $this->unpublished_to_published_node
      ->id(),
  ]);
  $result = $this
    ->executeDataProducer('route_entity', [
    'url' => $url,
  ]);
  $this
    ->assertNull($result);
  $result = $this
    ->executeDataProducer('route_entity', [
    'url' => $url,
    'language' => 'fr',
  ]);
  $this
    ->assertEquals($this->translation_fr_unpublished_to_published
    ->id(), $result
    ->id());
  $this
    ->assertEquals($this->translation_fr_unpublished_to_published
    ->label(), $result
    ->label());
  $result = $this
    ->executeDataProducer('route_entity', [
    'url' => $url,
    'language' => 'de',
  ]);
  $this
    ->assertEquals($this->translation_de_unpublished_to_published
    ->id(), $result
    ->id());
  $this
    ->assertEquals($this->translation_de_unpublished_to_published
    ->label(), $result
    ->label());

  // Published node to unpublished translations. Make sure we are able to get
  // published source, but we are not able to get unpublished translations.
  $url = Url::fromRoute('entity.node.canonical', [
    'node' => $this->published_to_unpublished_node
      ->id(),
  ]);
  $result = $this
    ->executeDataProducer('route_entity', [
    'url' => $url,
  ]);
  $this
    ->assertEquals($this->published_to_unpublished_node
    ->id(), $result
    ->id());
  $this
    ->assertEquals($this->published_to_unpublished_node
    ->label(), $result
    ->label());
  foreach ([
    'fr',
    'de',
  ] as $lang) {
    $result = $this
      ->executeDataProducer('route_entity', [
      'url' => $url,
      'language' => $lang,
    ]);
    $this
      ->assertNull($result);
  }

  // Test with something which is not a URL.
  $this
    ->assertNull($this
    ->executeDataProducer('route_entity', [
    'url' => 'not_a_url',
  ]));

  // Test the 4xx response.
  $temp_node = Node::create([
    'title' => 'Temp node',
    'type' => 'event',
    'status' => NodeInterface::PUBLISHED,
  ]);
  $temp_node
    ->save();
  $url = Url::fromRoute('entity.node.canonical', [
    'node' => $temp_node
      ->id(),
  ]);
  $temp_node
    ->delete();
  $result = $this
    ->executeDataProducer('route_entity', [
    'url' => $url,
  ]);

  // The result has to be null and the metadata has to contain the node_list
  // and the 4xx-response cache tags.
  $this
    ->assertNull($result);

  // @todo Add cache checks.
  // $this->assertContains('node_list', $metadata->getCacheTags());
  // $this->assertContains('4xx-response', $metadata->getCacheTags());
}