<?php

declare(strict_types=1);

namespace Drupal\KernelTests\Core\Extension;

use Drupal\Core\Extension\Requirement\RequirementSeverity;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

include_once \DRUPAL_ROOT . '/core/includes/install.inc';

/**
 * Tests the legacy requirements severity deprecations.
 */
#[CoversClass(RequirementSeverity::class)]
#[Group('extension')]
#[IgnoreDeprecations]
#[RunTestsInSeparateProcesses]
class LegacyRequirementSeverityTest extends KernelTestBase {

  /**
   * Tests get max severity.
   *
   * @legacy-covers \drupal_requirements_severity
   */
  #[DataProvider('requirementProvider')]
  public function testGetMaxSeverity(array $requirements, int $expectedSeverity): void {
    $this->expectUserDeprecationMessage(
      'drupal_requirements_severity() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Use Drupal\Core\Extension\Requirement\RequirementSeverity::maxSeverityFromRequirements() instead. See https://www.drupal.org/node/3410939'
    );
    $this->expectUserDeprecationMessage(
      'Calling Drupal\Core\Extension\Requirement\RequirementSeverity::maxSeverityFromRequirements() with an array of $requirements with \'severity\' with values not of type Drupal\Core\Extension\Requirement\RequirementSeverity enums is deprecated in drupal:11.2.0 and is required in drupal:12.0.0. See https://www.drupal.org/node/3410939'
    );
    $severity = drupal_requirements_severity($requirements);
    $this->assertEquals($expectedSeverity, $severity);
  }

  /**
   * Data provider for requirement helper test.
   *
   * @return array
   *   Test data.
   */
  public static function requirementProvider(): array {
    $info = [
      'title' => 'Foo',
      'severity' => \REQUIREMENT_INFO,
    ];
    $warning = [
      'title' => 'Baz',
      'severity' => \REQUIREMENT_WARNING,
    ];
    $error = [
      'title' => 'Wiz',
      'severity' => \REQUIREMENT_ERROR,
    ];
    $ok = [
      'title' => 'Bar',
      'severity' => \REQUIREMENT_OK,
    ];

    return [
      'error is most severe' => [
        [
          $info,
          $error,
          $ok,
        ],
        \REQUIREMENT_ERROR,
      ],
      'ok is most severe' => [
        [
          $info,
          $ok,
        ],
        \REQUIREMENT_OK,
      ],
      'warning is most severe' => [
        [
          $warning,
          $info,
          $ok,
        ],
        \REQUIREMENT_WARNING,
      ],
    ];
  }

}
