<?php

declare(strict_types=1);

namespace Drupal\Tests\ckeditor5\Kernel;

use Drupal\ckeditor5\Hook\Ckeditor5Hooks;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
 * Test the ckeditor5-stylesheets theme config property.
 */
#[Group('ckeditor5')]
#[RunTestsInSeparateProcesses]
class CKEditor5StylesheetsTest extends KernelTestBase {

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

  /**
   * Tests loading of theme's CKEditor 5 stylesheets defined in the .info file.
   *
   * @param string $theme
   *   The machine name of the theme.
   * @param array $expected
   *   The expected CKEditor 5 CSS paths from the theme.
   */
  #[DataProvider('externalStylesheetsProvider')]
  public function testExternalStylesheets($theme, $expected): void {
    \Drupal::service('theme_installer')->install([$theme]);
    $this->config('system.theme')->set('default', $theme)->save();

    // Access protected \Drupal\ckeditor5\Hook\Ckeditor5Hooks::themeCss() method
    // so we can test it.
    $hooks_service = $this->container->get(Ckeditor5Hooks::class);
    $class = new \ReflectionClass($hooks_service);
    $theme_css_method = $class->getMethod('themeCss');

    $this->assertSame($expected, $theme_css_method->invokeArgs($hooks_service, [$theme]));
  }

  /**
   * Provides test cases for external stylesheets.
   *
   * @return array
   *   An array of test cases.
   */
  public static function externalStylesheetsProvider() {
    return [
      'Install theme which has an absolute external CSS URL' => [
        'test_ckeditor_stylesheets_external',
        ['https://fonts.googleapis.com/css?family=Open+Sans'],
      ],
      'Install theme which has an external protocol-relative CSS URL' => [
        'test_ckeditor_stylesheets_protocol_relative',
        ['//fonts.googleapis.com/css?family=Open+Sans'],
      ],
      'Install theme which has a relative CSS URL' => [
        'test_ckeditor_stylesheets_relative',
        ['/core/modules/system/tests/themes/test_ckeditor_stylesheets_relative/css/yokotsoko.css'],
      ],
      'Install theme which has a Drupal root CSS URL' => [
        'test_ckeditor_stylesheets_drupal_root',
        ['/core/modules/system/tests/themes/test_ckeditor_stylesheets_drupal_root/css/yokotsoko.css'],
      ],
    ];
  }

}
