summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoreffulgentsia2015-10-07 17:16:40 (GMT)
committereffulgentsia2015-10-07 17:16:40 (GMT)
commit1672445a15c83da4c24bca8b0bde0195ffe0d861 (patch)
treea0f794f0c5f17311afbbb90477f245a12f21fbde
parent18a5c7933312fc99c2b94da34310411c7257cb5b (diff)
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
-rw-r--r--core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php7
-rw-r--r--core/lib/Drupal/Core/Validation/DrupalTranslator.php6
-rw-r--r--core/modules/field/src/Tests/FormTest.php4
-rw-r--r--core/modules/system/tests/modules/module_test/module_test.module2
-rw-r--r--core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php24
5 files changed, 39 insertions, 4 deletions
diff --git a/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php b/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php
index 8b4902a..82b76a0 100644
--- a/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php
+++ b/core/lib/Drupal/Core/StringTranslation/TranslatableMarkup.php
@@ -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;
diff --git a/core/lib/Drupal/Core/Validation/DrupalTranslator.php b/core/lib/Drupal/Core/Validation/DrupalTranslator.php
index c7db71c..da9c11a 100644
--- a/core/lib/Drupal/Core/Validation/DrupalTranslator.php
+++ b/core/lib/Drupal/Core/Validation/DrupalTranslator.php
@@ -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));
}
/**
diff --git a/core/modules/field/src/Tests/FormTest.php b/core/modules/field/src/Tests/FormTest.php
index 9e70b38..ce00011 100644
--- a/core/modules/field/src/Tests/FormTest.php
+++ b/core/modules/field/src/Tests/FormTest.php
@@ -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.
diff --git a/core/modules/system/tests/modules/module_test/module_test.module b/core/modules/system/tests/modules/module_test/module_test.module
index cead329..ff7af57 100644
--- a/core/modules/system/tests/modules/module_test/module_test.module
+++ b/core/modules/system/tests/modules/module_test/module_test.module
@@ -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';
}
}
diff --git a/core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php b/core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php
index 0b243cc..b038fd0 100644
--- a/core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php
+++ b/core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php
@@ -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 @@ class TranslatableMarkupTest extends UnitTestCase {
$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);
+ }
+
}