You are here

public function PathElementFormTest::testPathElement in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Element/PathElementFormTest.php \Drupal\system\Tests\Element\PathElementFormTest::testPathElement()

Tests that default handlers are added even if custom are specified.

File

core/modules/system/src/Tests/Element/PathElementFormTest.php, line 149
Contains \Drupal\system\Tests\Element\PathElementFormTest.

Class

PathElementFormTest
Tests PathElement validation and conversion functionality.

Namespace

Drupal\system\Tests\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
    ->assertEqual(count($form_state
    ->getErrors()), 0);
  $this
    ->assertEqual($form_state
    ->getValue('required_validate_route'), array(
    'route_name' => 'entity.user.canonical',
    'route_parameters' => array(
      'user' => $this->testUser
        ->id(),
    ),
  ));

  /** @var \Drupal\Core\Url $url */
  $url = $form_state
    ->getValue('required_validate_url');
  $this
    ->assertTrue($url instanceof Url);
  $this
    ->assertEqual($url
    ->getRouteName(), 'entity.user.canonical');
  $this
    ->assertEqual($url
    ->getRouteParameters(), array(
    '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
    ->assertEqual(count($errors), 1);
  $this
    ->assertEqual($errors, array(
    'required_validate' => t('@name field is required.', array(
      '@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
    ->assertEqual(count($errors), 3);
  $this
    ->assertEqual($errors, array(
    'required_validate' => t('This path does not exist or you do not have permission to link to %path.', array(
      '%path' => 'user/74',
    )),
    'required_validate_route' => t('This path does not exist or you do not have permission to link to %path.', array(
      '%path' => 'user/74',
    )),
    'required_validate_url' => t('This path does not exist or you do not have permission to link to %path.', array(
      '%path' => 'user/74',
    )),
  ));
}