View source
<?php
namespace Drupal\Tests\Core\StringTranslation;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\Tests\UnitTestCase;
class TranslationManagerTest extends UnitTestCase {
protected $translationManager;
protected function setUp() : void {
$this->translationManager = new TestTranslationManager();
}
public function providerTestFormatPlural() {
return [
[
1,
'Singular',
'@count plural',
[],
[],
'Singular',
],
[
2,
'Singular',
'@count plural',
[],
[],
'2 plural',
],
[
2,
'Singular',
'@count @arg',
[
'@arg' => '<script>',
],
[],
'2 <script>',
],
[
2,
'Singular',
'@count %arg',
[
'%arg' => '<script>',
],
[],
'2 <em class="placeholder"><script></em>',
],
[
1,
'Singular',
'@count plural',
[],
[
'langcode' => NULL,
],
'Singular',
],
[
1,
'Singular',
'@count plural',
[],
[
'langcode' => 'es',
],
'Singular',
],
];
}
public function testFormatPlural($count, $singular, $plural, array $args, array $options, $expected) {
$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);
}
public function testTranslatePlaceholder($string, array $args, $expected_string) {
$actual = $this->translationManager
->translate($string, $args);
$this
->assertInstanceOf(MarkupInterface::class, $actual);
$this
->assertEquals($expected_string, (string) $actual);
}
public 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',
],
];
}
}
class TestTranslationManager extends TranslationManager {
public function __construct() {
}
}