TranslationManagerTest.php 3.57 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Contains \Drupal\Tests\Core\StringTranslation\TranslationManagerTest.
 */

8
namespace Drupal\Tests\Core\StringTranslation;
9

10
use Drupal\Component\Render\MarkupInterface;
11 12 13 14
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\Tests\UnitTestCase;

/**
15 16
 * @coversDefaultClass \Drupal\Core\StringTranslation\TranslationManager
 * @group StringTranslation
17 18 19 20 21 22 23 24 25 26
 */
class TranslationManagerTest extends UnitTestCase {

  /**
   * The tested translation manager.
   *
   * @var \Drupal\Core\StringTranslation\TranslationManager
   */
  protected $translationManager;

27 28 29
  /**
   * {@inheritdoc}
   */
30 31 32 33 34 35 36 37 38 39
  protected function setUp() {
    $this->translationManager = new TestTranslationManager();
  }

  /**
   * Provides some test data for formatPlural()
   * @return array
   */
  public function providerTestFormatPlural() {
    return array(
40 41
      [1, 'Singular', '@count plural', array(), array(), 'Singular'],
      [2, 'Singular', '@count plural', array(), array(), '2 plural'],
42
      // @todo support locale_get_plural
43 44
      [2, 'Singular', '@count @arg', array('@arg' => '<script>'), array(), '2 &lt;script&gt;'],
      [2, 'Singular', '@count %arg', array('%arg' => '<script>'), array(), '2 <em class="placeholder">&lt;script&gt;</em>'],
45 46
      [1, 'Singular', '@count plural', array(), array('langcode' => NULL), 'Singular'],
      [1, 'Singular', '@count plural', array(), array('langcode' => 'es'), 'Singular'],
47 48 49 50 51 52
    );
  }

  /**
   * @dataProvider providerTestFormatPlural
   */
53
  public function testFormatPlural($count, $singular, $plural, array $args = array(), array $options = array(), $expected) {
54
    $langcode = empty($options['langcode']) ? 'fr' : $options['langcode'];
55 56 57
    $translator = $this->getMock('\Drupal\Core\StringTranslation\Translator\TranslatorInterface');
    $translator->expects($this->once())
      ->method('getStringTranslation')
58 59
      ->with($langcode, $this->anything(), $this->anything())
      ->will($this->returnCallback(function ($langcode, $string, $context) {
60 61
        return $string;
      }));
62
    $this->translationManager->setDefaultLangcode('fr');
63 64 65
    $this->translationManager->addTranslator($translator);
    $result = $this->translationManager->formatPlural($count, $singular, $plural, $args, $options);
    $this->assertEquals($expected, $result);
66
    $this->assertInstanceOf(MarkupInterface::class, $result);
67 68
  }

69 70 71 72
  /**
   * Tests translation using placeholders.
   *
   * @param string $string
73
   *   A string containing the English text to translate.
74 75 76 77 78 79 80
   * @param array $args
   *   An associative array of replacements to make after translation.
   * @param string $expected_string
   *   The expected translated string value.
   *
   * @dataProvider providerTestTranslatePlaceholder
   */
81
  public function testTranslatePlaceholder($string, array $args = array(), $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 89 90 91 92 93
  }

  /**
   * Provides test data for translate().
   *
   * @return array
   */
  public function providerTestTranslatePlaceholder() {
    return [
94 95 96 97
      ['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'],
98 99
    ];
  }
100 101 102 103 104 105 106 107
}

class TestTranslationManager extends TranslationManager {

  public function __construct() {
  }

}