diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php index c3e2f68..f7d311f 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php @@ -41,6 +41,24 @@ public function translate($string, array $args = array(), array $options = array()); /** + * Translates a string to the current language or to a given language. + * + * @param string $string + * A string containing the English string to translate. + * @param array $options + * An associative array of additional options, with the following elements: + * - 'langcode': The language code to translate to a language other than + * what is used to display the page. + * - 'context': The context the source string belongs to. + * + * @return string + * The translated string. + * + * @internal + */ + public function doTranslate($string, array $options = array()); + + /** * Formats a string containing a count of items. * * This function ensures that the string is pluralized correctly. Since t() is diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php index af6aa2b..c22c762 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationManager.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php @@ -140,38 +140,13 @@ public function getStringTranslation($langcode, $string, $context) { * {@inheritdoc} */ public function translate($string, array $args = array(), array $options = array()) { - $string = $this->doTranslate($string, $options); - if (empty($args)) { - // We add the string to the safe list as opposed to making it an object - // implementing SafeStringInterface as we may need to call __toString() - // on the object before render time, at which point the string ceases to - // be safe, and working around this would require significant rework. - // Adding this string to the safe list is assumed to be safe because - // translate() should only be called with strings defined in code. - // @see \Drupal\Core\StringTranslation\TranslationInterface::translate() - SafeMarkup::setMultiple([$string => ['html' => TRUE]]); - return $string; - } - else { - return SafeMarkup::format($string, $args); - } + return new TranslationWrapper($string, $args, $options); } /** - * Translates a string to the current language or to a given language. - * - * @param string $string - * A string containing the English string to translate. - * @param array $options - * An associative array of additional options, with the following elements: - * - 'langcode': The language code to translate to a language other than - * what is used to display the page. - * - 'context': The context the source string belongs to. - * - * @return string - * The translated string. + * {@inheritdoc} */ - protected function doTranslate($string, array $options = array()) { + public function doTranslate($string, array $options = array()) { // Merge in defaults. if (empty($options['langcode'])) { $options['langcode'] = $this->defaultLangcode; diff --git a/core/lib/Drupal/Core/StringTranslation/TranslationWrapper.php b/core/lib/Drupal/Core/StringTranslation/TranslationWrapper.php index 10e9b12..162b267 100644 --- a/core/lib/Drupal/Core/StringTranslation/TranslationWrapper.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationWrapper.php @@ -7,6 +7,8 @@ namespace Drupal\Core\StringTranslation; +use Drupal\Component\Utility\Html; +use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SafeStringInterface; use Drupal\Component\Utility\ToStringTrait; @@ -103,7 +105,36 @@ public function getOptions() { * The translated string. */ public function render() { - return $this->t($this->string, $this->arguments, $this->options); + $string = $this->getStringTranslation()->doTranslate($this->string, $this->options); + // @todo fix this pending a decision on https://www.drupal.org/node/2506427 + if (!empty($this->arguments)) { + // Transform arguments before inserting them. + $args = $this->arguments; + foreach ($args as $key => $value) { + switch ($key[0]) { + case '@': + if (!SafeMarkup::isSafe($value)) { + // Escaped only. + $args[$key] = Html::escape($value); + } + break; + + case '%': + default: + // Escaped and placeholder. + if (!SafeMarkup::isSafe($value)) { + $value = Html::escape($value); + } + $args[$key] = '' . $value . ''; + break; + + case '!': + // Pass-through. This should be safe. + } + } + $string = strtr($string, $args); + } + return $string; } /** @@ -123,5 +154,4 @@ public function jsonSerialize() { return $this->__toString(); } - } diff --git a/core/lib/Drupal/Core/Template/Attribute.php b/core/lib/Drupal/Core/Template/Attribute.php index cc0d591..76d7c6a 100644 --- a/core/lib/Drupal/Core/Template/Attribute.php +++ b/core/lib/Drupal/Core/Template/Attribute.php @@ -109,7 +109,8 @@ protected function createAttributeValue($name, $value) { elseif (is_bool($value)) { $value = new AttributeBoolean($name, $value); } - elseif (!is_object($value)) { + // As a development aid, we allow the value to be a safe string object. + elseif (!is_object($value) || $value instanceof SafeStringInterface) { $value = new AttributeString($name, $value); } return $value; diff --git a/core/lib/Drupal/Core/Validation/DrupalTranslator.php b/core/lib/Drupal/Core/Validation/DrupalTranslator.php index a2bbf5b..dfbef68 100644 --- a/core/lib/Drupal/Core/Validation/DrupalTranslator.php +++ b/core/lib/Drupal/Core/Validation/DrupalTranslator.php @@ -7,6 +7,8 @@ namespace Drupal\Core\Validation; +use Drupal\Component\Utility\SafeStringInterface; + /** * Translates strings using Drupal's translation system. * @@ -73,8 +75,13 @@ public function getLocale() { protected function processParameters(array $parameters) { $return = array(); foreach ($parameters as $key => $value) { + // We allow the values in the parameters to be safe string objects. This can be + // useful when we want to use parameter values that are TranslationWrappers. + if ($value instanceof SafeStringInterface) { + $value = (string) $value; + } if (is_object($value)) { - // t() does not work will objects being passed as replacement strings. + // t() does not work with objects being passed as replacement strings. } // Check for symfony replacement patterns in the form "{{ name }}". elseif (strpos($key, '{{ ') === 0 && strrpos($key, ' }}') == strlen($key) - 3) { diff --git a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php index d873820..6fa8771 100644 --- a/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php +++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidator.php @@ -16,6 +16,7 @@ use Drupal\Core\TypedData\Type\StringInterface; use Drupal\Core\TypedData\Type\UriInterface; use Drupal\Core\TypedData\Validation\TypedDataAwareValidatorTrait; +use Drupal\Component\Utility\SafeStringInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; @@ -49,7 +50,7 @@ public function validate($value, Constraint $constraint) { if ($typed_data instanceof IntegerInterface && filter_var($value, FILTER_VALIDATE_INT) === FALSE) { $valid = FALSE; } - if ($typed_data instanceof StringInterface && !is_scalar($value)) { + if ($typed_data instanceof StringInterface && !is_scalar($value) && !($value instanceof SafeStringInterface)) { $valid = FALSE; } // Ensure that URIs comply with http://tools.ietf.org/html/rfc3986, which diff --git a/core/modules/ckeditor/src/Tests/CKEditorAdminTest.php b/core/modules/ckeditor/src/Tests/CKEditorAdminTest.php index ca1954f..a321940 100644 --- a/core/modules/ckeditor/src/Tests/CKEditorAdminTest.php +++ b/core/modules/ckeditor/src/Tests/CKEditorAdminTest.php @@ -87,23 +87,23 @@ function testExistingFormat() { // Button groups array( array( - 'name' => t('Formatting'), + 'name' => 'Formatting', 'items' => array('Bold', 'Italic',), ), array( - 'name' => t('Links'), + 'name' => 'Links', 'items' => array('DrupalLink', 'DrupalUnlink',), ), array( - 'name' => t('Lists'), + 'name' => 'Lists', 'items' => array('BulletedList', 'NumberedList',), ), array( - 'name' => t('Media'), + 'name' => 'Media', 'items' => array('Blockquote', 'DrupalImage',), ), array( - 'name' => t('Tools'), + 'name' => 'Tools', 'items' => array('Source',), ), ), diff --git a/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php b/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php index 05af294..02e364b 100644 --- a/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php +++ b/core/modules/locale/src/Tests/LocaleImportFunctionalTest.php @@ -238,8 +238,10 @@ public function testLanguageContext() { 'langcode' => 'hr', )); - $this->assertIdentical(t('May', array(), array('langcode' => 'hr', 'context' => 'Long month name')), 'Svibanj', 'Long month name context is working.'); - $this->assertIdentical(t('May', array(), array('langcode' => 'hr')), 'Svi.', 'Default context is working.'); + // We cast the return value of t() to string so as to retrieve the translated + // value, rendered as a string. + $this->assertIdentical((string) t('May', array(), array('langcode' => 'hr', 'context' => 'Long month name')), 'Svibanj', 'Long month name context is working.'); + $this->assertIdentical((string) t('May', array(), array('langcode' => 'hr')), 'Svi.', 'Default context is working.'); } /** @@ -254,7 +256,7 @@ public function testEmptyMsgstr() { )); $this->assertRaw(t('One translation file imported. %number translations were added, %update translations were updated and %delete translations were removed.', array('%number' => 1, '%update' => 0, '%delete' => 0)), 'The translation file was successfully imported.'); - $this->assertIdentical(t('Operations', array(), array('langcode' => $langcode)), 'Műveletek', 'String imported and translated.'); + $this->assertIdentical((string) t('Operations', array(), array('langcode' => $langcode)), 'Műveletek', 'String imported and translated.'); // Try importing a .po file. $this->importPoFile($this->getPoFileWithEmptyMsgstr(), array( diff --git a/core/modules/locale/src/Tests/LocaleTranslationUiTest.php b/core/modules/locale/src/Tests/LocaleTranslationUiTest.php index 6c35b18..a708311 100644 --- a/core/modules/locale/src/Tests/LocaleTranslationUiTest.php +++ b/core/modules/locale/src/Tests/LocaleTranslationUiTest.php @@ -64,7 +64,7 @@ public function testStringTranslation() { ); $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); // Add string. - t($name, array(), array('langcode' => $langcode)); + (string) t($name, array(), array('langcode' => $langcode)); // Reset locale cache. $this->container->get('string_translation')->reset(); $this->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.'); @@ -302,7 +302,7 @@ public function testStringValidation() { ); $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); // Add string. - t($name, array(), array('langcode' => $langcode)); + (string) t($name, array(), array('langcode' => $langcode)); // Reset locale cache. $search = array( 'string' => $name, @@ -361,7 +361,7 @@ public function testStringSearch() { $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); // Add string. - t($name, array(), array('langcode' => $langcode)); + (string) t($name, array(), array('langcode' => $langcode)); // Reset locale cache. $this->container->get('string_translation')->reset(); $this->drupalLogout(); diff --git a/core/modules/system/src/Tests/Form/FormTest.php b/core/modules/system/src/Tests/Form/FormTest.php index e89b78f..b02cbdc 100644 --- a/core/modules/system/src/Tests/Form/FormTest.php +++ b/core/modules/system/src/Tests/Form/FormTest.php @@ -144,7 +144,7 @@ function testRequiredFields() { // Select elements are going to have validation errors with empty // input, since those are illegal choices. Just make sure the // error is not "field is required". - $this->assertTrue((empty($errors[$element]) || strpos('field is required', $errors[$element]) === FALSE), "Optional '$type' field '$element' is not treated as a required element"); + $this->assertTrue((empty($errors[$element]) || strpos('field is required', (string) $errors[$element]) === FALSE), "Optional '$type' field '$element' is not treated as a required element"); } else { // Make sure there is *no* form error for this element. diff --git a/core/modules/system/tests/modules/plugin_test/src/Plugin/MockBlockManager.php b/core/modules/system/tests/modules/plugin_test/src/Plugin/MockBlockManager.php index e435c0a..d4aeb4f 100644 --- a/core/modules/system/tests/modules/plugin_test/src/Plugin/MockBlockManager.php +++ b/core/modules/system/tests/modules/plugin_test/src/Plugin/MockBlockManager.php @@ -40,7 +40,7 @@ public function __construct() { // A simple plugin: the user login block. $this->discovery->setDefinition('user_login', array( - 'label' => t('User login'), + 'label' => 'User login', 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserLoginBlock', )); @@ -67,7 +67,7 @@ public function __construct() { // MockLayoutBlockDeriver class ensures that both the base plugin and the // derivatives are available to the system. $this->discovery->setDefinition('layout', array( - 'label' => t('Layout'), + 'label' => 'Layout', 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock', 'deriver' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlockDeriver', )); @@ -78,7 +78,7 @@ public function __construct() { 'label' => t('User name'), 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock', 'context' => array( - 'user' => new ContextDefinition('entity:user', t('User')), + 'user' => new ContextDefinition('entity:user', 'User'), ), )); @@ -87,7 +87,7 @@ public function __construct() { 'label' => t('User name optional'), 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserNameBlock', 'context' => array( - 'user' => new ContextDefinition('entity:user', t('User'), FALSE), + 'user' => new ContextDefinition('entity:user', 'User', FALSE), ), )); @@ -102,8 +102,8 @@ public function __construct() { 'label' => t('Complex context'), 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockComplexContextBlock', 'context' => array( - 'user' => new ContextDefinition('entity:user', t('User')), - 'node' => new ContextDefinition('entity:node', t('Node')), + 'user' => new ContextDefinition('entity:user', 'User'), + 'node' => new ContextDefinition('entity:node', 'Node'), ), )); diff --git a/core/modules/user/src/Tests/UserCancelTest.php b/core/modules/user/src/Tests/UserCancelTest.php index 5357013..648f56f 100644 --- a/core/modules/user/src/Tests/UserCancelTest.php +++ b/core/modules/user/src/Tests/UserCancelTest.php @@ -535,7 +535,7 @@ function testMassUserCancelByAdmin() { $this->drupalPostForm(NULL, NULL, t('Cancel accounts')); $status = TRUE; foreach ($users as $account) { - $status = $status && (strpos($this->content, t('%name has been deleted.', array('%name' => $account->getUsername()))) !== FALSE); + $status = $status && (strpos($this->content, $account->getUsername() . ' has been deleted.') !== FALSE); $user_storage->resetCache(array($account->id())); $status = $status && !$user_storage->load($account->id()); } diff --git a/core/modules/views/src/Plugin/views/PluginBase.php b/core/modules/views/src/Plugin/views/PluginBase.php index 49b7f1d..66661df 100644 --- a/core/modules/views/src/Plugin/views/PluginBase.php +++ b/core/modules/views/src/Plugin/views/PluginBase.php @@ -15,6 +15,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginBase as ComponentPluginBase; use Drupal\Core\Render\Element; +use Drupal\Core\StringTranslation\TranslationWrapper; use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\ViewExecutable; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -552,7 +553,14 @@ protected function listLanguages($flags = LanguageInterface::STATE_ALL, array $c // Since this is not a real language, surround it by '***LANGUAGE_...***', // like the negotiated languages below. if ($flags & LanguageInterface::STATE_SITE_DEFAULT) { - $list[PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT] = $this->t($languages[LanguageInterface::LANGCODE_SITE_DEFAULT]->getName()); + $name = $languages[LanguageInterface::LANGCODE_SITE_DEFAULT]->getName(); + // The language name may have already been translated, no need to + // translate it again. + // @see Drupal\Core\Language::filterLanguages(). + if (!$name instanceof TranslationWrapper) { + $name = $this->t($name); + } + $list[PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT] = $name; // Remove site default language from $languages so it's not added // twice with the real languages below. unset($languages[LanguageInterface::LANGCODE_SITE_DEFAULT]); diff --git a/core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php b/core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php index be8cca3..df3ff1a 100644 --- a/core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php +++ b/core/tests/Drupal/Tests/Core/Annotation/TranslationTest.php @@ -46,8 +46,8 @@ public function testGet(array $values, $expected) { 'context' => $values['context'], ) : array(); $this->translationManager->expects($this->once()) - ->method('translate') - ->with($values['value'], $arguments, $options); + ->method('doTranslate') + ->with($values['value'], $options); $annotation = new Translation($values); @@ -61,9 +61,9 @@ public function providerTestGet() { $data = array(); $data[] = array( array( - 'value' => 'Foo', + 'value' => 'Foo' ), - 'Foo' + 'Foo', ); $random = $this->randomMachineName(); $random_html_entity = '&' . $random; diff --git a/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php index ac92088..14758e1 100644 --- a/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php @@ -74,8 +74,8 @@ public function testGetTitle() { $this->pluginDefinition['title'] = (new TranslationWrapper($title)) ->setStringTranslation($this->stringTranslation); $this->stringTranslation->expects($this->once()) - ->method('translate') - ->with($title, array(), array()) + ->method('doTranslate') + ->with($title, array()) ->will($this->returnValue('Example translated')); $this->setupContextualLinkDefault(); @@ -90,8 +90,8 @@ public function testGetTitleWithContext() { $this->pluginDefinition['title'] = (new TranslationWrapper($title, array(), array('context' => 'context'))) ->setStringTranslation($this->stringTranslation); $this->stringTranslation->expects($this->once()) - ->method('translate') - ->with($title, array(), array('context' => 'context')) + ->method('doTranslate') + ->with($title, array('context' => 'context')) ->will($this->returnValue('Example translated with context')); $this->setupContextualLinkDefault(); @@ -106,8 +106,8 @@ public function testGetTitleWithTitleArguments() { $this->pluginDefinition['title'] = (new TranslationWrapper($title, array('@test' => 'value'))) ->setStringTranslation($this->stringTranslation); $this->stringTranslation->expects($this->once()) - ->method('translate') - ->with($title, array('@test' => 'value'), array()) + ->method('doTranslate') + ->with($title, array()) ->will($this->returnValue('Example value')); $this->setupContextualLinkDefault(); diff --git a/core/tests/Drupal/Tests/Core/Menu/LocalActionDefaultTest.php b/core/tests/Drupal/Tests/Core/Menu/LocalActionDefaultTest.php index 02d4578..105ae5e 100644 --- a/core/tests/Drupal/Tests/Core/Menu/LocalActionDefaultTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/LocalActionDefaultTest.php @@ -86,8 +86,8 @@ public function testGetTitle() { $this->pluginDefinition['title'] = (new TranslationWrapper('Example')) ->setStringTranslation($this->stringTranslation); $this->stringTranslation->expects($this->once()) - ->method('translate') - ->with('Example', array(), array()) + ->method('doTranslate') + ->with('Example', array()) ->will($this->returnValue('Example translated')); $this->setupLocalActionDefault(); @@ -103,8 +103,8 @@ public function testGetTitleWithContext() { $this->pluginDefinition['title'] = (new TranslationWrapper('Example', array(), array('context' => 'context'))) ->setStringTranslation($this->stringTranslation); $this->stringTranslation->expects($this->once()) - ->method('translate') - ->with('Example', array(), array('context' => 'context')) + ->method('doTranslate') + ->with('Example', array('context' => 'context')) ->will($this->returnValue('Example translated with context')); $this->setupLocalActionDefault(); @@ -118,8 +118,8 @@ public function testGetTitleWithTitleArguments() { $this->pluginDefinition['title'] = (new TranslationWrapper('Example @test', array('@test' => 'value'))) ->setStringTranslation($this->stringTranslation); $this->stringTranslation->expects($this->once()) - ->method('translate') - ->with('Example @test', array('@test' => 'value'), array()) + ->method('doTranslate') + ->with('Example @test', array()) ->will($this->returnValue('Example value')); $this->setupLocalActionDefault(); diff --git a/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php b/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php index 6e393a8..2420b37 100644 --- a/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php @@ -236,8 +236,8 @@ public function testGetTitle() { $this->pluginDefinition['title'] = (new TranslationWrapper('Example')) ->setStringTranslation($this->stringTranslation); $this->stringTranslation->expects($this->once()) - ->method('translate') - ->with('Example', array(), array()) + ->method('doTranslate') + ->with('Example', array()) ->will($this->returnValue('Example translated')); $this->setupLocalTaskDefault(); @@ -252,8 +252,8 @@ public function testGetTitleWithContext() { $this->pluginDefinition['title'] = (new TranslationWrapper($title, array(), array('context' => 'context'))) ->setStringTranslation($this->stringTranslation); $this->stringTranslation->expects($this->once()) - ->method('translate') - ->with($title, array(), array('context' => 'context')) + ->method('doTranslate') + ->with($title, array('context' => 'context')) ->will($this->returnValue('Example translated with context')); $this->setupLocalTaskDefault(); @@ -268,8 +268,8 @@ public function testGetTitleWithTitleArguments() { $this->pluginDefinition['title'] = (new TranslationWrapper('Example @test', array('@test' => 'value'))) ->setStringTranslation($this->stringTranslation); $this->stringTranslation->expects($this->once()) - ->method('translate') - ->with($title, array('@test' => 'value'), array()) + ->method('doTranslate') + ->with($title, array()) ->will($this->returnValue('Example value')); $this->setupLocalTaskDefault(); diff --git a/core/tests/Drupal/Tests/Core/StringTranslation/TranslationWrapperTest.php b/core/tests/Drupal/Tests/Core/StringTranslation/TranslationWrapperTest.php index 3facf38..b6cdc09 100644 --- a/core/tests/Drupal/Tests/Core/StringTranslation/TranslationWrapperTest.php +++ b/core/tests/Drupal/Tests/Core/StringTranslation/TranslationWrapperTest.php @@ -66,7 +66,7 @@ public function testToString() { ->willReturn(''); $translation = $this->prophesize(TranslationInterface::class); - $translation->translate($string, [], [])->will(function () { + $translation->doTranslate($string, [])->will(function () { throw new \Exception('Yes you may.'); }); $text->setStringTranslation($translation->reveal()); diff --git a/core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest.php b/core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest.php index 2bcb318..0707fa3 100644 --- a/core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest.php +++ b/core/tests/Drupal/Tests/Core/Validation/Plugin/Validation/Constraint/PrimitiveTypeConstraintValidatorTest.php @@ -16,6 +16,7 @@ use Drupal\Core\TypedData\PrimitiveInterface; use Drupal\Core\Validation\Plugin\Validation\Constraint\PrimitiveTypeConstraint; use Drupal\Core\Validation\Plugin\Validation\Constraint\PrimitiveTypeConstraintValidator; +use Drupal\Core\StringTranslation\TranslationWrapper; use Drupal\Tests\UnitTestCase; /** @@ -63,6 +64,7 @@ public function provideTestValidate() { $data[] = [new IntegerData(DataDefinition::create('integer')), 1.5, FALSE]; $data[] = [new IntegerData(DataDefinition::create('integer')), 'test', FALSE]; $data[] = [new StringData(DataDefinition::create('string')), 'test', TRUE]; + $data[] = [new StringData(DataDefinition::create('string')), new TranslationWrapper('test'), TRUE]; // It is odd that 1 is a valid string. // $data[] = [$this->getMock('Drupal\Core\TypedData\Type\StringInterface'), 1, FALSE]; $data[] = [new StringData(DataDefinition::create('string')), [], FALSE]; diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php index 3433c94..5819c39 100644 --- a/core/tests/Drupal/Tests/UnitTestCase.php +++ b/core/tests/Drupal/Tests/UnitTestCase.php @@ -220,6 +220,11 @@ public function getStringTranslationStub() { ->willReturnCallback(function ($count, $singular, $plural, array $args = [], array $options = []) { return $count === 1 ? SafeMarkup::format($singular, $args) : SafeMarkup::format($plural, $args + ['@count' => $count]); }); + $translation->expects($this->any()) + ->method('doTranslate') + ->willReturnCallback(function ($string, array $options = []) { + return $string; + }); return $translation; }