View source
<?php
namespace Drupal\Tests\gatsby_instantpreview\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
class GatsbyInstantPreviewTest extends KernelTestBase {
use ContentTypeCreationTrait;
use UserCreationTrait;
use EntityReferenceTestTrait;
protected $strictConfigSchema = FALSE;
protected static $modules = [
'gatsby_instantpreview',
'node',
'gatsby',
'jsonapi',
'serialization',
'jsonapi_extras',
'field',
'text',
'options',
'path_alias',
'system',
'user',
'filter',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'node',
'filter',
]);
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installEntitySchema('path_alias');
$this
->createContentType([
'type' => 'page',
]);
$config_factory = \Drupal::configFactory();
$config_factory
->getEditable('gatsby.settings')
->set('preview_entity_types', [
'node',
])
->save();
$this
->setUpCurrentUser([], [
'access content',
]);
$this
->createEntityReferenceField('node', 'page', 'field_reference', 'Referencs', 'node');
}
public function testBuildRelationshipJson() {
$node = Node::create([
'type' => 'page',
'status' => 1,
'title' => 'foo',
]);
$node
->save();
$preview = \Drupal::service('gatsby.gatsby_instantpreview');
$entity_data = [];
$data = $preview
->getJson($node);
$preview
->buildRelationshipJson($data['data']['relationships'], $entity_data, [
'user',
]);
$this
->assertTrue(!empty($entity_data[$node->uid->entity
->uuid()]));
$entity_data = [];
$preview
->buildRelationshipJson($data['data']['relationships'], $entity_data);
$this
->assertTrue(!empty($entity_data[$node->uid->entity
->uuid()]));
$node2 = Node::create([
'type' => 'page',
'status' => 1,
'title' => 'foo',
'field_reference' => $node,
]);
$node2
->save();
$node->field_reference = $node2;
$node
->save();
$entity_data = [];
$data = $preview
->getJson($node);
$preview
->buildRelationshipJson($data['data']['relationships'], $entity_data, [
'node',
]);
$this
->assertTrue(!empty($entity_data[$node2
->uuid()]));
}
}