View source
<?php
namespace Drupal\Tests\graphql\Kernel\DataProducer\Routing;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
class RouteEntityTest extends GraphQLTestBase {
public function setUp() : void {
parent::setUp();
$content_type = NodeType::create([
'type' => 'event',
'name' => 'Event',
'translatable' => TRUE,
'display_submitted' => FALSE,
]);
$content_type
->save();
$this->published_node = Node::create([
'title' => 'Test Event',
'type' => 'event',
'status' => NodeInterface::PUBLISHED,
]);
$this->published_node
->save();
$this->translation_fr_published = $this->published_node
->addTranslation('fr', [
'title' => 'Test Event FR',
]);
$this->translation_fr_published
->save();
$this->translation_de_published = $this->published_node
->addTranslation('de', [
'title' => 'Test Event DE',
]);
$this->translation_de_published
->save();
$this->unpublished_node = Node::create([
'title' => 'Test Unpublished Event',
'type' => 'event',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$this->unpublished_node
->save();
$this->translation_fr_unpublished = $this->unpublished_node
->addTranslation('fr', [
'title' => 'Test Unpublished Event FR',
]);
$this->translation_fr_unpublished->status = NodeInterface::NOT_PUBLISHED;
$this->translation_fr_unpublished
->save();
$this->translation_de_unpublished = $this->unpublished_node
->addTranslation('de', [
'title' => 'Test Unpublished Event DE',
]);
$this->translation_de_unpublished->status = NodeInterface::NOT_PUBLISHED;
$this->translation_de_unpublished
->save();
$this->unpublished_to_published_node = Node::create([
'title' => 'Test Unpublished to Published Event',
'type' => 'event',
'status' => NodeInterface::NOT_PUBLISHED,
]);
$this->unpublished_to_published_node
->save();
$this->translation_fr_unpublished_to_published = $this->unpublished_to_published_node
->addTranslation('fr', [
'title' => 'Test Unpublished to Published Event FR',
]);
$this->translation_fr_unpublished_to_published->status = NodeInterface::PUBLISHED;
$this->translation_fr_unpublished_to_published
->save();
$this->translation_de_unpublished_to_published = $this->unpublished_to_published_node
->addTranslation('de', [
'title' => 'Test Unpublished to Published Event DE',
]);
$this->translation_de_unpublished_to_published->status = NodeInterface::PUBLISHED;
$this->translation_de_unpublished_to_published
->save();
$this->published_to_unpublished_node = Node::create([
'title' => 'Test Published to Unpublished Event',
'type' => 'event',
'status' => NodeInterface::PUBLISHED,
]);
$this->published_to_unpublished_node
->save();
$this->translation_fr_published_to_unpublished = $this->published_to_unpublished_node
->addTranslation('fr', [
'title' => 'Test Published to Unpublished Event FR',
]);
$this->translation_fr_published_to_unpublished->status = NodeInterface::NOT_PUBLISHED;
$this->translation_fr_published_to_unpublished
->save();
$this->translation_de_published_to_unpublished = $this->published_to_unpublished_node
->addTranslation('de', [
'title' => 'Test Published to Unpublished Event DE',
]);
$this->translation_de_published_to_unpublished->status = NodeInterface::NOT_PUBLISHED;
$this->translation_de_published_to_unpublished
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'event', TRUE);
}
public function testRouteEntity() : void {
$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());
$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);
}
$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());
$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);
}
$this
->assertNull($this
->executeDataProducer('route_entity', [
'url' => 'not_a_url',
]));
$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,
]);
$this
->assertNull($result);
}
}