function PathElementFormTest::testPathElement
Same name in other branches
- 8.9.x core/tests/Drupal/KernelTests/Core/Element/PathElementFormTest.php \Drupal\KernelTests\Core\Element\PathElementFormTest::testPathElement()
- 10 core/tests/Drupal/KernelTests/Core/Element/PathElementFormTest.php \Drupal\KernelTests\Core\Element\PathElementFormTest::testPathElement()
- 11.x core/tests/Drupal/KernelTests/Core/Element/PathElementFormTest.php \Drupal\KernelTests\Core\Element\PathElementFormTest::testPathElement()
Tests that default handlers are added even if custom are specified.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Element/ PathElementFormTest.php, line 143
Class
- PathElementFormTest
- Tests PathElement validation and conversion functionality.
Namespace
Drupal\KernelTests\Core\ElementCode
public function testPathElement() {
$form_state = (new FormState())->setValues([
'required_validate' => 'user/' . $this->testUser
->id(),
'required_non_validate' => 'magic-ponies',
'required_validate_route' => 'user/' . $this->testUser
->id(),
'required_validate_url' => 'user/' . $this->testUser
->id(),
]);
$form_builder = $this->container
->get('form_builder');
$form_builder->submitForm($this, $form_state);
// Valid form state.
$this->assertCount(0, $form_state->getErrors());
$this->assertEquals([
'route_name' => 'entity.user.canonical',
'route_parameters' => [
'user' => $this->testUser
->id(),
],
], $form_state->getValue('required_validate_route'));
/** @var \Drupal\Core\Url $url */
$url = $form_state->getValue('required_validate_url');
$this->assertInstanceOf(Url::class, $url);
$this->assertEquals('entity.user.canonical', $url->getRouteName());
$this->assertEquals([
'user' => $this->testUser
->id(),
], $url->getRouteParameters());
// Test #required.
$form_state = (new FormState())->setValues([
'required_non_validate' => 'magic-ponies',
'required_validate_route' => 'user/' . $this->testUser
->id(),
'required_validate_url' => 'user/' . $this->testUser
->id(),
]);
$form_builder->submitForm($this, $form_state);
$errors = $form_state->getErrors();
// Should be missing 'required_validate' field.
$this->assertCount(1, $errors);
$this->assertEquals([
'required_validate' => t('@name field is required.', [
'@name' => 'required_validate',
]),
], $errors);
// Test invalid parameters.
$form_state = (new FormState())->setValues([
'required_validate' => 'user/74',
'required_non_validate' => 'magic-ponies',
'required_validate_route' => 'user/74',
'required_validate_url' => 'user/74',
]);
$form_builder = $this->container
->get('form_builder');
$form_builder->submitForm($this, $form_state);
// Valid form state.
$errors = $form_state->getErrors();
$this->assertCount(3, $errors);
$this->assertEquals([
'required_validate' => t('This path does not exist or you do not have permission to link to %path.', [
'%path' => 'user/74',
]),
'required_validate_route' => t('This path does not exist or you do not have permission to link to %path.', [
'%path' => 'user/74',
]),
'required_validate_url' => t('This path does not exist or you do not have permission to link to %path.', [
'%path' => 'user/74',
]),
], $errors);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.