function GenerateThemeTest::testSimpleStarterkitTheme

Tests generating theme from a simple named Starterkit enabled theme.

File

core/tests/Drupal/BuildTests/Command/GenerateThemeTest.php, line 52

Class

GenerateThemeTest
Tests the generate-theme commands.

Namespace

Drupal\BuildTests\Command

Code

public function testSimpleStarterkitTheme() : void {
  $starterkit_theme_path_relative = 'themes/simple';
  $starterkit_theme_path_absolute = $this->getWorkspaceDirectory() . '/' . $starterkit_theme_path_relative;
  mkdir($starterkit_theme_path_absolute);
  file_put_contents($starterkit_theme_path_absolute . '/simple.info.yml', Yaml::encode([
    'name' => 'Simple',
    'type' => 'theme',
    'base theme' => FALSE,
    'core_version_requirement' => '*',
  ]));
  file_put_contents($starterkit_theme_path_absolute . '/simple.starterkit.yml', Yaml::encode([
    'ignore' => [
      '/simple.starterkit.yml',
    ],
    'no_edit' => [],
    'no_rename' => [],
    'info' => [
      'version' => '1.0.0',
    ],
  ]));
  mkdir($starterkit_theme_path_absolute . '/src');
  file_put_contents($starterkit_theme_path_absolute . '/src/SimpleUtility.php', <<<PHP
  <?php
  
  namespace Drupal\\simple;
  
  final class SimpleUtility {
  
    public const MACHINE_NAME = 'simple';
    public const MACHINE_CLASS_NAME = 'Simple';
  
  }
  PHP);
  file_put_contents($starterkit_theme_path_absolute . '/simple.theme', <<<PHP
  <?php
  
  /**
   * @file
   * Drupal theme functions for Simple.
   */
  
  function simple_preprocess_html(array &\$variables): void {
    \$variables['#attached']['drupalSettings']['simple'] = 'simple';
  }
  PHP);
  $fixture = <<<FIXTURE
  #@starterkit:machine_name
  simple
  #@starterkit:label
  A Simple Theme
  #@starterkit:machine_class_name
  Simple
  #@starterkit:label_class_name
  Simple
  FIXTURE;
  file_put_contents($starterkit_theme_path_absolute . '/README.md', $fixture);
  $this->assertFileExists($starterkit_theme_path_absolute . '/simple.info.yml');
  $this->assertThemeExists($starterkit_theme_path_relative);
  $tester = $this->runCommand([
    'machine-name' => 'simple_theme',
    '--name' => 'My Simple Theme',
    '--description' => 'Custom theme generated from a Simple Starterkit theme',
    '--starterkit' => 'simple',
  ]);
  $tester->assertCommandIsSuccessful();
  $theme_path_relative = 'themes/simple_theme';
  $this->assertThemeExists($theme_path_relative);
  $info = $this->assertThemeExists($theme_path_relative);
  self::assertEquals('My Simple Theme', $info['name']);
  $readme_file = $this->getWorkspaceDirectory() . "/{$theme_path_relative}/README.md";
  $this->assertFileExists($readme_file);
  $fixture = <<<FIXTURE
  #@starterkit:machine_name
  simple_theme
  #@starterkit:label
  My Simple Theme
  #@starterkit:machine_class_name
  SimpleTheme
  #@starterkit:label_class_name
  SimpleTheme
  FIXTURE;
  $this->assertSame($fixture, file_get_contents($readme_file));
  // The .theme file should be renamed and contain updated machine and label
  // values.
  $dot_theme_path = $this->getWorkspaceDirectory() . "/{$theme_path_relative}/simple_theme.theme";
  $this->assertFileExists($dot_theme_path);
  $dot_theme_contents = file_get_contents($dot_theme_path);
  self::assertStringContainsString("\$variables['#attached']['drupalSettings']['simple_theme']", $dot_theme_contents);
  // Ensure content replacements respect the namespace and class fragments.
  $utility_file = $this->getWorkspaceDirectory() . "/{$theme_path_relative}/src/SimpleThemeUtility.php";
  $this->assertFileExists($utility_file);
  $utility_contents = file_get_contents($utility_file);
  self::assertStringContainsString('namespace Drupal\\simple_theme;', $utility_contents);
  self::assertStringContainsString('class SimpleThemeUtility', $utility_contents);
  self::assertStringContainsString("public const MACHINE_NAME = 'simple_theme'", $utility_contents);
  self::assertStringContainsString("public const MACHINE_CLASS_NAME = 'SimpleTheme'", $utility_contents);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.