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