You are here

public function PathElementFormTest::testPathElement in Drupal 8

Same name and namespace in other branches
  1. 9 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 144

Class

PathElementFormTest
Tests PathElement validation and conversion functionality.

Namespace

Drupal\KernelTests\Core\Element

Code

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
    ->assertEqual($form_state
    ->getValue('required_validate_route'), [
    'route_name' => 'entity.user.canonical',
    'route_parameters' => [
      'user' => $this->testUser
        ->id(),
    ],
  ]);

  /** @var \Drupal\Core\Url $url */
  $url = $form_state
    ->getValue('required_validate_url');
  $this
    ->assertInstanceOf(Url::class, $url);
  $this
    ->assertEqual($url
    ->getRouteName(), 'entity.user.canonical');
  $this
    ->assertEqual($url
    ->getRouteParameters(), [
    '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
    ->assertEqual($errors, [
    'required_validate' => t('@name field is required.', [
      '@name' => 'required_validate',
    ]),
  ]);

  // 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
    ->assertEqual($errors, [
    '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',
    ]),
  ]);
}