Verified Commit f2d23228 authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

task: #3613629 It should be easier to test Drupal in a different language

By: alexpott
By: kentr
By: gábor hojtsy
parent 72844a3c
Loading
Loading
Loading
Loading
Loading
+11 −11
Changes for core/modules/help/tests/src/Functional/HelpTopicTranslatedTestBase.php: 11 added lines, 11 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
namespace Drupal\Tests\help\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\Locale\InstallerLanguageTrait;

// cspell:ignore hilfetestmodul übersetzung

@@ -15,6 +16,7 @@
 * page title blocks.
 */
abstract class HelpTopicTranslatedTestBase extends BrowserTestBase {
  use InstallerLanguageTrait;

  /**
   * {@inheritdoc}
@@ -59,15 +61,15 @@ protected function setUp(): void {
  /**
   * {@inheritdoc}
   */
  protected function installParameters(): array {
    $parameters = parent::installParameters();
    // Install in German. This will ensure the language and locale modules are
    // installed.
    $parameters['parameters']['langcode'] = 'de';
    // Create a po file so we don't attempt to download one from
    // localize.drupal.org and to have a test translation that will not change.
    \Drupal::service('file_system')->mkdir($this->publicFilesDirectory . '/translations', NULL, TRUE);
    $contents = <<<PO
  protected function getInstallLangcode(): string {
    return 'de';
  }

  /**
   * {@inheritdoc}
   */
  protected function getTranslationFileContents(): string {
    return <<<PO
msgid ""
msgstr ""

@@ -81,8 +83,6 @@ protected function installParameters(): array {
msgstr "Non-word-german sdfwedrsdf."

PO;
    file_put_contents($this->publicFilesDirectory . '/translations/drupal-' . \Drupal::VERSION . '.de.po', $contents);
    return $parameters;
  }

}
+11 −12
Changes for core/modules/locale/tests/src/Functional/LocaleNonInteractiveInstallTest.php: 11 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
namespace Drupal\Tests\locale\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\Locale\InstallerLanguageTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

@@ -14,7 +15,7 @@
#[Group('locale')]
#[RunTestsInSeparateProcesses]
class LocaleNonInteractiveInstallTest extends BrowserTestBase {

  use InstallerLanguageTrait;
  /**
   * {@inheritdoc}
   */
@@ -23,14 +24,15 @@ class LocaleNonInteractiveInstallTest extends BrowserTestBase {
  /**
   * {@inheritdoc}
   */
  protected function installParameters(): array {
    $parameters = parent::installParameters();
    // Install Drupal in German.
    $parameters['parameters']['langcode'] = 'de';
    // Create a po file so we don't attempt to download one from
    // localize.drupal.org and to have a test translation that will not change.
    \Drupal::service('file_system')->mkdir($this->publicFilesDirectory . '/translations', NULL, TRUE);
    $contents = <<<PO
  protected function getInstallLangcode(): string {
    return 'de';
  }

  /**
   * {@inheritdoc}
   */
  protected function getTranslationFileContents(): string {
    return <<<PO
msgid ""
msgstr ""

@@ -38,9 +40,6 @@ protected function installParameters(): array {
msgstr "Anmelden"

PO;

    file_put_contents($this->publicFilesDirectory . '/translations/drupal-' . \Drupal::VERSION . '.de.po', $contents);
    return $parameters;
  }

  /**
+4 −13
Changes for core/modules/locale/tests/src/Functional/LocaleThemeInstallTest.php: 4 added lines, 13 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
namespace Drupal\Tests\locale\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\Locale\InstallerLanguageTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

@@ -18,6 +19,7 @@
#[Group('locale')]
#[RunTestsInSeparateProcesses]
class LocaleThemeInstallTest extends BrowserTestBase {
  use InstallerLanguageTrait;

  /**
   * {@inheritdoc}
@@ -32,19 +34,8 @@ class LocaleThemeInstallTest extends BrowserTestBase {
  /**
   * {@inheritdoc}
   */
  protected function installParameters(): array {
    $parameters = parent::installParameters();
    $parameters['parameters']['langcode'] = 'de';
    // Create a po file so we don't attempt to download one from
    // localize.drupal.org and to have a test translation that will not change.
    \Drupal::service('file_system')->mkdir($this->publicFilesDirectory . '/translations', NULL, TRUE);
    $contents = <<<PO
msgid ""
msgstr ""

PO;
    file_put_contents($this->publicFilesDirectory . '/translations/drupal-' . \Drupal::VERSION . '.de.po', $contents);
    return $parameters;
  protected function getInstallLangcode(): string {
    return 'de';
  }

  /**
+9 −27
Changes for core/modules/node/tests/src/Functional/NodeTypeTranslationTest.php: 9 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\Locale\InstallerLanguageTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

@@ -22,6 +23,7 @@
class NodeTypeTranslationTest extends BrowserTestBase {

  use StringTranslationTrait;
  use InstallerLanguageTrait;

  /**
   * {@inheritdoc}
@@ -38,13 +40,6 @@ class NodeTypeTranslationTest extends BrowserTestBase {
   */
  protected $defaultTheme = 'stark';

  /**
   * The default language code to use in this test.
   *
   * @var array
   */
  protected $defaultLangcode = 'fr';

  /**
   * Languages to enable.
   *
@@ -59,6 +54,13 @@ class NodeTypeTranslationTest extends BrowserTestBase {
   */
  protected $adminUser;

  /**
   * {@inheritdoc}
   */
  protected function getInstallLangcode(): string {
    return 'fr';
  }

  /**
   * {@inheritdoc}
   */
@@ -84,26 +86,6 @@ protected function setUp(): void {
    }
  }

  /**
   * {@inheritdoc}
   *
   * Install Drupal in a language other than English for this test. This is not
   * needed to test the node type translation itself but acts as a regression
   * test.
   *
   * @see https://www.drupal.org/node/2584603
   */
  protected function installParameters(): array {
    $parameters = parent::installParameters();
    $parameters['parameters']['langcode'] = $this->defaultLangcode;
    // Create an empty po file so we don't attempt to download one from
    // localize.drupal.org. It does not need to match the version exactly as the
    // multi-lingual system will fallback.
    \Drupal::service('file_system')->mkdir($this->publicFilesDirectory . '/translations', NULL, TRUE);
    file_put_contents($this->publicFilesDirectory . '/translations/drupal-' . \Drupal::VERSION . ".{$this->defaultLangcode}.po", '');
    return $parameters;
  }

  /**
   * Tests the node type translation.
   */
+53 −0
Changes for core/tests/Drupal/Tests/Traits/Core/Locale/InstallerLanguageTrait.php: 53 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\Traits\Core\Locale;

/**
 * A trait to install Drupal in a specific language.
 */
trait InstallerLanguageTrait {

  /**
   * Gets the langcode to install Drupal with.
   *
   * @return string
   *   The langcode to install Drupal with.
   */
  abstract protected function getInstallLangcode(): string;

  /**
   * Returns the contents of the translation file.
   *
   * This method should be overridden by the test class to provide a translation
   * file that will be used during the installation.
   *
   * @return string
   *   The contents of the translation file.
   */
  protected function getTranslationFileContents(): string {
    return <<<PO
msgid ""
msgstr ""

PO;
  }

  /**
   * {@inheritdoc}
   */
  protected function installParameters(): array {
    $parameters = parent::installParameters();
    $parameters['parameters']['langcode'] = $this->getInstallLangcode();
    // Create a po file so we don't attempt to download one from
    // localize.drupal.org and to have a test translation that will not change.
    \Drupal::service('file_system')->mkdir($this->publicFilesDirectory . '/translations', NULL, TRUE);
    file_put_contents(
      $this->publicFilesDirectory . '/translations/drupal-' . \Drupal::VERSION . '.' . $this->getInstallLangcode() . '.po',
      $this->getTranslationFileContents()
    );
    return $parameters;
  }

}