View source
<?php
namespace Drupal\Tests\Core\Command;
use Drupal\BuildTests\QuickStart\QuickStartTestBase;
use Drupal\Core\Serialization\Yaml;
use Drupal\sqlite\Driver\Database\sqlite\Install\Tasks;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
class GenerateThemeTest extends QuickStartTestBase {
protected $php;
protected function setUp() : void {
if (version_compare(\SQLite3::version()['versionString'], Tasks::SQLITE_MINIMUM_VERSION) < 0) {
$this
->markTestSkipped();
}
parent::setUp();
$php_executable_finder = new PhpExecutableFinder();
$this->php = $php_executable_finder
->find();
$this
->copyCodebase();
$this
->executeCommand('COMPOSER_DISCARD_CHANGES=true composer install --no-dev --no-interaction');
chdir($this
->getWorkingPath());
}
private function generateThemeFromStarterkit() : Process {
$install_command = [
$this->php,
'core/scripts/drupal',
'generate-theme',
'test_custom_theme',
'--name="Test custom starterkit theme"',
'--description="Custom theme generated from a starterkit theme"',
];
$process = new Process($install_command, NULL);
$process
->setTimeout(60);
return $process;
}
private function assertThemeExists(string $theme_path_relative) : array {
$theme_path_absolute = $this
->getWorkspaceDirectory() . "/{$theme_path_relative}";
$theme_name = basename($theme_path_relative);
$info_yml_filename = "{$theme_name}.info.yml";
$this
->assertFileExists($theme_path_absolute . '/' . $info_yml_filename);
$info = Yaml::decode(file_get_contents($theme_path_absolute . '/' . $info_yml_filename));
return $info;
}
public function test() {
$starterkit_info_yml = $this
->getWorkspaceDirectory() . '/core/themes/starterkit_theme/starterkit_theme.info.yml';
$info = Yaml::decode(file_get_contents($starterkit_info_yml));
$info['version'] = '9.4.0';
file_put_contents($starterkit_info_yml, Yaml::encode($info));
$process = $this
->generateThemeFromStarterkit();
$result = $process
->run();
$this
->assertEquals('Theme generated successfully to themes/test_custom_theme', trim($process
->getOutput()), $process
->getErrorOutput());
$this
->assertSame(0, $result);
$theme_path_relative = 'themes/test_custom_theme';
$info = $this
->assertThemeExists($theme_path_relative);
self::assertArrayNotHasKey('hidden', $info);
self::assertArrayHasKey('generator', $info);
self::assertEquals('starterkit_theme:9.4.0', $info['generator']);
$this
->installQuickStart('minimal');
$this
->formLogin($this->adminUsername, $this->adminPassword);
$this
->visit('/admin/appearance');
$this
->getMink()
->assertSession()
->pageTextContains('Test custom starterkit');
$this
->getMink()
->assertSession()
->pageTextContains('Custom theme generated from a starterkit theme');
$this
->getMink()
->getSession()
->getPage()
->clickLink('Install "Test custom starterkit theme" theme');
$this
->getMink()
->assertSession()
->pageTextContains('The "Test custom starterkit theme" theme has been installed.');
$theme_path_absolute = $this
->getWorkspaceDirectory() . "/{$theme_path_relative}";
$this
->assertFileExists($theme_path_absolute . '/test_custom_theme.theme');
unlink($theme_path_absolute . '/test_custom_theme.theme');
$process = $this
->generateThemeFromStarterkit();
$result = $process
->run();
$this
->assertStringContainsString('Theme could not be generated because the destination directory', $process
->getErrorOutput());
$this
->assertStringContainsString($theme_path_relative, $process
->getErrorOutput());
$this
->assertSame(1, $result);
$this
->assertFileDoesNotExist($theme_path_absolute . '/test_custom_theme.theme');
}
public function testDevSnapshot() {
$starterkit_info_yml = $this
->getWorkspaceDirectory() . '/core/themes/starterkit_theme/starterkit_theme.info.yml';
$info = Yaml::decode(file_get_contents($starterkit_info_yml));
$info['version'] = '9.4.0-dev';
file_put_contents($starterkit_info_yml, Yaml::encode($info));
$process = $this
->generateThemeFromStarterkit();
$result = $process
->run();
$this
->assertEquals('Theme generated successfully to themes/test_custom_theme', trim($process
->getOutput()), $process
->getErrorOutput());
$this
->assertSame(0, $result);
$theme_path_relative = 'themes/test_custom_theme';
$info = $this
->assertThemeExists($theme_path_relative);
self::assertArrayNotHasKey('hidden', $info);
self::assertArrayHasKey('generator', $info);
self::assertMatchesRegularExpression('/^starterkit_theme\\:9.4.0-dev#[0-9a-f]+$/', $info['generator']);
}
public function testContribStarterkit() : void {
$starterkit_info_yml = $this
->getWorkspaceDirectory() . '/core/themes/starterkit_theme/starterkit_theme.info.yml';
$info = Yaml::decode(file_get_contents($starterkit_info_yml));
$info['version'] = '1.20';
file_put_contents($starterkit_info_yml, Yaml::encode($info));
$process = $this
->generateThemeFromStarterkit();
$result = $process
->run();
$this
->assertEquals('Theme generated successfully to themes/test_custom_theme', trim($process
->getOutput()), $process
->getErrorOutput());
$this
->assertSame(0, $result);
$info = $this
->assertThemeExists('themes/test_custom_theme');
self::assertArrayNotHasKey('hidden', $info);
self::assertArrayHasKey('generator', $info);
self::assertEquals('starterkit_theme:1.20', $info['generator']);
}
public function testContribStarterkitDevSnapshot() : void {
$starterkit_info_yml = $this
->getWorkspaceDirectory() . '/core/themes/starterkit_theme/starterkit_theme.info.yml';
$info = Yaml::decode(file_get_contents($starterkit_info_yml));
$info['core_version_requirement'] = '*';
$info['version'] = '7.x-dev';
file_put_contents($starterkit_info_yml, Yaml::encode($info));
Process::fromShellCommandline('mv core/themes/starterkit_theme themes/', $this
->getWorkspaceDirectory())
->run();
$process = $this
->generateThemeFromStarterkit();
$result = $process
->run();
$this
->assertEquals("The source theme starterkit_theme has a development version number (7.x-dev). Because it is not a git checkout, a specific commit could not be identified. This makes tracking changes in the source theme difficult. Are you sure you want to continue? (yes/no) [yes]:\n > Theme generated successfully to themes/test_custom_theme", trim($process
->getOutput()), $process
->getErrorOutput());
$this
->assertSame(0, $result);
$info = $this
->assertThemeExists('themes/test_custom_theme');
self::assertArrayNotHasKey('hidden', $info);
self::assertArrayHasKey('generator', $info);
self::assertEquals('starterkit_theme:7.x-dev#unknown-commit', $info['generator']);
}
public function testContribStarterkitDevSnapshotWithGitNotInstalled() : void {
$starterkit_info_yml = $this
->getWorkspaceDirectory() . '/core/themes/starterkit_theme/starterkit_theme.info.yml';
$info = Yaml::decode(file_get_contents($starterkit_info_yml));
$info['core_version_requirement'] = '*';
$info['version'] = '7.x-dev';
file_put_contents($starterkit_info_yml, Yaml::encode($info));
Process::fromShellCommandline('mv core/themes/starterkit_theme themes/', $this
->getWorkspaceDirectory())
->run();
$output = [];
exec('git --help', $output, $status);
$this
->assertEquals(0, $status);
$unavailableGitPath = $this
->getWorkspaceDirectory() . '/bin';
mkdir($unavailableGitPath);
$bash = <<<SH
#!/bin/bash
exit 127
SH;
file_put_contents($unavailableGitPath . '/git', $bash);
chmod($unavailableGitPath . '/git', 0755);
$oldPath = getenv('PATH');
putenv('PATH=' . $unavailableGitPath . ':' . getenv('PATH'));
$output = [];
exec('git --help', $output, $status);
$this
->assertEquals(127, $status);
$process = $this
->generateThemeFromStarterkit();
$result = $process
->run();
$this
->assertEquals("[ERROR] The source theme starterkit_theme has a development version number \n (7.x-dev). Determining a specific commit is not possible because git is\n not installed. Either install git or use a tagged release to generate a\n theme.", trim($process
->getOutput()), $process
->getErrorOutput());
$this
->assertSame(1, $result);
$this
->assertFileDoesNotExist($this
->getWorkspaceDirectory() . "/themes/test_custom_theme");
putenv('PATH=' . $oldPath . ':' . getenv('PATH'));
}
public function testCustomStarterkit() : void {
$starterkit_info_yml = $this
->getWorkspaceDirectory() . '/core/themes/starterkit_theme/starterkit_theme.info.yml';
$info = Yaml::decode(file_get_contents($starterkit_info_yml));
unset($info['version']);
file_put_contents($starterkit_info_yml, Yaml::encode($info));
$process = $this
->generateThemeFromStarterkit();
$result = $process
->run();
$this
->assertEquals("The source theme starterkit_theme does not have a version specified. This makes tracking changes in the source theme difficult. Are you sure you want to continue? (yes/no) [yes]:\n > Theme generated successfully to themes/test_custom_theme", trim($process
->getOutput()), $process
->getErrorOutput());
$this
->assertSame(0, $result);
$info = $this
->assertThemeExists('themes/test_custom_theme');
self::assertArrayNotHasKey('hidden', $info);
self::assertArrayHasKey('generator', $info);
self::assertEquals('starterkit_theme:unknown-version', $info['generator']);
}
public function testThemeDoesNotExist() : void {
$install_command = [
$this->php,
'core/scripts/drupal',
'generate-theme',
'test_custom_theme',
'--name="Test custom starterkit theme"',
'--description="Custom theme generated from a starterkit theme"',
'--starterkit',
'foobarbaz',
];
$process = new Process($install_command, NULL);
$process
->setTimeout(60);
$result = $process
->run();
$this
->assertStringContainsString('Theme source theme foobarbaz cannot be found.', trim($process
->getErrorOutput()));
$this
->assertSame(1, $result);
}
public function testStarterKitFlag() : void {
$install_command = [
$this->php,
'core/scripts/drupal',
'generate-theme',
'test_custom_theme',
'--name="Test custom starterkit theme"',
'--description="Custom theme generated from a starterkit theme"',
'--starterkit',
'stark',
];
$process = new Process($install_command, NULL);
$process
->setTimeout(60);
$result = $process
->run();
$this
->assertStringContainsString('Theme source theme stark is not a valid starter kit.', trim($process
->getErrorOutput()));
$this
->assertSame(1, $result);
$install_command = [
$this->php,
'core/scripts/drupal',
'generate-theme',
'test_custom_theme',
'--name="Test custom starterkit theme"',
'--description="Custom theme generated from a starterkit theme"',
'--starterkit',
'olivero',
];
$process = new Process($install_command, NULL);
$process
->setTimeout(60);
$result = $process
->run();
$this
->assertStringContainsString('Theme source theme olivero is not a valid starter kit.', trim($process
->getErrorOutput()));
$this
->assertSame(1, $result);
}
}