function PathElementFormTest::testPathElement

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Element/PathElementFormTest.php \Drupal\KernelTests\Core\Element\PathElementFormTest::testPathElement()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Element/PathElementFormTest.php \Drupal\KernelTests\Core\Element\PathElementFormTest::testPathElement()
  3. 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 141

Class

PathElementFormTest
Tests PathElement validation and conversion functionality.

Namespace

Drupal\KernelTests\Core\Element

Code

public function testPathElement() : void {
    $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(),
        'optional_validate' => 'user/' . $this->testUser
            ->id(),
        'optional_validate_route' => '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());
    $this->assertEquals($form_state->getValue('optional_validate_route'), [
        'route_name' => 'entity.user.canonical',
        'route_parameters' => [
            'user' => $this->testUser
                ->id(),
        ],
    ]);
    // 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' => 'required_validate field is required.',
    ], $errors);
    // Test invalid required parameters.
    $form_state = (new FormState())->setValues([
        'required_validate' => 'user/74',
        '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(4, $errors);
    $this->assertEquals([
        'required_validate' => 'This path does not exist or you do not have permission to link to user/74.',
        'required_validate_route' => 'This path does not exist or you do not have permission to link to user/74.',
        'required_validate_url' => 'This path does not exist or you do not have permission to link to user/74.',
        'required_non_validate' => 'required_non_validate field is required.',
    ], $errors);
    // Test invalid optional parameters.
    $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(),
        // Set invalid optional parameters should cause an error.
'optional_validate' => 'user/74',
        'optional_validate_route' => 'user/74',
    ]);
    $form_builder = $this->container
        ->get('form_builder');
    $form_builder->submitForm($this, $form_state);
    // Valid form state.
    $errors = $form_state->getErrors();
    $this->assertEquals(count($errors), 2);
    $this->assertEquals($errors, [
        'optional_validate' => 'This path does not exist or you do not have permission to link to user/74.',
        'optional_validate_route' => 'This path does not exist or you do not have permission to link to user/74.',
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.