<?php

declare(strict_types=1);

namespace Drupal\Tests\flowdrop\Kernel;

use Drupal\KernelTests\KernelTestBase;

/**
 * Tests that every FlowDrop module can be enabled independently.
 *
 * Each test case simulates `drush en <module>` by invoking the Drupal module
 * installer, which resolves dependencies and runs install hooks exactly as a
 * real site would. This catches regressions where entity definitions reference
 * entity types from modules not declared as dependencies.
 *
 * @group flowdrop
 *
 * @see https://www.drupal.org/project/flowdrop/issues/3579822
 */
class ModuleInstallationTest extends KernelTestBase {

  use FlowDropKernelTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['system'];

  /**
   * Tests that a given FlowDrop module can be enabled.
   *
   * @dataProvider moduleProvider
   */
  public function testModuleCanBeEnabled(string $module): void {
    $result = $this->container->get('module_installer')->install([$module]);

    $this->assertTrue($result, "module_installer->install(['$module']) must return true.");
    $this->assertTrue(
      \Drupal::moduleHandler()->moduleExists($module),
      "'$module' must be enabled after installation."
    );
  }

  /**
   * Provides all FlowDrop modules to test.
   *
   * @return array<string, array{string}>
   *   A keyed array of module names.
   */
  public static function moduleProvider(): array {
    $modules = [
      'flowdrop',
      'flowdrop_interrupt',
      'flowdrop_job',
      'flowdrop_memory',
      'flowdrop_node_category',
      'flowdrop_node_processor',
      'flowdrop_node_type',
      'flowdrop_orchestration',
      'flowdrop_orchestration_connector',
      'flowdrop_pipeline',
      'flowdrop_playground',
      'flowdrop_runtime',
      'flowdrop_session',
      'flowdrop_stategraph',
      'flowdrop_trigger',
      'flowdrop_ui_components',
      'flowdrop_workflow',
      'flowdrop_workflow_executor',
    ];

    return array_combine($modules, array_map(fn($m) => [$m], $modules));
  }

}
