summaryrefslogtreecommitdiffstats
path: root/core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php')
-rw-r--r--core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php101
1 files changed, 64 insertions, 37 deletions
diff --git a/core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php b/core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php
index f110b36..fccf9b6 100644
--- a/core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php
+++ b/core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php
@@ -3,6 +3,7 @@
namespace Drupal\Tests\inline_form_errors\Unit;
use Drupal\Core\Form\FormState;
+use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\inline_form_errors\FormErrorHandler;
@@ -15,34 +16,69 @@ use Drupal\Tests\UnitTestCase;
class FormErrorHandlerTest extends UnitTestCase {
/**
+ * The form error handler.
+ *
+ * @var \Drupal\inline_form_errors\FormErrorHandler
+ */
+ protected $formErrorHandler;
+
+ /**
+ * The messenger.
+ *
+ * @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ protected $messenger;
+
+ /**
+ * The renderer.
+ *
+ * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ protected $renderer;
+
+ /**
+ * The link generator.
+ *
+ * @var \Drupal\Core\Utility\LinkGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
+ */
+ protected $linkGenerator;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setUp() {
+ parent::setUp();
+ $this->linkGenerator = $this->createMock(LinkGeneratorInterface::class);
+ $this->renderer = $this->createMock(RendererInterface::class);
+ $this->messenger = $this->createMock(MessengerInterface::class);
+
+ $this->formErrorHandler = new FormErrorHandler($this->getStringTranslationStub(), $this->linkGenerator, $this->renderer, $this->messenger);
+ }
+
+ /**
* @covers ::handleFormErrors
* @covers ::displayErrorMessages
*/
public function testDisplayErrorMessagesInline() {
- $link_generator = $this->getMock(LinkGeneratorInterface::class);
- $link_generator->expects($this->any())
+
+ $this->linkGenerator->expects($this->any())
->method('generate')
->willReturnArgument(0);
- $renderer = $this->getMock(RendererInterface::class);
- $form_error_handler = $this->getMockBuilder(FormErrorHandler::class)
- ->setConstructorArgs([$this->getStringTranslationStub(), $link_generator, $renderer])
- ->setMethods(['drupalSetMessage'])
- ->getMock();
-
- $form_error_handler->expects($this->at(0))
- ->method('drupalSetMessage')
- ->with('no title given', 'error');
- $form_error_handler->expects($this->at(1))
- ->method('drupalSetMessage')
- ->with('element is invisible', 'error');
- $form_error_handler->expects($this->at(2))
- ->method('drupalSetMessage')
- ->with('this missing element is invalid', 'error');
- $form_error_handler->expects($this->at(3))
- ->method('drupalSetMessage')
- ->with('3 errors have been found: <ul-comma-list-mock><li-mock>Test 1</li-mock><li-mock>Test 2 &amp; a half</li-mock><li-mock>Test 3</li-mock></ul-comma-list-mock>', 'error');
-
- $renderer->expects($this->any())
+
+ $this->messenger->expects($this->at(0))
+ ->method('addError')
+ ->with('no title given');
+ $this->messenger->expects($this->at(1))
+ ->method('addError')
+ ->with('element is invisible');
+ $this->messenger->expects($this->at(2))
+ ->method('addError')
+ ->with('this missing element is invalid');
+ $this->messenger->expects($this->at(3))
+ ->method('addError')
+ ->with('3 errors have been found: <ul-comma-list-mock><li-mock>Test 1</li-mock><li-mock>Test 2 &amp; a half</li-mock><li-mock>Test 3</li-mock></ul-comma-list-mock>');
+
+ $this->renderer->expects($this->any())
->method('renderPlain')
->will($this->returnCallback(function ($render_array) {
return $render_array[0]['#markup'] . '<ul-comma-list-mock><li-mock>' . implode(array_map('htmlspecialchars', $render_array[1]['#items']), '</li-mock><li-mock>') . '</li-mock></ul-comma-list-mock>';
@@ -107,7 +143,7 @@ class FormErrorHandlerTest extends UnitTestCase {
$form_state->setErrorByName('test5', 'no title given');
$form_state->setErrorByName('test6', 'element is invisible');
$form_state->setErrorByName('missing_element', 'this missing element is invalid');
- $form_error_handler->handleFormErrors($form, $form_state);
+ $this->formErrorHandler->handleFormErrors($form, $form_state);
$this->assertSame('invalid', $form['test1']['#errors']);
}
@@ -116,11 +152,6 @@ class FormErrorHandlerTest extends UnitTestCase {
* @covers ::setElementErrorsFromFormState
*/
public function testSetElementErrorsFromFormState() {
- $form_error_handler = $this->getMockBuilder(FormErrorHandler::class)
- ->setConstructorArgs([$this->getStringTranslationStub(), $this->getMock(LinkGeneratorInterface::class), $this->getMock(RendererInterface::class)])
- ->setMethods(['drupalSetMessage'])
- ->getMock();
-
$form = [
'#parents' => [],
'#form_id' => 'test_form',
@@ -135,7 +166,7 @@ class FormErrorHandlerTest extends UnitTestCase {
];
$form_state = new FormState();
$form_state->setErrorByName('test', 'invalid');
- $form_error_handler->handleFormErrors($form, $form_state);
+ $this->formErrorHandler->handleFormErrors($form, $form_state);
$this->assertSame('invalid', $form['test']['#errors']);
}
@@ -143,14 +174,10 @@ class FormErrorHandlerTest extends UnitTestCase {
* Tests that opting out of Inline Form Errors works.
*/
public function testDisplayErrorMessagesNotInline() {
- $form_error_handler = $this->getMockBuilder(FormErrorHandler::class)
- ->setConstructorArgs([$this->getStringTranslationStub(), $this->getMock(LinkGeneratorInterface::class), $this->getMock(RendererInterface::class)])
- ->setMethods(['drupalSetMessage'])
- ->getMock();
- $form_error_handler->expects($this->at(0))
- ->method('drupalSetMessage')
- ->with('invalid', 'error');
+ $this->messenger->expects($this->at(0))
+ ->method('addMessage')
+ ->with('invalid', 'error', FALSE);
$form = [
'#parents' => [],
@@ -166,7 +193,7 @@ class FormErrorHandlerTest extends UnitTestCase {
];
$form_state = new FormState();
$form_state->setErrorByName('test', 'invalid');
- $form_error_handler->handleFormErrors($form, $form_state);
+ $this->formErrorHandler->handleFormErrors($form, $form_state);
}
}