diff --git a/core/core.services.yml b/core/core.services.yml index d050591..419beaa 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -125,7 +125,7 @@ services: arguments: ['@form_validator', '@module_handler', '@keyvalue.expirable', '@event_dispatcher', '@url_generator', '@request_stack', '@?csrf_token', '@?http_kernel'] form_validator: class: Drupal\Core\Form\FormValidator - arguments: ['@request_stack', '@?csrf_token'] + arguments: ['@request_stack', '@string_translation', '@csrf_token'] keyvalue: class: Drupal\Core\KeyValueStore\KeyValueFactory arguments: ['@service_container', '@settings'] diff --git a/core/lib/Drupal/Core/Form/FormBuilder.php b/core/lib/Drupal/Core/Form/FormBuilder.php index c1fa894..3efa1f2 100644 --- a/core/lib/Drupal/Core/Form/FormBuilder.php +++ b/core/lib/Drupal/Core/Form/FormBuilder.php @@ -555,6 +555,7 @@ public function processForm($form_id, &$form, &$form_state) { $this->drupalStaticReset('drupal_html_id'); } + // @todo Move into a dedicated class in https://drupal.org/node/2257835. if ($form_state['submitted'] && !$this->getAnyErrors() && !$form_state['rebuild']) { // Execute form submit handlers. $this->executeSubmitHandlers($form, $form_state); diff --git a/core/lib/Drupal/Core/Form/FormValidator.php b/core/lib/Drupal/Core/Form/FormValidator.php index 2cb1141..9e73738 100644 --- a/core/lib/Drupal/Core/Form/FormValidator.php +++ b/core/lib/Drupal/Core/Form/FormValidator.php @@ -12,7 +12,7 @@ use Drupal\Core\Access\CsrfTokenGenerator; use Drupal\Core\Render\Element; use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\Core\Form\OptGroup; +use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\HttpFoundation\RequestStack; /** @@ -41,11 +41,14 @@ class FormValidator implements FormValidatorInterface { * * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack. + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation + * The string translation service. * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token * The CSRF token generator. */ - public function __construct(RequestStack $request_stack, CsrfTokenGenerator $csrf_token = NULL) { + public function __construct(RequestStack $request_stack, TranslationInterface $string_translation, CsrfTokenGenerator $csrf_token) { $this->requestStack = $request_stack; + $this->stringTranslation = $string_translation; $this->csrfToken = $csrf_token; } diff --git a/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php b/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php index 81bfeb8..efb06c4 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php @@ -125,15 +125,10 @@ public function testValidateInvalidFormToken() { ->will($this->returnValue(FALSE)); $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator') - ->setConstructorArgs(array($request_stack, $csrf_token)) - ->setMethods(array('setErrorByName', 't', 'doValidateForm')) + ->setConstructorArgs(array($request_stack, $this->getStringTranslationStub(), $csrf_token)) + ->setMethods(array('setErrorByName', 'doValidateForm')) ->getMock(); $form_validator->expects($this->once()) - ->method('t') - ->will($this->returnCallback(function ($string, $args) { - return String::format($string, $args); - })); - $form_validator->expects($this->once()) ->method('setErrorByName') ->with('form_token', $this->isType('array'), 'The form has become outdated. Copy any unsaved work in the form below and then reload this page.'); $form_validator->expects($this->never()) @@ -159,7 +154,7 @@ public function testValidateValidFormToken() { ->will($this->returnValue(TRUE)); $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator') - ->setConstructorArgs(array($request_stack, $csrf_token)) + ->setConstructorArgs(array($request_stack, $this->getStringTranslationStub(), $csrf_token)) ->setMethods(array('setErrorByName', 'doValidateForm')) ->getMock(); $form_validator->expects($this->never()) @@ -237,8 +232,11 @@ public function testSetErrorByName($limit_validation_errors, $expected_errors, $ $request_stack = new RequestStack(); $request = new Request(); $request_stack->push($request); + $csrf_token = $this->getMockBuilder('Drupal\Core\Access\CsrfTokenGenerator') + ->disableOriginalConstructor() + ->getMock(); $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator') - ->setConstructorArgs(array($request_stack)) + ->setConstructorArgs(array($request_stack, $this->getStringTranslationStub(), $csrf_token)) ->setMethods(array('drupalSetMessage')) ->getMock(); $form_validator->expects($set_message ? $this->once() : $this->never()) @@ -273,8 +271,11 @@ public function testSetElementErrorsFromFormState() { $request_stack = new RequestStack(); $request = new Request(); $request_stack->push($request); + $csrf_token = $this->getMockBuilder('Drupal\Core\Access\CsrfTokenGenerator') + ->disableOriginalConstructor() + ->getMock(); $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator') - ->setConstructorArgs(array($request_stack)) + ->setConstructorArgs(array($request_stack, $this->getStringTranslationStub(), $csrf_token)) ->setMethods(array('drupalSetMessage')) ->getMock(); @@ -424,17 +425,15 @@ public function testExecuteValidateHandlers() { * @dataProvider providerTestRequiredErrorMessage */ public function testRequiredErrorMessage($element, $expected_message) { - $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator') + $csrf_token = $this->getMockBuilder('Drupal\Core\Access\CsrfTokenGenerator') ->disableOriginalConstructor() - ->setMethods(array('executeValidateHandlers', 't', 'setErrorByName')) + ->getMock(); + $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator') + ->setConstructorArgs(array(new RequestStack(), $this->getStringTranslationStub(), $csrf_token)) + ->setMethods(array('executeValidateHandlers', 'setErrorByName')) ->getMock(); $form_validator->expects($this->once()) ->method('executeValidateHandlers'); - $form_validator->expects($this->any()) - ->method('t') - ->will($this->returnCallback(function ($string, $args) { - return String::format($string, $args); - })); $form_validator->expects($this->once()) ->method('setErrorByName') ->with('test', $this->isType('array'), $expected_message); @@ -477,15 +476,10 @@ public function providerTestRequiredErrorMessage() { public function testElementValidate() { $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator') ->disableOriginalConstructor() - ->setMethods(array('executeValidateHandlers', 't', 'setErrorByName')) + ->setMethods(array('executeValidateHandlers', 'setErrorByName')) ->getMock(); $form_validator->expects($this->once()) ->method('executeValidateHandlers'); - $form_validator->expects($this->any()) - ->method('t') - ->will($this->returnCallback(function ($string, $args) { - return String::format($string, $args); - })); $mock = $this->getMock('stdClass', array('element_validate')); $mock->expects($this->once()) ->method('element_validate') @@ -508,15 +502,13 @@ public function testElementValidate() { * @dataProvider providerTestPerformRequiredValidation */ public function testPerformRequiredValidation($element, $expected_message, $call_watchdog) { - $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator') + $csrf_token = $this->getMockBuilder('Drupal\Core\Access\CsrfTokenGenerator') ->disableOriginalConstructor() - ->setMethods(array('t', 'setErrorByName', 'watchdog')) ->getMock(); - $form_validator->expects($this->any()) - ->method('t') - ->will($this->returnCallback(function ($string, $args) { - return String::format($string, $args); - })); + $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator') + ->setConstructorArgs(array(new RequestStack(), $this->getStringTranslationStub(), $csrf_token)) + ->setMethods(array('setErrorByName', 'watchdog')) + ->getMock(); $form_validator->expects($this->once()) ->method('setErrorByName') ->with('test', $this->isType('array'), $expected_message);