More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / StringTranslation / TranslationManagerTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\StringTranslation\TranslationManagerTest.
6  */
7
8 namespace Drupal\Tests\Core\StringTranslation;
9
10 use Drupal\Component\Render\MarkupInterface;
11 use Drupal\Core\StringTranslation\TranslationManager;
12 use Drupal\Tests\UnitTestCase;
13
14 /**
15  * @coversDefaultClass \Drupal\Core\StringTranslation\TranslationManager
16  * @group StringTranslation
17  */
18 class TranslationManagerTest extends UnitTestCase {
19
20   /**
21    * The tested translation manager.
22    *
23    * @var \Drupal\Core\StringTranslation\TranslationManager
24    */
25   protected $translationManager;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     $this->translationManager = new TestTranslationManager();
32   }
33
34   /**
35    * Provides some test data for formatPlural()
36    * @return array
37    */
38   public function providerTestFormatPlural() {
39     return [
40       [1, 'Singular', '@count plural', [], [], 'Singular'],
41       [2, 'Singular', '@count plural', [], [], '2 plural'],
42       // @todo support locale_get_plural
43       [2, 'Singular', '@count @arg', ['@arg' => '<script>'], [], '2 &lt;script&gt;'],
44       [2, 'Singular', '@count %arg', ['%arg' => '<script>'], [], '2 <em class="placeholder">&lt;script&gt;</em>'],
45       [1, 'Singular', '@count plural', [], ['langcode' => NULL], 'Singular'],
46       [1, 'Singular', '@count plural', [], ['langcode' => 'es'], 'Singular'],
47     ];
48   }
49
50   /**
51    * @dataProvider providerTestFormatPlural
52    */
53   public function testFormatPlural($count, $singular, $plural, array $args, array $options, $expected) {
54     $langcode = empty($options['langcode']) ? 'fr' : $options['langcode'];
55     $translator = $this->getMock('\Drupal\Core\StringTranslation\Translator\TranslatorInterface');
56     $translator->expects($this->once())
57       ->method('getStringTranslation')
58       ->with($langcode, $this->anything(), $this->anything())
59       ->will($this->returnCallback(function ($langcode, $string, $context) {
60         return $string;
61       }));
62     $this->translationManager->setDefaultLangcode('fr');
63     $this->translationManager->addTranslator($translator);
64     $result = $this->translationManager->formatPlural($count, $singular, $plural, $args, $options);
65     $this->assertEquals($expected, $result);
66     $this->assertInstanceOf(MarkupInterface::class, $result);
67   }
68
69   /**
70    * Tests translation using placeholders.
71    *
72    * @param string $string
73    *   A string containing the English text to translate.
74    * @param array $args
75    *   An associative array of replacements to make after translation.
76    * @param string $expected_string
77    *   The expected translated string value.
78    *
79    * @dataProvider providerTestTranslatePlaceholder
80    */
81   public function testTranslatePlaceholder($string, array $args, $expected_string) {
82     $actual = $this->translationManager->translate($string, $args);
83     $this->assertInstanceOf(MarkupInterface::class, $actual);
84     $this->assertEquals($expected_string, (string) $actual);
85   }
86
87   /**
88    * Provides test data for translate().
89    *
90    * @return array
91    */
92   public function providerTestTranslatePlaceholder() {
93     return [
94       ['foo @bar', ['@bar' => 'bar'], 'foo bar'],
95       ['bar %baz', ['%baz' => 'baz'], 'bar <em class="placeholder">baz</em>'],
96       ['bar @bar %baz', ['@bar' => 'bar', '%baz' => 'baz'], 'bar bar <em class="placeholder">baz</em>'],
97       ['bar %baz @bar', ['%baz' => 'baz', '@bar' => 'bar'], 'bar <em class="placeholder">baz</em> bar'],
98     ];
99   }
100
101 }
102
103 class TestTranslationManager extends TranslationManager {
104
105   public function __construct() {
106   }
107
108 }