1d20340819ea50e91c4388f4f999fd7ec8b9db1f
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Form / FormValidationMessageOrderTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Form;
4
5 use Drupal\Core\Form\FormInterface;
6 use Drupal\Core\Form\FormState;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\KernelTests\KernelTestBase;
9
10 /**
11  * Tests form validation mesages are displayed in the same order as the fields.
12  *
13  * @group Form
14  */
15 class FormValidationMessageOrderTest extends KernelTestBase implements FormInterface {
16
17   /**
18    * {@inheritdoc}
19    */
20   public function getFormId() {
21     return 'form_validation_error_message_order_test';
22   }
23
24   /**
25    * {@inheritdoc}
26    */
27   public function buildForm(array $form, FormStateInterface $form_state) {
28     // Prepare fields with weights specified.
29     $form['one'] = [
30       '#type' => 'textfield',
31       '#title' => 'One',
32       '#required' => TRUE,
33       '#weight' => 40,
34     ];
35     $form['two'] = [
36       '#type' => 'textfield',
37       '#title' => 'Two',
38       '#required' => TRUE,
39       '#weight' => 30,
40     ];
41     $form['three'] = [
42       '#type' => 'textfield',
43       '#title' => 'Three',
44       '#required' => TRUE,
45       '#weight' => 10,
46     ];
47     $form['four'] = [
48       '#type' => 'textfield',
49       '#title' => 'Four',
50       '#required' => TRUE,
51       '#weight' => 20,
52     ];
53     $form['actions'] = [
54       '#type' => 'actions',
55       'submit' => [
56         '#type' => 'submit',
57         '#value' => 'Submit',
58       ],
59     ];
60     return $form;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function validateForm(array &$form, FormStateInterface $form_state) {
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function submitForm(array &$form, FormStateInterface $form_state) {
73   }
74
75   /**
76    * Tests that fields validation messages are sorted in the fields order.
77    */
78   public function testLimitValidationErrors() {
79     $form_state = new FormState();
80     $form_builder = $this->container->get('form_builder');
81     $form_builder->submitForm($this, $form_state);
82
83     $messages = drupal_get_messages();
84     $this->assertTrue(isset($messages['error']));
85     $error_messages = $messages['error'];
86     $this->assertEqual($error_messages[0], 'Three field is required.');
87     $this->assertEqual($error_messages[1], 'Four field is required.');
88     $this->assertEqual($error_messages[2], 'Two field is required.');
89     $this->assertEqual($error_messages[3], 'One field is required.');
90   }
91
92 }