PathElementFormTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace Drupal\KernelTests\Core\Element;
  3. use Drupal\Core\Form\FormInterface;
  4. use Drupal\Core\Form\FormState;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\Render\Element\PathElement;
  7. use Drupal\Core\Url;
  8. use Drupal\KernelTests\KernelTestBase;
  9. use Drupal\user\Entity\Role;
  10. use Drupal\user\Entity\User;
  11. /**
  12. * Tests PathElement validation and conversion functionality.
  13. *
  14. * @group Form
  15. */
  16. class PathElementFormTest extends KernelTestBase implements FormInterface {
  17. /**
  18. * User for testing.
  19. *
  20. * @var \Drupal\user\UserInterface
  21. */
  22. protected $testUser;
  23. /**
  24. * Modules to enable.
  25. *
  26. * @var array
  27. */
  28. public static $modules = ['system', 'user'];
  29. /**
  30. * Sets up the test.
  31. */
  32. protected function setUp() {
  33. parent::setUp();
  34. $this->installSchema('system', ['sequences', 'key_value_expire']);
  35. $this->installEntitySchema('user');
  36. \Drupal::service('router.builder')->rebuild();
  37. /** @var \Drupal\user\RoleInterface $role */
  38. $role = Role::create([
  39. 'id' => 'admin',
  40. 'label' => 'admin',
  41. ]);
  42. $role->grantPermission('link to any page');
  43. $role->save();
  44. $this->testUser = User::create([
  45. 'name' => 'foobar',
  46. 'mail' => 'foobar@example.com',
  47. ]);
  48. $this->testUser->addRole($role->id());
  49. $this->testUser->save();
  50. \Drupal::service('current_user')->setAccount($this->testUser);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getFormId() {
  56. return 'test_path_element';
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function buildForm(array $form, FormStateInterface $form_state) {
  62. // A required validated path.
  63. $form['required_validate'] = [
  64. '#type' => 'path',
  65. '#required' => TRUE,
  66. '#title' => 'required_validate',
  67. '#convert_path' => PathElement::CONVERT_NONE,
  68. ];
  69. // A non validated required path.
  70. $form['required_non_validate'] = [
  71. '#type' => 'path',
  72. '#required' => TRUE,
  73. '#title' => 'required_non_validate',
  74. '#convert_path' => PathElement::CONVERT_NONE,
  75. '#validate_path' => FALSE,
  76. ];
  77. // A non required validated path.
  78. $form['optional_validate'] = [
  79. '#type' => 'path',
  80. '#required' => FALSE,
  81. '#title' => 'optional_validate',
  82. '#convert_path' => PathElement::CONVERT_NONE,
  83. ];
  84. // A non required converted path.
  85. $form['optional_validate'] = [
  86. '#type' => 'path',
  87. '#required' => FALSE,
  88. '#title' => 'optional_validate',
  89. '#convert_path' => PathElement::CONVERT_ROUTE,
  90. ];
  91. // A converted required validated path.
  92. $form['required_validate_route'] = [
  93. '#type' => 'path',
  94. '#required' => TRUE,
  95. '#title' => 'required_validate_route',
  96. ];
  97. // A converted required validated path.
  98. $form['required_validate_url'] = [
  99. '#type' => 'path',
  100. '#required' => TRUE,
  101. '#title' => 'required_validate_url',
  102. '#convert_path' => PathElement::CONVERT_URL,
  103. ];
  104. $form['submit'] = [
  105. '#type' => 'submit',
  106. '#value' => t('Submit'),
  107. ];
  108. return $form;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function submitForm(array &$form, FormStateInterface $form_state) {}
  114. /**
  115. * Form validation handler.
  116. *
  117. * @param array $form
  118. * An associative array containing the structure of the form.
  119. * @param \Drupal\Core\Form\FormStateInterface $form_state
  120. * The current state of the form.
  121. */
  122. public function validateForm(array &$form, FormStateInterface $form_state) {}
  123. /**
  124. * Tests that default handlers are added even if custom are specified.
  125. */
  126. public function testPathElement() {
  127. $form_state = (new FormState())
  128. ->setValues([
  129. 'required_validate' => 'user/' . $this->testUser->id(),
  130. 'required_non_validate' => 'magic-ponies',
  131. 'required_validate_route' => 'user/' . $this->testUser->id(),
  132. 'required_validate_url' => 'user/' . $this->testUser->id(),
  133. ]);
  134. $form_builder = $this->container->get('form_builder');
  135. $form_builder->submitForm($this, $form_state);
  136. // Valid form state.
  137. $this->assertEqual(count($form_state->getErrors()), 0);
  138. $this->assertEqual($form_state->getValue('required_validate_route'), [
  139. 'route_name' => 'entity.user.canonical',
  140. 'route_parameters' => [
  141. 'user' => $this->testUser->id(),
  142. ],
  143. ]);
  144. /** @var \Drupal\Core\Url $url */
  145. $url = $form_state->getValue('required_validate_url');
  146. $this->assertTrue($url instanceof Url);
  147. $this->assertEqual($url->getRouteName(), 'entity.user.canonical');
  148. $this->assertEqual($url->getRouteParameters(), [
  149. 'user' => $this->testUser->id(),
  150. ]);
  151. // Test #required.
  152. $form_state = (new FormState())
  153. ->setValues([
  154. 'required_non_validate' => 'magic-ponies',
  155. 'required_validate_route' => 'user/' . $this->testUser->id(),
  156. 'required_validate_url' => 'user/' . $this->testUser->id(),
  157. ]);
  158. $form_builder->submitForm($this, $form_state);
  159. $errors = $form_state->getErrors();
  160. // Should be missing 'required_validate' field.
  161. $this->assertEqual(count($errors), 1);
  162. $this->assertEqual($errors, ['required_validate' => t('@name field is required.', ['@name' => 'required_validate'])]);
  163. // Test invalid parameters.
  164. $form_state = (new FormState())
  165. ->setValues([
  166. 'required_validate' => 'user/74',
  167. 'required_non_validate' => 'magic-ponies',
  168. 'required_validate_route' => 'user/74',
  169. 'required_validate_url' => 'user/74',
  170. ]);
  171. $form_builder = $this->container->get('form_builder');
  172. $form_builder->submitForm($this, $form_state);
  173. // Valid form state.
  174. $errors = $form_state->getErrors();
  175. $this->assertEqual(count($errors), 3);
  176. $this->assertEqual($errors, [
  177. 'required_validate' => t('This path does not exist or you do not have permission to link to %path.', ['%path' => 'user/74']),
  178. 'required_validate_route' => t('This path does not exist or you do not have permission to link to %path.', ['%path' => 'user/74']),
  179. 'required_validate_url' => t('This path does not exist or you do not have permission to link to %path.', ['%path' => 'user/74']),
  180. ]);
  181. }
  182. }