View source
<?php
namespace Drupal\Tests\tome_sync\Kernel;
use Drupal\Core\Site\Settings;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\link\LinkItemInterface;
use Drupal\node\Entity\Node;
use Drupal\Tests\tome_base\Kernel\TestBase;
use Drupal\tome_sync\TomeSyncHelper;
class LinkItemTest extends TestBase {
public static $modules = [
'link',
'tome_sync',
'path_alias',
];
public function setUp() {
parent::setUp();
$this
->installSchema('tome_sync', [
'tome_sync_content_hash',
]);
$this
->installEntitySchema('path_alias');
FieldStorageConfig::create([
'entity_type' => 'node',
'field_name' => 'field_link',
'type' => 'link',
])
->save();
FieldConfig::create([
'entity_type' => 'node',
'field_name' => 'field_link',
'bundle' => 'article',
'settings' => [
'link_type' => LinkItemInterface::LINK_GENERIC,
],
])
->save();
}
public function testLinkFields() {
$serializer = \Drupal::service('serializer');
$storage = \Drupal::service('tome_sync.storage.content');
$article1 = Node::create([
'type' => 'article',
'title' => 'Article 1',
]);
$article1
->save();
$article2 = Node::create([
'type' => 'article',
'title' => 'My article',
'field_link' => [
'uri' => 'entity:node/' . $article1
->id(),
],
]);
$article2
->save();
$data = $storage
->read(TomeSyncHelper::getContentName($article2));
$this
->assertEquals($data['field_link'][0]['uri'], 'entity:node/' . $article1
->uuid());
$exported_node = $serializer
->denormalize($data, Node::class, 'json');
$this
->assertEquals($exported_node
->get('field_link')
->get(0)
->get('uri')
->getValue(), 'entity:node/' . $article1
->id());
$index_file = Settings::get('tome_content_directory', '../content') . '/meta/index.json';
$this
->assertFileExists($index_file);
$index = json_decode(file_get_contents($index_file), TRUE);
$article1_name = TomeSyncHelper::getContentName($article1);
$article2_name = TomeSyncHelper::getContentName($article2);
$this
->assertArrayHasKey($article2_name, $index);
$this
->assertContains($article1_name, $index[$article2_name]);
}
}