-
Notifications
You must be signed in to change notification settings - Fork 31
/
RenderingTest.php
128 lines (111 loc) · 3.43 KB
/
RenderingTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
declare(strict_types = 1);
namespace Drupal\Tests\oe_theme\Kernel;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
/**
* Tests that rendering of elements follows the theme implementation.
*
* @group batch2
*/
class RenderingTest extends AbstractKernelTestBase implements FormInterface {
/**
* {@inheritdoc}
*/
public static $modules = [
'oe_theme_patterns_render_test',
'text',
'filter',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig('filter');
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'oe_theme_rendering_test_form';
}
/**
* Form constructor.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $structure
* The structure of the form, read from the fixtures files.
*
* @return array
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state, array $structure = NULL): array {
$form['test'] = $structure;
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Recurse through all the form elements and check if they have a property
// "#set_validation_error". If they have, set a generic error on the
// element.
$add_errors = function (array $element) use (&$add_errors, $form_state): void {
if (!empty($element['#set_validation_error'])) {
// When the title is not present for a form element, fallback to its
// path in the form.
$label = !empty($element['#title']) ? $element['#title'] : implode('][', $element['#array_parents']);
$form_state->setError($element, t('Validation error on @label', ['@label' => $label]));
}
foreach (Element::children($element) as $key) {
// Recursively call this closure on all the children elements.
$add_errors($element[$key]);
}
};
$add_errors($form['test']);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {}
/**
* Test rendering of elements.
*
* @param array $structure
* A render array.
* @param array $assertions
* Test assertions.
*
* @throws \Exception
*
* @dataProvider renderingDataProvider
*/
public function testRendering(array $structure, array $assertions): void {
// Wrap all the test structure inside a form. This will allow proper
// processing of form elements and invocation of form alter hooks.
// Even if the elements being tested are not form related, the form can
// host them without causing any issues.
$form_state = new FormState();
$form_state->addBuildInfo('args', [$structure]);
$form_state->setProgrammed();
$form = $this->container->get('form_builder')->buildForm($this, $form_state);
$this->assertRendering($this->renderRoot($form), $assertions);
}
/**
* Data provider for rendering tests.
*
* The actual data is read from fixtures stored in a YAML configuration.
*
* @return array
* A set of dump data for testing.
*/
public function renderingDataProvider(): array {
return $this->getFixtureContent('rendering.yml');
}
}