class FormErrorHandlerTest in Zircon Profile 8
Same name in this branch
- 8 core/tests/Drupal/Tests/Core/Form/FormErrorHandlerTest.php \Drupal\Tests\Core\Form\FormErrorHandlerTest
- 8 core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest
Same name and namespace in other branches
- 8.0 core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest
@coversDefaultClass \Drupal\inline_form_errors\FormErrorHandler @group InlineFormErrors
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest
Expanded class hierarchy of FormErrorHandlerTest
File
- core/
modules/ inline_form_errors/ tests/ src/ Unit/ FormErrorHandlerTest.php, line 20 - Contains \Drupal\Tests\inline_form_errors\Unit\FormErrorHandlerTest.
Namespace
Drupal\Tests\inline_form_errors\UnitView source
class FormErrorHandlerTest extends UnitTestCase {
/**
* @covers ::handleFormErrors
* @covers ::displayErrorMessages
*/
public function testDisplayErrorMessagesInline() {
$link_generator = $this
->getMock(LinkGeneratorInterface::class);
$link_generator
->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 & a half</li-mock><li-mock>Test 3</li-mock></ul-comma-list-mock>', 'error');
$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>';
}));
$form = [
'#parents' => [],
];
$form['test1'] = [
'#type' => 'textfield',
'#title' => 'Test 1',
'#parents' => [
'test1',
],
'#id' => 'edit-test1',
];
$form['test2'] = [
'#type' => 'textfield',
'#title' => 'Test 2 & a half',
'#parents' => [
'test2',
],
'#id' => 'edit-test2',
];
$form['fieldset'] = [
'#parents' => [
'fieldset',
],
'test3' => [
'#type' => 'textfield',
'#title' => 'Test 3',
'#parents' => [
'fieldset',
'test3',
],
'#id' => 'edit-test3',
],
];
$form['test4'] = [
'#type' => 'textfield',
'#title' => 'Test 4',
'#parents' => [
'test4',
],
'#id' => 'edit-test4',
'#error_no_message' => TRUE,
];
$form['test5'] = [
'#type' => 'textfield',
'#parents' => [
'test5',
],
'#id' => 'edit-test5',
];
$form['test6'] = [
'#type' => 'value',
'#title' => 'Test 6',
'#parents' => [
'test6',
],
'#id' => 'edit-test6',
];
$form_state = new FormState();
$form_state
->setErrorByName('test1', 'invalid');
$form_state
->setErrorByName('test2', 'invalid');
$form_state
->setErrorByName('fieldset][test3', 'invalid');
$form_state
->setErrorByName('test4', 'no error message');
$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
->assertSame('invalid', $form['test1']['#errors']);
}
/**
* @covers ::handleFormErrors
* @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['test'] = [
'#type' => 'textfield',
'#title' => 'Test',
'#parents' => [
'test',
],
'#id' => 'edit-test',
];
$form_state = new FormState();
$form_state
->setErrorByName('test', 'invalid');
$form_error_handler
->handleFormErrors($form, $form_state);
$this
->assertSame('invalid', $form['test']['#errors']);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FormErrorHandlerTest:: |
public | function | @covers ::handleFormErrors @covers ::displayErrorMessages | |
FormErrorHandlerTest:: |
public | function | @covers ::handleFormErrors @covers ::setElementErrorsFromFormState | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 259 |