Commit deba295b authored by catch's avatar catch

Issue #2600118 by aerozeppelin, anil280988, stefan.r, dawehner, andypost,...

Issue #2600118 by aerozeppelin, anil280988, stefan.r, dawehner, andypost, sdstyles, ramirez.gerardo: Return TranslationWrapper in t() and $this->t() directly
parent 7d029cb7
......@@ -13,6 +13,7 @@
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Utility\Error;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Minimum supported version of PHP.
......@@ -292,7 +293,7 @@ function drupal_get_path($type, $name) {
* @ingroup sanitization
*/
function t($string, array $args = array(), array $options = array()) {
return \Drupal::translation()->translate($string, $args, $options);
return new TranslatableMarkup($string, $args, $options);
}
/**
......
......@@ -71,7 +71,7 @@ trait StringTranslationTrait {
* @ingroup sanitization
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->getStringTranslation()->translate($string, $args, $options);
return new TranslatableMarkup($string, $args, $options, $this->getStringTranslation());
}
/**
......@@ -80,7 +80,7 @@ protected function t($string, array $args = array(), array $options = array()) {
* @see \Drupal\Core\StringTranslation\TranslationInterface::formatPlural()
*/
protected function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()) {
return $this->getStringTranslation()->formatPlural($count, $singular, $plural, $args, $options);
return new PluralTranslatableMarkup($count, $singular, $plural, $args, $options, $this->getStringTranslation());
}
/**
......
......@@ -9,6 +9,7 @@
use Drupal\breakpoint\Breakpoint;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* @coversDefaultClass \Drupal\breakpoint\Breakpoint
......@@ -65,12 +66,8 @@ protected function setupBreakpoint() {
*/
public function testGetLabel() {
$this->pluginDefinition['label'] = 'Test label';
$this->stringTranslation->expects($this->once())
->method('translate')
->with($this->pluginDefinition['label'], array(), array('context' => 'breakpoint'))
->will($this->returnValue('Test label translated'));
$this->setupBreakpoint();
$this->assertEquals('Test label translated', $this->breakpoint->getLabel());
$this->assertEquals(new TranslatableMarkup('Test label', array(), array('context' => 'breakpoint'), $this->stringTranslation), $this->breakpoint->getLabel());
}
/**
......
......@@ -12,6 +12,7 @@
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* @coversDefaultClass \Drupal\Core\Controller\TitleResolver
......@@ -55,13 +56,7 @@ protected function setUp() {
public function testStaticTitle() {
$request = new Request();
$route = new Route('/test-route', array('_title' => 'static title'));
$this->translationManager->expects($this->once())
->method('translate')
->with('static title', array(), array())
->will($this->returnValue('translated title'));
$this->assertEquals('translated title', $this->titleResolver->getTitle($request, $route));
$this->assertEquals(new TranslatableMarkup('static title', array(), array(), $this->translationManager), $this->titleResolver->getTitle($request, $route));
}
/**
......@@ -72,13 +67,7 @@ public function testStaticTitle() {
public function testStaticTitleWithContext() {
$request = new Request();
$route = new Route('/test-route', array('_title' => 'static title', '_title_context' => 'context'));
$this->translationManager->expects($this->once())
->method('translate')
->with('static title', array(), array('context' => 'context'))
->will($this->returnValue('translated title with context'));
$this->assertEquals('translated title with context', $this->titleResolver->getTitle($request, $route));
$this->assertEquals(new TranslatableMarkup('static title', array(), array('context' => 'context'), $this->translationManager), $this->titleResolver->getTitle($request, $route));
}
/**
......@@ -94,20 +83,14 @@ public function testStaticTitleWithParameter($title, $expected_title) {
$request->attributes->set('_raw_variables', $raw_variables);
$route = new Route('/test-route', array('_title' => $title));
$this->translationManager->expects($this->once())
->method('translate')
->with($title, $this->logicalOr($this->arrayHasKey('@test'), $this->arrayHasKey('%test'), $this->arrayHasKey('!test')), array())
->will($this->returnValue('static title value'));
$this->assertEquals($expected_title, $this->titleResolver->getTitle($request, $route));
}
public function providerTestStaticTitleWithParameter() {
$translation_manager = $this->getMock('\Drupal\Core\StringTranslation\TranslationInterface');
return array(
array('static title @test', 'static title value'),
array('static title !test', 'static title value'),
array('static title %test', 'static title value'),
array('static title @test', new TranslatableMarkup('static title @test', ['@test' => 'value', '%test' => 'value', '@test2' => 'value2', '%test2' => 'value2'], array(), $translation_manager)),
array('static title %test', new TranslatableMarkup('static title %test', ['@test' => 'value', '%test' => 'value', '@test2' => 'value2', '%test2' => 'value2'], array(), $translation_manager)),
);
}
......
......@@ -12,6 +12,7 @@
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* @coversDefaultClass \Drupal\Core\Datetime\DateFormatter
......@@ -97,11 +98,10 @@ protected function setUp() {
public function testFormatInterval($interval, $granularity, $expected, $langcode = NULL) {
// Mocks a simple formatPlural implementation.
$this->stringTranslation->expects($this->any())
->method('formatPlural')
->with($this->anything(), $this->anything(), $this->anything(), array(), array('langcode' => $langcode))
->will($this->returnCallback(function($count, $one, $multiple) {
return $count == 1 ? $one : str_replace('@count', $count, $multiple);
}));
->method('translateString')
->willReturnCallback(function (TranslatableMarkup $arg) {
return $arg->getUntranslatedString();
});
// Check if the granularity is specified.
if ($granularity) {
......@@ -111,7 +111,7 @@ public function testFormatInterval($interval, $granularity, $expected, $langcode
$result = $this->dateFormatter->formatInterval($interval);
}
$this->assertEquals($expected, $result);
$this->assertEquals(new TranslatableMarkup($expected, [], ['langcode' => $langcode], $this->stringTranslation), $result);
}
/**
......@@ -153,14 +153,8 @@ public function providerTestFormatInterval() {
* Tests the formatInterval method for 0 second.
*/
public function testFormatIntervalZeroSecond() {
$this->stringTranslation->expects($this->once())
->method('translate')
->with('0 sec', array(), array('langcode' => 'xxx-lolspeak'))
->will($this->returnValue('0 sec'));
$result = $this->dateFormatter->formatInterval(0, 1, 'xxx-lolspeak');
$this->assertEquals('0 sec', $result);
$this->assertEquals(new TranslatableMarkup('0 sec', array(), array('langcode' => 'xxx-lolspeak'), $this->stringTranslation), $result);
}
/**
......@@ -264,24 +258,20 @@ public function testFormatTimeDiffSince() {
* @covers ::formatDiff
*/
public function testformatDiff($expected, $max_age, $timestamp1, $timestamp2, $options = array()) {
// Mocks a simple formatPlural implementation.
// Mocks a simple translateString implementation.
$this->stringTranslation->expects($this->any())
->method('formatPlural')
->with($this->anything(), $this->anything(), $this->anything(), array(), array('langcode' => isset($options['langcode']) ? $options['langcode'] : NULL))
->will($this->returnCallback(function($count, $one, $multiple) {
return $count == 1 ? $one : str_replace('@count', $count, $multiple);
}));
->method('translateString')
->willReturnCallback(function (TranslatableMarkup $arg) {
return $arg->getUntranslatedString();
});
// Mocks a simple translate implementation.
$this->stringTranslation->expects($this->any())
->method('translate')
->with($this->anything())
->will($this->returnCallback(function($string, $args, $options) {
return $string;
}));
$this->assertEquals($expected, $this->dateFormatter->formatDiff($timestamp1, $timestamp2, $options));
if (isset($options['langcode'])) {
$expected_markup = new TranslatableMarkup($expected, [], ['langcode' => $options['langcode']], $this->stringTranslation);
}
else {
$expected_markup = new TranslatableMarkup($expected, [], [], $this->stringTranslation);
}
$this->assertEquals($expected_markup, $this->dateFormatter->formatDiff($timestamp1, $timestamp2, $options));
$options['return_as_object'] = TRUE;
$expected_object = new FormattedDateDiff($expected, $max_age);
......
......@@ -311,15 +311,7 @@ public function testGetGroupLabel() {
$this->assertSame($default_label, $entity_type->getGroupLabel());
$default_label = new TranslatableMarkup('Other', array(), array('context' => 'Entity type group'));
$entity_type = $this->setUpEntityType([]);
$string_translation = $this->getMock(TranslationInterface::class);
$string_translation->expects($this->atLeastOnce())
->method('translate')
->with('Other', array(), array('context' => 'Entity type group'))
->willReturn($default_label);
$entity_type->setStringTranslation($string_translation);
$entity_type = $this->setUpEntityType(array('group_label' => $default_label));
$this->assertSame($default_label, $entity_type->getGroupLabel());
}
......
......@@ -7,7 +7,11 @@
namespace Drupal\Tests\Core\StringTranslation;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
/**
* @coversDefaultClass \Drupal\Core\StringTranslation\StringTranslationTrait
......@@ -35,11 +39,13 @@ class StringTranslationTraitTest extends UnitTestCase {
*/
protected function setUp() {
$this->translation = $this->getObjectForTrait('\Drupal\Core\StringTranslation\StringTranslationTrait');
$stub = $this->getStringTranslationStub();
$stub->expects($this->any())
->method('formatPlural')
->will($this->returnArgument(2));
$this->translation->setStringTranslation($stub);
$mock = $this->prophesize(TranslationInterface::class);
$mock->translate(Argument::cetera())->shouldNotBeCalled();
$mock->formatPlural(Argument::cetera())->shouldNotBeCalled();
$mock->translateString(Argument::cetera())->will(function ($args) {
return $args[0]->getUntranslatedString();
});
$this->translation->setStringTranslation($mock->reveal());
$this->reflection = new \ReflectionClass(get_class($this->translation));
}
......@@ -50,7 +56,9 @@ public function testT() {
$method = $this->reflection->getMethod('t');
$method->setAccessible(TRUE);
$this->assertEquals('something', $method->invoke($this->translation, 'something'));
$result = $method->invoke($this->translation, 'something');
$this->assertInstanceOf(TranslatableMarkup::class, $result);
$this->assertEquals('something', $result);
}
/**
......@@ -60,7 +68,9 @@ public function testFormatPlural() {
$method = $this->reflection->getMethod('formatPlural');
$method->setAccessible(TRUE);
$this->assertEquals('apples', $method->invoke($this->translation, 2, 'apple', 'apples'));
$result = $method->invoke($this->translation, 2, 'apple', 'apples');
$this->assertInstanceOf(PluralTranslatableMarkup::class, $result);
$this->assertEquals('apples', $result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment