View source
<?php
namespace Drupal\Tests\default_content\Kernel;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\KernelTests\KernelTestBase;
class MenuLinkContentNormalizerTest extends KernelTestBase {
use EntityReferenceTestTrait;
public static $modules = [
'system',
'user',
'field',
'serialization',
'hal',
'default_content',
'link',
'menu_link_content',
'node',
];
protected $exporter;
protected function setUp() : void {
parent::setUp();
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('menu_link_content');
NodeType::create([
'type' => 'page',
'name' => 'page',
])
->save();
}
public function testMenuLinks() {
$referenced_node = Node::create([
'type' => 'page',
'title' => 'Referenced node',
]);
$referenced_node
->save();
$link = MenuLinkContent::create([
'title' => 'Parent menu link',
'link' => 'entity:node/' . $referenced_node
->id(),
]);
$link
->save();
$child_link = MenuLinkContent::create([
'title' => 'Child menu link',
'parent' => 'menu_link_content:' . $link
->uuid(),
'link' => [
'uri' => 'https://www.example.org',
'options' => [
'attributes' => [
'target' => '_blank',
],
],
],
]);
$child_link
->save();
$normalizer = \Drupal::service('default_content.content_entity_normalizer');
$normalized = $normalizer
->normalize($link);
$expected = [
'_meta' => [
'version' => '1.0',
'entity_type' => 'menu_link_content',
'uuid' => $link
->uuid(),
'bundle' => 'menu_link_content',
'default_langcode' => 'en',
'depends' => [
$referenced_node
->uuid() => 'node',
],
],
'default' => [
'enabled' => [
0 => [
'value' => TRUE,
],
],
'title' => [
0 => [
'value' => 'Parent menu link',
],
],
'menu_name' => [
0 => [
'value' => 'tools',
],
],
'link' => [
0 => [
'target_uuid' => $referenced_node
->uuid(),
'title' => '',
'options' => [],
],
],
'external' => [
0 => [
'value' => FALSE,
],
],
'rediscover' => [
0 => [
'value' => FALSE,
],
],
'weight' => [
0 => [
'value' => 0,
],
],
'expanded' => [
0 => [
'value' => FALSE,
],
],
'revision_translation_affected' => [
0 => [
'value' => TRUE,
],
],
],
];
$this
->assertEquals($expected, $normalized);
$normalized_child = $normalizer
->normalize($child_link);
$expected_child = [
'_meta' => [
'version' => '1.0',
'entity_type' => 'menu_link_content',
'uuid' => $child_link
->uuid(),
'bundle' => 'menu_link_content',
'default_langcode' => 'en',
'depends' => [
$link
->uuid() => 'menu_link_content',
],
],
'default' => [
'enabled' => [
0 => [
'value' => TRUE,
],
],
'title' => [
0 => [
'value' => 'Child menu link',
],
],
'menu_name' => [
0 => [
'value' => 'tools',
],
],
'link' => [
0 => [
'uri' => 'https://www.example.org',
'title' => '',
'options' => [
'attributes' => [
'target' => '_blank',
],
],
],
],
'external' => [
0 => [
'value' => FALSE,
],
],
'rediscover' => [
0 => [
'value' => FALSE,
],
],
'weight' => [
0 => [
'value' => 0,
],
],
'expanded' => [
0 => [
'value' => FALSE,
],
],
'parent' => [
0 => [
'value' => $child_link
->getParentId(),
],
],
'revision_translation_affected' => [
0 => [
'value' => TRUE,
],
],
],
];
$this
->assertEquals($expected_child, $normalized_child);
$normalized_node = $normalizer
->normalize($referenced_node);
$child_link
->delete();
$link
->delete();
$referenced_node
->delete();
$recreated_node = $normalizer
->denormalize($normalized_node);
$recreated_node
->save();
$this
->assertNotEquals($referenced_node
->id(), $recreated_node
->id());
$recreated_link = $normalizer
->denormalize($normalized);
$recreated_link
->save();
$this
->assertEquals('entity:node/' . $recreated_node
->id(), $recreated_link
->get('link')->uri);
}
}