class TranslationManagerTest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/StringTranslation/TranslationManagerTest.php \Drupal\Tests\Core\StringTranslation\TranslationManagerTest
  2. 8.9.x core/tests/Drupal/Tests/Core/StringTranslation/TranslationManagerTest.php \Drupal\Tests\Core\StringTranslation\TranslationManagerTest
  3. 11.x core/tests/Drupal/Tests/Core/StringTranslation/TranslationManagerTest.php \Drupal\Tests\Core\StringTranslation\TranslationManagerTest

@coversDefaultClass \Drupal\Core\StringTranslation\TranslationManager
@group StringTranslation

Hierarchy

Expanded class hierarchy of TranslationManagerTest

File

core/tests/Drupal/Tests/Core/StringTranslation/TranslationManagerTest.php, line 15

Namespace

Drupal\Tests\Core\StringTranslation
View source
class TranslationManagerTest extends UnitTestCase {
  
  /**
   * The tested translation manager.
   *
   * @var \Drupal\Core\StringTranslation\TranslationManager
   */
  protected $translationManager;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->translationManager = new TestTranslationManager();
  }
  
  /**
   * Provides some test data for formatPlural()
   * @return array
   */
  public static function providerTestFormatPlural() {
    return [
      [
        1,
        'Singular',
        '@count plural',
        [],
        [],
        'Singular',
      ],
      [
        2,
        'Singular',
        '@count plural',
        [],
        [],
        '2 plural',
      ],
      // @todo support locale_get_plural
[
        2,
        'Singular',
        '@count @arg',
        [
          '@arg' => '<script>',
        ],
        [],
        '2 &lt;script&gt;',
      ],
      [
        2,
        'Singular',
        '@count %arg',
        [
          '%arg' => '<script>',
        ],
        [],
        '2 <em class="placeholder">&lt;script&gt;</em>',
      ],
      [
        1,
        'Singular',
        '@count plural',
        [],
        [
          'langcode' => NULL,
        ],
        'Singular',
      ],
      [
        1,
        'Singular',
        '@count plural',
        [],
        [
          'langcode' => 'es',
        ],
        'Singular',
      ],
    ];
  }
  
  /**
   * @dataProvider providerTestFormatPlural
   */
  public function testFormatPlural($count, $singular, $plural, array $args, array $options, $expected) : void {
    $langcode = empty($options['langcode']) ? 'fr' : $options['langcode'];
    $translator = $this->createMock('\\Drupal\\Core\\StringTranslation\\Translator\\TranslatorInterface');
    $translator->expects($this->once())
      ->method('getStringTranslation')
      ->with($langcode, $this->anything(), $this->anything())
      ->willReturnCallback(function ($langcode, $string, $context) {
      return $string;
    });
    $this->translationManager
      ->setDefaultLangcode('fr');
    $this->translationManager
      ->addTranslator($translator);
    $result = $this->translationManager
      ->formatPlural($count, $singular, $plural, $args, $options);
    $this->assertEquals($expected, $result);
    $this->assertInstanceOf(MarkupInterface::class, $result);
  }
  
  /**
   * Tests translation using placeholders.
   *
   * @param string $string
   *   A string containing the English text to translate.
   * @param array $args
   *   An associative array of replacements to make after translation.
   * @param string $expected_string
   *   The expected translated string value.
   *
   * @dataProvider providerTestTranslatePlaceholder
   */
  public function testTranslatePlaceholder($string, array $args, $expected_string) : void {
    $actual = $this->translationManager
      ->translate($string, $args);
    $this->assertInstanceOf(MarkupInterface::class, $actual);
    $this->assertEquals($expected_string, (string) $actual);
  }
  
  /**
   * Provides test data for translate().
   *
   * @return array
   */
  public static function providerTestTranslatePlaceholder() {
    return [
      [
        'foo @bar',
        [
          '@bar' => 'bar',
        ],
        'foo bar',
      ],
      [
        'bar %baz',
        [
          '%baz' => 'baz',
        ],
        'bar <em class="placeholder">baz</em>',
      ],
      [
        'bar @bar %baz',
        [
          '@bar' => 'bar',
          '%baz' => 'baz',
        ],
        'bar bar <em class="placeholder">baz</em>',
      ],
      [
        'bar %baz @bar',
        [
          '%baz' => 'baz',
          '@bar' => 'bar',
        ],
        'bar <em class="placeholder">baz</em> bar',
      ],
    ];
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
TranslationManagerTest::$translationManager protected property The tested translation manager.
TranslationManagerTest::providerTestFormatPlural public static function Provides some test data for formatPlural()
TranslationManagerTest::providerTestTranslatePlaceholder public static function Provides test data for translate().
TranslationManagerTest::setUp protected function Overrides UnitTestCase::setUp
TranslationManagerTest::testFormatPlural public function @dataProvider providerTestFormatPlural
TranslationManagerTest::testTranslatePlaceholder public function Tests translation using placeholders.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

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