class TranslationManagerTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/StringTranslation/TranslationManagerTest.php \Drupal\Tests\Core\StringTranslation\TranslationManagerTest
@coversDefaultClass \Drupal\Core\StringTranslation\TranslationManager @group StringTranslation
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Core\StringTranslation\TranslationManagerTest
Expanded class hierarchy of TranslationManagerTest
File
- core/
tests/ Drupal/ Tests/ Core/ StringTranslation/ TranslationManagerTest.php, line 19 - Contains \Drupal\Tests\Core\StringTranslation\TranslationManagerTest.
Namespace
Drupal\Tests\Core\StringTranslationView source
class TranslationManagerTest extends UnitTestCase {
/**
* The tested translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationManager
*/
protected $translationManager;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->translationManager = new TestTranslationManager();
}
/**
* Provides some test data for formatPlural()
* @return array
*/
public function providerTestFormatPlural() {
return array(
[
1,
'Singular',
'@count plural',
array(),
array(),
'Singular',
],
[
2,
'Singular',
'@count plural',
array(),
array(),
'2 plural',
],
// @todo support locale_get_plural
[
2,
'Singular',
'@count @arg',
array(
'@arg' => '<script>',
),
array(),
'2 <script>',
],
[
2,
'Singular',
'@count %arg',
array(
'%arg' => '<script>',
),
array(),
'2 <em class="placeholder"><script></em>',
],
[
1,
'Singular',
'@count plural',
array(),
array(
'langcode' => NULL,
),
'Singular',
],
[
1,
'Singular',
'@count plural',
array(),
array(
'langcode' => 'es',
),
'Singular',
],
);
}
/**
* @dataProvider providerTestFormatPlural
*/
public function testFormatPlural($count, $singular, $plural, array $args = array(), array $options = array(), $expected) {
$langcode = empty($options['langcode']) ? 'fr' : $options['langcode'];
$translator = $this
->getMock('\\Drupal\\Core\\StringTranslation\\Translator\\TranslatorInterface');
$translator
->expects($this
->once())
->method('getStringTranslation')
->with($langcode, $this
->anything(), $this
->anything())
->will($this
->returnCallback(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
->assertTrue(SafeMarkup::isSafe($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 = array(), $expected_string) {
$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 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
TranslationManagerTest:: |
protected | property | The tested translation manager. | |
TranslationManagerTest:: |
public | function | Provides some test data for formatPlural() | |
TranslationManagerTest:: |
public | function | Provides test data for translate(). | |
TranslationManagerTest:: |
protected | function |
Overrides UnitTestCase:: |
|
TranslationManagerTest:: |
public | function | @dataProvider providerTestFormatPlural | |
TranslationManagerTest:: |
public | function | Tests translation using placeholders. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |