Commit 1672445a authored by effulgentsia's avatar effulgentsia

Issue #2570285 by mr.baileys, cpj, stefan.r, alexpott, nlisgo, joelpittet, Aki...

Issue #2570285 by mr.baileys, cpj, stefan.r, alexpott, nlisgo, joelpittet, Aki Tendo, borisson_, chx, dawehner, s_leu, Berdir: Make sure TranslatableMarkup accepts string values only
parent 18a5c793
......@@ -72,9 +72,16 @@ class TranslatableMarkup extends FormattableMarkup {
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* (optional) The string translation service.
*
* @throws \InvalidArgumentException
* Exception thrown when $string is not a string.
*
* @see \Drupal\Component\Render\FormattableMarkup::placeholderFormat()
*/
public function __construct($string, array $arguments = array(), array $options = array(), TranslationInterface $string_translation = NULL) {
if (!is_string($string)) {
$message = $string instanceof TranslatableMarkup ? '$string ("' . $string->getUntranslatedString() . '") must be a string.' : '$string ("' . (string) $string . '") must be a string.';
throw new \InvalidArgumentException($message);
}
$this->string = $string;
$this->arguments = $arguments;
$this->options = $options;
......
......@@ -8,6 +8,7 @@
namespace Drupal\Core\Validation;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Translates strings using Drupal's translation system.
......@@ -27,8 +28,9 @@ class DrupalTranslator implements TranslatorInterface {
* Implements \Symfony\Component\Translation\TranslatorInterface::trans().
*/
public function trans($id, array $parameters = array(), $domain = NULL, $locale = NULL) {
return t($id, $this->processParameters($parameters), $this->getOptions($domain, $locale));
// If a TranslatableMarkup object is passed in as $id, return it since the
// message has already been translated.
return $id instanceof TranslatableMarkup ? $id : t($id, $this->processParameters($parameters), $this->getOptions($domain, $locale));
}
/**
......
......@@ -23,9 +23,11 @@ class FormTest extends FieldTestBase {
/**
* Modules to enable.
*
* Locale is installed so that TranslatableMarkup actually does something.
*
* @var array
*/
public static $modules = array('node', 'field_test', 'options', 'entity_test');
public static $modules = array('node', 'field_test', 'options', 'entity_test', 'locale');
/**
* An array of values defining a field single.
......
......@@ -47,7 +47,7 @@ function module_test_system_info_alter(&$info, Extension $file, $type) {
}
}
if ($file->getName() == 'seven' && $type == 'theme') {
$info['regions']['test_region'] = t('Test region');
$info['regions']['test_region'] = 'Test region';
}
}
......
......@@ -7,6 +7,7 @@
namespace Drupal\Tests\Core\StringTranslation;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Tests\UnitTestCase;
......@@ -85,4 +86,27 @@ public function testToString() {
$this->assertRegExp('/Exception thrown while calling __toString on a .*Mock_TranslatableMarkup_.* object in .*TranslatableMarkupTest.php on line [0-9]+: Yes you may./', $this->lastErrorMessage);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage $string ("foo") must be a string.
*
* @covers ::__construct
*/
public function testIsStringAssertion() {
$translation = $this->getStringTranslationStub();
new TranslatableMarkup(new TranslatableMarkup('foo', [], [], $translation));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage $string ("foo") must be a string.
*
* @covers ::__construct
*/
public function testIsStringAssertionWithFormattableMarkup() {
$translation = $this->getStringTranslationStub();
$formattable_string = new FormattableMarkup('@bar', ['@bar' => 'foo']);
new TranslatableMarkup($formattable_string);
}
}
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