View source
<?php
namespace Drupal\Tests\graphql\Kernel\DataProducer;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\UserInterface;
use Drupal\node\Entity\NodeType;
use Drupal\node\Entity\Node;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTestBundle;
class EntityTest extends GraphQLTestBase {
protected $node;
public function setUp() : void {
parent::setUp();
$this->entity = $this
->getMockBuilder(NodeInterface::class)
->disableOriginalConstructor()
->getMock();
$this->entity_interface = $this
->getMockBuilder(EntityInterface::class)
->disableOriginalConstructor()
->getMock();
$this->user = $this
->getMockBuilder(UserInterface::class)
->disableOriginalConstructor()
->getMock();
$content_type = NodeType::create([
'type' => 'lorem',
'name' => 'ipsum',
'translatable' => TRUE,
'display_submitted' => FALSE,
]);
$content_type
->save();
$content_type = NodeType::create([
'type' => 'otherbundle',
'name' => 'otherbundle',
'translatable' => TRUE,
'display_submitted' => FALSE,
]);
$content_type
->save();
$this->node = Node::create([
'title' => 'Dolor',
'type' => 'lorem',
]);
$this->node
->save();
$this->translation_fr = $this->node
->addTranslation('fr', [
'title' => 'sit amet fr',
]);
$this->translation_fr
->save();
$this->translation_de = $this->node
->addTranslation('de', [
'title' => 'sit amet de',
]);
$this->translation_de
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'lorem', TRUE);
}
public function testResolveBundle() : void {
$this->entity
->expects($this
->once())
->method('bundle')
->willReturn('page');
$result = $this
->executeDataProducer('entity_bundle', [
'entity' => $this->entity,
]);
$this
->assertEquals('page', $result);
}
public function testResolveChanged() : void {
$this->entity
->expects($this
->once())
->method('getChangedTime')
->willReturn(17000000000);
$this
->assertEquals('2508-09-16', $this
->executeDataProducer('entity_changed', [
'format' => 'Y-m-d',
'entity' => $this->entity,
]));
$this
->assertNull($this
->executeDataProducer('entity_changed', [
'format' => 'Y-m-d',
'entity' => $this->entity_interface,
]));
}
public function testResolveCreated() : void {
$this->entity
->expects($this
->once())
->method('getCreatedTime')
->willReturn(17000000000);
$this
->assertEquals('2508-09-16', $this
->executeDataProducer('entity_created', [
'format' => 'Y-m-d',
'entity' => $this->entity,
]));
$this
->assertNull($this
->executeDataProducer('entity_created', [
'format' => 'Y-m-d',
'entity' => $this->entity_interface,
]));
}
public function testResolveDescription() : void {
$entity = $this
->getMockBuilder(EntityTestBundle::class)
->disableOriginalConstructor()
->getMock();
$entity
->expects($this
->once())
->method('getDescription')
->willReturn('Dummy description');
$this
->assertEquals('Dummy description', $this
->executeDataProducer('entity_description', [
'entity' => $entity,
]));
}
public function testResolveId() : void {
$this->entity
->expects($this
->once())
->method('id')
->willReturn(5);
$this
->assertEquals(5, $this
->executeDataProducer('entity_id', [
'entity' => $this->entity,
]));
}
public function testResolveLabel() : void {
$this->entity
->expects($this
->once())
->method('label')
->willReturn('Dummy label');
$this
->assertEquals('Dummy label', $this
->executeDataProducer('entity_label', [
'entity' => $this->entity,
]));
}
public function testResolveLanguage() : void {
$language = $this
->getMockBuilder(LanguageInterface::class)
->disableOriginalConstructor()
->getMock();
$this->entity
->expects($this
->once())
->method('language')
->willReturn($language);
$this
->assertEquals($language, $this
->executeDataProducer('entity_language', [
'entity' => $this->entity,
]));
}
public function testResolveOwner() : void {
$this->entity
->expects($this
->once())
->method('getOwner')
->willReturn($this->user);
$this
->assertEquals($this->user, $this
->executeDataProducer('entity_owner', [
'entity' => $this->entity,
]));
$this
->assertNull($this
->executeDataProducer('entity_owner', [
'entity' => $this->entity_interface,
]));
}
public function testResolveEntityTypeId() : void {
$this->entity
->expects($this
->once())
->method('getEntityTypeId')
->willReturn('test_graphql');
$this
->assertEquals('test_graphql', $this
->executeDataProducer('entity_type_id', [
'entity' => $this->entity,
]));
}
public function testResolvePublished() : void {
$this->entity
->expects($this
->once())
->method('isPublished')
->willReturn(TRUE);
$this
->assertEquals(TRUE, $this
->executeDataProducer('entity_published', [
'entity' => $this->entity,
]));
$this
->assertNull($this
->executeDataProducer('entity_published', [
'entity' => $this->entity_interface,
]));
}
public function testResolveAccess() : void {
$this->entity
->expects($this
->any())
->method('access')
->willReturn(FALSE);
$this
->assertFalse($this
->executeDataProducer('entity_access', [
'entity' => $this->entity,
'user' => $this->user,
'operation' => 'delete',
]));
}
public function testResolveTranslation() : void {
$french = $this
->executeDataProducer('entity_translation', [
'entity' => $this->node,
'language' => 'fr',
]);
$this
->assertEquals('sit amet fr', $french
->label());
$translations = $this
->executeDataProducer('entity_translations', [
'entity' => $this->node,
]);
$this
->assertEquals('Dolor', $translations['en']
->label());
$this
->assertEquals('sit amet fr', $translations['fr']
->label());
$this
->assertEquals('sit amet de', $translations['de']
->label());
}
public function testResolveUrl() : void {
$url = $this
->getMockBuilder(Url::class)
->disableOriginalConstructor()
->getMock();
$this->entity
->expects($this
->once())
->method('toUrl')
->willReturn($url);
$this
->assertEquals($url, $this
->executeDataProducer('entity_url', [
'entity' => $this->entity,
]));
}
public function testResolveUuid() : void {
$this->entity
->expects($this
->once())
->method('uuid')
->willReturn('some uuid');
$this
->assertEquals('some uuid', $this
->executeDataProducer('entity_uuid', [
'entity' => $this->entity,
]));
}
public function testResolveEntityLoad() : void {
$result = $this
->executeDataProducer('entity_load', [
'type' => $this->node
->getEntityTypeId(),
'id' => $this->node
->id(),
]);
$this
->assertEquals($this->node
->id(), $result
->id());
}
public function testResolveEntityLoadByUuid() : void {
$result = $this
->executeDataProducer('entity_load_by_uuid', [
'type' => $this->node
->getEntityTypeId(),
'uuid' => $this->node
->uuid(),
]);
$this
->assertEquals($this->node
->id(), $result
->id());
}
public function testResolveUnknownEntityLoad() : void {
$result = $this
->executeDataProducer('entity_load', [
'type' => $this->node
->getEntityTypeId(),
'id' => 0,
]);
$this
->assertNull($result);
}
public function testResolveMismatchEntityLoad() : void {
$result = $this
->executeDataProducer('entity_load', [
'type' => $this->node
->getEntityTypeId(),
'id' => $this->node
->id(),
'bundles' => [
'otherbundle',
],
]);
$this
->assertNull($result);
}
public function testResolveTranslatedEntityLoad() : void {
$result = $this
->executeDataProducer('entity_load', [
'type' => $this->node
->getEntityTypeId(),
'id' => $this->node
->id(),
'language' => 'fr',
]);
$this
->assertEquals('fr', $result
->language()
->getId());
$this
->assertEquals('sit amet fr', $result
->getTitle());
}
public function testResolveEntityRendered() : void {
$result = $this
->executeDataProducer('entity_rendered', [
'entity' => $this->node,
'mode' => 'default',
]);
$this
->assertStringContainsString('<a href="/node/1" rel="bookmark"><span>' . $this->node
->getTitle() . '</span>', $result);
}
}