<?php

declare(strict_types=1);

namespace Drupal\Tests\navigation\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Tests \Drupal\navigation\NavigationContentLinks.
 *
 * @see \Drupal\navigation\NavigationContentLinks
 */
#[Group('navigation')]
#[RunTestsInSeparateProcesses]
class NavigationContentLinksTest extends KernelTestBase {

  use ContentTypeCreationTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'navigation',
    'node',
    'file',
    'media',
    'layout_builder',
    'system',
    'views',
    'field',
    'media_test_source',
    'image',
    'text',
    'user',
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
    $this->installEntitySchema('node');
    $this->installConfig(['node', 'file', 'media']);

    $this->createContentType(['type' => 'article']);
    $this->createContentType(['type' => 'blog']);
    $this->createContentType(['type' => 'landing_page']);
  }

  /**
   * Tests if the expected navigation content links are added/removed correctly.
   */
  public function testNavigationContentLinks(): void {
    $module_installer = \Drupal::service('module_installer');
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    $links = $menu_link_manager->getDefinitions();

    // Assert that the "Create" link is added to the menu.
    $this->assertArrayHasKey('navigation.create', $links);
    $this->assertEquals('entity.node.add_page', $links['navigation.create']['route_name']);
    $this->assertEquals('Create', $links['navigation.create']['title']);

    // Assert that the "Content" link is added to the menu.
    $this->assertArrayHasKey('navigation.content', $links);
    $this->assertEquals('system.admin_content', $links['navigation.content']['route_name']);
    $this->assertEquals('Content', $links['navigation.content']['title']);

    // Assert that the "Article" submenu link is added to the menu.
    $this->assertArrayHasKey('navigation.content.node_type.article', $links);
    $this->assertEquals('entity.node.add_form', $links['navigation.content.node_type.article']['route_name']);
    $this->assertEquals('article', $links['navigation.content.node_type.article']['title']);

    // Assert that the "Blog" submenu link is added to the menu.
    $this->assertArrayHasKey('navigation.content.node_type.blog', $links);
    $this->assertEquals('entity.node.add_form', $links['navigation.content.node_type.blog']['route_name']);
    $this->assertEquals('blog', $links['navigation.content.node_type.blog']['title']);

    // Assert that the "Landing Page" submenu link is added to the menu.
    $this->assertArrayHasKey('navigation.content.node_type.landing_page', $links);
    $this->assertEquals('entity.node.add_form', $links['navigation.content.node_type.landing_page']['route_name']);
    $this->assertEquals('landing_page', $links['navigation.content.node_type.landing_page']['title']);

    // Assert that the "Media" link is added.
    $this->assertArrayHasKey('navigation.media', $links);
    $this->assertEquals('entity.media.collection', $links['navigation.media']['route_name']);
    $this->assertEquals('Media', $links['navigation.media']['title']);

    // Assert that the "Files" link is added.
    $this->assertArrayHasKey('navigation.files', $links);
    $this->assertEquals('view.files.page_1', $links['navigation.files']['route_name']);
    $this->assertEquals('Files', $links['navigation.files']['title']);

    // Assert that "Blocks" link is not added.
    $this->assertArrayNotHasKey('navigation.blocks', $links);

    // Install the block_content module and rebuild the menu links.
    $module_installer->install(['block_content']);
    // Rebuild the links after module installation.
    $menu_link_manager->rebuild();
    $links = $menu_link_manager->getDefinitions();

    // Assert that "Blocks" link is added.
    $this->assertArrayHasKey('navigation.blocks', $links);
    $this->assertEquals('entity.block_content.collection', $links['navigation.blocks']['route_name']);
    $this->assertEquals('Blocks', $links['navigation.blocks']['title']);
  }

}
