You are here

public function MenuLinkContentNormalizerTest::testMenuLinks in Default Content for D8 2.0.x

Tests menu_link_content entities.

File

tests/src/Kernel/MenuLinkContentNormalizerTest.php, line 65

Class

MenuLinkContentNormalizerTest
Tests export functionality.

Namespace

Drupal\Tests\default_content\Kernel

Code

public function testMenuLinks() {

  /** @var \Drupal\node\NodeInterface $referenced_node */
  $referenced_node = Node::create([
    'type' => 'page',
    'title' => 'Referenced node',
  ]);
  $referenced_node
    ->save();

  /** @var \Drupal\menu_link_content\MenuLinkContentInterface $link */
  $link = MenuLinkContent::create([
    'title' => 'Parent menu link',
    'link' => 'entity:node/' . $referenced_node
      ->id(),
  ]);
  $link
    ->save();

  /** @var \Drupal\menu_link_content\MenuLinkContentInterface $child_link */
  $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();

  /** @var \Drupal\default_content\Normalizer\ContentEntityNormalizerInterface $normalizer */
  $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);

  // Delete the link and referenced node and recreate them.
  $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);
}